2015-09-24 07:49:45 +08:00
|
|
|
require 'nn'
|
2015-12-21 11:23:15 +08:00
|
|
|
|
2015-09-24 07:49:45 +08:00
|
|
|
require 'cunn'
|
|
|
|
require 'dpnn'
|
|
|
|
|
2015-12-21 11:23:15 +08:00
|
|
|
require 'fbnn'
|
|
|
|
require 'fbcunn'
|
|
|
|
|
2015-09-24 07:49:45 +08:00
|
|
|
require 'optim'
|
|
|
|
|
2016-01-07 05:04:10 +08:00
|
|
|
require 'cudnn'
|
|
|
|
|
|
|
|
cudnn.benchmark = false
|
|
|
|
cudnn.fastest = true
|
|
|
|
cudnn.verbose = false
|
|
|
|
|
2015-09-24 07:49:45 +08:00
|
|
|
paths.dofile('torch-TripletEmbedding/TripletEmbedding.lua')
|
|
|
|
|
2016-01-07 05:04:10 +08:00
|
|
|
-- From https://groups.google.com/d/msg/torch7/i8sJYlgQPeA/wiHlPSa5-HYJ
|
|
|
|
function replaceModules(net, orig_class_name, replacer)
|
|
|
|
local nodes, container_nodes = net:findModules(orig_class_name)
|
|
|
|
for i = 1, #nodes do
|
|
|
|
for j = 1, #(container_nodes[i].modules) do
|
|
|
|
if container_nodes[i].modules[j] == nodes[i] then
|
|
|
|
local orig_mod = container_nodes[i].modules[j]
|
|
|
|
container_nodes[i].modules[j] = replacer(orig_mod)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function nn_to_cudnn(net)
|
2016-01-08 07:28:05 +08:00
|
|
|
local net_cudnn = net:clone():float()
|
2016-01-07 05:04:10 +08:00
|
|
|
|
2016-01-08 07:28:05 +08:00
|
|
|
replaceModules(net_cudnn, 'nn.SpatialConvolutionMM',
|
2016-01-07 05:04:10 +08:00
|
|
|
function(nn_mod)
|
|
|
|
local cudnn_mod = cudnn.SpatialConvolution(
|
|
|
|
nn_mod.nInputPlane, nn_mod.nOutputPlane,
|
|
|
|
nn_mod.kW, nn_mod.kH,
|
|
|
|
nn_mod.dW, nn_mod.dH,
|
|
|
|
nn_mod.padW, nn_mod.padH
|
|
|
|
)
|
|
|
|
cudnn_mod.weight:copy(nn_mod.weight)
|
|
|
|
cudnn_mod.bias:copy(nn_mod.bias)
|
|
|
|
return cudnn_mod
|
|
|
|
end
|
|
|
|
)
|
2016-01-08 07:28:05 +08:00
|
|
|
replaceModules(net_cudnn, 'nn.SpatialAveragePooling',
|
2016-01-07 05:04:10 +08:00
|
|
|
function(nn_mod)
|
|
|
|
return cudnn.SpatialAveragePooling(
|
|
|
|
nn_mod.kW, nn_mod.kH,
|
|
|
|
nn_mod.dW, nn_mod.dH,
|
|
|
|
nn_mod.padW, nn_mod.padH
|
|
|
|
)
|
|
|
|
end
|
|
|
|
)
|
2016-01-08 07:28:05 +08:00
|
|
|
replaceModules(net_cudnn, 'nn.SpatialMaxPooling',
|
2016-01-07 05:04:10 +08:00
|
|
|
function(nn_mod)
|
|
|
|
return cudnn.SpatialMaxPooling(
|
|
|
|
nn_mod.kW, nn_mod.kH,
|
|
|
|
nn_mod.dW, nn_mod.dH,
|
|
|
|
nn_mod.padW, nn_mod.padH
|
|
|
|
)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2016-01-08 07:28:05 +08:00
|
|
|
replaceModules(net_cudnn, 'nn.ReLU', function() return cudnn.ReLU() end)
|
|
|
|
replaceModules(net_cudnn, 'nn.SpatialCrossMapLRN',
|
2016-01-07 05:04:10 +08:00
|
|
|
function(nn_mod)
|
2016-01-08 07:28:05 +08:00
|
|
|
return cudnn.SpatialCrossMapLRN(nn_mod.size, nn_mod.alpha,
|
|
|
|
nn_mod.beta, nn_mod.k)
|
2016-01-07 05:04:10 +08:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2016-01-08 07:28:05 +08:00
|
|
|
return net_cudnn
|
2016-01-07 05:04:10 +08:00
|
|
|
end
|
|
|
|
|
2015-09-24 07:49:45 +08:00
|
|
|
if opt.retrain ~= 'none' then
|
|
|
|
assert(paths.filep(opt.retrain), 'File not found: ' .. opt.retrain)
|
|
|
|
print('Loading model from file: ' .. opt.retrain);
|
2016-01-07 05:04:10 +08:00
|
|
|
model = torch.load(opt.retrain)
|
2015-09-24 07:49:45 +08:00
|
|
|
else
|
|
|
|
paths.dofile(opt.modelDef)
|
2016-01-07 05:04:10 +08:00
|
|
|
model = createModel()
|
2015-09-24 07:49:45 +08:00
|
|
|
end
|
|
|
|
|
2016-01-08 07:28:05 +08:00
|
|
|
if opt.cudnn then
|
|
|
|
model = nn_to_cudnn(model)
|
|
|
|
end
|
2016-01-07 05:04:10 +08:00
|
|
|
criterion = nn.TripletEmbeddingCriterion(opt.alpha)
|
2015-09-24 07:49:45 +08:00
|
|
|
|
|
|
|
model = model:cuda()
|
|
|
|
criterion:cuda()
|
|
|
|
|
|
|
|
print('=> Model')
|
|
|
|
print(model)
|
2016-01-09 00:18:44 +08:00
|
|
|
print(('Number of Parameters: %d'):format(model:getParameters():size(1)))
|
2015-09-24 07:49:45 +08:00
|
|
|
|
|
|
|
print('=> Criterion')
|
|
|
|
print(criterion)
|
|
|
|
|
|
|
|
collectgarbage()
|