File size: 12,518 Bytes
d7e58f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import math
import os
import time
import datetime
import sys
from typing import Iterable
import os.path as osp
import torch
import util.misc as utils
from collections import OrderedDict
import mmcv
import torch
import numpy as np
import torch.distributed as dist
from mmcv.runner import get_dist_info
from detrsmpl.apis.test import collect_results_cpu, collect_results_gpu
from detrsmpl.utils.ffmpeg_utils import images_to_video
from torch.utils.tensorboard import SummaryWriter   
import json
from mmcv.runner import get_dist_info, init_dist

def round_float(items):
    if isinstance(items, list):
        return [round_float(item) for item in items]
    elif isinstance(items, float):
        return round(items, 3)
    elif isinstance(items, np.ndarray):
        return round_float(float(items))
    elif isinstance(items, torch.Tensor):
        return round_float(items.detach().cpu().numpy())
    else:
        return items

def train_one_epoch(model: torch.nn.Module,
                    criterion: torch.nn.Module,
                    data_loader: Iterable,
                    optimizer: torch.optim.Optimizer,
                    device: torch.device,
                    epoch: int,
                    max_norm: float = 0,
                    wo_class_error=False,
                    lr_scheduler=None,
                    args=None,
                    logger=None,
                    ema_m=None,
                    tf_writer=None):
    scaler = torch.cuda.amp.GradScaler(enabled=args.amp)

    try:
        need_tgt_for_training = args.use_dn
    except:
        need_tgt_for_training = False

    model.train()
    criterion.train()
    metric_logger = utils.MetricLogger(delimiter='  ')
    metric_logger.add_meter(
        'lr', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
    if not wo_class_error:
        metric_logger.add_meter(
            'class_error', utils.SmoothedValue(window_size=1,
                                               fmt='{value:.2f}'))
    header = 'Epoch: [{}]'.format(epoch)
    print_freq = 10

    _cnt = 0

    for step_i, data_batch in enumerate(metric_logger.log_every(data_loader,
                                              print_freq,
                                              header,
                                              logger=logger)):
        with torch.cuda.amp.autocast(enabled=args.amp):
            if need_tgt_for_training:
                outputs, targets, data_batch_nc = model(data_batch)
            else:
                outputs, targets, data_batch_nc = model(data_batch)
            
            ['hand_kp3d_4', 'face_kp3d_4', 'hand_kp2d_4',]
            loss_dict = criterion(outputs, targets, data_batch=data_batch_nc)
            weight_dict = criterion.weight_dict
            
            for k,v in weight_dict.items():
                for n in ['hand_kp3d_4', 'face_kp3d_4', 'hand_kp2d_4']:
                    if n in k:
                        weight_dict[k] = weight_dict[k]/10

            losses = sum(loss_dict[k] * weight_dict[k]
                         for k in loss_dict.keys() if k in weight_dict)

        loss_dict_reduced = utils.reduce_dict(loss_dict)
        loss_dict_reduced_unscaled = {
            f'{k}_unscaled': v
            for k, v in loss_dict_reduced.items()
        }
        loss_dict_reduced_scaled = {
            k: v * weight_dict[k]
            for k, v in loss_dict_reduced.items() if k in weight_dict
        }
        losses_reduced_scaled = sum(loss_dict_reduced_scaled.values())

        loss_value = losses_reduced_scaled.item()
        # loss_value = loss_value+loss_value_smpl
        for k,v in weight_dict.items():
            for n in ['hand_kp3d_4', 'face_kp3d_4', 'hand_kp2d_4']:
                if n in k:
                    weight_dict[k] = weight_dict[k]*10
        if not math.isfinite(loss_value):
            print('Loss is {}, stopping training'.format(loss_value))
            print(loss_dict_reduced)
            sys.exit(1)

        # amp backward function
        if args.amp:
            optimizer.zero_grad()
            scaler.scale(losses).backward()
            if max_norm > 0:
                scaler.unscale_(optimizer)
                torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm)
            scaler.step(optimizer)
            scaler.update()
        else:
            optimizer.zero_grad()
            losses.backward()
            if max_norm > 0:
                torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm)
            optimizer.step()
        if args.onecyclelr:
            lr_scheduler.step()
        if args.use_ema:
            if epoch >= args.ema_epoch:
                ema_m.update(model)
        rank, _ = get_dist_info()

        if rank == 0:
            tf_writer.add_scalar(
                'loss', round_float(loss_value), step_i + len(data_loader) * epoch)
            for k, v in loss_dict_reduced_scaled.items():
                tf_writer.add_scalar(
                    k, round_float(v), step_i + len(data_loader) * epoch)
            for k, v in loss_dict_reduced_unscaled.items():
                tf_writer.add_scalar(
                    k, round_float(v), step_i + len(data_loader) * epoch)
        json_log = OrderedDict()
        json_log['now_time'] = str(datetime.datetime.now())
        json_log['epoch'] = epoch
        json_log['lr'] = optimizer.param_groups[0]['lr']
        json_log['loss'] = round_float(loss_value)
        for k, v in loss_dict_reduced_scaled.items():
            json_log[k] = round_float(v)

        for k, v in loss_dict_reduced_unscaled.items():
            json_log[k] = round_float(v)

        if rank == 0:
            log_path = os.path.join(args.output_dir, 'train.log.json')
            with open(log_path, 'a+') as f:
                mmcv.dump(json_log, f, file_format='json')
                f.write('\n')

        # metric_logger.update(loss=loss_value, **loss_dict_reduced_scaled, **loss_dict_reduced_unscaled)
        metric_logger.update(loss=loss_value, **loss_dict_reduced_scaled)
        if 'class_error' in loss_dict_reduced:
            metric_logger.update(class_error=loss_dict_reduced['class_error'])
        metric_logger.update(lr=optimizer.param_groups[0]['lr'])

        _cnt += 1
        if args.debug:
            if _cnt % 15 == 0:
                print('BREAK!' * 5)
                break

    if getattr(criterion, 'loss_weight_decay', False):
        criterion.loss_weight_decay(epoch=epoch)
    if getattr(criterion, 'tuning_matching', False):
        criterion.tuning_matching(epoch)

    metric_logger.synchronize_between_processes()
    print('Averaged stats:', metric_logger)
    resstat = {
        k: meter.global_avg
        for k, meter in metric_logger.meters.items() if meter.count > 0
    }
    if getattr(criterion, 'loss_weight_decay', False):
        resstat.update(
            {f'weight_{k}': v
             for k, v in criterion.weight_dict.items()})
    return resstat


@torch.no_grad()
def evaluate(model,
             criterion,
             postprocessors,
             data_loader,
             device,
             output_dir,
             wo_class_error=False,
             tmpdir=None,
             gpu_collect=False,
             args=None,
             logger=None):
    try:
        need_tgt_for_training = args.use_dn
    except:
        need_tgt_for_training = False
    model.eval()
    criterion.eval()

    metric_logger = utils.MetricLogger(delimiter='  ')
    if not wo_class_error:
        metric_logger.add_meter(
            'class_error', utils.SmoothedValue(window_size=1,
                                               fmt='{value:.2f}'))
    header = 'Test:'
    iou_types = tuple(k for k in ('bbox', 'keypoints'))
    try:
        useCats = args.useCats
    except:
        useCats = True
    if not useCats:
        print('useCats: {} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'.format(
            useCats))

    _cnt = 0
    results = []
    dataset = data_loader.dataset
    rank, world_size = get_dist_info()

    if rank == 0:
        # Check if tmpdir is valid for cpu_collect
        if (not gpu_collect) and (tmpdir is not None and osp.exists(tmpdir)):
            raise OSError((f'The tmpdir {tmpdir} already exists.',
                           ' Since tmpdir will be deleted after testing,',
                           ' please make sure you specify an empty one.'))
        prog_bar = mmcv.ProgressBar(len(dataset))
    time.sleep(2)
    # i=0
    cur_sample_idx = 0
    eval_result = {}
    # print()
    cur_eval_result_list = []
    rank, world_size = get_dist_info()

    for data_batch in metric_logger.log_every(
        data_loader, 10, header, logger=logger):
        # i = i+1
        with torch.cuda.amp.autocast(enabled=args.amp):
            if need_tgt_for_training:
                # outputs = model(samples, targets)
                outputs, targets, data_batch_nc = model(data_batch)
            else:
                outputs,targets, data_batch_nc = model(data_batch)
        
        orig_target_sizes = torch.stack([t["size"] for t in targets], dim=0)
        result = postprocessors['bbox'](outputs, orig_target_sizes, targets, data_batch_nc,dataset = dataset)    
        
        # DOING SMPLer-X Test
        cur_eval_result = dataset.evaluate(result,cur_sample_idx)
        
        cur_eval_result_list.append(cur_eval_result)
        # for cur_eval_result in cur_eval_result_list:
        #     for k, v in cur_eval_result.items():
        #         if k in eval_result:
        #             eval_result[k] += v
        #         else:
        #             eval_result[k] = v
        cur_sample_idx += len(result)
    cur_eval_result_new = collect_results_cpu(cur_eval_result_list, len(dataset))
    
    if rank == 0:
        
        cntt = 0
        for res in cur_eval_result_new:

            for k,v in res.items():
                if len(v)>0:
                    if k != 'ann_idx' and k != 'img_path':                 
                        if k in eval_result:
                            eval_result[k].append(v)
                        else:
                            eval_result[k] = [v]

        for k,v in eval_result.items():
            
            # if k == 'mpvpe_all' or k == 'pa_mpvpe_all':
            eval_result[k] = np.concatenate(v)
            
            
        dataset.print_eval_result(eval_result)
        # print(len(cur_eval_result_new))


@torch.no_grad()
def inference(model,
             criterion,
             postprocessors,
             data_loader,
             device,
             output_dir,
             wo_class_error=False,
             tmpdir=None,
             gpu_collect=False,
             args=None,
             logger=None):
    try:
        need_tgt_for_training = args.use_dn
    except:
        need_tgt_for_training = False
    model.eval()
    criterion.eval()

    metric_logger = utils.MetricLogger(delimiter='  ')
    if not wo_class_error:
        metric_logger.add_meter(
            'class_error', utils.SmoothedValue(window_size=1,
                                               fmt='{value:.2f}'))
    header = 'Test:'
    iou_types = tuple(k for k in ('bbox', 'keypoints'))
    try:
        useCats = args.useCats
    except:
        useCats = True
    if not useCats:
        print('useCats: {} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'.format(
            useCats))

    _cnt = 0
    results = []
    dataset = data_loader.dataset
    rank, world_size = get_dist_info()
    for data_batch in metric_logger.log_every(data_loader, 10, header, logger=logger):
        with torch.cuda.amp.autocast(enabled=args.amp):
            if need_tgt_for_training:
                # outputs = model(samples, targets)
                outputs, targets, data_batch_nc = model(data_batch)
            else:
                outputs,targets, data_batch_nc = model(data_batch)
        
        orig_target_sizes = torch.stack([t["size"] for t in targets], dim=0)
        result = postprocessors['bbox'](outputs, orig_target_sizes, targets, data_batch_nc)    
        dataset.inference(result)

    time.sleep(3)
    if rank == 0 and args.to_vid:
        # img_tmp = dataset.img_path[0]
        if hasattr(dataset,'result_img_dir'):
            import shutil
            images_to_video(dataset.result_img_dir, os.path.join(dataset.output_path,'demo_vid.mp4'),remove_raw_file=False, fps=30)
            # shutil.rmtree(dataset.result_img_dir)
            # shutil.rmtree(dataset.tmp_dir)