forked from Mezmeriz/Yolo-v2-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_twoShapes.py
199 lines (183 loc) · 9.79 KB
/
train_twoShapes.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
"""
@author: Viet Nguyen <[email protected]>
"""
import os
import argparse
import torch.nn as nn
from torch.utils.data import DataLoader
from src.twoShapes_dataset import TwoShapesDataset
from src.utils import *
from src.loss import YoloLoss
from src.yolo_net import Yolo
from tensorboardX import SummaryWriter
import shutil
def weights_init(m):
if isinstance(m, nn.Conv2d):
nn.init.kaiming_uniform_(m.weight.data)
nn.init.kaiming_uniform_(m.bias.data)
def get_args():
parser = argparse.ArgumentParser("You Only Look Once: Unified, Real-Time Object Detection")
parser.add_argument("--image_size", type=int, default=448, help="The common width and height for all images")
parser.add_argument("--batch_size", type=int, default=10, help="The number of images per batch")
parser.add_argument("--momentum", type=float, default=0.9)
parser.add_argument("--decay", type=float, default=0.0005)
parser.add_argument("--dropout", type=float, default=0.5)
parser.add_argument("--num_epoches", type=int, default=160)
parser.add_argument("--test_interval", type=int, default=5, help="Number of epoches between testing phases")
parser.add_argument("--object_scale", type=float, default=1.0)
parser.add_argument("--noobject_scale", type=float, default=0.5)
parser.add_argument("--class_scale", type=float, default=1.0)
parser.add_argument("--coord_scale", type=float, default=5.0)
parser.add_argument("--reduction", type=int, default=32)
parser.add_argument("--es_min_delta", type=float, default=0.0,
help="Early stopping's parameter: minimum change loss to qualify as an improvement")
parser.add_argument("--es_patience", type=int, default=0,
help="Early stopping's parameter: number of epochs with no improvement after which training will be stopped. Set to 0 to disable this technique.")
parser.add_argument("--train_set", type=str, default="train")
parser.add_argument("--test_set", type=str, default="val")
parser.add_argument("--year", type=str, default="2014", help="The year of dataset (2014 or 2017)")
parser.add_argument("--data_path", type=str, default="/home/sadams/dataNeural/yolo1", help="the root folder of dataset")
parser.add_argument("--pre_trained_model_type", type=str, choices=["model", "params", "none"], default="model")
parser.add_argument("--pre_trained_model_path", type=str, default="trained_models/test_freshModel")
parser.add_argument("--log_path", type=str, default="tensorboard/yolo_twoShapes")
parser.add_argument("--saved_path", type=str, default="trained_models")
parser.add_argument("--save_file", type=str, default="test_freshModelB")
args = parser.parse_args()
return args
def train(opt):
if torch.cuda.is_available():
torch.cuda.manual_seed(123)
else:
torch.manual_seed(123)
learning_rate_schedule = {"0": 1e-5, "5": 1e-4,
"80": 1e-5, "110": 1e-6}
twoShapesAnchors = [(1.0, 1.0), (3.0, 3.0), (6,6), (9.0, 9.0), (10.0, 5.0), (5.0, 10.0)]
twoShapesAnchors = [(1.3221, 1.73145), (3.19275, 4.00944), (5.05587, 8.09892), (9.47112, 4.84053),
(11.2364, 10.0071)]
training_params = {"batch_size": opt.batch_size,
"shuffle": True,
"drop_last": True,
"collate_fn": custom_collate_fn}
test_params = {"batch_size": opt.batch_size,
"shuffle": False,
"drop_last": False,
"collate_fn": custom_collate_fn}
training_set = TwoShapesDataset(root_path=opt.data_path, mode=opt.train_set, trainingSet="annotations", image_size=opt.image_size)
training_generator = DataLoader(training_set, **training_params)
test_set = TwoShapesDataset(root_path=opt.data_path, mode=opt.test_set, trainingSet="annotations", image_size=opt.image_size, is_training=False)
test_generator = DataLoader(test_set, **test_params)
if torch.cuda.is_available():
if opt.pre_trained_model_type == "model":
model = torch.load(opt.pre_trained_model_path)
elif opt.pre_trained_model_type == "params":
model = Yolo(training_set.num_classes, twoShapesAnchors)
model.load_state_dict(torch.load(opt.pre_trained_model_path))
else:
print("Just loading the model definintion")
model = Yolo(training_set.num_classes, twoShapesAnchors)
else:
if opt.pre_trained_model_type == "model":
model = torch.load(opt.pre_trained_model_path, map_location=lambda storage, loc: storage)
elif opt.pre_trained_model_type == "params":
model = Yolo(training_set.num_classes, twoShapesAnchors)
model.load_state_dict(torch.load(opt.pre_trained_model_path, map_location=lambda storage, loc: storage))
# The following line will re-initialize weight for the last layer, which is useful
# when you want to retrain the model based on my trained weights. if you uncomment it,
# you will see the loss is already very small at the beginning.
# nn.init.normal_(list(model.modules())[:].weight, 0, 0.01)
# model.apply(weights_init)
log_path = os.path.join(opt.log_path, "{}".format("test1"))
if os.path.isdir(log_path):
shutil.rmtree(log_path)
os.makedirs(log_path)
writer = SummaryWriter(log_path)
if torch.cuda.is_available():
writer.add_graph(model.cpu(), torch.rand(opt.batch_size, 3, opt.image_size, opt.image_size))
model.cuda()
else:
writer.add_graph(model, torch.rand(opt.batch_size, 3, opt.image_size, opt.image_size))
criterion = YoloLoss(training_set.num_classes, model.anchors, opt.reduction)
optimizer = torch.optim.SGD(model.parameters(), lr=1e-5, momentum=opt.momentum, weight_decay=opt.decay)
best_loss = 1e10
best_epoch = 0
model.train()
num_iter_per_epoch = len(training_generator)
for epoch in range(opt.num_epoches):
if str(epoch) in learning_rate_schedule.keys():
for param_group in optimizer.param_groups:
param_group['lr'] = learning_rate_schedule[str(epoch)]
for iter, batch in enumerate(training_generator):
image, label = batch
if torch.cuda.is_available():
image = Variable(image.cuda(), requires_grad=True)
else:
image = Variable(image, requires_grad=True)
optimizer.zero_grad()
logits = model(image)
loss, loss_coord, loss_conf, loss_cls = criterion(logits, label)
loss.backward()
optimizer.step()
print("Epoch: {}/{}, Iteration: {}/{}, Lr: {}, Loss:{:.2f} (Coord:{:.2f} Conf:{:.2f} Cls:{:.2f})".format(
epoch + 1,
opt.num_epoches,
iter + 1,
num_iter_per_epoch,
optimizer.param_groups[0]['lr'],
loss,
loss_coord,
loss_conf,
loss_cls))
writer.add_scalar('Train/Total_loss', loss, epoch * num_iter_per_epoch + iter)
writer.add_scalar('Train/Coordination_loss', loss_coord, epoch * num_iter_per_epoch + iter)
writer.add_scalar('Train/Confidence_loss', loss_conf, epoch * num_iter_per_epoch + iter)
writer.add_scalar('Train/Class_loss', loss_cls, epoch * num_iter_per_epoch + iter)
if epoch % opt.test_interval == 0:
model.eval()
loss_ls = []
loss_coord_ls = []
loss_conf_ls = []
loss_cls_ls = []
for te_iter, te_batch in enumerate(test_generator):
te_image, te_label = te_batch
num_sample = len(te_label)
if torch.cuda.is_available():
te_image = te_image.cuda()
with torch.no_grad():
te_logits = model(te_image)
batch_loss, batch_loss_coord, batch_loss_conf, batch_loss_cls = criterion(te_logits, te_label)
loss_ls.append(batch_loss * num_sample)
loss_coord_ls.append(batch_loss_coord * num_sample)
loss_conf_ls.append(batch_loss_conf * num_sample)
loss_cls_ls.append(batch_loss_cls * num_sample)
te_loss = sum(loss_ls) / test_set.__len__()
te_coord_loss = sum(loss_coord_ls) / test_set.__len__()
te_conf_loss = sum(loss_conf_ls) / test_set.__len__()
te_cls_loss = sum(loss_cls_ls) / test_set.__len__()
print("Epoch: {}/{}, Lr: {}, Loss:{:.2f} (Coord:{:.2f} Conf:{:.2f} Cls:{:.2f})".format(
epoch + 1,
opt.num_epoches,
optimizer.param_groups[0]['lr'],
te_loss,
te_coord_loss,
te_conf_loss,
te_cls_loss))
writer.add_scalar('Test/Total_loss', te_loss, epoch)
writer.add_scalar('Test/Coordination_loss', te_coord_loss, epoch)
writer.add_scalar('Test/Confidence_loss', te_conf_loss, epoch)
writer.add_scalar('Test/Class_loss', te_cls_loss, epoch)
model.train()
if te_loss + opt.es_min_delta < best_loss:
best_loss = te_loss
best_epoch = epoch
# torch.save(model, opt.saved_path + os.sep + "trained_yolo_coco")
torch.save(model.state_dict(), opt.saved_path + os.sep + "only_params_trained_yolo_coco")
torch.save(model, opt.saved_path + os.sep + opt.save_file)
# Early stopping
if epoch - best_epoch > opt.es_patience > 0:
print("Stop training at epoch {}. The lowest loss achieved is {}".format(epoch, te_loss))
break
writer.export_scalars_to_json(log_path + os.sep + "all_logs.json")
writer.close()
if __name__ == "__main__":
opt = get_args()
train(opt)