-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpytorch.py
371 lines (275 loc) · 9.78 KB
/
pytorch.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# coding: utf-8
# In[1]:
import time
import os
import random
import math
import torch
import torchvision
import torch.nn as nn
import torch.optim as optim
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset
from torch.optim import lr_scheduler
from torch.autograd import Variable
from torchvision import datasets, models, transforms
from PIL import Image
import progressbar
plt.ion() # interactive mode
# In[2]:
### Some utilities
# In[3]:
def to_2d_tensor(inp):
inp = torch.Tensor(inp)
if len(inp.size()) < 2:
inp = inp.unsqueeze(0)
return inp
def xywh_to_x1y1x2y2(boxes):
boxes = to_2d_tensor(boxes)
boxes[:, 2] += boxes[:, 0] - 1
boxes[:, 3] += boxes[:, 1] - 1
return boxes
def x1y1x2y2_to_xywh(boxes):
boxes = to_2d_tensor(boxes)
boxes[:, 2] -= boxes[:, 0] - 1
boxes[:, 3] -= boxes[:, 1] - 1
return boxes
def crop_boxes(boxes, im_sizes):
boxes = to_2d_tensor(boxes)
im_sizes = to_2d_tensor(im_sizes)
boxes = xywh_to_x1y1x2y2(boxes)
zero = torch.Tensor([0])
boxes[:, 0] = torch.max(torch.min(boxes[:, 0], im_sizes[:, 0]), zero)
boxes[:, 1] = torch.max(torch.min(boxes[:, 1], im_sizes[:, 1]), zero)
boxes[:, 2] = torch.max(torch.min(boxes[:, 2], im_sizes[:, 0]), zero)
boxes[:, 3] = torch.max(torch.min(boxes[:, 3], im_sizes[:, 1]), zero)
boxes = x1y1x2y2_to_xywh(boxes)
return boxes
def box_transform(boxes, im_sizes):
# box in (x, y, w, h) format
boxes = to_2d_tensor(boxes)
im_sizes = to_2d_tensor(im_sizes)
boxes[:, 0] = 2 * boxes[:, 0] / im_sizes[:, 0] - 1
boxes[:, 1] = 2 * boxes[:, 1] / im_sizes[:, 1] - 1
boxes[:, 2] = 2 * boxes[:, 2] / im_sizes[:, 0]
boxes[:, 3] = 2 * boxes[:, 3] / im_sizes[:, 1]
return boxes
def box_transform_inv(boxes, im_sizes):
# box in (x, y, w, h) format
boxes = to_2d_tensor(boxes)
im_sizes = to_2d_tensor(im_sizes)
boxes[:, 0] = (boxes[:, 0] + 1) / 2 * im_sizes[:, 0]
boxes[:, 1] = (boxes[:, 1] + 1) / 2 * im_sizes[:, 1]
boxes[:, 2] = boxes[:, 2] / 2 * im_sizes[:, 0]
boxes[:, 3] = boxes[:, 3] / 2 * im_sizes[:, 1]
return boxes
def compute_IoU(boxes1, boxes2):
boxes1 = to_2d_tensor(boxes1)
boxes1 = xywh_to_x1y1x2y2(boxes1)
boxes2 = to_2d_tensor(boxes2)
boxes2 = xywh_to_x1y1x2y2(boxes2)
intersec = boxes1.clone()
intersec[:, 0] = torch.max(boxes1[:, 0], boxes2[:, 0])
intersec[:, 1] = torch.max(boxes1[:, 1], boxes2[:, 1])
intersec[:, 2] = torch.min(boxes1[:, 2], boxes2[:, 2])
intersec[:, 3] = torch.min(boxes1[:, 3], boxes2[:, 3])
def compute_area(boxes):
# in (x1, y1, x2, y2) format
dx = boxes[:, 2] - boxes[:, 0]
dx[dx < 0] = 0
dy = boxes[:, 3] - boxes[:, 1]
dy[dy < 0] = 0
return dx * dy
a1 = compute_area(boxes1)
a2 = compute_area(boxes2)
ia = compute_area(intersec)
assert((a1 + a2 - ia <= 0).sum() == 0)
return ia / (a1 + a2 - ia)
def compute_acc(preds, targets, im_sizes, theta=0.75):
preds = box_transform_inv(preds.clone(), im_sizes)
preds = crop_boxes(preds, im_sizes)
targets = box_transform_inv(targets.clone(), im_sizes)
IoU = compute_IoU(preds, targets)
corr = (IoU >= theta).sum()
return corr / preds.size(0)
class AverageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.cnt = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.cnt += n
self.avg = self.sum / self.cnt
# ### Split dataset
# In[4]:
def split(ratio):
with open('data/images.txt') as f:
lines = f.read().splitlines()
class_groups = dict()
for line in lines:
value, line = line.split(' ', 1)
key = line.split('.', 1)[0]
value = value
if key in class_groups:
class_groups[key].append(value)
else:
class_groups[key] = [value]
test_id = []
for _, group in class_groups.items():
test_id.extend(random.sample(group, int(math.ceil(len(group)*ratio))))
train_id = [i for i in map(str, range(1, len(lines)+1)) if i not in test_id]
return train_id, test_id
train_id, test_id = split(0.2)
# In[5]:
class CUBDataset(Dataset):
def __init__(self, im_ids, transform=None):
with open('data/images.txt') as f:
id_to_path = dict([l.split(' ', 1) for l in f.read().splitlines()])
with open('data/bounding_boxes.txt') as f:
id_to_box = dict()
for line in f.read().splitlines():
im_id, *box = line.split(' ')
id_to_box[im_id] = list(map(float, box))
self.imgs = [(os.path.join('data/images', id_to_path[i]), id_to_box[i])
for i in im_ids]
if transform is None:
self.transform = transforms.Compose([
transforms.Scale((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
else:
self.transform = transform
def __getitem__(self, index):
path, box = self.imgs[index]
im = Image.open(path).convert('RGB')
im_size = np.array(im.size, dtype='float32')
box = np.array(box, dtype='float32')
im = self.transform(im)
return im, box, im_size
def __len__(self):
return len(self.imgs)
# In[6]:
### Define Dataset
# In[7]:
splits = {'train': train_id, 'test': test_id}
datasets = {split: CUBDataset(splits[split]) for split in ('train', 'test')}
# ### Visualize training data
# In[8]:
def imshow(img, gt_box, pred_box=None):
plt.imshow(img)
def draw_box(box, color='green'):
x, y, w, h = box_transform_inv(box, img.shape[:2][::-1])[0]
if x == 0:
x = 1
if y == 0:
y = 1
plt.gca().add_patch(
plt.Rectangle((x, y), w, h,
fill=False, edgecolor=color, linewidth=2, alpha=0.5)
)
draw_box(gt_box)
if pred_box is not None:
draw_box(pred_box, 'red')
# In[9]:
ind = random.choice(range(len(datasets['train'])))
im, box, im_size = datasets['train'][ind]
box = box_transform(box, im_size)
inp = im.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
imshow(inp, box[0])
# ### Training
# In[10]:
# prepare data
dataloaders = {split: torch.utils.data.DataLoader(
datasets[split], batch_size=32,shuffle=(split=='train'),
num_workers=2, pin_memory=True) for split in ('train', 'test')}
# construct model
model = models.resnet18(pretrained=True)
fc_in_size = model.fc.in_features
model.fc = nn.Linear(fc_in_size, 4)
model = model.cuda()
criterion = nn.SmoothL1Loss().cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
scheduler = lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.1)
best_model_state = model.state_dict()
best_epoch = -1
best_acc = 0.0
epoch_loss = {'train': [], 'test': []}
epoch_acc = {'train': [], 'test': []}
epochs = 20
for epoch in range(20):
accs = AverageMeter()
losses = AverageMeter()
for phase in ('train', 'test'):
if phase == 'train':
scheduler.step()
model.train(True)
else:
model.train(False)
end = time.time()
bar = progressbar.ProgressBar()
for ims, boxes, im_sizes in bar(dataloaders[phase]):
boxes = crop_boxes(boxes, im_sizes)
boxes = box_transform(boxes, im_sizes)
inputs = Variable(ims.cuda())
targets = Variable(boxes.cuda())
optimizer.zero_grad()
# forward
outputs = model(inputs)
loss = criterion(outputs, targets)
acc = compute_acc(outputs.data.cpu(), targets.data.cpu(), im_sizes)
nsample = inputs.size(0)
accs.update(acc, nsample)
losses.update(loss.data[0], nsample)
if phase == 'train':
loss.backward()
optimizer.step()
if phase == 'test' and accs.avg > best_acc:
best_acc = accs.avg
best_epoch = epoch
best_model_state = model.state_dict()
elapsed_time = time.time() - end
print('[{}]\tEpoch: {}/{}\tLoss: {:.4f}\tAcc: {:.2%}\tTime: {:.3f}'.format(
phase, epoch+1, epochs, losses.avg, accs.avg, elapsed_time))
epoch_loss[phase].append(losses.avg)
epoch_acc[phase].append(accs.avg)
print('[Info] best test acc: {:.2%} at {}th epoch'.format(best_acc, best_epoch))
torch.save(best_model_state, 'best_model_state.path.tar')
# In[11]:
plt.figure(figsize=(15,10))
for phase in ('train', 'test'):
plt.plot(range(len(epoch_loss[phase])), epoch_loss[phase], label=(phase + '_loss'))
plt.plot(range(len(epoch_acc[phase])), epoch_acc[phase], label=(phase + '_acc'))
plt.legend(prop={'size': 15})
# ### Visualize predicting result
# In[12]:
model.load_state_dict(best_model_state)
model = model.cpu()
# In[15]:
ind = random.choice(range(len(datasets['test'])))
im, box, im_size = datasets['test'][ind]
path, _ = datasets['test'].imgs[ind]
print(box)
print(im_size)
box = box_transform(box, im_size)[0]
#print(im_size)
#print(box)
pred_box = model(Variable(im.unsqueeze(0))).data[0]
ori_im = np.array(Image.open(path))
inp = im.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
imshow(ori_im, box, pred_box)
# In[ ]: