-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathlf_pretraining.py
More file actions
126 lines (95 loc) · 4.19 KB
/
Copy pathlf_pretraining.py
File metadata and controls
126 lines (95 loc) · 4.19 KB
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
import torch
from torch.utils.data import DataLoader
from torch.autograd import Variable
import lf
from lf import lf_dataset, lf_loss
from lf.lf_dataset import LfDataset
from lf.line_follower import LineFollower
from utils.dataset_wrapper import DatasetWrapper
from utils.dataset_parse import load_file_list
import numpy as np
import cv2
import sys
import json
import os
import yaml
with open(sys.argv[1]) as f:
config = yaml.load(f)
sol_network_config = config['network']['sol']
pretrain_config = config['pretraining']
training_set_list = load_file_list(pretrain_config['training_set'])
train_dataset = LfDataset(training_set_list,
augmentation=True)
train_dataloader = DataLoader(train_dataset,
batch_size=1,
shuffle=True, num_workers=0,
collate_fn=lf_dataset.collate)
batches_per_epoch = int(pretrain_config['lf']['images_per_epoch']/pretrain_config['lf']['batch_size'])
train_dataloader = DatasetWrapper(train_dataloader, batches_per_epoch)
test_set_list = load_file_list(pretrain_config['validation_set'])
test_dataset = LfDataset(test_set_list)
test_dataloader = DataLoader(test_dataset,
batch_size=1,
shuffle=False, num_workers=0,
collate_fn=lf_dataset.collate)
line_follower = LineFollower()
line_follower.cuda()
optimizer = torch.optim.Adam(line_follower.parameters(), lr=pretrain_config['lf']['learning_rate'])
dtype = torch.cuda.FloatTensor
lowest_loss = np.inf
cnt_since_last_improvement = 0
for epoch in xrange(1000):
print "Epoch", epoch
sum_loss = 0.0
steps = 0.0
line_follower.train()
for x in train_dataloader:
#Only single batch for now
x = x[0]
positions = [Variable(x_i.type(dtype), requires_grad=False)[None,...] for x_i in x['lf_xyrs']]
xy_positions = [Variable(x_i.type(dtype), requires_grad=False)[None,...] for x_i in x['lf_xyxy']]
img = Variable(x['img'].type(dtype), requires_grad=False)[None,...]
#There might be a way to handle this case later,
#but for now we will skip it
if len(xy_positions) <= 1:
continue
reset_interval = 4
grid_line, _, _, xy_output = line_follower(img, positions[:1], steps=len(positions), all_positions=positions,
reset_interval=reset_interval, randomize=True, skip_grid=True)
loss = lf_loss.point_loss(xy_output, xy_positions)
optimizer.zero_grad()
loss.backward()
optimizer.step()
sum_loss += loss.data[0]
steps += 1
print "Train Loss", sum_loss/steps
print "Real Epoch", train_dataloader.epoch
sum_loss = 0.0
steps = 0.0
line_follower.eval()
for x in test_dataloader:
x = x[0]
positions = [Variable(x_i.type(dtype), requires_grad=False, volatile=True)[None,...] for x_i in x['lf_xyrs']]
xy_positions = [Variable(x_i.type(dtype), requires_grad=False, volatile=True)[None,...] for x_i in x['lf_xyxy']]
img = Variable(x['img'].type(dtype), requires_grad=False, volatile=True)[None,...]
if len(xy_positions) <= 1:
continue
grid_line, _, _, xy_output = line_follower(img, positions[:1], steps=len(positions), skip_grid=True)
# line = torch.nn.functional.grid_sample(img.transpose(2,3), grid_line)
# line = (line + 1.0) * 128
# cv2.imwrite("tra/{}.png".format(steps), line.data[0].cpu().numpy().transpose())
loss = lf_loss.point_loss(xy_output, xy_positions)
sum_loss += loss.data[0]
steps += 1
cnt_since_last_improvement += 1
if lowest_loss > sum_loss/steps:
cnt_since_last_improvement = 0
lowest_loss = sum_loss/steps
print "Saving Best"
if not os.path.exists(pretrain_config['snapshot_path']):
os.makedirs(pretrain_config['snapshot_path'])
torch.save(line_follower.state_dict(), os.path.join(pretrain_config['snapshot_path'], 'lf.pt'))
print "Test Loss", sum_loss/steps, lowest_loss
print ""
if cnt_since_last_improvement >= pretrain_config['lf']['stop_after_no_improvement']:
break