Skip to content

Instantly share code, notes, and snippets.

@szagoruyko
Last active March 23, 2017 22:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szagoruyko/8828e09cc4687afd324d to your computer and use it in GitHub Desktop.
Save szagoruyko/8828e09cc4687afd324d to your computer and use it in GitHub Desktop.

Export ResNet-34

Trained by facebook using https://github.com/facebook/fb.resnet.torch

The model was converted to nn backend and BatchNorm folded into convolutional layers with this script https://github.com/szagoruyko/imagine-nn/blob/utils/utils.lua

gradWeight and gradBias were removed from convolutional layers.

top-1 central crop accuracy: 72.94%

Download link: https://www.dropbox.com/s/1pvw1xa5726iqe9/resnet-34-export.t7 (84MB)

Preprocessing

Separate mean std per channel is saved with the network:

> print(net.transform)
{
  mean :
    {
      1 : 0.48462227599918
      2 : 0.45624044862054
      3 : 0.40588363755159
    }
  std :
    {
      1 : 0.22889466674951
      2 : 0.22446679341259
      3 : 0.22495548344775
    }
}

Can be loaded without CUDA support.

require 'cudnn'
require 'cunn'
require 'inn'
local utils = require 'inn.utils'
local iterm = require 'iterm'
local generateGraph = require 'optnet.graphgen'
local name = 'resnet-34.t7'
local net = torch.load(name)
net:cuda():evaluate()
local input = torch.CudaTensor(10,3,224,224)
local function restoreBias(net)
net:apply(function(x)
if torch.typename(x):find'SpatialConvolution' then
if not x.bias then
x.bias = x.weight.new():resize(x.weight:size(1)):zero()
x:resetWeightDescriptors()
end
end
end)
end
print(utils.testSurgery(input, utils.foldBatchNorm, net))
print(utils.testSurgery(input, restoreBias, net))
print(utils.testSurgery(input, cudnn.convert, net, nn))
iterm.dot(generateGraph(net, input), name..'.pdf')
net.transform = {
mean = {
0.48462227599918,
0.45624044862054,
0.40588363755159,
},
std = {
0.22889466674951,
0.22446679341259,
0.22495548344775,
}
}
net:apply(function(x)
if x.weight then
x.gradWeight = nil
x.gradBias = nil
end
end)
net:clearState()
net:float()
torch.save('resnet-34-export.t7', net)
@ProGamerGov
Copy link

Does this conversion tool only support ResNet and Inception models? Or does it also support AlexNet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment