forked from FloretCat/CMRAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weakly_main.py
289 lines (237 loc) · 10.3 KB
/
weakly_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
import os
import time
import random
import json
from tqdm import tqdm
import torch
import torch.nn as nn
from torch.nn.utils import clip_grad_norm_
from torch.utils.data import DataLoader
from tensorboardX import SummaryWriter
from torch.optim.lr_scheduler import StepLR, MultiStepLR
import numpy as np
from configs.opts import parser
from model.main_model import weak_main_model as main_model
from utils import AverageMeter, Prepare_logger, get_and_save_args
from utils.Recorder import Recorder
from dataset.AVE_dataset_weak import AVEDataset
# ================================= seed config ============================
SEED = 666
random.seed(SEED)
np.random.seed(seed=SEED)
torch.manual_seed(seed=SEED)
torch.cuda.manual_seed(seed=SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# =============================================================================
def main():
# utils variable
global args, logger, writer, dataset_configs
# statistics variable
global best_accuracy, best_accuracy_epoch
best_accuracy, best_accuracy_epoch = 0, 0
# configs
dataset_configs = get_and_save_args(parser)
parser.set_defaults(**dataset_configs)
args = parser.parse_args()
# select GPUs
os.environ['CUDA_DEVICE_ORDER'] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
'''Create snapshot_pred dir for copying code and saving model '''
if not os.path.exists(args.snapshot_pref):
os.makedirs(args.snapshot_pref)
if os.path.isfile(args.resume):
args.snapshot_pref = os.path.dirname(args.resume)
logger = Prepare_logger(args, eval=args.evaluate)
if not args.evaluate:
logger.info(f'\nCreating folder: {args.snapshot_pref}')
logger.info('\nRuntime args\n\n{}\n'.format(json.dumps(vars(args), indent=4)))
else:
logger.info(f'\nLog file will be save in {args.snapshot_pref}/Eval.log.')
'''Dataset'''
train_dataloader = DataLoader(
AVEDataset('./data/', split='train'),
batch_size=args.batch_size,
shuffle=True,
num_workers=8,
pin_memory=True
)
test_dataloader = DataLoader(
AVEDataset('./data/',split='test'),
batch_size=args.test_batch_size,
shuffle=False,
num_workers=8,
pin_memory=True
)
'''model setting'''
mainModel = main_model()
mainModel = nn.DataParallel(mainModel).cuda()
learned_parameters = mainModel.parameters()
optimizer = torch.optim.Adam(learned_parameters, lr=args.lr)
# scheduler = CosineAnnealingLR(optimizer, T_max=40)
# scheduler = StepLR(optimizer, step_size=30, gamma=0.5)
scheduler = MultiStepLR(optimizer, milestones=[5, 20, 40], gamma=0.5)
criterion = nn.BCEWithLogitsLoss().cuda()
# criterion_event = nn.CrossEntropyLoss().cuda()
criterion_event = nn.MultiLabelSoftMarginLoss().cuda()
'''Resume from a checkpoint'''
if os.path.isfile(args.resume):
logger.info(f"\nLoading Checkpoint: {args.resume}\n")
mainModel.load_state_dict(torch.load(args.resume))
elif args.resume != "" and (not os.path.isfile(args.resume)):
raise FileNotFoundError
'''Only Evaluate'''
if args.evaluate:
logger.info(f"\nStart Evaluation..")
validate_epoch(mainModel, test_dataloader, criterion, criterion_event, epoch=0, eval_only=True)
return
'''Tensorboard and Code backup'''
writer = SummaryWriter(args.snapshot_pref)
recorder = Recorder(args.snapshot_pref, ignore_folder="Exps/")
recorder.writeopt(args)
'''Training and Testing'''
for epoch in range(args.n_epoch):
loss = train_epoch(mainModel, train_dataloader, criterion, criterion_event, optimizer, epoch)
if ((epoch + 1) % args.eval_freq == 0) or (epoch == args.n_epoch - 1):
acc = validate_epoch(mainModel, test_dataloader, criterion, criterion_event, epoch)
if acc > best_accuracy:
best_accuracy = acc
best_accuracy_epoch = epoch
save_checkpoint(
mainModel.state_dict(),
top1=best_accuracy,
task='Supervised',
epoch=epoch + 1,
)
scheduler.step()
def train_epoch(model, train_dataloader, criterion, criterion_event, optimizer, epoch):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
train_acc = AverageMeter()
end_time = time.time()
model.train()
model.double()
optimizer.zero_grad()
for n_iter, batch_data in enumerate(train_dataloader):
data_time.update(time.time() - end_time)
'''Feed input to model'''
visual_feature, audio_feature, labels = batch_data
# visual_feature = visual_feature.float()
# audio_feature = audio_feature.float()
labels = labels.cuda()
is_event_scores, raw_logits, event_scores = model(visual_feature, audio_feature)
# is_event_scores = is_event_scores.transpose(1, 0).squeeze().contiguous()
import numpy
# labels_foreground = labels[:, :, :-1]
# labels_BCE, labels_evn = labels_foreground.max(-1)
# labels_event, _ = labels_evn.max(-1)
# loss_is_event = criterion(is_event_scores, labels_BCE.double().cuda())
loss_event_class = criterion_event(event_scores, labels.double())
# loss = loss_is_event + loss_event_class
loss = loss_event_class
loss.backward()
'''Compute Accuracy'''
# acc = compute_accuracy_supervised(is_event_scores, event_scores, labels)
acc = torch.tensor([0])
train_acc.update(acc.item(), visual_feature.size(0) * 10)
'''Clip Gradient'''
if args.clip_gradient is not None:
total_norm = clip_grad_norm_(model.parameters(), args.clip_gradient)
if total_norm > args.clip_gradient:
logger.info(f'Clipping gradient: {total_norm} with coef {args.clip_gradient/total_norm}.')
'''Update parameters'''
optimizer.step()
optimizer.zero_grad()
losses.update(loss.item(), visual_feature.size(0) * 10)
batch_time.update(time.time() - end_time)
end_time = time.time()
'''Add loss of a iteration in Tensorboard'''
writer.add_scalar('Train_data/loss', losses.val, epoch * len(train_dataloader) + n_iter + 1)
'''Print logs in Terminal'''
if n_iter % args.print_freq == 0:
logger.info(
f'Train Epoch: [{epoch}][{n_iter}/{len(train_dataloader)}]\t'
f'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
f'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
f'Loss {losses.val:.4f} ({losses.avg:.4f})\t'
f'Prec@1 {train_acc.val:.3f} ({train_acc.avg: .3f})'
)
'''Add loss of an epoch in Tensorboard'''
writer.add_scalar('Train_epoch_data/epoch_loss', losses.avg, epoch)
return losses.avg
@torch.no_grad()
def validate_epoch(model, test_dataloader, criterion, criterion_event, epoch, eval_only=False):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
accuracy = AverageMeter()
end_time = time.time()
model.eval()
model.double()
for n_iter, batch_data in enumerate(test_dataloader):
data_time.update(time.time() - end_time)
'''Feed input to model'''
visual_feature, audio_feature, labels = batch_data
# visual_feature = visual_feature.float()
# audio_feature = audio_feature.float()
labels = labels.cuda()
bs = visual_feature.size(0)
is_event_scores, raw_logits, event_scores = model(visual_feature, audio_feature)
# is_event_scores = is_event_scores.transpose(1, 0).squeeze()
#
# labels_foreground = labels[:, :, :-1]
# labels_BCE, labels_evn = labels_foreground.max(-1)
# labels_event, _ = labels_evn.max(-1)
# loss_is_event = criterion(is_event_scores, labels_BCE.double().cuda())
# acc = compute_accuracy_supervised(is_event_scores, event_scores, labels)
acc = compute_accuracy_supervised(is_event_scores, raw_logits, labels)
accuracy.update(acc.item(), bs)
batch_time.update(time.time() - end_time)
end_time = time.time()
'''Print logs in Terminal'''
if n_iter % args.print_freq == 0:
logger.info(
f'Test Epoch [{epoch}][{n_iter}/{len(test_dataloader)}]\t'
f'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
f'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
f'Prec@1 {accuracy.val:.3f} ({accuracy.avg:.3f})'
)
if not eval_only:
writer.add_scalar('Val_epoch/Accuracy', accuracy.avg, epoch)
logger.info(
f"\tEvaluation results (acc): {accuracy.avg:.4f}%."
)
return accuracy.avg
def compute_accuracy_supervised(is_event_scores, event_scores, labels):
# labels = labels[:, :, :-1] # 28 denote background
_, targets = labels.max(-1)
# pos pred
is_event_scores = is_event_scores.sigmoid()
scores_pos_ind = is_event_scores > 0.5
scores_mask = scores_pos_ind == 0
# bg_mask = scores_mask * 28 # 28 denotes bg
_, event_class = event_scores.max(-1) # foreground classification
pred = scores_pos_ind.long()
pred *= event_class[:, None]
# add mask
pred[scores_mask] = 28
# pred += bg_mask
correct = pred.eq(targets)
correct_num = correct.sum().double()
acc = correct_num * (100. / correct.numel())
return acc
def compute_accuracy_weak(event_scores, labels):
# event_scores: [batch, 10, 29]
_, pred = event_scores.max(-1)
pred = pred.transpose(1, 0).contiguous()
_, target = labels.max(-1)
correct = pred.eq(target)
correct_num = correct.sum().double()
acc = correct_num * (100. / correct.numel())
return acc
def save_checkpoint(state_dict, top1, task, epoch):
model_name = f'{args.snapshot_pref}/model_epoch_{epoch}_top1_{top1:.3f}_task_{task}_best_model.pth.tar'
torch.save(state_dict, model_name)
if __name__ == '__main__':
main()