-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
249 lines (204 loc) · 9.44 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from model import AdvancedGRUModel as GRUModel
import torch.nn as nn
import torch, math
import argparse
import torch.nn.functional as F
import os
import torch.optim as optim
from torch.utils.data import DataLoader
from dataloader import SensorDataset
from tqdm import tqdm
import glob
import numpy as np
import time
from datetime import date
import csv
from utils import final_displacement_error, average_displacement_error, final_intersection_over_union
os.environ['CUDA_VISIBLE_DEVICES']= '1'
device = ("cuda" if torch.cuda.is_available() else "cpu")
seed = 123
np.random.seed(seed)
torch.manual_seed(seed)
learning_rate = 0.0001
batch_size = 1
shuffle = True
pin_memory = True
training_length = 1
forecast_window = 12
input_size = 772
num_epochs= 50
path_to_save_loss= 'results/'
path_to_save_model = 'saved_models'
# os.makedirs(path_to_save_model, exist_ok=True)
# os.makedirs(path_to_save_loss, exist_ok=True)
train_csv = 'train_dataset.csv'
train_dataset = SensorDataset(csv_name=train_csv, root_dir="Data/", training_length=training_length, forecast_window=forecast_window)
train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_csv = 'test_dataset.csv'
test_dataset = SensorDataset(csv_name=test_csv, root_dir="Data/", training_length=training_length, forecast_window=forecast_window)
test_dataloader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
def test_eval(test_dataloader, model, device):
model.eval()
fde_all =[]
ade_all =[]
fiou_all =[]
criterion = nn.MSELoss()
total_loss = 0
total_samples = 0
test_loss = 0
with torch.no_grad():
for input_feat, sensor_number, frames in test_dataloader:
for tt in range(input_feat.shape[1]):
bb_input = input_feat[:, tt, :4].unsqueeze(0).to(device)
vit_feat = input_feat[:, tt, 4:].unsqueeze(0).to(device)
# Check if there are enough timesteps remaining for y
if tt + forecast_window+1 <= input_feat.shape[1]:
y = input_feat[:, tt + 1: tt + forecast_window+1, :4].to(device)
mask = torch.ones_like(y, dtype=torch.bool).to(device) # All ones, no padding
else:
# Pad the remaining timesteps with zeros (or some other padding value)
remaining_length = input_feat.shape[1] - (tt + 1)
y = input_feat[:, tt + 1:, :4].to(device)
padding = torch.zeros((1, forecast_window - remaining_length, 4)).to(device)
y = torch.cat((y, padding), dim=1)
mask = torch.cat((torch.ones((1, remaining_length, 4)).to(device),
torch.zeros((1, forecast_window - remaining_length, 4)).to(device)), dim=1)
output = model(bb_input, vit_feat)
# Apply the mask to the output and y
masked_output = output * mask
masked_y = y * mask
# Calculate the loss only on the masked regions
loss = criterion(masked_output, masked_y)
test_loss+= loss.item()
fde = final_displacement_error(y, output, 640,480)
fde_all.append(fde)
ade = average_displacement_error(y, output, 640,480)
ade_all.append(ade)
fiou = final_intersection_over_union(y,output)
fiou_all.append(fiou)
total_samples+=1
average_loss = total_loss/ total_samples
return sum(fde_all) / len(fde_all), sum(ade_all) / len(ade_all), sum(fiou_all) / len(fiou_all), average_loss
def improved_custom_bbox_loss(masked_output, masked_y, mask):
# Use Mean Squared Error as the base loss
mse_loss = nn.MSELoss(reduction='none')
loss = mse_loss(masked_output, masked_y)
# Give more weight to position (x, y) than size (w, h)
loss[:, :, :2] *= 2
# Sum the loss and normalize by the number of valid entries
total_loss = loss.sum()
num_valid = mask.sum()
return total_loss / (num_valid + 1e-6) # Add small epsilon to avoid division by zero
def auxiliary_loss(predictions, targets, mask, alpha=0.2):
mse_loss = nn.MSELoss(reduction='none')
# Calculate losses
total_loss = mse_loss(predictions, targets)
# Apply mask
masked_loss = total_loss * mask
# Calculate FDE (using only the last timestep)
fde_loss = masked_loss[:, -1, :].mean()
# Calculate ADE (using all timesteps)
ade_loss = masked_loss.mean()
# Combine losses
combined_loss = (1 - alpha) * fde_loss + alpha * ade_loss
return combined_loss
def train():
model_dir ='./snapshot'
if not os.path.exists(model_dir):
os.makedirs(model_dir)
input_size = 772
h_dim = 256
model = GRUModel().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=5)
# criterion = nn.MSELoss()
criterion = auxiliary_loss
min_train_loss = float('inf')
min_fde = float('inf')
### time
today = date.today()
date_saved = today.strftime("%b-%d-%Y")
t = time.localtime()
current_time = time.strftime("%H-%M-%S", t)
result_csv = os.path.join(path_to_save_loss, f'result{date_saved}_{current_time}.csv')
with open(result_csv, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(['epoch', 'Train_Loss', 'Test_loss', 'fde', 'ade', 'fiou'])
model.train()
for epoch in range(num_epochs):
train_loss = 0
loop = tqdm(train_dataloader,total = len(train_dataloader), leave = True)
for input_feat, sensor_number, frames in loop:
loop.set_description(f"Epoch [{epoch+1}/{num_epochs}]")
batch_loss = 0
optimizer.zero_grad()
for tt in range(input_feat.shape[1]):
bb_input = input_feat[:, tt, :4].unsqueeze(0).to(device)
vit_feat = input_feat[:, tt, 4:].unsqueeze(0).to(device)
# Check if there are enough timesteps remaining for y
if tt + forecast_window+1 <= input_feat.shape[1]:
y = input_feat[:, tt + 1: tt + forecast_window+1, :4].to(device)
mask = torch.ones_like(y, dtype=torch.bool).to(device) # All ones, no padding
else:
# Pad the remaining timesteps with zeros (or some other padding value)
remaining_length = input_feat.shape[1] - (tt + 1)
y = input_feat[:, tt + 1:, :4].to(device)
padding = torch.zeros((1, forecast_window - remaining_length, 4)).to(device)
y = torch.cat((y, padding), dim=1)
mask = torch.cat((torch.ones((1, remaining_length, 4)).to(device),
torch.zeros((1, forecast_window - remaining_length, 4)).to(device)), dim=1)
output = model(bb_input, vit_feat)
# Apply the mask to the output and y
masked_output = output * mask
masked_y = y * mask
# Calculate the loss only on the masked regions
# loss = criterion(masked_output, masked_y)
loss = criterion(masked_output, masked_y,mask)
batch_loss += loss.item()
loss.backward()
# torch.nn.utils.clip_grad_norm_(model.parameters(), 10)
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()
train_loss+= batch_loss
avg_train_loss = train_loss / len(train_dataloader)
loop.set_description(f"Epoch [{epoch+1}/{num_epochs}]")
loop.set_postfix(loss = avg_train_loss)
if avg_train_loss < min_train_loss:
torch.save(model.state_dict(), os.path.join(model_dir, "best_train_model.pth"))
min_train_loss = avg_train_loss
best_train_model = "best_train_model.pth"
print(f'Best train model saved in {epoch} epoch')
if epoch % 2 == 0: # Plot 1-Step Predictions
print('================================')
fde, ade, fiou , test_loss = test_eval(test_dataloader, model, device)
print(f'FDE : {fde}')
print(f'ADE : {ade}')
print(f'FIoU : {fiou}')
with open(result_csv, 'a+', newline='') as saving_result:
writer = csv.writer(saving_result)
writer.writerow([epoch, train_loss, test_loss, fde, ade, fiou])
if fde < min_fde:
torch.save(model.state_dict(), os.path.join(model_dir, "best_test_model.pth"))
min_fde = fde
best_test_model = "best_test_model.pth"
print(f'Min FDE model saved in {epoch} epoch')
scheduler.step(avg_train_loss)
model.train()
def sanity_check():
model_path = 'snapshot/best_test_model.pth'
model = GRUModel() # Initialize your model
model.load_state_dict(torch.load(model_path))
model.to(device)
model.eval()
fde, ade, fiou , test_loss = test_eval(test_dataloader, model, device)
print(fde)
return fde, ade, fiou , test_loss
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--phase', type=str, default='train', choices=['check', 'train', 'test'],
help='dimension of the resnet output. Default: 2048')
p = parser.parse_args()
if p.phase == 'test':
sanity_check()
else:
train()