-
Notifications
You must be signed in to change notification settings - Fork 16
/
train_retinanet.py
306 lines (207 loc) · 8.93 KB
/
train_retinanet.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
#!/usr/bin/env python
# coding: utf-8
# # setup dataset
# In[1]:
# import stuff
import os
import numpy as np
import time
import pandas as pd
import torch
import torch.utils.data as data
from itertools import product as product
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
from torch.autograd import Function
backbone = "resnet50"
# In[2]:
# import dataset
from utils.dataset import VOCDataset, DatasetTransform, make_datapath_list, Anno_xml2list, od_collate_fn
# ## make data.Dataset for training
# In[3]:
# load files
# set your VOCdevkit path!
vocpath = "../VOCdevkit/VOC2007"
train_img_list, train_anno_list, val_img_list, val_anno_list = make_datapath_list(vocpath)
vocpath = "../VOCdevkit/VOC2012"
train_img_list2, train_anno_list2, _, _ = make_datapath_list(vocpath)
train_img_list.extend(train_img_list2)
train_anno_list.extend(train_anno_list2)
print("trainlist: ", len(train_img_list))
print("vallist: ", len(val_img_list))
# make Dataset
voc_classes = ['aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor']
color_mean = (104, 117, 123) # (BGR)の色の平均値
input_size = 300 # 画像のinputサイズを300×300にする
## DatasetTransformを適応
transform = DatasetTransform(input_size, color_mean)
transform_anno = Anno_xml2list(voc_classes)
# Dataloaderに入れるデータセットファイル。
# ゲットで叩くと画像とGTを前処理して出力してくれる。
train_dataset = VOCDataset(train_img_list, train_anno_list, phase = "train", transform=transform, transform_anno = transform_anno)
val_dataset = VOCDataset(val_img_list, val_anno_list, phase="val", transform=DatasetTransform(
input_size, color_mean), transform_anno=Anno_xml2list(voc_classes))
batch_size = 24
train_dataloader = data.DataLoader(
train_dataset, batch_size=batch_size, shuffle=True, collate_fn=od_collate_fn, num_workers=8)
val_dataloader = data.DataLoader(
val_dataset, batch_size=batch_size, shuffle=False, collate_fn=od_collate_fn, num_workers=8)
# 辞書型変数にまとめる
dataloaders_dict = {"train": train_dataloader, "val": val_dataloader}
# In[4]:
# 動作の確認
batch_iterator = iter(dataloaders_dict["val"]) # イタレータに変換
images, targets = next(batch_iterator) # 1番目の要素を取り出す
print(images.size()) # torch.Size([4, 3, 300, 300])
print(len(targets))
print(targets[1].shape) # ミニバッチのサイズのリスト、各要素は[n, 5]、nは物体数
# # define SSD model
# In[5]:
from utils.retinanet import RetinaFPN as SSD
from utils.retinanet import Bottleneck
# In[6]:
# SSD300の設定
ssd_cfg = {
'num_classes': 21, # 背景クラスを含めた合計クラス数
'input_size': 300, # 画像の入力サイズ
'bbox_aspect_num': [4, 6, 6, 6, 4, 4], # 出力するDBoxのアスペクト比の種類
'feature_maps': [38, 19, 10, 5, 3, 1], # 各sourceの画像サイズ
'steps': [8, 16, 32, 64, 100, 300], # DBOXの大きさを決める
'min_sizes': [30, 60, 111, 162, 213, 264], # DBOXの大きさを決める
'max_sizes': [60, 111, 162, 213, 264, 315], # DBOXの大きさを決める
'aspect_ratios': [[2], [2, 3], [2, 3], [2, 3], [2], [2]],
}
net = SSD(Bottleneck, [2,2,2,2], phase="train", cfg=ssd_cfg, model=backbone)
# SSDのweightsを設定
#print("using vgg weights")
#vgg_weights = torch.load("./weights/vgg16_reducedfc.pth")
#net.vgg.load_state_dict(vgg_weights)
def weights_init(m):
if isinstance(m, nn.Conv2d):
init.kaiming_normal_(m.weight.data)
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
# GPUが使えるか確認
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("using:", device)
print("set weights!")
# In[7]:
print(net)
# In[8]:
from utils.ssd_model import MultiBoxLoss
# define loss
criterion = MultiBoxLoss(jaccard_thresh=0.5,neg_pos=3, device=device)
# optim
import torch.optim as optim
optimizer = optim.SGD(net.parameters(), lr=1e-3, momentum=0.9, weight_decay=5e-4)
# In[9]:
def get_current_lr(epoch):
lr = 1e-3
for i,lr_decay_epoch in enumerate([120,180]):
if epoch >= lr_decay_epoch:
lr *= 0.1
return lr
def adjust_learning_rate(optimizer, epoch):
lr = get_current_lr(epoch)
print("lr is:", lr)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
# In[10]:
# モデルを学習させる関数を作成
def train_model(net, dataloaders_dict, criterion, optimizer, num_epochs):
# GPUが使えるかを確認
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("used device:", device)
# ネットワークをGPUへ
net.to(device)
# ネットワークがある程度固定であれば、高速化させる
torch.backends.cudnn.benchmark = True
# イテレーションカウンタをセット
iteration = 1
epoch_train_loss = 0.0 # epochの損失和
epoch_val_loss = 0.0 # epochの損失和
logs = []
# epochのループ
for epoch in range(num_epochs+1):
adjust_learning_rate(optimizer, epoch)
# 開始時刻を保存
t_epoch_start = time.time()
t_iter_start = time.time()
print('-------------')
print('Epoch {}/{}'.format(epoch+1, num_epochs))
print('-------------')
# epochごとの訓練と検証のループ
for phase in ['train', 'val']:
if phase == 'train':
net.train() # モデルを訓練モードに
print('(train)')
else:
if((epoch+1) % 10 == 0):
net.eval() # モデルを検証モードに
print('-------------')
print('(val)')
else:
# 検証は10回に1回だけ行う
continue
# データローダーからminibatchずつ取り出すループ
for images, targets in dataloaders_dict[phase]:
# GPUが使えるならGPUにデータを送る
images = images.to(device)
targets = [ann.to(device)
for ann in targets] # リストの各要素のテンソルをGPUへ
# optimizerを初期化
optimizer.zero_grad()
# 順伝搬(forward)計算
with torch.set_grad_enabled(phase == 'train'):
# 順伝搬(forward)計算
outputs = net(images)
# 損失の計算
loss_l, loss_c = criterion(outputs, targets)
loss = loss_l + loss_c
# 訓練時はバックプロパゲーション
if phase == 'train':
loss.backward() # 勾配の計算
# 勾配が大きくなりすぎると計算が不安定になるので、clipで最大でも勾配2.0に留める
nn.utils.clip_grad_value_(
net.parameters(), clip_value=2.0)
optimizer.step() # パラメータ更新
if (iteration % 10 == 0): # 10iterに1度、lossを表示
t_iter_finish = time.time()
duration = t_iter_finish - t_iter_start
print('Iter {} || Loss: {:.4f} || 10iter: {:.4f} sec.'.format(
iteration, loss.item(), duration))
t_iter_start = time.time()
epoch_train_loss += loss.item()
iteration += 1
# 検証時
else:
epoch_val_loss += loss.item()
# epochのphaseごとのlossと正解率
t_epoch_finish = time.time()
print('-------------')
print('epoch {} || Epoch_TRAIN_Loss:{:.4f} ||Epoch_VAL_Loss:{:.4f}'.format(
epoch+1, epoch_train_loss, epoch_val_loss))
print('timer: {:.4f} sec.'.format(t_epoch_finish - t_epoch_start))
t_epoch_start = time.time()
# ログを保存
log_epoch = {'epoch': epoch+1,
'train_loss': epoch_train_loss, 'val_loss': epoch_val_loss}
logs.append(log_epoch)
df = pd.DataFrame(logs)
df.to_csv("log_output.csv")
epoch_train_loss = 0.0 # epochの損失和
epoch_val_loss = 0.0 # epochの損失和
# ネットワークを保存する
if ((epoch+1) % 10 == 0):
torch.save(net.state_dict(), 'weights/retinanet300_' +
str(epoch+1) + '.pth')
# In[ ]:
num_epochs = 200
train_model(net, dataloaders_dict, criterion, optimizer, num_epochs=num_epochs)
# In[ ]: