-
Notifications
You must be signed in to change notification settings - Fork 8
/
train_carla.py
184 lines (148 loc) · 6.73 KB
/
train_carla.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import sys
sys.path.append("./Models")
from Models.utils import *
from Data.dataset import *
from torch.utils.data import Dataset, DataLoader
import torch.optim as optim
import numpy as np
import os
from torch.utils.tensorboard import SummaryWriter
# PARAMETERS
seed = 42
x_dim = 128
y_dim = 128
z_dim = 8
T = 1
binary_counts = True
transform_pose = True
model_name = "SSC_Full"
num_workers = 16
train_dir = "./Data/Scenes/Cartesian/Train"
val_dir = "./Data/Scenes/Cartesian/Val"
cylindrical = False
epoch_num = 500
remap = True
# Device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if remap:
num_classes = 11
frequncies_mapped = np.zeros(num_classes)
for cls in range(23):
frequncies_mapped[LABELS_REMAP[cls]] += frequencies_cartesian[cls]
frequencies_cartesian = frequncies_mapped
else:
num_classes = 23
# Weights
epsilon_w = 0.001 # eps to avoid zero division
weights = torch.from_numpy(1 / np.log(frequencies_cartesian + epsilon_w)).to(torch.float32)
criterion = nn.CrossEntropyLoss(weight=weights.to(device))
# Grid Parameters
carla_ds = CarlaDataset(directory=train_dir, device=device, num_frames=1, cylindrical=cylindrical)
coor_ranges = carla_ds._eval_param['min_bound'] + carla_ds._eval_param['max_bound']
voxel_sizes = [abs(coor_ranges[3] - coor_ranges[0]) / x_dim,
abs(coor_ranges[4] - coor_ranges[1]) / y_dim,
abs(coor_ranges[5] - coor_ranges[2]) / z_dim] # since BEV
# Load model
lr = 0.001
BETA1 = 0.9
BETA2 = 0.999
model, B, __, decayRate, resample_free = get_model(model_name, num_classes, voxel_sizes, coor_ranges, [x_dim, y_dim, z_dim], device, T=T)
model_name += "_" + str(num_classes) + "_T" + str(T)
if binary_counts:
model_name += "B"
print("Running:", model_name)
# Data Loaders
carla_ds = CarlaDataset(directory=train_dir, device=device, num_frames=T, cylindrical=cylindrical, random_flips=True, remap=remap, binary_counts=binary_counts, transform_pose=transform_pose)
dataloader = DataLoader(carla_ds, batch_size=B, shuffle=True, collate_fn=carla_ds.collate_fn, num_workers=num_workers)
val_ds = CarlaDataset(directory=val_dir, device=device, num_frames=T, cylindrical=cylindrical, remap=remap, binary_counts=binary_counts, transform_pose=transform_pose)
dataloader_val = DataLoader(val_ds, batch_size=B, shuffle=True, collate_fn=val_ds.collate_fn, num_workers=num_workers)
test_ds = CarlaDataset(directory=val_dir, device=device, num_frames=T, cylindrical=cylindrical, remap=remap, binary_counts=binary_counts, transform_pose=transform_pose)
dataloader_test = DataLoader(test_ds, batch_size=1, shuffle=False, collate_fn=test_ds.collate_fn, num_workers=num_workers)
writer = SummaryWriter("./Models/Runs/" + model_name)
save_dir = "./Models/Weights/" + model_name
if not os.path.exists(save_dir):
os.mkdir(save_dir)
if device == "cuda":
torch.cuda.empty_cache()
setup_seed(seed)
optimizer = optim.Adam(model.parameters(), lr=lr, betas=(BETA1, BETA2))
my_lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer=optimizer, gamma=decayRate)
train_count = 0
for epoch in range(epoch_num):
# Training
model.train()
for input_data, output, counts in dataloader:
optimizer.zero_grad()
input_data = torch.tensor(input_data).to(device)
output = torch.tensor(output).to(device)
counts = torch.tensor(counts).to(device)
preds = model(input_data)
counts = counts.view(-1)
output = output.view(-1).long()
preds = preds.contiguous().view(-1, preds.shape[4])
# Criterion requires input (NxC), output (N) dimension
mask = counts > 0
output_masked = output[mask]
preds_masked = preds[mask]
if resample_free:
preds_masked, output_masked = resample_free_space(preds_masked, output_masked)
loss = criterion(preds_masked, output_masked)
loss.backward()
optimizer.step()
# Accuracy
with torch.no_grad():
probs = nn.functional.softmax(preds_masked, dim=1)
preds_masked = np.argmax(probs.detach().cpu().numpy(), axis=1)
outputs_np = output_masked.detach().cpu().numpy()
accuracy = np.sum(preds_masked == outputs_np) / outputs_np.shape[0]
# Record
writer.add_scalar(model_name + '/Loss/Train', loss.item(), train_count)
writer.add_scalar(model_name + '/Accuracy/Train', accuracy, train_count)
train_count += input_data.shape[0]
# Save model, decreaser learning rate
my_lr_scheduler.step()
torch.save(model.state_dict(), os.path.join(save_dir, "Epoch" + str(epoch) + ".pt"))
# Validation
model.eval()
with torch.no_grad():
running_loss = 0.0
counter = 0
num_correct = 0
num_total = 0
all_intersections = np.zeros(num_classes)
all_unions = np.zeros(num_classes) + 1e-6 # SMOOTHING
for input_data, output, counts in dataloader_val:
optimizer.zero_grad()
input_data = torch.tensor(input_data).to(device)
output = torch.tensor(output).to(device)
counts = torch.tensor(counts).to(device)
preds = model(input_data)
counts = counts.view(-1)
output = output.view(-1).long()
preds = preds.contiguous().view(-1, preds.shape[4])
# Criterion requires input (NxC), output (N) dimension
mask = counts > 0
output_masked = output[mask]
preds_masked = preds[mask]
loss = criterion(preds_masked, output_masked)
running_loss += loss.item()
counter += input_data.shape[0]
# Accuracy
probs = nn.functional.softmax(preds_masked, dim=1)
preds_masked = np.argmax(probs.detach().cpu().numpy(), axis=1)
outputs_np = output_masked.detach().cpu().numpy()
num_correct += np.sum(preds_masked == outputs_np)
num_total += outputs_np.shape[0]
intersection, union = iou_one_frame(torch.tensor(preds_masked), torch.tensor(output_masked), n_classes=num_classes)
all_intersections += intersection
all_unions += union
print(f'Eppoch Num: {epoch} ------ average val loss: {running_loss/counter}')
print(f'Eppoch Num: {epoch} ------ average val accuracy: {num_correct/num_total}')
print(f'Eppoch Num: {epoch} ------ val miou: {np.mean(all_intersections / all_unions)}')
writer.add_scalar(model_name + '/Loss/Val', running_loss/counter, epoch)
writer.add_scalar(model_name + '/Accuracy/Val', num_correct/num_total, epoch)
writer.add_scalar(model_name + '/mIoU/Val', np.mean(all_intersections / all_unions), epoch)
writer.close()