-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
132 lines (109 loc) · 4.58 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
import cv2
import torch
import visdom
#import sys
import Assistant
import numpy as np
from data_loader import Generator
from parameters import Parameters
import test
import evaluation
import util
import copy
p = Parameters()
###############################################################
##
## Training
##
###############################################################
def Training():
print('Training')
####################################################################
## Hyper parameter
####################################################################
print('Initializing hyper parameter')
vis = visdom.Visdom()
loss_window = vis.line(X=torch.zeros((1,)).cpu(),
Y=torch.zeros((1)).cpu(),
opts=dict(xlabel='epoch',
ylabel='Loss',
title='Training Loss',
legend=['Loss']))
#########################################################################
## Get dataset
#########################################################################
print("Get dataset")
loader = Generator()
##############################
## Get agent and model
##############################
print('Get agent')
if p.model_path == "":
lane_assistant = Assistant.Assistant()
else:
lane_assistant = Assistant.Assistant()
lane_assistant.load_weights(1912, "tensor(0.9420)")
##############################
## Check GPU
##############################
print('Setup GPU mode')
if torch.cuda.is_available():
lane_assistant.cuda()
#torch.backends.cudnn.benchmark=True
##############################
## Loop for training
##############################
print('Training loop')
step = 0
sampling_list = None
for epoch in range(p.n_epoch):
lane_assistant.training_mode()
for inputs, target_lanes, target_h, test_image, data_list in loader.Generate(sampling_list):
#training
#util.visualize_points(inputs[0], target_lanes[0], target_h[0])
print("epoch : " + str(epoch))
print("step : " + str(step))
loss_p = lane_assistant.train(inputs, target_lanes, target_h, epoch, lane_assistant, data_list)
torch.cuda.synchronize()
loss_p = loss_p.cpu().data
if step%50 == 0:
vis.line(
X=torch.ones((1, 1)).cpu() * int(step/50),
Y=torch.Tensor([loss_p]).unsqueeze(0).cpu(),
win=loss_window,
update='append')
if step%100 == 0:
lane_assistant.save_model(int(step/100), loss_p)
testing(lane_assistant, test_image, step, loss_p)
step += 1
sampling_list = copy.deepcopy(lane_assistant.get_data_list())
lane_assistant.sample_reset()
#evaluation
if epoch >= 0 and epoch%1 == 0:
print("evaluation")
lane_assistant.evaluate_mode()
th_list = [0.8]
index = [3]
lane_assistant.save_model(int(step/100), loss_p)
for idx in index:
print("generate result")
test.evaluation(loader, lane_assistant, index = idx, name="test_result_"+str(epoch)+"_"+str(idx)+".json")
for idx in index:
print("compute score")
with open("/home/kym/Dropbox/eval_result2_"+str(idx)+"_.txt", 'a') as make_file:
make_file.write( "epoch : " + str(epoch) + " loss : " + str(loss_p.cpu().data) )
make_file.write(evaluation.LaneEval.bench_one_submit("test_result_"+str(epoch)+"_"+str(idx)+".json", "test_label.json"))
make_file.write("\n")
with open("eval_result_"+str(idx)+"_.txt", 'a') as make_file:
make_file.write( "epoch : " + str(epoch) + " loss : " + str(loss_p.cpu().data) )
make_file.write(evaluation.LaneEval.bench_one_submit("test_result_"+str(epoch)+"_"+str(idx)+".json", "test_label.json"))
make_file.write("\n")
if int(step)>700000:
break
def testing(lane_assistant, test_image, step, loss):
lane_assistant.evaluate_mode()
_, _, ti = test.test(lane_assistant, np.array([test_image]))
cv2.imwrite('test_result/result_'+str(step)+'_'+str(loss)+'.png', ti[0])
lane_assistant.training_mode()
if __name__ == '__main__':
Training()