forked from motokimura/yolo_v1_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_yolo.py
185 lines (147 loc) · 5.48 KB
/
train_yolo.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
import torch
from torch.utils.data import DataLoader
from torch.autograd import Variable
from voc import VOCDataset
from darknet import DarkNet
from yolo_v1 import YOLOv1
from loss import Loss
import os
import numpy as np
import math
from datetime import datetime
from tensorboardX import SummaryWriter
# Check if GPU devices are available.
use_gpu = torch.cuda.is_available()
assert use_gpu, 'Current implementation does not support CPU mode. Enable CUDA.'
print('CUDA current_device: {}'.format(torch.cuda.current_device()))
print('CUDA device_count: {}'.format(torch.cuda.device_count()))
# Path to data dir.
image_dir = 'data/VOC_allimgs/'
# Path to label files.
train_label = ('data/voc2007.txt', 'data/voc2012.txt')
val_label = 'data/voc2007test.txt'
# Path to checkpoint file containing pre-trained DarkNet weight.
checkpoint_path = 'weights/darknet/model_best.pth.tar'
# Frequency to print/log the results.
print_freq = 5
tb_log_freq = 5
# Training hyper parameters.
init_lr = 0.001
base_lr = 0.01
momentum = 0.9
weight_decay = 5.0e-4
num_epochs = 135
batch_size = 64
# Learning rate scheduling.
def update_lr(optimizer, epoch, burnin_base, burnin_exp=4.0):
if epoch == 0:
lr = init_lr + (base_lr - init_lr) * math.pow(burnin_base, burnin_exp)
elif epoch == 1:
lr = base_lr
elif epoch == 75:
lr = 0.001
elif epoch == 105:
lr = 0.0001
else:
return
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group['lr']
# Load pre-trained darknet.
darknet = DarkNet(conv_only=True, bn=True, init_weight=True)
darknet.features = torch.nn.DataParallel(darknet.features)
src_state_dict = torch.load(checkpoint_path)['state_dict']
dst_state_dict = darknet.state_dict()
for k in dst_state_dict.keys():
print('Loading weight of', k)
dst_state_dict[k] = src_state_dict[k]
darknet.load_state_dict(dst_state_dict)
# Load YOLO model.
yolo = YOLOv1(darknet.features)
yolo.conv_layers = torch.nn.DataParallel(yolo.conv_layers)
yolo.cuda()
# Setup loss and optimizer.
criterion = Loss(feature_size=yolo.feature_size)
optimizer = torch.optim.SGD(yolo.parameters(), lr=init_lr, momentum=momentum, weight_decay=weight_decay)
# Load Pascal-VOC dataset.
train_dataset = VOCDataset(True, image_dir, train_label)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=4)
val_dataset = VOCDataset(False, image_dir, val_label)
val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, num_workers=4)
print('Number of training images: ', len(train_dataset))
# Open TensorBoardX summary writer
log_dir = datetime.now().strftime('%b%d_%H-%M-%S')
log_dir = os.path.join('results/yolo', log_dir)
writer = SummaryWriter(log_dir=log_dir)
# Training loop.
logfile = open(os.path.join(log_dir, 'log.txt'), 'w')
best_val_loss = np.inf
for epoch in range(num_epochs):
print('\n')
print('Starting epoch {} / {}'.format(epoch, num_epochs))
# Training.
yolo.train()
total_loss = 0.0
total_batch = 0
for i, (imgs, targets) in enumerate(train_loader):
# Update learning rate.
update_lr(optimizer, epoch, float(i) / float(len(train_loader) - 1))
lr = get_lr(optimizer)
# Load data as a batch.
batch_size_this_iter = imgs.size(0)
imgs = Variable(imgs)
targets = Variable(targets)
imgs, targets = imgs.cuda(), targets.cuda()
# Forward to compute loss.
preds = yolo(imgs)
loss = criterion(preds, targets)
loss_this_iter = loss.item()
total_loss += loss_this_iter * batch_size_this_iter
total_batch += batch_size_this_iter
# Backward to update model weight.
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Print current loss.
if i % print_freq == 0:
print('Epoch [%d/%d], Iter [%d/%d], LR: %.6f, Loss: %.4f, Average Loss: %.4f'
% (epoch, num_epochs, i, len(train_loader), lr, loss_this_iter, total_loss / float(total_batch)))
# TensorBoard.
n_iter = epoch * len(train_loader) + i
if n_iter % tb_log_freq == 0:
writer.add_scalar('train/loss', loss_this_iter, n_iter)
writer.add_scalar('lr', lr, n_iter)
# Validation.
yolo.eval()
val_loss = 0.0
total_batch = 0
for i, (imgs, targets) in enumerate(val_loader):
# Load data as a batch.
batch_size_this_iter = imgs.size(0)
imgs = Variable(imgs)
targets = Variable(targets)
imgs, targets = imgs.cuda(), targets.cuda()
# Forward to compute validation loss.
with torch.no_grad():
preds = yolo(imgs)
loss = criterion(preds, targets)
loss_this_iter = loss.item()
val_loss += loss_this_iter * batch_size_this_iter
total_batch += batch_size_this_iter
val_loss /= float(total_batch)
# Save results.
logfile.writelines(str(epoch + 1) + '\t' + str(val_loss) + '\n')
logfile.flush()
torch.save(yolo.state_dict(), os.path.join(log_dir, 'model_latest.pth'))
if best_val_loss > val_loss:
best_val_loss = val_loss
torch.save(yolo.state_dict(), os.path.join(log_dir, 'model_best.pth'))
# Print.
print('Epoch [%d/%d], Val Loss: %.4f, Best Val Loss: %.4f'
% (epoch + 1, num_epochs, val_loss, best_val_loss))
# TensorBoard.
writer.add_scalar('test/loss', val_loss, epoch + 1)
writer.close()
logfile.close()