-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexperiment6.lua
More file actions
200 lines (148 loc) · 4.5 KB
/
experiment6.lua
File metadata and controls
200 lines (148 loc) · 4.5 KB
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
require 'torch'
require 'nn'
require 'optim'
require 'image'
require 'sys'
training_images = {}
training_labels = {}
batch_size = 64
files = 0
label_counter = 1
for folder in paths.files('/data/datasets/imagenet_resized/train/') do
if #folder > 2 then
before = #training_images
for file in paths.files('/data/datasets/imagenet_resized/train/' .. folder) do
if #file > 2 then
table.insert(training_images, '/data/datasets/imagenet_resized/train/' .. folder .. '/'.. file)
table.insert(training_labels, label_counter)
end
end
after = #training_images
if (after - before) > 0 then
label_counter = label_counter + 1
end
end
end
nice_n = (math.floor(#training_images / batch_size) * batch_size)
print(#training_images)
print(#training_labels)
function swap(array, index1, index2)
array[index1], array[index2] = array[index2], array[index1]
end
function shuffle(array1, array2)
local counter = #array1
while counter > 1 do
local index = math.random(counter)
swap(array1, index, counter)
swap(array2, index, counter)
counter = counter - 1
end
end
shuffle(training_images, training_labels)
function get_batch()
local index = 1
B = torch.Tensor(batch_size,3,256,256)
L = torch.Tensor(batch_size)
while index <= batch_size do
local imagetensor = image.load(training_images[current_index])
if imagetensor:size(1) == 1 then
B[index][1] = imagetensor
B[index][2] = imagetensor
B[index][3] = imagetensor
else
B[index] = imagetensor
end
B[index] = B[index] / 255
L[index] = training_labels[current_index]
index = index + 1
current_index = current_index + 1
end
collectgarbage()
return B:double(), L
end
model = nn.Sequential()
--- conv1
model:add(nn.SpatialConvolution(3, 16, 3, 3, 1, 1, 1, 1))
model:add(nn.ReLU())
model:add(nn.SpatialMaxPooling(2, 2, 2, 2))
--- conv2
model:add(nn.SpatialConvolution(16, 16, 3, 3, 1, 1, 1, 1))
model:add(nn.ReLU())
model:add(nn.SpatialMaxPooling(2, 2, 2, 2))
--- conv3
model:add(nn.SpatialConvolution(16, 32, 3, 3, 1, 1, 1, 1))
model:add(nn.ReLU())
model:add(nn.SpatialMaxPooling(2, 2, 2, 2))
--- conv4
model:add(nn.SpatialConvolution(32, 32, 3, 3, 1, 1, 1, 1))
model:add(nn.ReLU())
model:add(nn.SpatialMaxPooling(2, 2, 2, 2))
--- conv5
model:add(nn.SpatialConvolution(32, 32, 3, 3, 1, 1, 1, 1))
model:add(nn.ReLU())
model:add(nn.SpatialMaxPooling(2, 2, 2, 2))
model:add(nn.Reshape(2048))
--- fc1
model:add(nn.Linear(2048, 2048))
model:add(nn.ReLU())
--- fc2
model:add(nn.Linear(2048, 1000))
sgd_params = {
learningRate = 0.01,
learningRateDecay = 0.0,
weightDecay = 0.0,
momentum = 0.9
}
model:add(nn.LogSoftMax())
criterion = nn.ClassNLLCriterion()
x, dl_dx = model:getParameters()
print('<mnist> using model:')
print(model)
run_epoch = function()
local current_loss = 0
local count = 0
current_index = 1
while current_index + batch_size < #training_images do
sys.tic()
local inputs, targets = get_batch()
local feval = function(x_new)
-- reset data
if x ~= x_new then x:copy(x_new) end
dl_dx:zero()
-- perform mini-batch gradient descent
local loss = criterion:forward(model:forward(inputs), targets)
model:backward(inputs, criterion:backward(model.output, targets))
return loss, dl_dx
end
_, fs = optim.sgd(feval, x, sgd_params)
count = count + 1
current_loss = current_loss + fs[1]
t = sys.toc()
print(string.format("Batches: %d/%d loss: %4f time:%4fms", current_index, #training_images,fs[1], 1000 * t))
end
-- normalize loss
return current_loss / count
end
eval = function()
local count = 0
current_index = 1
while current_index + batch_size < #training_images do
local inputs, targets = get_batch()
local outputs = model:forward(inputs)
local _, indices = torch.max(outputs, 2)
indices:add(-1)
local guessed_right = indices:eq(targets:long()):sum()
count = count + guessed_right
end
return count / nice_n
end
max_iters = 5
print("Start training")
do
for i = 1,max_iters do
local loss = run_epoch()
print(string.format('Epoch: %d loss: %4f', i, loss))
end
end
local accuracy = eval()
print(string.format('Final: acc: %5f', accuracy))