Advertisement
Guest User

Untitled

a guest
May 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. ----------------------------------------------------------------------
  2. -- Create CNN and loss to optimize.
  3. --
  4. -- Clement Farabet
  5. ----------------------------------------------------------------------
  6.  
  7. require 'torch' -- torch
  8. require 'image' -- to visualize the dataset
  9. require 'nn' -- provides all sorts of trainable modules/layers
  10. --require 'Dropout' -- Hinton dropout technique
  11.  
  12. if opt.type == 'cuda' then
  13. nn.SpatialConvolutionMM = nn.SpatialConvolution
  14. end
  15.  
  16. ----------------------------------------------------------------------
  17. print(sys.COLORS.red .. '==> define parameters')
  18.  
  19. -- 2-class problem: faces!
  20. local noutputs = 2
  21.  
  22. -- input dimensions: faces!
  23. local nfeats = 3
  24. local width = 256
  25. local height = 256
  26.  
  27. -- hidden units, filter sizes (for ConvNet only):
  28. local nstates = {16,32}
  29. local filtsize = {5, 7}
  30. local poolsize = 4
  31.  
  32. ----------------------------------------------------------------------
  33. print(sys.COLORS.red .. '==> construct CNN')
  34.  
  35. local CNN = nn.Sequential()
  36.  
  37. -- stage 1: conv+max
  38. CNN:add(nn.SpatialConvolution(3, 16, 5, 5, 1, 1, 0, 0))
  39. CNN:add(nn.ReLU())
  40. CNN:add(nn.SpatialMaxPooling(2,2,2,2))
  41.  
  42. -- stage 2: conv+max
  43. CNN:add(nn.SpatialConvolution(16, 16, 12, 12, 2, 2, 0, 0))
  44. CNN:add(nn.ReLU())
  45. CNN:add(nn.SpatialMaxPooling(3,3,1,1))
  46. CNN:add(nn.SpatialConvolution(16, 32, 4, 4, 4, 4, 0, 0))
  47. CNN:add(nn.ReLU())
  48. CNN:add(nn.SpatialMaxPooling(2,2,2,2))
  49.  
  50. local classifier = nn.Sequential()
  51. -- stage 3: linear
  52. classifier:add(nn.Reshape(7*7*32))
  53. classifier:add(nn.Linear(7*7*32, 2))
  54.  
  55. -- stage 4 : log probabilities
  56. classifier:add(nn.LogSoftMax())
  57. --CNN:remove()
  58. for _,layer in ipairs(CNN.modules) do
  59. if layer.bias then
  60. layer.bias:fill(.2)
  61. if i == #CNN.modules-1 then
  62. layer.bias:zero()
  63. end
  64. end
  65. end
  66.  
  67. local model = nn.Sequential()
  68. model:add(CNN)
  69. model:add(classifier)
  70. -- Loss: NLL
  71. loss = nn.ClassNLLCriterion()
  72.  
  73.  
  74. ----------------------------------------------------------------------
  75. print(sys.COLORS.red .. '==> here is the CNN:')
  76. print(model)
  77.  
  78. if opt.type == 'cuda' then
  79. model:cuda()
  80. loss:cuda()
  81. end
  82.  
  83. -- return package:
  84. return {
  85. model = model,
  86. loss = loss,
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement