-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
300 lines (255 loc) · 10.7 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import os
import torch
import random
from dataset import EPIC
from config import config
from experiment import Experiment
from torch.utils.data import DataLoader
from custom_resnet import EPICModel
from samplers import CategoriesSampler
import sys
from optparse import OptionParser
import transforms
import torchvision
import pandas as pd
import numpy as np
# Parser options
parser = OptionParser()
parser.add_option("--gpu", type=int, help="gpu id", default=1)
parser.add_option("--config", type=str, help="configuration")
def main(argv):
# Read arguments passed
(opts, args) = parser.parse_args(argv)
# Reading config
cfg = config(opts.config, debugging=False, additionalText="training_ERM_seen_resnet18")
# Use CUDA
# os.environ['CUDA_VISIBLE_DEVICES'] = 1
use_cuda = torch.cuda.is_available()
# If the manual seed is not yet choosen
if cfg.manualSeed == None:
cfg.manualSeed = 1
# Set seed for reproducibility for CPU and GPU randomizaton process
random.seed(cfg.manualSeed)
torch.manual_seed(cfg.manualSeed)
if use_cuda:
torch.cuda.manual_seed_all(cfg.manualSeed)
dataloader_train = None
if hasattr(cfg, "train_mode"):
# Preprocessing (transformation) instantiation for training groupwise
transformation_train = torchvision.transforms.Compose(
[
transforms.GroupMultiScaleCrop(224, [1, 0.875, 0.75, 0.66]),
transforms.GroupRandomHorizontalFlip(is_flow=False),
transforms.Stack(), # concatenation of images
transforms.ToTorchFormatTensor(), # to torch
transforms.GroupNormalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
), # Normalization
]
)
if cfg.algo == "ERM" or cfg.algo == "MTGA":
# Loading training Dataset with N segment for TSN
EPICdata_train = EPIC(
mode=cfg.train_mode, cfg=cfg, transforms=transformation_train,
)
# Creating validation dataloader
# batch size = 16, num_workers = 8 are best fit for 12 Gb GPU and >= 16 Gb RAM
dataloader_train = DataLoader(
EPICdata_train,
batch_size=cfg.train_batch_size,
shuffle=True,
num_workers=cfg.num_worker_train,
pin_memory=True,
)
elif cfg.algo == "IRM":
df = pd.read_csv(cfg.anno_path)
p_ids = list(set(df["participant_id"].tolist()))
dataloader_train = []
for p_id in p_ids:
tmp_dataset = EPIC(
mode=cfg.train_mode,
cfg=cfg,
transforms=transformation_train,
participant_id=p_id,
)
if tmp_dataset.haveData:
dataloader_train.append(
DataLoader(
tmp_dataset,
batch_size=cfg.train_batch_size,
shuffle=True,
num_workers=cfg.num_worker_train,
pin_memory=True,
)
)
elif cfg.algo == "FSL":
dataloader_train = {}
# Loading training Dataset with N segment for TSN
EPICdata_train_verb = EPIC(
mode=cfg.train_mode, cfg=cfg, transforms=transformation_train
)
sampler = CategoriesSampler(EPICdata_train_verb.verb_label, 200, cfg.way, cfg.shot + cfg.query)
dataloader_train["verb"] = DataLoader(
dataset = EPICdata_train_verb,
batch_sampler=sampler,
num_workers=cfg.num_worker_train,
pin_memory=True,
)
EPICdata_train_noun = EPIC(
mode=cfg.train_mode, cfg=cfg, transforms=transformation_train
)
sampler = CategoriesSampler(EPICdata_train_noun.noun_label, 200, cfg.way, cfg.shot + cfg.query)
dataloader_train["noun"] = DataLoader(
dataset = EPICdata_train_noun,
batch_sampler=sampler,
num_workers=cfg.num_worker_train,
pin_memory=True,
)
dataloader_val = None
if hasattr(cfg, "val_mode") and hasattr(cfg, "train_mode"):
# Preprocessing (transformation) instantiation for validation groupwise
transformation_val = torchvision.transforms.Compose(
[
transforms.GroupOverSample(
224, 256
), # group sampling from images using multiple crops
transforms.Stack(), # concatenation of images
transforms.ToTorchFormatTensor(), # to torch
transforms.GroupNormalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
), # Normalization
]
)
# Loading validation Dataset with N segment for TSN
EPICdata_val = EPIC(mode=cfg.val_mode, cfg=cfg, transforms=transformation_val,)
# Creating validation dataloader
dataloader_val = DataLoader(
EPICdata_val,
batch_size=cfg.val_batch_size,
shuffle=False,
num_workers=cfg.num_worker_val,
pin_memory=True,
)
# Loading Models (Resnet50)
model = EPICModel(config=cfg)
if not cfg.feature_extraction:
if hasattr(cfg, "train_mode"):
policies = model.get_optim_policies()
# for group in policies:
# print(('group: {} has {} params, lr_mult: {}, decay_mult: {}'.format(
# group['name'], len(group['params']), group['lr_mult'], group['decay_mult'])))
# Optimizer
# initial lr = 0.01
# momentum = 0.9
# weight_decay = 5e-4
optimizer = torch.optim.SGD(
policies, lr=cfg.lr, momentum=cfg.momentum, weight_decay=cfg.weight_decay
)
# Loss function (CrossEntropy)
if cfg.algo == "IRM":
criterion = torch.nn.CrossEntropyLoss(reduction="none")
elif cfg.algo == "ERM" or cfg.algo == "MTGA":
criterion = torch.nn.CrossEntropyLoss()
elif cfg.algo == "FSL":
criterion = torch.nn.CrossEntropyLoss()
# If multiple GPUs are available (and bridged)
# if torch.cuda.device_count() > 1:
# print("Let's use", torch.cuda.device_count(), "GPUs!")
# model = torch.nn.DataParallel(model)
# Convert model and loss function to GPU if available for faster computation
if use_cuda:
model = model.cuda()
criterion = criterion.cuda()
# Loading Trainer
experiment = Experiment(
cfg=cfg,
model=model,
loss=criterion,
optimizer=optimizer,
use_cuda=use_cuda,
data_train=dataloader_train,
data_val=dataloader_val,
debugging=False,
)
# Train the model
experiment.train()
else:
# Load Model Checkpoint
checkpoint = torch.load(cfg.checkpoint_filename_final)
model.load_state_dict(checkpoint["model_state_dict"])
if use_cuda:
model = model.cuda()
transformation = torchvision.transforms.Compose(
[
transforms.GroupOverSample(
224, 256
), # group sampling from images using multiple crops
transforms.Stack(), # concatenation of images
transforms.ToTorchFormatTensor(), # to torch
transforms.GroupNormalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
), # Normalization
]
)
# Loading Predictor
experiment = Experiment(
cfg=cfg, model=model, use_cuda=use_cuda, debugging=False
)
filenames = ["seen.json", "unseen.json"]
for filename in filenames:
EPICdata = EPIC(
mode=cfg.val_mode,
cfg=cfg,
transforms=transformation,
test_mode=filename[:-5],
)
data_loader = torch.utils.data.DataLoader(
EPICdata, batch_size=8, shuffle=False, num_workers=4, pin_memory=True
)
experiment.data_val = data_loader
experiment.predict(filename)
else:
# Load Model Checkpoint
checkpoint = torch.load(cfg.checkpoint_filename_final)
model.load_state_dict(checkpoint["model_state_dict"])
if use_cuda:
model = model.cuda()
model.eval()
transformation = torchvision.transforms.Compose(
[
transforms.GroupOverSample(
224, 256
), # group sampling from images using multiple crops
transforms.Stack(), # concatenation of images
transforms.ToTorchFormatTensor(), # to torch
transforms.GroupNormalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
), # Normalization
]
)
# Loading Predictor
experiment = Experiment(
cfg=cfg, model=model, use_cuda=use_cuda, debugging=False
)
with torch.no_grad():
modes = ["train-unseen", "val-unseen"]
for mode in modes:
data = np.empty((1, 2050))
EPICdata = EPIC(
mode=mode,
cfg=cfg,
transforms=transformation,
)
data_loader = torch.utils.data.DataLoader(
EPICdata, batch_size=1, shuffle=False, num_workers=0, pin_memory=True
)
for i, sample_batch in enumerate(data_loader):
output = experiment.extract_features(sample_batch)
verb_ann = sample_batch["verb_id"].data.item()
noun_ann = sample_batch["noun_id"].data.item()
out = np.append(np.mean(output, 0), verb_ann)
out = np.append(out, noun_ann)
data = np.concatenate((data, np.expand_dims(out, 0)), 0)
np.save(mode, data)
if __name__ == "__main__":
main(sys.argv)