-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
132 lines (124 loc) · 6.6 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
from model import ORIYOLO, YOLORES
from loss import YOLOLoss
import torch as t
from torch import nn, optim
import os
from dataloader import YoloSet
from torch.utils import data
import numpy as np
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
def train_step(model, d_train, l_train, orig_image_sizes, optimizer, criterion):
model.train()
d_train_cuda = d_train.cuda(0)
l_train_cuda = l_train.cuda(0)
orig_image_sizes_cuda = orig_image_sizes.cuda(0)
train_output = model(d_train_cuda)
train_loss = criterion(train_output, l_train_cuda, orig_image_sizes_cuda)
optimizer.zero_grad()
train_loss.backward()
optimizer.step()
return model, train_loss.item()
def valid_step(model, valid_loader, criterion, batch_size, B, S, image_type, voc_Annotations_dir, voc_Main_dir, voc_JPEGImages_dir, classes, img_size, num_workers):
model.eval()
try:
d_valid, l_valid, orig_image_sizes = next(valid_loader)
except:
valid_loader = iter(data.DataLoader(YoloSet(B, S, image_type, "val", voc_Annotations_dir, voc_Main_dir, voc_JPEGImages_dir, classes, img_size), batch_size=batch_size, shuffle=True, drop_last=False, num_workers=num_workers))
d_valid, l_valid, orig_image_sizes = next(valid_loader)
d_valid_cuda = d_valid.cuda(0)
l_valid_cuda = l_valid.cuda(0)
orig_image_sizes_cuda = orig_image_sizes.cuda(0)
with t.no_grad():
valid_output = model(d_valid_cuda)
valid_loss = criterion(valid_output, l_valid_cuda, orig_image_sizes_cuda)
return model, valid_loss.item()
def train(train_conf, common_conf):
every_epoch_mean_train_losses = []
every_epoch_mean_valid_losses = []
current_best_valid_loss = float("inf")
voc_Annotations_dir = train_conf["voc_Annotations_dir"]
voc_Main_dir = train_conf["voc_Main_dir"]
voc_JPEGImages_dir = train_conf["voc_JPEGImages_dir"]
lamda_coord = train_conf["lamda_coord"]
lamda_noobj = train_conf["lamda_noobj"]
img_size = common_conf["img_size"]
lr = train_conf["lr"]
batch_size = train_conf["batch_size"]
epoch = train_conf["epoch"]
weight_decay = train_conf["weight_decay"]
num_workers = train_conf["dataloader_num_workers"]
lr_shrink_rate = train_conf["lr_shrink_rate"]
lr_shrink_epoch = train_conf["lr_shrink_epoch"]
best_model_save_path = common_conf["best_model_save_path"]
epoch_model_save_path = common_conf["epoch_model_save_path"]
image_type = train_conf["image_type"]
B = common_conf["B"]
S = common_conf["S"]
num_classes = common_conf["num_classes"]
classes = common_conf["classes"]
backbone_use_resnet = bool(common_conf["backbone_use_resnet"])
if backbone_use_resnet:
YOLO = YOLORES
else:
YOLO = ORIYOLO
model = YOLO(S, B, num_classes)
########################################
start_e = 1
if os.path.exists("./continue_train.txt"):
print("continue training load epoch model......")
model.load_state_dict(t.load(epoch_model_save_path))
with open("./continue_train.txt", "r", encoding="utf-8") as file:
start_e, current_best_valid_loss, lr = file.read().split(" ")
start_e = int(start_e)
current_best_valid_loss = float(current_best_valid_loss)
lr = float(lr)
with open("./every_epoch_mean_train_losses.txt", "r", encoding="utf-8") as file:
every_epoch_mean_train_losses = eval(file.read())
with open("./every_epoch_mean_valid_losses.txt", "r", encoding="utf-8") as file:
every_epoch_mean_valid_losses = eval(file.read())
#########################################
model = nn.DataParallel(module=model, device_ids=[0])
model = model.cuda(0)
criterion = YOLOLoss(S, B, num_classes, lamda_coord, lamda_noobj)
optimizer = optim.SGD(params=model.parameters(), lr=lr, momentum=0.8, weight_decay=weight_decay)
for e in range(start_e, 1 + epoch):
train_loader = iter(data.DataLoader(YoloSet(B, S, image_type, "train", voc_Annotations_dir, voc_Main_dir, voc_JPEGImages_dir, classes, img_size), batch_size=batch_size, shuffle=True, drop_last=False, num_workers=num_workers))
valid_loader = iter(data.DataLoader(YoloSet(B, S, image_type, "val", voc_Annotations_dir, voc_Main_dir, voc_JPEGImages_dir, classes, img_size), batch_size=batch_size, shuffle=True, drop_last=False, num_workers=num_workers))
all_steps = len(train_loader)
current_step = 0
every_batch_train_losses = []
every_batch_valid_losses = []
for d_train, l_train, orig_image_sizes in train_loader:
model, train_loss = train_step(model, d_train, l_train, orig_image_sizes, optimizer, criterion)
model, valid_loss = valid_step(model, valid_loader, criterion, batch_size, B, S, image_type, voc_Annotations_dir, voc_Main_dir, voc_JPEGImages_dir, classes, img_size, num_workers)
every_batch_train_losses.append(train_loss)
every_batch_valid_losses.append(valid_loss)
current_step += 1
print("epoch:%d/%d, step:%d/%d, train_loss:%.5f, valid_loss:%.5f" % (e, epoch, current_step, all_steps, train_loss, valid_loss))
mean_train_loss = float(np.mean(every_batch_train_losses))
mean_valid_loss = float(np.mean(every_batch_valid_losses))
every_epoch_mean_train_losses.append(mean_train_loss)
every_epoch_mean_valid_losses.append(mean_valid_loss)
if mean_valid_loss < current_best_valid_loss:
current_best_valid_loss = mean_valid_loss
print("saving best model......")
t.save(model.module.state_dict(), best_model_save_path)
with open("./every_epoch_mean_train_losses.txt", "w", encoding="utf-8") as file:
file.write(str(every_epoch_mean_train_losses))
with open("./every_epoch_mean_valid_losses.txt", "w", encoding="utf-8") as file:
file.write(str(every_epoch_mean_valid_losses))
print("saving epoch model......")
t.save(model.module.state_dict(), epoch_model_save_path)
if e % lr_shrink_epoch == 0:
lr *= lr_shrink_rate
optimizer = optim.SGD(params=model.parameters(), lr=lr, momentum=0.8, weight_decay=weight_decay)
##############################################################
with open("./continue_train.txt", "w", encoding="utf-8") as file:
file.write("%d %f %.10f" % (e + 1, current_best_valid_loss, lr))
##############################################################
if __name__ == "__main__":
with open("./conf.json", "r", encoding="utf-8") as file:
conf = eval(file.read())
train_conf = conf["train"]
common_conf = conf["common"]
train(train_conf, common_conf)