This repository has been archived by the owner on Oct 16, 2021. It is now read-only.
forked from roym899/dd2419_detector_baseline
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
348 lines (285 loc) · 12.9 KB
/
train.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""Training script for detector."""
from __future__ import print_function
import argparse
from datetime import datetime
import os
import torch
from torch import nn
from torchvision.datasets import CocoDetection
import torchvision.transforms.functional as TF
from PIL import Image
import matplotlib.pyplot as plt
import wandb
import utils
from detector import Detector
from config import *
from copy import deepcopy
def train(max_iter, device="cpu"):
"""Train the network.
Args:
max_iter: The maximum of training iterations.
device: The device to train on."""
wandb.init(project="detector_baseline")
# Init model
detector = Detector(NUM_CATEGORIES, device).to(device)
wandb.watch(detector)
dataset = CocoDetection(
root=ROOT,
annFile=ANNFILE,
transforms=detector.input_transform,
)
# TODO: hardcode here
dataset, valid_dataset=torch.utils.data.random_split(dataset, [3500, len(dataset)-3500])
dataloader = torch.utils.data.DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True)
valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=len(valid_dataset), shuffle=True)
# get validation set
for a, b in valid_loader:
img_valid, target_valid = a.to(device), b.to(device)
# training params
max_iterations = wandb.config.max_iterations = max_iter
learning_rate = wandb.config.learning_rate = LEARNING_RATE
weight_reg = wandb.config.weight_reg = WEIGHT_REG
weight_noobj = wandb.config.weight_noobj = WEIGHT_NOOBJ
weight_cls = wandb.config.weight_cls = WEIGHT_CLASS
# run name (to easily identify model later)
time_string = datetime.now().strftime("%Y-%m-%d_%H-%M-%S-%f")
run_name = wandb.config.run_name = "saved_models/det_{}".format(time_string)
# init optimizer
optimizer = torch.optim.Adam(detector.parameters(), lr=learning_rate)
# load training images
train_images = []
show_train_images = False
directory = "./dd2419_coco/training"
if not os.path.exists(directory):
os.makedirs(directory)
# get 1 image from 100
for idx, file_name in enumerate(os.listdir(directory)[::100]):
if file_name.endswith(".jpg"):
file_path = os.path.join(directory, file_name)
train_image = Image.open(file_path)
train_images.append(TF.to_tensor(train_image))
# if idx >= 9: # only use 5 images
# break
if train_images:
train_images = torch.stack(train_images)
train_images = train_images.to(device)
show_train_images = True
# load test images
# these will be evaluated in regular intervals
test_images = []
show_test_images = False
directory = "./test_images"
if not os.path.exists(directory):
os.makedirs(directory)
for file_name in os.listdir(directory):
if file_name.endswith(".jpg"):
file_path = os.path.join(directory, file_name)
test_image = Image.open(file_path)
test_images.append(TF.to_tensor(test_image))
if test_images:
test_images = torch.stack(test_images)
test_images = test_images.to(device)
show_test_images = True
print("Training started...")
current_iteration = 1
while current_iteration <= max_iterations:
detector.train()
train_cls_all = 0
train_cls_correct = 0
for img_batch, target_batch in dataloader:
img_batch = img_batch.to(device) # torch.Size([8, 3, 480, 640])
target_batch = target_batch.to(device) # Batch, 5+category, 15, 20
# run network
out = detector(img_batch) # torch.Size([8, 5+category, 15, 20])
# positive / negative indices
# (this could be passed from input_transform to avoid recomputation)
# check tensor: torch.Size([8, 15, 20])
pos_indices = torch.nonzero(target_batch[:, 4, :, :] == 1, as_tuple=True)
# (torch.Size([8]), torch.Size([8]), torch.Size([8]))
neg_indices = torch.nonzero(target_batch[:, 4, :, :] == 0, as_tuple=True)
# (torch.Size([2392]), torch.Size([2392]), torch.Size([2392]))
# compute loss
# bounding box err
reg_mse = nn.functional.mse_loss(
out[
pos_indices[0], 0:4, pos_indices[1], pos_indices[2]
], # torch.Size([8, 4])
# each [4]: out[xx[0][0], 0:4, xx[1][0], xx[2][0]
target_batch[
pos_indices[0], 0:4, pos_indices[1], pos_indices[2]
], # torch.Size([8, 4])
)
# confidence err where box exists
pos_mse = nn.functional.mse_loss(
out[pos_indices[0], 4, pos_indices[1], pos_indices[2]],
target_batch[pos_indices[0], 4, pos_indices[1], pos_indices[2]],
)
# confidence err where box not exists
# TODO: replace MSE with crossentropy, add a param in config.py
neg_mse = nn.functional.mse_loss(
out[neg_indices[0], 4, neg_indices[1], neg_indices[2]],
target_batch[neg_indices[0], 4, neg_indices[1], neg_indices[2]],
)
# class err
out_cls = out[pos_indices[0], 5:, pos_indices[1], pos_indices[2]]
label_cls = target_batch[pos_indices[0], 5:, pos_indices[1], pos_indices[2]]
if OUTPUT_FUNC == 'softmax':
# y_tensor = torch.tensor(label_cls, dtype=torch.long, device=device)
cls_mse = nn.functional.cross_entropy(out_cls, torch.max(label_cls, 1)[1])
else:
cls_mse = nn.functional.mse_loss(out_cls, label_cls)
# class acc
train_cls_predict = torch.argmax(out_cls, 1)
train_cls_label = torch.argmax(label_cls, 1)
train_cls_correct += torch.sum(train_cls_predict == train_cls_label).item()
train_cls_all += train_cls_label.numel()
loss = (
pos_mse
+ weight_reg * reg_mse
+ weight_noobj * neg_mse
+ weight_cls * cls_mse
)
# optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
wandb.log(
{
"total loss": loss.item(),
"loss pos": pos_mse.item(),
"loss neg": neg_mse.item(),
"loss reg": reg_mse.item(),
"loss cls": cls_mse.item(),
},
step=current_iteration,
)
print(
"\rIteration: {}, loss: {}".format(current_iteration, loss.item()),
end="",
)
# generate visualization every N iterations
show_images = show_train_images and show_test_images
if current_iteration % 250 == 0 and show_images:
with torch.no_grad():
detector.eval()
# train_images: torch.Size([5, 3, 480, 640])
# out: torch.Size([batch_size, channels, 15, 20])
out = detector(train_images).cpu() # training
bbs = detector.decode_output(out)
out_test = detector(test_images).cpu() # test
bbs_test = detector.decode_output(out_test)
# attr of bbs: width, height, x, y, category
for i, image in enumerate(train_images):
figure, ax = plt.subplots(1)
plt.imshow(image.cpu().permute(1, 2, 0))
plt.imshow(
out[i, 4, :, :],
interpolation="nearest",
extent=(0, 640, 480, 0),
alpha=0.7,
)
# add bounding boxes
utils.add_bounding_boxes(
ax, bbs[i], category_dict=CATEGORY_DICT
)
wandb.log(
{"train_img_{i}".format(i=i): figure}, step=current_iteration
)
plt.close()
for i, image in enumerate(test_images):
figure, ax = plt.subplots(1)
plt.imshow(image.cpu().permute(1, 2, 0))
plt.imshow(
out_test[i, 4, :, :],
interpolation="nearest",
extent=(0, 640, 480, 0),
alpha=0.7,
)
# add bounding boxes
utils.add_bounding_boxes(
ax, bbs_test[i], category_dict=CATEGORY_DICT
)
wandb.log(
{"test_img_{i}".format(i=i): figure}, step=current_iteration
)
plt.close()
detector.train()
if not (current_iteration % 20):
with torch.no_grad():
detector.eval()
out = detector(img_valid)
pos_indices = torch.nonzero(target_valid[:, 4, :, :] == 1, as_tuple=True)
neg_indices = torch.nonzero(target_valid[:, 4, :, :] == 0, as_tuple=True)
reg_mse = nn.functional.mse_loss(
out[
pos_indices[0], 0:4, pos_indices[1], pos_indices[2]
], # torch.Size([8, 4])
# each [4]: out[xx[0][0], 0:4, xx[1][0], xx[2][0]
target_valid[
pos_indices[0], 0:4, pos_indices[1], pos_indices[2]
], # torch.Size([8, 4])
)
# confidence err where box exists
pos_mse = nn.functional.mse_loss(
out[pos_indices[0], 4, pos_indices[1], pos_indices[2]],
target_valid[pos_indices[0], 4, pos_indices[1], pos_indices[2]],
)
# confidence err where box not exists
# TODO: replace MSE with crossentropy, add a param in config.py
neg_mse = nn.functional.mse_loss(
out[neg_indices[0], 4, neg_indices[1], neg_indices[2]],
target_valid[neg_indices[0], 4, neg_indices[1], neg_indices[2]],
)
# class err
out_cls = out[pos_indices[0], 5:, pos_indices[1], pos_indices[2]]
label_cls = target_valid[pos_indices[0], 5:, pos_indices[1], pos_indices[2]]
if OUTPUT_FUNC == 'softmax':
# y_tensor = torch.tensor(label_cls, dtype=torch.long, device=device)
cls_mse = nn.functional.cross_entropy(out_cls, torch.max(label_cls, 1)[1])
else:
cls_mse = nn.functional.mse_loss(out_cls, label_cls)
loss = (
pos_mse
+ weight_reg * reg_mse
+ weight_noobj * neg_mse
+ weight_cls * cls_mse
)
wandb.log(
{
"total loss _valid": loss.item(),
"loss pos _valid": pos_mse.item(),
"loss neg _valid": neg_mse.item(),
"loss reg _valid": reg_mse.item(),
"loss cls _valid": cls_mse.item(),
},
step=current_iteration,
)
detector.train()
current_iteration += 1
if current_iteration > max_iterations:
break
# training_acc = train_cls_correct/train_cls_all
# # TODO
# wandb.log(
# {
# "training acc": training_acc
# },
# step=current_iteration,
# )
# if current_iteration % 200 == 0:
# print("training_acc: %f" % training_acc)
print("\nTraining completed (max iterations reached)")
model_path = "{}.pt".format(run_name)
utils.save_model(detector, model_path)
print("save model:", model_path)
if device == "cpu":
wandb.save(model_path)
print("Model weights saved at {}".format(model_path))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
device = parser.add_mutually_exclusive_group(required=True)
device.add_argument("--cpu", dest="device", action="store_const", const="cpu")
device.add_argument("--gpu", dest="device", action="store_const", const="cuda")
parser.add_argument("MAX_ITER", type=int, default=3000)
args = parser.parse_args()
train(int(args.MAX_ITER), args.device)