[
  {
    "path": "README.md",
    "content": "# PyramidNet\nThis repository contains the code for the paper:\n\nDongyoon Han*, Jiwhan Kim*, and Junmo Kim, \"Deep Pyramidal Residual Networks\", CVPR 2017 (* equal contribution).\n\nArxiv: https://arxiv.org/abs/1610.02915. \n\nThe code is based on Facebook's implementation of ResNet (https://github.com/facebook/fb.resnet.torch).\n\n### Caffe implementation of PyramidNet: [site](https://github.com/jhkim89/PyramidNet-caffe)\n### PyTorch implementation of PyramidNet: [site](https://github.com/dyhan0920/PyramidNet-PyTorch)\n\n## Abstract\n Deep convolutional neural networks (DCNNs) have shown remarkable performance in image classification tasks in recent years. Generally, deep neural network architectures are stacks consisting of a large number of convolution layers, and they perform downsampling along the spatial dimension via pooling to reduce memory usage. At the same time, the feature map dimension (i.e., the number of channels) is sharply increased at downsampling locations, which is essential to ensure effective performance because it increases the capability of high-level attributes. Moreover, this also applies to residual networks and is very closely related to their performance. In this research, instead of using downsampling to achieve a sharp increase at each residual unit, we gradually increase the feature map dimension at all the units to involve as many locations as possible. This is discussed in depth together with our new insights as it has proven to be an effective design to improve the generalization ability. Furthermore, we propose a novel residual unit capable of further improving the classification accuracy with our new network architecture. Experiments on benchmark CIFAR datasets have shown that our network architecture has a superior generalization ability compared to the original residual networks.\n\n<p align=\"center\"><img src=\"https://cloud.githubusercontent.com/assets/22743125/19235579/7e7e33c6-8f2d-11e6-9397-1b505688e92a.png\" width=\"960\"></p>\n\nFigure 1: Schematic illustration of (a) basic residual units, (b) bottleneck, (c) wide residual units, and (d) our pyramidal residual units.\n\n<p align=\"center\"><img src=\"https://cloud.githubusercontent.com/assets/22743125/19235610/bb3d5fd0-8f2d-11e6-84bd-46c9b7a4797a.png\" width=\"640\"></p>\n\nFigure 2: Visual illustrations of (a) additive PyramidNet, (b) multiplicative PyramidNet, and (c) comparison of (a) and (b).\n\n## Usage\n\n1. Install Torch (http://torch.ch) and ResNet (https://github.com/facebook/fb.resnet.torch).\n2. Add the files addpyramidnet.lua and mulpyramidnet.lua to the folder \"models\".\n3. Manually set the parameter \"alpha\" in the files addpyramidnet.lua and mulpyramidnet.lua (Line 28).\n4. Change the learning rate schedule in the file train.lua: \"decay = epoch >= 122 and 2 or epoch >= 81 and 1 or 0\" to \"decay = epoch >= 225 and 2 or epoch >= 150 and 1 or 0\".\n5. Train our PyramidNet, by running main.lua as below:\n\nTo train additive PyramidNet-164 (alpha=48) on CIFAR-10 dataset:\n```bash\nth main.lua -dataset cifar10 -depth 164 -nEpochs 300 -LR 0.1 -netType addpyramidnet -batchSize 128 -shareGradInput true\n```\nTo train additive PyramidNet-164 (alpha=48) with 4 GPUs on CIFAR-100 dataset:\n```bash\nth main.lua -dataset cifar100 -depth 164 -nEpochs 300 -LR 0.5 -nGPU 4 -nThreads 8 -netType addpyramidNet -batchSize 128 -shareGradInput true\n```\n\n## Results\n\n#### CIFAR\n\nTop-1 error rates on CIFAR-10 and CIFAR-100 datasets.  \"alpha\" denotes the widening factor; \"add\" and \"mul\" denote the results obtained with additive and multiplicative pyramidal networks, respectively.\n\n| Network                           | # of parameters | Output feat. dimension | CIFAR-10    |  CIFAR-100  |\n| --------------------------------- | --------------- | ---------------------- | ----------- | ----------- |\n| PyramidNet-110 (mul), alpha=4.75  | 1.7M            |  76                    | 4.62        | 23.16       |\n| PyramidNet-110 (add), alpha=48    | 1.7M            |  **64**                | 4.62        | 23.31       |\n| PyramidNet-110 (mul), alpha=8     | 3.8M            |  128                   | 4.50        | 20.94       |\n| PyramidNet-110 (add), alpha=84    | 3.8M            |  **100**               | 4.27        | 20.21       |\n| PyramidNet-110 (mul), alpha=27    | 28.3M           |  432                   | 4.06        | 18.79       |\n| PyramidNet-110 (add), alpha=270   | 28.3M           |  **286**               | **3.73**    | **18.25**   |\n\nTop-1 error rates of our model with the **bottleneck architecture** on CIFAR-10 and CIFAR-100 datasets.  We use the additive pyramidal networks.\n\n| Network                           | # of parameters | Output feat. dimension | CIFAR-10    |  CIFAR-100  |\n| --------------------------------- | --------------- | ---------------------- | ----------- | ----------- |\n| PyramidNet-164 (add), alpha=48    | 1.7M            |  256                   | 4.21        | 19.52       |\n| PyramidNet-164 (add), alpha=84    | 3.8M            |  400                   | 3.96        | 18.32       |\n| PyramidNet-164 (add), alpha=270   | 27.0M           |  1144                  | **3.48**    | **17.01**   |\n| PyramidNet-200 (add), alpha=240   | 26.6M           |  1024                  | **3.44**    | **16.51**   |\n| PyramidNet-236 (add), alpha=220   | 26.8M           |  944                   | **3.40**    | **16.37**   |\n| PyramidNet-272 (add), alpha=200   | 26.0M           |  864                   | **3.31**    | **16.35**   |\n\n![cifar](https://user-images.githubusercontent.com/22743125/28292795-c058f7dc-6b8b-11e7-9d3a-280ed49a4191.png)\n\nFigure 3: Performance distribution according to number of parameters on CIFAR-10 (left) and CIFAR-100 (right).\n\n#### ImageNet\n\nTop-1 and Top-5 error rates of single-model, single-crop (224*224) on ImageNet dataset.  We use the additive PyramidNet for our results. \n\n| Network                                   | # of parameters | Output feat. dimension | Top-1 error | Top-5 error |\n| ----------------------------------------- | --------------- | ---------------------- | ----------- | ----------- |\n| PreResNet-200                             | 64.5M           |  2048                  | 21.66       | 5.79        |\n| PyramidNet-200, alpha=300                 | 62.1M           |  1456                  | 20.47       | 5.29        |\n| PyramidNet-200, alpha=450, Dropout (0.5)  | 116.4M          |  2056                  | 20.11       | 5.43        |\n\nModel files download: [link](https://1drv.ms/f/s!AmNvwgeB0n4GsiDFDNJWZkEbajJf)\n\n\n## Notes\n\n1. The parameter \"alpha\" can only be changed in the files addpyramidnet.lua and mulpyramidnet.lua (Line 28).\n2. We recommend to use multi-GPU when training additive PyramidNet with alpha=270 or multiplicative PyramidNet with alpha=27.  Otherwise you may get \"out of memory\" error.\n3. We are currently testing our code in the ImageNet dataset.  We will upload the result when the training is completed.\n\n## Updates\n\n07/17/2017:\n\n1. Caffe implementation of PyramidNet is released.\n\n02/12/2017:\n\n1. Results of the bottleneck architecture on CIFAR datasets are updated.\n\n01/23/2017:\n\n1. Added Imagenet pretrained models.\n\n## Contact\nJiwhan Kim (jhkim89@kaist.ac.kr),\nDongyoon Han (dyhan@kaist.ac.kr),\nJunmo Kim (junmo.kim@kaist.ac.kr)\n"
  },
  {
    "path": "addpyramidnet.lua",
    "content": "--  Implementation of \"Deep Pyramidal Residual Networks\" \n\n--  ************************************************************************\n--  This code incorporates material from:\n\n--  fb.resnet.torch (https://github.com/facebook/fb.resnet.torch)\n--  Copyright (c) 2016, Facebook, Inc.\n--  All rights reserved.\n--\n--  This source code is licensed under the BSD-style license found in the\n--  LICENSE file in the root directory of this source tree. An additional grant\n--  of patent rights can be found in the PATENTS file in the same directory.\n--\n--  ************************************************************************\n\nlocal nn = require 'nn'\nrequire 'cunn'\n\nlocal Convolution = cudnn.SpatialConvolution\nlocal Avg = cudnn.SpatialAveragePooling\nlocal ReLU = cudnn.ReLU\nlocal Max = nn.SpatialMaxPooling\nlocal SBatchNorm = nn.SpatialBatchNormalization\n\nlocal function createModel(opt)\n   local depth = opt.depth\n   local iChannels\n   local alpha = 48\n-- local alpha = 300\n   local function round(x)\n      return math.floor(x+0.5)\n   end\n\n   local function shortcut(nInputPlane, nOutputPlane, stride)\n      -- Strided, zero-padded identity shortcut\n      local short = nn.Sequential()\n      if stride == 2 then\n         short:add(nn.SpatialAveragePooling(2, 2, 2, 2))\n      end\n      if nInputPlane ~= nOutputPlane then\n         short:add(nn.Padding(1, (nOutputPlane - nInputPlane), 3))\n      else\n\t       short:add(nn.Identity())\n      end\n      return short\n   end\n\n   local function basicblock(n, stride)\n      local nInputPlane = iChannels\n      iChannels = n\n\n      local s = nn.Sequential()\n      s:add(SBatchNorm(nInputPlane))\n      s:add(Convolution(nInputPlane,n,3,3,stride,stride,1,1))\n      s:add(SBatchNorm(n))\n      s:add(ReLU(true))\n      s:add(Convolution(n,n,3,3,1,1,1,1))\n      s:add(SBatchNorm(n))\n      return nn.Sequential()\n         :add(nn.ConcatTable()\n            :add(s)\n            :add(shortcut(nInputPlane, n, stride)))\n         :add(nn.CAddTable(true))\n   end\n\n   local function bottleneck(n, stride, type)\n      local nInputPlane = iChannels\n      iChannels = n * 4\n\n      local s = nn.Sequential()\n      s:add(SBatchNorm(nInputPlane))\n      s:add(Convolution(nInputPlane,n,1,1,1,1,0,0))\n      s:add(SBatchNorm(n))\n      s:add(ReLU(true))\n      s:add(Convolution(n,n,3,3,stride,stride,1,1))\n      s:add(SBatchNorm(n))\n      s:add(ReLU(true))\n      s:add(Convolution(n,n*4,1,1,1,1,0,0))\n      s:add(SBatchNorm(n*4))\n\n      return nn.Sequential()\n         :add(nn.ConcatTable()\n            :add(s)\n            :add(shortcut(nInputPlane, n * 4, stride)))\n         :add(nn.CAddTable(true))\n   end\n\n   -- Creates count residual blocks with specified number of features\n   local function layer(block, features, count, stride)\n      local s = nn.Sequential()\n      if count < 1 then\n        return s\n      end\n      for i=1,count do\n         s:add(block(features, stride))\n      end\n      return s\n   end\n\n   local model = nn.Sequential()\n   if opt.dataset == 'imagenet' then\n   -- Configurations for ResNet:\n   --  num. residual blocks, num features, residual block function\n   local cfg = {\n      [18]  = {{2, 2, 2, 2}, 512, basicblock},\n      [34]  = {{3, 4, 6, 3}, 512, basicblock},\n      [50]  = {{3, 4, 6, 3}, 2048, bottleneck},\n      [101] = {{3, 4, 23, 3}, 2048, bottleneck},\n      [152] = {{3, 8, 36, 3}, 2048, bottleneck},\n      [200] = {{3, 24, 36, 3}, 2048, bottleneck},\n   }\n\n   assert(cfg[depth], 'Invalid depth: ' .. tostring(depth))\n   local def, nFeatures, block = table.unpack(cfg[depth])\n   iChannels = 64\n   Channeltemp = 64\n   local addrate = alpha/(def[1]+def[2]+def[3]+def[4])\n   print(' | PyramidNet-' .. depth .. ' ImageNet')\n\n   model:add(Convolution(3,64,7,7,2,2,3,3))\n   model:add(SBatchNorm(64))\n   model:add(ReLU(true))\n   model:add(Max(3,3,2,2,1,1))\n   Channeltemp = Channeltemp + addrate\n   model:add(bottleneck(round(Channeltemp), 1, 1, 'first'))\n   for i=2,def[1] do\n      Channeltemp = Channeltemp + addrate\n      model:add(bottleneck(round(Channeltemp), 1, 1))\n   end\n   Channeltemp = Channeltemp + addrate\n   model:add(bottleneck(round(Channeltemp), 2, 1))\n   for i=2,def[2] do\n      Channeltemp = Channeltemp + addrate\n      model:add(bottleneck(round(Channeltemp), 1, 1))\n   end\n   Channeltemp = Channeltemp + addrate\n   model:add(bottleneck(round(Channeltemp), 2, 1))\n   for i=2,def[3] do\n      Channeltemp = Channeltemp + addrate\n      model:add(bottleneck(round(Channeltemp), 1, 1))\n   end\n   Channeltemp = Channeltemp + addrate\n   model:add(bottleneck(round(Channeltemp), 2, 1))\n   for i=2,def[4] do\n      Channeltemp = Channeltemp + addrate\n      model:add(bottleneck(round(Channeltemp), 1, 1))\n   end\n   model:add(nn.Copy(nil, nil, true))\n   model:add(SBatchNorm(iChannels))\n   model:add(ReLU(true))\n   model:add(Avg(7, 7, 1, 1))\n   model:add(nn.View(iChannels):setNumInputDims(3))\n   model:add(nn.Linear(iChannels, 1000))\n\n   elseif opt.dataset == 'cifar10' or opt.dataset == 'cifar100' then\n   -- local n = (depth - 2) / 6  -- basicblock\n   local n = (depth - 2) / 9  -- bottleneck\n   iChannels = 16\n   local startChannel = 16\n   local Channeltemp = 16\n   addChannel = alpha/(3*n)\n   print(' | PyramidNet-' .. depth .. ' CIFAR')\n\n   model:add(Convolution(3,16,3,3,1,1,1,1))\n   model:add(SBatchNorm(iChannels))\n\n   Channeltemp = startChannel\n   startChannel = startChannel + addChannel\n   model:add(layer(bottleneck, round(startChannel), 1, 1, 1))\n   for i=2,n do \n      Channeltemp = startChannel\n      startChannel = startChannel + addChannel\n      model:add(layer(bottleneck, round(startChannel), 1, 1, 1))\n   end\n\n   Channeltemp = startChannel\n   startChannel = startChannel + addChannel\n   model:add(layer(bottleneck, round(startChannel), 1, 2, 1))\n   for i=2,n do \n      Channeltemp = startChannel\n      startChannel = startChannel + addChannel\n      model:add(layer(bottleneck, round(startChannel), 1, 1, 1))\n   end\n   Channeltemp = startChannel\n   startChannel = startChannel + addChannel\n   model:add(layer(bottleneck, round(startChannel), 1, 2, 1))\n   for i=2,n do \n      Channeltemp = startChannel\n      startChannel = startChannel + addChannel\n      model:add(layer(bottleneck, round(startChannel), 1, 1, 1))\n   end\n   model:add(nn.Copy(nil, nil, true))\n   model:add(SBatchNorm(iChannels))\n   model:add(ReLU(true))\n   model:add(Avg(8, 8, 1, 1))\n   model:add(nn.View(iChannels):setNumInputDims(3))\n   if opt.dataset == 'cifar10' then\n      model:add(nn.Linear(iChannels, 10))\n   elseif opt.dataset == 'cifar100' then\n      model:add(nn.Linear(iChannels, 100))\n   end\n   else\n      error('invalid dataset: ' .. opt.dataset)\n   end\n\n   local function ConvInit(name)\n      for k,v in pairs(model:findModules(name)) do\n         local n = v.kW*v.kH*v.nOutputPlane\n         v.weight:normal(0,math.sqrt(2/n))\n         if cudnn.version >= 4000 then\n            v.bias = nil\n            v.gradBias = nil\n         else\n            v.bias:zero()\n         end\n      end\n   end\n   local function BNInit(name)\n      for k,v in pairs(model:findModules(name)) do\n         v.weight:fill(1)\n         v.bias:zero()\n      end\n   end\n\n   ConvInit('cudnn.SpatialConvolution')\n   ConvInit('nn.SpatialConvolution')\n   BNInit('fbnn.SpatialBatchNormalization')\n   BNInit('cudnn.SpatialBatchNormalization')\n   BNInit('nn.SpatialBatchNormalization')\n   for k,v in pairs(model:findModules('nn.Linear')) do\n      v.bias:zero()\n   end\n   model:cuda()\n\n   if opt.cudnn == 'deterministic' then\n      model:apply(function(m)\n         if m.setMode then m:setMode(1,1,1) end\n      end)\n   end\n\n   model:get(1).gradInput = nil\n\n   return model\nend\n\nreturn createModel\n"
  },
  {
    "path": "mulpyramidnet.lua",
    "content": "--  Implementation of \"Deep Pyramidal Residual Networks\" \n\n--  ************************************************************************\n--  This code incorporates material from:\n\n--  fb.resnet.torch (https://github.com/facebook/fb.resnet.torch)\n--  Copyright (c) 2016, Facebook, Inc.\n--  All rights reserved.\n--\n--  This source code is licensed under the BSD-style license found in the\n--  LICENSE file in the root directory of this source tree. An additional grant\n--  of patent rights can be found in the PATENTS file in the same directory.\n--\n--  ************************************************************************\n\nlocal nn = require 'nn'\nrequire 'cunn'\n\nlocal Convolution = cudnn.SpatialConvolution\nlocal Avg = cudnn.SpatialAveragePooling\nlocal ReLU = cudnn.ReLU\nlocal Max = nn.SpatialMaxPooling\nlocal SBatchNorm = nn.SpatialBatchNormalization\n\nlocal function createModel(opt)\n   local depth = opt.depth\n   local iChannels\n   local alpha = 4.75\n   local function round(x)\n      return math.floor(x+0.5)\n   end\n\n   local function shortcut(nInputPlane, nOutputPlane, stride)\n      -- Strided, zero-padded identity shortcut\n      local short = nn.Sequential()\n      if stride == 2 then\n         short:add(nn.SpatialAveragePooling(2, 2, 2, 2))\n      end\n      if nInputPlane ~= nOutputPlane then\n         short:add(nn.Padding(1, (nOutputPlane - nInputPlane), 3))\n      else\n\t       short:add(nn.Identity())\n      end\n      return short\n   end\n\n   local function basicblock(n, stride)\n      local nInputPlane = iChannels\n      iChannels = n\n\n      local s = nn.Sequential()\n      s:add(SBatchNorm(nInputPlane))\n      s:add(Convolution(nInputPlane,n,3,3,stride,stride,1,1))\n      s:add(SBatchNorm(n))\n      s:add(ReLU(true))\n      s:add(Convolution(n,n,3,3,1,1,1,1))\n      s:add(SBatchNorm(n))\n      return nn.Sequential()\n         :add(nn.ConcatTable()\n            :add(s)\n            :add(shortcut(nInputPlane, n, stride)))\n         :add(nn.CAddTable(true))\n   end\n\t\n   local function bottleneck(n, stride, type)\n      local nInputPlane = iChannels\n      iChannels = n * 4\n\n      local s = nn.Sequential()\n      s:add(SBatchNorm(nInputPlane))\n      s:add(Convolution(nInputPlane,n,1,1,1,1,0,0))\n      s:add(SBatchNorm(n))\n      s:add(ReLU(true))\n      s:add(Convolution(n,n,3,3,stride,stride,1,1))\n      s:add(SBatchNorm(n))\n      s:add(ReLU(true))\n      s:add(Convolution(n,n*4,1,1,1,1,0,0))\n      s:add(SBatchNorm(n*4))\n\n      return nn.Sequential()\n         :add(nn.ConcatTable()\n            :add(s)\n            :add(shortcut(nInputPlane, n * 4, stride)))\n         :add(nn.CAddTable(true))\n   end\n\n\t\n   -- Creates count residual blocks with specified number of features\n   local function layer(block, features, count, stride)\n      local s = nn.Sequential()\n      if count < 1 then\n        return s\n      end\n      for i=1,count do\n         s:add(block(features, stride))\n      end\n      return s\n   end\n\n   local model = nn.Sequential()\n   if opt.dataset == 'imagenet' then\n   -- Configurations for ResNet:\n   --  num. residual blocks, num features, residual block function\n   local cfg = {\n      [18]  = {{2, 2, 2, 2}, 512, basicblock},\n      [34]  = {{3, 4, 6, 3}, 512, basicblock},\n      [50]  = {{3, 4, 6, 3}, 2048, bottleneck},\n      [101] = {{3, 4, 23, 3}, 2048, bottleneck},\n      [152] = {{3, 8, 36, 3}, 2048, bottleneck},\n      [200] = {{3, 24, 36, 3}, 2048, bottleneck},\n   }\n\n   assert(cfg[depth], 'Invalid depth: ' .. tostring(depth))\n   local def, nFeatures, block = table.unpack(cfg[depth])\n   iChannels = 64\n   Channeltemp = 64\n   local addrate = alpha^(1/(def[1]+def[2]+def[3]+def[4]))\n   print(' | ResNet-' .. depth .. ' ImageNet')\n\n   model:add(Convolution(3,64,7,7,2,2,3,3))\n   model:add(SBatchNorm(64))\n   model:add(ReLU(true))\n   model:add(Max(3,3,2,2,1,1))\n   Channeltemp = Channeltemp * addrate\n   model:add(bottleneck(round(Channeltemp), 1, 1, 'first'))\n   for i=2,def[1] do\n      Channeltemp = Channeltemp * addrate\n      model:add(bottleneck(round(Channeltemp), 1, 1))\n   end\n   Channeltemp = Channeltemp * addrate\n   model:add(bottleneck(round(Channeltemp), 2, 1))\n   for i=2,def[2] do\n      Channeltemp = Channeltemp * addrate\n      model:add(bottleneck(round(Channeltemp), 1, 1))\n   end\n   Channeltemp = Channeltemp * addrate\n   model:add(bottleneck(round(Channeltemp), 2, 1))\n   for i=2,def[3] do\n      Channeltemp = Channeltemp * addrate\n      model:add(bottleneck(round(Channeltemp), 1, 1))\n   end\n   Channeltemp = Channeltemp * addrate\n   model:add(bottleneck(round(Channeltemp), 2, 1))\n   for i=2,def[4] do\n      Channeltemp = Channeltemp * addrate\n      model:add(bottleneck(round(Channeltemp), 1, 1))\n   end\n   model:add(nn.Copy(nil, nil, true))\n   model:add(SBatchNorm(iChannels))\n   model:add(ReLU(true))\n   model:add(Avg(7, 7, 1, 1))\n   model:add(nn.View(iChannels):setNumInputDims(3))\n   model:add(nn.Linear(iChannels, 1000))\n\n   elseif opt.dataset == 'cifar10' or opt.dataset == 'cifar100' then\n   local n = (depth - 2) / 6\n   iChannels = 16\n   local startChannel = 16\n   local Channeltemp = 16\n   addChannel = alpha^(1/(3*n))\n   print(' | PyramidNet-' .. depth .. ' CIFAR-10')\n\n   model:add(Convolution(3,16,3,3,1,1,1,1))\n   model:add(SBatchNorm(iChannels))\n\n   Channeltemp = startChannel\n   startChannel = startChannel * addChannel\n   model:add(layer(basicblock, round(startChannel), 1, 1, 1))\n   for i=2,n do \n      Channeltemp = startChannel\n      startChannel = startChannel * addChannel\n      model:add(layer(basicblock, round(startChannel), 1, 1, 1))\n   end\n\n   Channeltemp = startChannel\n   startChannel = startChannel * addChannel\n   model:add(layer(basicblock, round(startChannel), 1, 2, 1))\n   for i=2,n do \n      Channeltemp = startChannel\n      startChannel = startChannel * addChannel\n      model:add(layer(basicblock, round(startChannel), 1, 1, 1))\n   end\n   Channeltemp = startChannel\n   startChannel = startChannel * addChannel\n   model:add(layer(basicblock, round(startChannel), 1, 2, 1))\n   for i=2,n do \n      Channeltemp = startChannel\n      startChannel = startChannel * addChannel\n      model:add(layer(basicblock, round(startChannel), 1, 1, 1))\n   end\n   model:add(nn.Copy(nil, nil, true))\n   model:add(SBatchNorm(iChannels))\n   model:add(ReLU(true))\n   model:add(Avg(8, 8, 1, 1))\n   model:add(nn.View(iChannels):setNumInputDims(3))\n   if opt.dataset == 'cifar10' then\n      model:add(nn.Linear(iChannels, 10))\n   elseif opt.dataset == 'cifar100' then\n      model:add(nn.Linear(iChannels, 100))\n   end\n   else\n      error('invalid dataset: ' .. opt.dataset)\n   end\n\t\t\n   local function ConvInit(name)\n      for k,v in pairs(model:findModules(name)) do\n         local n = v.kW*v.kH*v.nOutputPlane\n         v.weight:normal(0,math.sqrt(2/n))\n         if cudnn.version >= 4000 then\n            v.bias = nil\n            v.gradBias = nil\n         else\n            v.bias:zero()\n         end\n      end\n   end\n   local function BNInit(name)\n      for k,v in pairs(model:findModules(name)) do\n         v.weight:fill(1)\n         v.bias:zero()\n      end\n   end\n\n   ConvInit('cudnn.SpatialConvolution')\n   ConvInit('nn.SpatialConvolution')\n   BNInit('fbnn.SpatialBatchNormalization')\n   BNInit('cudnn.SpatialBatchNormalization')\n   BNInit('nn.SpatialBatchNormalization')\n   for k,v in pairs(model:findModules('nn.Linear')) do\n      v.bias:zero()\n   end\n   model:cuda()\n\n   if opt.cudnn == 'deterministic' then\n      model:apply(function(m)\n         if m.setMode then m:setMode(1,1,1) end\n      end)\n   end\n\n   model:get(1).gradInput = nil\n\n   return model\nend\n\nreturn createModel\n"
  }
]