-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain.py
executable file
·210 lines (176 loc) · 7.62 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
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
import os
import torchvision
import argparse
import numpy as np
import torch.utils.data as data
import torch.backends.cudnn as cudnn
import torch.nn.functional as F
import torch.utils.data
from torch.optim import Adam
from tqdm import tqdm
import math
import random
from model import CNN3D
from dataset import Senz3dDataset
from util import *
from i3dpt import *
from validation import *
import torch.nn.functional as F
train_path = "/home/sagar/data/senz3d_dataset/dataset/train/"
test_path = "/home/sagar/data/senz3d_dataset/dataset/test/"
img_x, img_y = 256, 256 # resize video 2d frame size
depth_x, depth_y = 320, 240
# Select which frame to begin & end in videos
begin_frame, end_frame, skip_frame = 1, 8, 1
n_epoch = 10
num_classes = 11
lr = 1e-4
_lambda = 0.05 # 50 x 10^-3
def train(args,
model_rgb,
model_depth,
optimizer_rgb,
optimizer_depth,
train_loader,
valid_loader,
criterion,
regularizer,
epoch,
tb_writer,
tq):
device = args.device
model_rgb.train()
model_depth.train()
rgb_losses = []
depth_losses = []
rgb_regularized_losses = []
depth_regularized_losses = []
train_result = {}
valid_result = {}
for batch_idx, (rgb, depth, y) in enumerate(train_loader):
# distribute data to device
rgb, depth, y = rgb.to(device), depth.to(device), y.to(device)
optimizer_rgb.zero_grad()
optimizer_depth.zero_grad()
rgb_out, rgb_feature_map = model_rgb(rgb)
depth_out, depth_feature_map = model_depth(depth)
rgb_feature_map_T = torch.transpose(rgb_feature_map, 1, 2)
depth_feature_map_T = torch.transpose(depth_feature_map, 1, 2)
# print("RGB fmap shape :: {}".format(rgb_feature_map.shape))
# print("depth fmap shape :: {}".format(depth_feature_map.shape))
# torch.save(rgb_feature_map_T, "rgbFeatureMapT.pt")
rgb_sq_ft_map = rgb_feature_map_T.squeeze()
rgb_avg_sq_ft_map = torch.mean(rgb_sq_ft_map, 0)
depth_sq_ft_map = depth_feature_map_T.squeeze()
depth_avg_sq_ft_map = torch.mean(depth_sq_ft_map, 0)
rgb_corr = torch.mul(rgb_feature_map, rgb_feature_map_T)
depth_corr = torch.mul(depth_feature_map, depth_feature_map_T)
# print("RGB correlation :: {}".format(rgb_corr.shape))
# print("depth correlation :: {}".format(depth_corr.shape))
loss_rgb = criterion(rgb_out, torch.max(y, 1)[1]) # index of the max log-probability
loss_depth = criterion(depth_out, torch.max(y, 1)[1])
# print("RGB loss :: {}".format(loss_rgb))
# print("depth loss :: {}".format(loss_depth))
focal_reg_param = regularizer(loss_rgb, loss_depth)
"""
norm || x ||
Take the difference element wise
Square all the values
Add them all together
Take the square root
Multiply it with rho
"""
corr_diff_rgb = torch.sqrt(torch.sum(torch.sub(rgb_corr, depth_corr) ** 2))
corr_diff_depth = torch.sqrt(torch.sum(torch.sub(depth_corr, rgb_corr) ** 2))
# loss (m,n)
ssa_loss_rgb = focal_reg_param * corr_diff_rgb
ssa_loss_depth = focal_reg_param * corr_diff_depth
# total loss
reg_loss_rgb = loss_rgb + (_lambda * ssa_loss_rgb)
reg_loss_depth = loss_depth + (_lambda * ssa_loss_depth)
reg_loss_rgb.backward(retain_graph=True)
reg_loss_depth.backward()
optimizer_rgb.step()
optimizer_depth.step()
rgb_losses.append(loss_rgb.item())
depth_losses.append(loss_depth.item())
rgb_regularized_losses.append(reg_loss_rgb.item())
depth_regularized_losses.append(reg_loss_depth.item())
tq.update(1)
if batch_idx == 0:
train_result.update({"rgb_ft_map": rgb_avg_sq_ft_map, "depth_ft_map": depth_avg_sq_ft_map})
valid_result = validation(model_rgb=model_rgb, model_depth=model_depth, criterion=criterion,
valid_loader=valid_loader, num_classes=num_classes)
mean_rgb = np.mean(rgb_losses)
mean_reg_rgb = np.mean(rgb_regularized_losses)
mean_depth = np.mean(depth_losses)
mean_reg_depth = np.mean(depth_regularized_losses)
train_result.update({"loss_rgb": mean_rgb, "loss_reg_rgb": mean_reg_rgb, "loss_depth": mean_depth,
"loss_reg_depth": mean_reg_depth})
tq.set_postfix(RGB_loss='{:.5f}'.format(train_result["loss_rgb"]),
regularized_rgb_loss='{:.5f}'.format(train_result["loss_reg_rgb"]))
update_tensorboard(tb_writer=tb_writer, epoch=epoch, train_dict=train_result, valid_dict=valid_result)
update_tensorboard_image(tb_writer, epoch, train_result)
def main():
# Detect devices
# print(torch.__version__)
args = parse()
use_cuda = torch.cuda.is_available() # check if GPU exists
device = torch.device("cuda" if use_cuda else "cpu") # use CPU or GPU
# Initialize Tensorboard
tb_writer = initialize_tensorboard(
log_dir="/home/sagar/PycharmProjects/multimodal_3d_experiments/tensorboard_logs/",
common_name="experiment{}".format(args.save_as))
train_videos_path = []
test_videos_path = []
for folder in os.listdir(train_path):
train_videos_path.append(train_path + folder)
for folder in os.listdir(test_path):
test_videos_path.append(test_path + folder)
selected_frames = np.arange(begin_frame, end_frame, skip_frame).tolist()
train_rgb_set = Senz3dDataset(train_videos_path, selected_frames, to_augment=False, mode='train')
test_rgb_set = Senz3dDataset(test_videos_path, selected_frames, to_augment=False, mode='test')
train_loader = data.DataLoader(train_rgb_set, pin_memory=True, batch_size=1)
valid_loader = data.DataLoader(test_rgb_set, pin_memory=True, batch_size=1)
# model_rgb_cnn = CNN3D(t_dim=len(selected_frames), img_x=img_x, img_y=img_y, num_classes=11).to(device)
model_rgb_cnn = I3D(num_classes=num_classes,
modality='rgb',
dropout_prob=0,
name='inception').to(device)
# model_depth_cnn = CNN3D(t_dim=len(selected_frames), img_x=depth_x, img_y=depth_y, num_classes=11).to(device)
model_depth_cnn = I3D(num_classes=num_classes,
modality='rgb',
dropout_prob=0,
name='inception').to(device)
optimizer_rgb = torch.optim.Adam(model_rgb_cnn.parameters(), lr=lr) # optimize all cnn parameters
optimizer_depth = torch.optim.Adam(model_depth_cnn.parameters(), lr=lr) # optimize all cnn parameters
criterion = torch.nn.CrossEntropyLoss()
args = parse()
args.device = device
def regularizer(loss1, loss2):
beta = 2.0
if loss1 - loss2 > 0:
return (beta * math.exp(loss1 - loss2)) - 1
return 0.0
# print(model_rgb_cnn)
for epoch in range(n_epoch):
tq = tqdm(total=(len(train_loader)))
tq.set_description('ep {}, {}'.format(epoch, lr))
"""
{"loss_rgb": mean_rgb, "loss_reg_rgb": mean_reg_rgb, "loss_depth": mean_depth,
"loss_reg_depth": mean_reg_depth}
"""
train(args=args,
model_rgb=model_rgb_cnn,
model_depth=model_depth_cnn,
optimizer_rgb=optimizer_rgb,
optimizer_depth=optimizer_depth,
train_loader=train_loader,
valid_loader=valid_loader,
criterion=criterion,
regularizer=regularizer,
epoch=epoch,
tb_writer=tb_writer,
tq=tq)
if __name__ == "__main__":
main()