-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcifar10_utils.lua
214 lines (199 loc) · 7.16 KB
/
cifar10_utils.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require "optim"
require "image"
utils = {}
utils.opt = { batch_size = 64,
cuda = true,
--opt.train_size = 60000,
test_size = 0,
epochs = 2,
eval_batch_size = 256,
eval_iters = 5000,
eval_train_iters = 1000,
noise = 0.25,
optimMethod = optim.sgd,
optimState = {
learningRate = 1,
weightDecay = 1e-3,
momentum = 0.9,
learningRateDecay = 0
}
}
utils.err = {any = false}
utils.feval_counter = 0
-- different data augmentation methods
utils.flip_augment = function(self, minibatch)
--augmented = torch.FloatTensor(minibatch:size())
for i = 1, minibatch:size(1) do
if torch.rand(1)[1] > 0.5 then image.hflip(augmented[i], minibatch[i]) end
end
return augmented
end
utils.crop5_flip_augment = function(self, minibatch)
augmented = torch.FloatTensor(minibatch:size())
for i = 1, minibatch:size(1) do
local crop_size = 5
local x = torch.random(1, crop_size)
local y = torch.random(1, crop_size)
local x_crop = {x , 32 - crop_size + x}
local y_crop = {y, 32 - crop_size + y}
augmented[i] = image.scale(minibatch[i][{{}, x_crop, y_crop}],32,32)
if torch.rand(1)[1] > 0.5 then image.hflip(augmented[i], augmented[i]) end
end
return augmented
end
utils.edge_crop_flip_augment = function(self, minibatch)
augmented = torch.FloatTensor(minibatch:size())
for i = 1, minibatch:size(1) do
local crop_size = 26
local crops = { {{1, crop_size},{1, crop_size}},
{{1, crop_size}, {33 - crop_size, 32}},
{{33 - crop_size, 32}, {1, crop_size, 32}},
{{33 - crop_size, 32}, {33 - crop_size, 32}},
{{(33-crop_size)/2, 33 - (33-crop_size)/2}, {(33-crop_size)/2, 33 - (33-crop_size)/2}}
}
local n = torch.random(1,5)
augmented[i] = image.scale(minibatch[i][{{},crops[n][1],crops[n][2]}],32,32)
if torch.rand(1)[1] > 0.5 then image.hflip(augmented[i], augmented[i]) end
end
return augmented
end
utils.rand_crop_flip_augment = function(self, minibatch)
augmented = torch.FloatTensor(minibatch:size())
for i = 1, minibatch:size(1) do
local crop_size = torch.random(1,5)
local x = torch.random(1, crop_size)
local y = torch.random(1, crop_size)
local x_crop = {x , 32 - crop_size + x}
local y_crop = {y, 32 - crop_size + y}
augmented[i] = image.scale(minibatch[i][{{}, x_crop, y_crop}],32,32)
if torch.rand(1)[1] > 0.5 then image.hflip(augmented[i], augmented[i]) end
end
return augmented
end
utils.no_augment = function(self, minibatch)
return minibatch
end
utils.augment = function(self)
print("error: no augment method defined")
end
utils.train_epoch = function(self)
utils.net:training()
local last_iter = false
local minibatch = {}
minibatch.data = torch.CudaTensor(self.opt.batch_size, 3, 32, 32)
minibatch.labels= torch.CudaTensor(self.opt.batch_size)
local counter = 0
local epoch_loss = 0
while last_iter == false do
local start_index = math.min(counter * utils.opt.batch_size + 1, trainset.data:size(1))
local end_index = math.min((counter + 1)* utils.opt.batch_size, trainset.data:size(1))
if end_index >= trainset:size() then
last_iter = true
end
counter = counter + 1
augmented = self:augment(trainset.data[{{start_index, end_index}}])
minibatch.data[{{1, end_index - start_index + 1}}]:copy(augmented) -- now in gpu mem
minibatch.labels:copy(trainset.label[{{start_index, end_index}}])
local feval = function (x)
if utils.parameters ~= x then
print("x and parameters are different tensors")
utils.parameters:copy(x)
end
utils.net:zeroGradParameters()
minibatch.outputs = utils.net:forward(minibatch.data)
minibatch.loss = utils.criterion:forward(minibatch.outputs, minibatch.labels)
minibatch.dloss_doutput = utils.criterion:backward(minibatch.outputs, minibatch.labels)
utils.net:backward(minibatch.data, minibatch.dloss_doutput)
if minibatch.loss == math.huge or minibatch.loss == -math.huge then
print("err: loss is " .. minibatch.loss)
last_iter = true
end
epoch_loss = epoch_loss + minibatch.loss
return bm_loss, utils.gradParameters
end
utils.opt.optimMethod(feval, utils.parameters, utils.opt.optimState)
-- DataParallelTable return true
if(self.net.name ~= nil and self.net:name() == "DataParallelTable") then
self.net:syncParameters()
end
end
return epoch_loss/counter
end
utils.evaluate = function (self, set)
self.net:evaluate()
local size = set:size()
local minib_counter = 0
local loss_acc = 0
local last_minibatch = false
local prediction_list = torch.Tensor(size):cuda()
local minibatch = {}
local correct_count = 0
while(last_minibatch ~= true) do
local start_index = minib_counter * self.opt.eval_batch_size +1
local end_index = math.min((minib_counter + 1)* self.opt.eval_batch_size, size)
if end_index == size then
last_minibatch = true
else
minib_counter = minib_counter +1
end
minibatch.data = set.data[{{start_index,end_index}}]:cuda()
minibatch.labels = set.label[{{start_index,end_index}}]:cuda()
minibatch.outputs = self.net:forward(minibatch.data)
minibatch.loss = self.criterion:forward(minibatch.outputs, minibatch.labels)
for j = 1, minibatch.data:size(1) do
local groundtruth = minibatch.labels[j]
local prediction = minibatch.outputs[j]
local confidences, indices = torch.sort(prediction, true)
if groundtruth == indices[1] then
correct_count = correct_count +1
end
end
loss_acc = loss_acc + minibatch.loss
local _maxs, inds = minibatch.outputs:max(2)
prediction_list[{{start_index, end_index}}] = inds[{{},1}]
end
loss_acc = loss_acc/(minib_counter+1)
local hist = torch.Tensor(10):zero()
prediction_list:apply(function(x)
hist[x] = hist[x] +1
end)
local error_rate = 1 - correct_count/size
return loss_acc, hist, error_rate
end
utils.visualize_example = function (self, set, i)
print(classes[set.label[i]])
itorch.image(image.scale(set.data[i],90,90))
local predicted = utils.net:forward(set.data[i]:cuda())
predicted:exp()
local confidences, indices = torch.sort(predicted, true)
for j = 1, predicted:size(1) do
print(classes[indices[j]]..string.format(" %.3f%%", confidences[j]*100))
end
end
utils.visualize = function (self, n)
utils.net:evaluate()
for i = 1, n do
print("=======")
utils:visualize_example(testset, math.ceil(torch.uniform(1,testset:size())))
end
end
utils.class_error = function(set)
local class_perf = {0,0,0,0,0,0,0,0,0,0}
local t_size = set:size()
for i = 1, t_size do
local groundtruth = set.label[i]
local prediction = utils.net:forward(set.data[i])
local confidences, indices = torch.sort(prediction, true)
if groundtruth == indices[1] then
class_perf[groundtruth] = class_perf[groundtruth] +1
end
end
sorted_perf = torch.Tensor(class_perf):sort(true)
for i = 1, 10 do
print(classes[i], 1000*sorted_perf[i]/t_size..' %')
end
end
utils.ping = function(self)
os.execute("mpg123 ~/workspace/SVHN_torch/sounds/ping.mp3 &")
end
return utils