-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytorch_img_to_bezier_test.py
81 lines (73 loc) · 2.28 KB
/
pytorch_img_to_bezier_test.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
from pytorch_high_level import *
from pytorch_model import Bezier
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.enabled = True
import matplotlib.pyplot as plt
from Bezier import *
import argparse
import os
parser = argparse.ArgumentParser(description='IL')
parser.add_argument('--dataset_name', type=str, default="4", help='suffix for the dataset name')
args = parser.parse_args()
HEIGHT = 240
WIDTH = 320
CHANNELS = 1
model_name = 'bezier.h5'
checkpoint = torch.load(model_name)
model = checkpoint['net']
train_log = []
output_path = os.getcwd()+"/Outputs_pytorch"
def plot_steering(s):
WHEELBASE = 1
Curvature = m.tan(s/57.3)/WHEELBASE
if(m.fabs(Curvature)<1e-4):
y = np.arange(0,1,0.05)
x = np.zeros_like(y)
else:
radius = 1/Curvature
yc = 0
y = np.arange(0,1,0.05)
x = np.sign(radius)*np.sqrt(radius**2 - y**2) - radius
return x,y
counter = 0
torch.cuda.empty_cache()
train_data = np.load('MUSHR_320x240_shuffled_Bezier_{}.npy'.format(args.dataset_name),allow_pickle=True)
X = ([i[0] for i in train_data])
Y = ([i[1] for i in train_data])
st = ([i[2] for i in train_data])
with torch.no_grad():
for j in range(0,len(X),1000):
img = X[j]
img = torch.Tensor(img).view(-1,1,240,320).to(device)
expected = Y[j]
angle = -st[j]
now = time.time()
traj = model(img)[0].cpu().numpy()
dt = time.time()-now
# print(dt*1000)
print(traj,Y[j])
Bx,By = plot_bezier_coeffs(traj)
_Bx,_By = plot_bezier_coeffs(expected)
st_x,st_y = plot_steering(angle)
plt.plot(Bx*6.5,By*6.5,label='predicted')
# plt.plot(_Bx,_By,label='ground_truth')
# plt.plot(st_x,st_y,label='instantaneous steering trajectory')
plt.xlabel('x axis (m)')
plt.ylabel('y axis (m)')
plt.legend()
plt.axis('equal')
# plt.show()
plt.savefig(output_path+"/Bezier_{}.jpg".format(str(counter)))
cv2.imwrite(output_path+"/Bezier_input_{}.jpg".format(str(counter)),X[j])
plt.clf()
counter += 1
loss_function = None
optimizer = None
train_data = None
X = None
Y = None
del train_data
del X
del Y
del loss_function
del optimizer