-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrain.lua
167 lines (153 loc) · 4.93 KB
/
train.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require 'xlua'
require 'optim'
require 'nn'
require 'torch'
require 'pl'
require 'paths'
require 'loadcaffe'
require 'cunn'
require 'image'
require 'cudnn'
local c = require 'trepl.colorize'
opt = lapp[[
--save (default "logs")
-b,--batchSize (default 128) batch size
-r,--learningRate (default .1) learning rate
--gamma (default .0001) learning rate decay using inverse policy
--p (default 0.75) power
--optimization (default 'SGD')
--weightDecay (default 0.0005) weightDecay
--learningRateDecay (default 1e-4)
-m,--momentum (default 0.9) momentum
-m,--model (default alexnet) model name
-t,--type (default cuda) float/cuda
]]
--opt.model='alexnet'
print(opt.model,opt.type,opt.batchSize)
local function cast(t)
if opt.type == 'cuda' then
require 'cunn'
return t:cuda()
elseif opt.type == 'float' then
return t:float()
elseif opt.type == 'cl' then
require 'clnn'
return t:cl()
else
error('Unknown type '..opt.type)
end
end
print(c.blue '==>' ..' configuring model')
print(c.blue'==>' ..' setting criterion')
criterion = cast(nn.CrossEntropyCriterion())
print(c.blue'==>' ..' configuring optimizer')
confusion = optim.ConfusionMatrix(871)
local datapath_file = io.open("datasets/cuhk01_test100.txt")
image_paths={}
labels={}
if datapath_file then
i=0
for line in datapath_file:lines() do
local path,label=unpack(line:split(" "))
image_paths[i+1]=path
labels[i+1]=label
i=i+1
end
end
pretrained_model = 'alexnet'
img_size = 227
if(pretrained_model == 'alexnet') then
net=torch.load('saved_model/finetuned_alexnet.t7'):cuda()
-- net = loadcaffe.load('deploy.prototxt', 'bvlc_alexnet.caffemodel', 'cudnn')
--reinitialize the weights for fc layers and change the last fc layer
-- fc1=net:get(17)
-- --print(fc1.weight[1])
-- fc1:reset()
-- fc2=net:get(20)
-- fc2:reset()
-- net:remove(23)
-- net:insert(nn.Linear(4096,871):cuda(),23)
-- net:remove(24)
-- print(net)
parameters,gradParameters = net:getParameters()
mean_image=image.load("mean_image.png"):cuda() * 255
temp=mean_image[3]:clone()
mean_image[3]=mean_image[1]
mean_image[1]=temp
img_size = 227
else
net = loadcaffe.load('VGG_ILSVRC_16_layers_deploy.prototxt', 'VGG_ILSVRC_16_layers.caffemodel', 'cudnn')
print(net)
--net:get(33):reset()
--net:get(36):reset()
net:remove(39)
net:insert(nn.Linear(4096, 871):cuda(), 39)
net:remove(40)
print(net)
parameters,gradParameters = net:getParameters()
mean_image=torch.zeros(3,224,224):cuda()
mean_image[1] = torch.Tensor({103.939}):repeatTensor(224,224)
mean_image[2] = torch.Tensor({116.779}):repeatTensor(224,224)
mean_image[3] = torch.Tensor({123.68}):repeatTensor(224,224)
img_size = 224
end
lrs = torch.zeros(parameters:size(1)):fill(0.001)
lrs[{{parameters:size(1) - (4096*871 + 871) + 1, parameters:size(1)}}] = 1
optimState = {
learningRates = lrs,
learningRate = opt.learningRate,
weightDecay = opt.weightDecay,
momentum = opt.momentum,
learningRateDecay = opt.learningRateDecay,
}
print(optimState)
epoch=0
--local trainLogger = optim.Logger(paths.concat(opt.save, 'train.log'))
function train()
net:training()
epoch = epoch+1
print(c.blue '==>'.." online epoch # " .. epoch .. ' [batchSize = ' ..opt.batchSize .. ']')
local target = cast(torch.FloatTensor(opt.batchSize))
local indices = torch.randperm(#labels):long():split(opt.batchSize)
indices[#indices] = nil
for t,v in ipairs(indices) do
input=torch.CudaTensor(opt.batchSize,3,img_size,img_size)
for i=1,opt.batchSize do
target[i]=labels[v[i]]
img=image.scale(image.load(image_paths[v[i]]), img_size, img_size):cuda() * 255
temp=img[3]:clone()
img[3]=img[1]
img[1]=temp
input[i]=img - mean_image
end
input=input:cuda()
target=target:cuda()
local feval = function(x)
gradParameters:zero()
local outputs = net:forward(input)
local f = criterion:forward(outputs, target)
--print(target)
local df_do = criterion:backward(outputs, target)
net:backward(input, df_do)
confusion:batchAdd(outputs, target)
confusion:updateValids()
--print(outputs:size())
print('epoch ',epoch,' loss ',f,'train accuracy ',confusion.totalValid * 100)
--trainLogger:add{[' % loss'] = f}
return f,gradParameters
end
optim.sgd(feval, parameters, optimState)
--print(fc1.weight[1])
end
--trainLogger:plot()
confusion:updateValids()
--print(('Train accuracy: '..c.cyan'%.2f'..' %%\t time: %.2f s'):format(
--confusion.totalValid * 100, torch.toc)
train_acc = confusion.totalValid * 100
confusion:zero()
print(c.blue'==>' ..' saving model')
torch.save('saved_model/finetuned_'.. pretrained_model.. '.caffemodel',net)
end
for i=1,40 do
train()
end