galaxygummybear commited on
Commit
d67a160
·
1 Parent(s): d9c9757

Add new files

Browse files
Files changed (30) hide show
  1. experiments/cls_hrnet_w48_sgd_lr5e-2_wd1e-4_bs32_x100.yaml +92 -0
  2. lib/config/__init__.py +9 -0
  3. lib/config/__pycache__/__init__.cpython-310.pyc +0 -0
  4. lib/config/__pycache__/__init__.cpython-38.pyc +0 -0
  5. lib/config/__pycache__/default.cpython-310.pyc +0 -0
  6. lib/config/__pycache__/default.cpython-38.pyc +0 -0
  7. lib/config/__pycache__/models.cpython-310.pyc +0 -0
  8. lib/config/__pycache__/models.cpython-38.pyc +0 -0
  9. lib/config/default.py +154 -0
  10. lib/config/models.py +47 -0
  11. lib/core/__pycache__/evaluate.cpython-310.pyc +0 -0
  12. lib/core/__pycache__/evaluate.cpython-38.pyc +0 -0
  13. lib/core/__pycache__/function.cpython-310.pyc +0 -0
  14. lib/core/__pycache__/function.cpython-38.pyc +0 -0
  15. lib/core/evaluate.py +28 -0
  16. lib/core/function.py +149 -0
  17. lib/models/__init__.py +11 -0
  18. lib/models/__pycache__/__init__.cpython-310.pyc +0 -0
  19. lib/models/__pycache__/__init__.cpython-38.pyc +0 -0
  20. lib/models/__pycache__/cls_hrnet.cpython-310.pyc +0 -0
  21. lib/models/__pycache__/cls_hrnet.cpython-38.pyc +0 -0
  22. lib/models/cls_hrnet.py +518 -0
  23. lib/utils/__pycache__/modelsummary.cpython-310.pyc +0 -0
  24. lib/utils/__pycache__/modelsummary.cpython-38.pyc +0 -0
  25. lib/utils/__pycache__/utils.cpython-310.pyc +0 -0
  26. lib/utils/__pycache__/utils.cpython-38.pyc +0 -0
  27. lib/utils/modelsummary.py +135 -0
  28. lib/utils/utils.py +92 -0
  29. log/imagenet/cls_hrnet/cls_hrnet_w48_sgd_lr5e-2_wd1e-4_bs32_x100_2023-04-01-01-50/events.out.tfevents.1680285049.DESKTOP-E93KC17 +3 -0
  30. log/imagenet/cls_hrnet/cls_hrnet_w48_sgd_lr5e-2_wd1e-4_bs32_x100_2023-04-01-17-49/events.out.tfevents.1680342578.DESKTOP-E93KC17 +3 -0
experiments/cls_hrnet_w48_sgd_lr5e-2_wd1e-4_bs32_x100.yaml ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ GPUS: (0,)
2
+ LOG_DIR: 'log/'
3
+ DATA_DIR: 'data/'
4
+ OUTPUT_DIR: 'output/'
5
+ WORKERS: 4
6
+ PRINT_FREQ: 1000
7
+
8
+ MODEL:
9
+ NAME: cls_hrnet
10
+ IMAGE_SIZE:
11
+ - 224
12
+ - 224
13
+ EXTRA:
14
+ STAGE1:
15
+ NUM_MODULES: 1
16
+ NUM_RANCHES: 1
17
+ BLOCK: BOTTLENECK
18
+ NUM_BLOCKS:
19
+ - 4
20
+ NUM_CHANNELS:
21
+ - 64
22
+ FUSE_METHOD: SUM
23
+ STAGE2:
24
+ NUM_MODULES: 1
25
+ NUM_BRANCHES: 2
26
+ BLOCK: BASIC
27
+ NUM_BLOCKS:
28
+ - 4
29
+ - 4
30
+ NUM_CHANNELS:
31
+ - 48
32
+ - 96
33
+ FUSE_METHOD: SUM
34
+ STAGE3:
35
+ NUM_MODULES: 4
36
+ NUM_BRANCHES: 3
37
+ BLOCK: BASIC
38
+ NUM_BLOCKS:
39
+ - 4
40
+ - 4
41
+ - 4
42
+ NUM_CHANNELS:
43
+ - 48
44
+ - 96
45
+ - 192
46
+ FUSE_METHOD: SUM
47
+ STAGE4:
48
+ NUM_MODULES: 3
49
+ NUM_BRANCHES: 4
50
+ BLOCK: BASIC
51
+ NUM_BLOCKS:
52
+ - 4
53
+ - 4
54
+ - 4
55
+ - 4
56
+ NUM_CHANNELS:
57
+ - 48
58
+ - 96
59
+ - 192
60
+ - 384
61
+ FUSE_METHOD: SUM
62
+ CUDNN:
63
+ BENCHMARK: true
64
+ DETERMINISTIC: false
65
+ ENABLED: true
66
+ DATASET:
67
+ DATASET: 'imagenet'
68
+ DATA_FORMAT: 'jpg'
69
+ ROOT: 'data/imagenet/'
70
+ TEST_SET: 'val'
71
+ TRAIN_SET: 'train'
72
+ TEST:
73
+ BATCH_SIZE_PER_GPU: 12
74
+ MODEL_FILE: ''
75
+ TRAIN:
76
+ BATCH_SIZE_PER_GPU: 10
77
+ BEGIN_EPOCH: 0
78
+ END_EPOCH: 500
79
+ RESUME: true
80
+ LR_FACTOR: 0.1
81
+ LR_STEP:
82
+ - 30
83
+ - 60
84
+ - 90
85
+ OPTIMIZER: sgd
86
+ LR: 0.05
87
+ WD: 0.0001
88
+ MOMENTUM: 0.9
89
+ NESTEROV: true
90
+ SHUFFLE: true
91
+ DEBUG:
92
+ DEBUG: false
lib/config/__init__.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ # ------------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft
3
+ # Licensed under the MIT License.
4
+ # Written by Bin Xiao (Bin.Xiao@microsoft.com)
5
+ # ------------------------------------------------------------------------------
6
+
7
+ from .default import _C as config
8
+ from .default import update_config
9
+ from .models import MODEL_EXTRAS
lib/config/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (302 Bytes). View file
 
lib/config/__pycache__/__init__.cpython-38.pyc ADDED
Binary file (350 Bytes). View file
 
lib/config/__pycache__/default.cpython-310.pyc ADDED
Binary file (2.85 kB). View file
 
lib/config/__pycache__/default.cpython-38.pyc ADDED
Binary file (2.87 kB). View file
 
lib/config/__pycache__/models.cpython-310.pyc ADDED
Binary file (968 Bytes). View file
 
lib/config/__pycache__/models.cpython-38.pyc ADDED
Binary file (962 Bytes). View file
 
lib/config/default.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # ------------------------------------------------------------------------------
3
+ # Copyright (c) Microsoft
4
+ # Licensed under the MIT License.
5
+ # Written by Bin Xiao (Bin.Xiao@microsoft.com)
6
+ # Modified by Ke Sun (sunk@mail.ustc.edu.cn)
7
+ # ------------------------------------------------------------------------------
8
+
9
+ from __future__ import absolute_import
10
+ from __future__ import division
11
+ from __future__ import print_function
12
+
13
+ import os
14
+
15
+ from yacs.config import CfgNode as CN
16
+
17
+
18
+ _C = CN()
19
+
20
+ _C.OUTPUT_DIR = ''
21
+ _C.LOG_DIR = ''
22
+ _C.DATA_DIR = ''
23
+ _C.GPUS = (0,)
24
+ _C.WORKERS = 4
25
+ _C.PRINT_FREQ = 20
26
+ _C.AUTO_RESUME = False
27
+ _C.PIN_MEMORY = True
28
+ _C.RANK = 0
29
+
30
+ # Cudnn related params
31
+ _C.CUDNN = CN()
32
+ _C.CUDNN.BENCHMARK = True
33
+ _C.CUDNN.DETERMINISTIC = False
34
+ _C.CUDNN.ENABLED = True
35
+
36
+ # common params for NETWORK
37
+ _C.MODEL = CN()
38
+ _C.MODEL.NAME = 'cls_hrnet'
39
+ _C.MODEL.INIT_WEIGHTS = True
40
+ _C.MODEL.PRETRAINED = ''
41
+ _C.MODEL.NUM_JOINTS = 17
42
+ _C.MODEL.NUM_CLASSES = 1000
43
+ _C.MODEL.TAG_PER_JOINT = True
44
+ _C.MODEL.TARGET_TYPE = 'gaussian'
45
+ _C.MODEL.IMAGE_SIZE = [256, 256] # width * height, ex: 192 * 256
46
+ _C.MODEL.HEATMAP_SIZE = [64, 64] # width * height, ex: 24 * 32
47
+ _C.MODEL.SIGMA = 2
48
+ _C.MODEL.EXTRA = CN(new_allowed=True)
49
+
50
+ _C.LOSS = CN()
51
+ _C.LOSS.USE_OHKM = False
52
+ _C.LOSS.TOPK = 8
53
+ _C.LOSS.USE_TARGET_WEIGHT = True
54
+ _C.LOSS.USE_DIFFERENT_JOINTS_WEIGHT = False
55
+
56
+ # DATASET related params
57
+ _C.DATASET = CN()
58
+ _C.DATASET.ROOT = ''
59
+ _C.DATASET.DATASET = 'mpii'
60
+ _C.DATASET.TRAIN_SET = 'train'
61
+ _C.DATASET.TEST_SET = 'valid'
62
+ _C.DATASET.DATA_FORMAT = 'jpg'
63
+ _C.DATASET.HYBRID_JOINTS_TYPE = ''
64
+ _C.DATASET.SELECT_DATA = False
65
+
66
+ # training data augmentation
67
+ _C.DATASET.FLIP = True
68
+ _C.DATASET.SCALE_FACTOR = 0.25
69
+ _C.DATASET.ROT_FACTOR = 30
70
+ _C.DATASET.PROB_HALF_BODY = 0.0
71
+ _C.DATASET.NUM_JOINTS_HALF_BODY = 8
72
+ _C.DATASET.COLOR_RGB = False
73
+
74
+ # train
75
+ _C.TRAIN = CN()
76
+
77
+ _C.TRAIN.LR_FACTOR = 0.1
78
+ _C.TRAIN.LR_STEP = [90, 110]
79
+ _C.TRAIN.LR = 0.001
80
+
81
+ _C.TRAIN.OPTIMIZER = 'adam'
82
+ _C.TRAIN.MOMENTUM = 0.9
83
+ _C.TRAIN.WD = 0.0001
84
+ _C.TRAIN.NESTEROV = False
85
+ _C.TRAIN.GAMMA1 = 0.99
86
+ _C.TRAIN.GAMMA2 = 0.0
87
+
88
+ _C.TRAIN.BEGIN_EPOCH = 0
89
+ _C.TRAIN.END_EPOCH = 140
90
+
91
+ _C.TRAIN.RESUME = False
92
+ _C.TRAIN.CHECKPOINT = ''
93
+
94
+ _C.TRAIN.BATCH_SIZE_PER_GPU = 32
95
+ _C.TRAIN.SHUFFLE = True
96
+
97
+ # testing
98
+ _C.TEST = CN()
99
+
100
+ # size of images for each device
101
+ _C.TEST.BATCH_SIZE_PER_GPU = 32
102
+ # Test Model Epoch
103
+ _C.TEST.FLIP_TEST = False
104
+ _C.TEST.POST_PROCESS = False
105
+ _C.TEST.SHIFT_HEATMAP = False
106
+
107
+ _C.TEST.USE_GT_BBOX = False
108
+
109
+ # nms
110
+ _C.TEST.IMAGE_THRE = 0.1
111
+ _C.TEST.NMS_THRE = 0.6
112
+ _C.TEST.SOFT_NMS = False
113
+ _C.TEST.OKS_THRE = 0.5
114
+ _C.TEST.IN_VIS_THRE = 0.0
115
+ _C.TEST.COCO_BBOX_FILE = ''
116
+ _C.TEST.BBOX_THRE = 1.0
117
+ _C.TEST.MODEL_FILE = ''
118
+
119
+ # debug
120
+ _C.DEBUG = CN()
121
+ _C.DEBUG.DEBUG = False
122
+ _C.DEBUG.SAVE_BATCH_IMAGES_GT = False
123
+ _C.DEBUG.SAVE_BATCH_IMAGES_PRED = False
124
+ _C.DEBUG.SAVE_HEATMAPS_GT = False
125
+ _C.DEBUG.SAVE_HEATMAPS_PRED = False
126
+
127
+
128
+ def update_config(cfg, args):
129
+ cfg.defrost()
130
+ cfg.merge_from_file(args.cfg)
131
+
132
+ if args.modelDir:
133
+ cfg.OUTPUT_DIR = args.modelDir
134
+
135
+ if args.logDir:
136
+ cfg.LOG_DIR = args.logDir
137
+
138
+ if args.dataDir:
139
+ cfg.DATA_DIR = args.dataDir
140
+
141
+ if args.testModel:
142
+ cfg.TEST.MODEL_FILE = args.testModel
143
+
144
+ cfg.DATASET.ROOT = os.path.join(
145
+ cfg.DATA_DIR, cfg.DATASET.DATASET, 'images')
146
+
147
+ cfg.freeze()
148
+
149
+
150
+ if __name__ == '__main__':
151
+ import sys
152
+ with open(sys.argv[1], 'w') as f:
153
+ print(_C, file=f)
154
+
lib/config/models.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ------------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft
3
+ # Licensed under the MIT License.
4
+ # Create by Bin Xiao (Bin.Xiao@microsoft.com)
5
+ # Modified by Ke Sun (sunk@mail.ustc.edu.cn)
6
+ # ------------------------------------------------------------------------------
7
+
8
+ from __future__ import absolute_import
9
+ from __future__ import division
10
+ from __future__ import print_function
11
+
12
+ from yacs.config import CfgNode as CN
13
+
14
+ # high_resoluton_net related params for classification
15
+ POSE_HIGH_RESOLUTION_NET = CN()
16
+ POSE_HIGH_RESOLUTION_NET.PRETRAINED_LAYERS = ['*']
17
+ POSE_HIGH_RESOLUTION_NET.STEM_INPLANES = 64
18
+ POSE_HIGH_RESOLUTION_NET.FINAL_CONV_KERNEL = 1
19
+ POSE_HIGH_RESOLUTION_NET.WITH_HEAD = True
20
+
21
+ POSE_HIGH_RESOLUTION_NET.STAGE2 = CN()
22
+ POSE_HIGH_RESOLUTION_NET.STAGE2.NUM_MODULES = 1
23
+ POSE_HIGH_RESOLUTION_NET.STAGE2.NUM_BRANCHES = 2
24
+ POSE_HIGH_RESOLUTION_NET.STAGE2.NUM_BLOCKS = [4, 4]
25
+ POSE_HIGH_RESOLUTION_NET.STAGE2.NUM_CHANNELS = [32, 64]
26
+ POSE_HIGH_RESOLUTION_NET.STAGE2.BLOCK = 'BASIC'
27
+ POSE_HIGH_RESOLUTION_NET.STAGE2.FUSE_METHOD = 'SUM'
28
+
29
+ POSE_HIGH_RESOLUTION_NET.STAGE3 = CN()
30
+ POSE_HIGH_RESOLUTION_NET.STAGE3.NUM_MODULES = 1
31
+ POSE_HIGH_RESOLUTION_NET.STAGE3.NUM_BRANCHES = 3
32
+ POSE_HIGH_RESOLUTION_NET.STAGE3.NUM_BLOCKS = [4, 4, 4]
33
+ POSE_HIGH_RESOLUTION_NET.STAGE3.NUM_CHANNELS = [32, 64, 128]
34
+ POSE_HIGH_RESOLUTION_NET.STAGE3.BLOCK = 'BASIC'
35
+ POSE_HIGH_RESOLUTION_NET.STAGE3.FUSE_METHOD = 'SUM'
36
+
37
+ POSE_HIGH_RESOLUTION_NET.STAGE4 = CN()
38
+ POSE_HIGH_RESOLUTION_NET.STAGE4.NUM_MODULES = 1
39
+ POSE_HIGH_RESOLUTION_NET.STAGE4.NUM_BRANCHES = 4
40
+ POSE_HIGH_RESOLUTION_NET.STAGE4.NUM_BLOCKS = [4, 4, 4, 4]
41
+ POSE_HIGH_RESOLUTION_NET.STAGE4.NUM_CHANNELS = [32, 64, 128, 256]
42
+ POSE_HIGH_RESOLUTION_NET.STAGE4.BLOCK = 'BASIC'
43
+ POSE_HIGH_RESOLUTION_NET.STAGE4.FUSE_METHOD = 'SUM'
44
+
45
+ MODEL_EXTRAS = {
46
+ 'cls_hrnet': POSE_HIGH_RESOLUTION_NET,
47
+ }
lib/core/__pycache__/evaluate.cpython-310.pyc ADDED
Binary file (881 Bytes). View file
 
lib/core/__pycache__/evaluate.cpython-38.pyc ADDED
Binary file (915 Bytes). View file
 
lib/core/__pycache__/function.cpython-310.pyc ADDED
Binary file (3.5 kB). View file
 
lib/core/__pycache__/function.cpython-38.pyc ADDED
Binary file (3.5 kB). View file
 
lib/core/evaluate.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ------------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft
3
+ # Licensed under the MIT License.
4
+ # Written by Bin Xiao (Bin.Xiao@microsoft.com)
5
+ # ------------------------------------------------------------------------------
6
+
7
+ from __future__ import absolute_import
8
+ from __future__ import division
9
+ from __future__ import print_function
10
+
11
+ import torch
12
+
13
+
14
+ def accuracy(output, target, topk=(1,)):
15
+ """Computes the precision@k for the specified values of k"""
16
+ with torch.no_grad():
17
+ maxk = max(topk)
18
+ batch_size = target.size(0)
19
+
20
+ _, pred = output.topk(maxk, 1, True, True)
21
+ pred = pred.t()
22
+ correct = pred.eq(target.reshape(1, -1).expand_as(pred))
23
+
24
+ res = []
25
+ for k in topk:
26
+ correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
27
+ res.append(correct_k.mul_(100.0 / batch_size))
28
+ return res
lib/core/function.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ------------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft
3
+ # Licensed under the MIT License.
4
+ # Written by Bin Xiao (Bin.Xiao@microsoft.com)
5
+ # ------------------------------------------------------------------------------
6
+
7
+ from __future__ import absolute_import
8
+ from __future__ import division
9
+ from __future__ import print_function
10
+
11
+ import time
12
+ import logging
13
+
14
+ import torch
15
+
16
+ from core.evaluate import accuracy
17
+
18
+
19
+ logger = logging.getLogger(__name__)
20
+
21
+
22
+ def train(config, train_loader, model, criterion, optimizer, epoch,
23
+ output_dir, tb_log_dir, writer_dict):
24
+ batch_time = AverageMeter()
25
+ data_time = AverageMeter()
26
+ losses = AverageMeter()
27
+ top1 = AverageMeter()
28
+ top5 = AverageMeter()
29
+
30
+
31
+ # switch to train mode
32
+ model.train()
33
+
34
+ end = time.time()
35
+ for i, (input, target) in enumerate(train_loader):
36
+ # measure data loading time
37
+ data_time.update(time.time() - end)
38
+ #target = target - 1 # Specific for imagenet
39
+
40
+ # compute output
41
+ output = model(input)
42
+ target = target.cuda(non_blocking=True)
43
+
44
+ loss = criterion(output, target)
45
+
46
+ # compute gradient and do update step
47
+ optimizer.zero_grad()
48
+ loss.backward()
49
+ optimizer.step()
50
+
51
+ # measure accuracy and record loss
52
+ losses.update(loss.item(), input.size(0))
53
+
54
+ prec1, prec5 = accuracy(output, target, (1, 5))
55
+
56
+ top1.update(prec1[0], input.size(0))
57
+ top5.update(prec5[0], input.size(0))
58
+
59
+ # measure elapsed time
60
+ batch_time.update(time.time() - end)
61
+ end = time.time()
62
+
63
+ if i % config.PRINT_FREQ == 0:
64
+ msg = 'Epoch: [{0}][{1}/{2}]\t' \
65
+ 'Time {batch_time.val:.3f}s ({batch_time.avg:.3f}s)\t' \
66
+ 'Speed {speed:.1f} samples/s\t' \
67
+ 'Data {data_time.val:.3f}s ({data_time.avg:.3f}s)\t' \
68
+ 'Loss {loss.val:.5f} ({loss.avg:.5f})\t' \
69
+ 'Accuracy@1 {top1.val:.3f} ({top1.avg:.3f})\t' \
70
+ 'Accuracy@5 {top5.val:.3f} ({top5.avg:.3f})\t'.format(
71
+ epoch, i, len(train_loader), batch_time=batch_time,
72
+ speed=input.size(0)/batch_time.val,
73
+ data_time=data_time, loss=losses, top1=top1, top5=top5)
74
+ logger.info(msg)
75
+
76
+ if writer_dict:
77
+ writer = writer_dict['writer']
78
+ global_steps = writer_dict['train_global_steps']
79
+ writer.add_scalar('train_loss', losses.val, global_steps)
80
+ writer.add_scalar('train_top1', top1.val, global_steps)
81
+ writer_dict['train_global_steps'] = global_steps + 1
82
+
83
+
84
+ def validate(config, val_loader, model, criterion, output_dir, tb_log_dir,
85
+ writer_dict=None):
86
+ batch_time = AverageMeter()
87
+ losses = AverageMeter()
88
+ top1 = AverageMeter()
89
+ top5 = AverageMeter()
90
+
91
+ # switch to evaluate mode
92
+ model.eval()
93
+
94
+ with torch.no_grad():
95
+ end = time.time()
96
+ for i, (input, target) in enumerate(val_loader):
97
+ # compute output
98
+ output = model(input)
99
+
100
+ target = target.cuda(non_blocking=True)
101
+
102
+ loss = criterion(output, target)
103
+
104
+ # measure accuracy and record loss
105
+ losses.update(loss.item(), input.size(0))
106
+ prec1, prec5 = accuracy(output, target, (1, 5))
107
+ top1.update(prec1[0], input.size(0))
108
+ top5.update(prec5[0], input.size(0))
109
+
110
+ # measure elapsed time
111
+ batch_time.update(time.time() - end)
112
+ end = time.time()
113
+
114
+ msg = 'Test: Time {batch_time.avg:.3f}\t' \
115
+ 'Loss {loss.avg:.4f}\t' \
116
+ 'Error@1 {error1:.3f}\t' \
117
+ 'Error@5 {error5:.3f}\t' \
118
+ 'Accuracy@1 {top1.avg:.3f}\t' \
119
+ 'Accuracy@5 {top5.avg:.3f}\t'.format(
120
+ batch_time=batch_time, loss=losses, top1=top1, top5=top5,
121
+ error1=100-top1.avg, error5=100-top5.avg)
122
+ logger.info(msg)
123
+
124
+ if writer_dict:
125
+ writer = writer_dict['writer']
126
+ global_steps = writer_dict['valid_global_steps']
127
+ writer.add_scalar('valid_loss', losses.avg, global_steps)
128
+ writer.add_scalar('valid_top1', top1.avg, global_steps)
129
+ writer_dict['valid_global_steps'] = global_steps + 1
130
+
131
+ return top1.avg
132
+
133
+
134
+ class AverageMeter(object):
135
+ """Computes and stores the average and current value"""
136
+ def __init__(self):
137
+ self.reset()
138
+
139
+ def reset(self):
140
+ self.val = 0
141
+ self.avg = 0
142
+ self.sum = 0
143
+ self.count = 0
144
+
145
+ def update(self, val, n=1):
146
+ self.val = val
147
+ self.sum += val * n
148
+ self.count += n
149
+ self.avg = self.sum / self.count
lib/models/__init__.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ------------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft
3
+ # Licensed under the MIT License.
4
+ # Written by Ke Sun (sunk@mail.ustc.edu.cn)
5
+ # ------------------------------------------------------------------------------
6
+
7
+ from __future__ import absolute_import
8
+ from __future__ import division
9
+ from __future__ import print_function
10
+
11
+ import models.cls_hrnet
lib/models/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (335 Bytes). View file
 
lib/models/__pycache__/__init__.cpython-38.pyc ADDED
Binary file (383 Bytes). View file
 
lib/models/__pycache__/cls_hrnet.cpython-310.pyc ADDED
Binary file (12.2 kB). View file
 
lib/models/__pycache__/cls_hrnet.cpython-38.pyc ADDED
Binary file (12.4 kB). View file
 
lib/models/cls_hrnet.py ADDED
@@ -0,0 +1,518 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ------------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft
3
+ # Licensed under the MIT License.
4
+ # Written by Bin Xiao (Bin.Xiao@microsoft.com)
5
+ # Modified by Ke Sun (sunk@mail.ustc.edu.cn)
6
+ # ------------------------------------------------------------------------------
7
+
8
+ from __future__ import absolute_import
9
+ from __future__ import division
10
+ from __future__ import print_function
11
+
12
+ import os
13
+ import logging
14
+ import functools
15
+
16
+ import numpy as np
17
+
18
+ import torch
19
+ import torch.nn as nn
20
+ import torch._utils
21
+ import torch.nn.functional as F
22
+
23
+ BN_MOMENTUM = 0.1
24
+ logger = logging.getLogger(__name__)
25
+
26
+
27
+ def conv3x3(in_planes, out_planes, stride=1):
28
+ """3x3 convolution with padding"""
29
+ return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
30
+ padding=1, bias=False)
31
+
32
+
33
+ class BasicBlock(nn.Module):
34
+ expansion = 1
35
+
36
+ def __init__(self, inplanes, planes, stride=1, downsample=None):
37
+ super(BasicBlock, self).__init__()
38
+ self.conv1 = conv3x3(inplanes, planes, stride)
39
+ self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
40
+ self.relu = nn.ReLU(inplace=True)
41
+ self.conv2 = conv3x3(planes, planes)
42
+ self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
43
+ self.downsample = downsample
44
+ self.stride = stride
45
+
46
+ def forward(self, x):
47
+ residual = x
48
+
49
+ out = self.conv1(x)
50
+ out = self.bn1(out)
51
+ out = self.relu(out)
52
+
53
+ out = self.conv2(out)
54
+ out = self.bn2(out)
55
+
56
+ if self.downsample is not None:
57
+ residual = self.downsample(x)
58
+
59
+ out += residual
60
+ out = self.relu(out)
61
+
62
+ return out
63
+
64
+
65
+ class Bottleneck(nn.Module):
66
+ expansion = 4
67
+
68
+ def __init__(self, inplanes, planes, stride=1, downsample=None):
69
+ super(Bottleneck, self).__init__()
70
+ self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
71
+ self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
72
+ self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
73
+ padding=1, bias=False)
74
+ self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
75
+ self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1,
76
+ bias=False)
77
+ self.bn3 = nn.BatchNorm2d(planes * self.expansion,
78
+ momentum=BN_MOMENTUM)
79
+ self.relu = nn.ReLU(inplace=True)
80
+ self.downsample = downsample
81
+ self.stride = stride
82
+
83
+ def forward(self, x):
84
+ residual = x
85
+
86
+ out = self.conv1(x)
87
+ out = self.bn1(out)
88
+ out = self.relu(out)
89
+
90
+ out = self.conv2(out)
91
+ out = self.bn2(out)
92
+ out = self.relu(out)
93
+
94
+ out = self.conv3(out)
95
+ out = self.bn3(out)
96
+
97
+ if self.downsample is not None:
98
+ residual = self.downsample(x)
99
+
100
+ out += residual
101
+ out = self.relu(out)
102
+
103
+ return out
104
+
105
+
106
+ class HighResolutionModule(nn.Module):
107
+ def __init__(self, num_branches, blocks, num_blocks, num_inchannels,
108
+ num_channels, fuse_method, multi_scale_output=True):
109
+ super(HighResolutionModule, self).__init__()
110
+ self._check_branches(
111
+ num_branches, blocks, num_blocks, num_inchannels, num_channels)
112
+
113
+ self.num_inchannels = num_inchannels
114
+ self.fuse_method = fuse_method
115
+ self.num_branches = num_branches
116
+
117
+ self.multi_scale_output = multi_scale_output
118
+
119
+ self.branches = self._make_branches(
120
+ num_branches, blocks, num_blocks, num_channels)
121
+ self.fuse_layers = self._make_fuse_layers()
122
+ self.relu = nn.ReLU(False)
123
+
124
+ def _check_branches(self, num_branches, blocks, num_blocks,
125
+ num_inchannels, num_channels):
126
+ if num_branches != len(num_blocks):
127
+ error_msg = 'NUM_BRANCHES({}) <> NUM_BLOCKS({})'.format(
128
+ num_branches, len(num_blocks))
129
+ logger.error(error_msg)
130
+ raise ValueError(error_msg)
131
+
132
+ if num_branches != len(num_channels):
133
+ error_msg = 'NUM_BRANCHES({}) <> NUM_CHANNELS({})'.format(
134
+ num_branches, len(num_channels))
135
+ logger.error(error_msg)
136
+ raise ValueError(error_msg)
137
+
138
+ if num_branches != len(num_inchannels):
139
+ error_msg = 'NUM_BRANCHES({}) <> NUM_INCHANNELS({})'.format(
140
+ num_branches, len(num_inchannels))
141
+ logger.error(error_msg)
142
+ raise ValueError(error_msg)
143
+
144
+ def _make_one_branch(self, branch_index, block, num_blocks, num_channels,
145
+ stride=1):
146
+ downsample = None
147
+ if stride != 1 or \
148
+ self.num_inchannels[branch_index] != num_channels[branch_index] * block.expansion:
149
+ downsample = nn.Sequential(
150
+ nn.Conv2d(self.num_inchannels[branch_index],
151
+ num_channels[branch_index] * block.expansion,
152
+ kernel_size=1, stride=stride, bias=False),
153
+ nn.BatchNorm2d(num_channels[branch_index] * block.expansion,
154
+ momentum=BN_MOMENTUM),
155
+ )
156
+
157
+ layers = []
158
+ layers.append(block(self.num_inchannels[branch_index],
159
+ num_channels[branch_index], stride, downsample))
160
+ self.num_inchannels[branch_index] = \
161
+ num_channels[branch_index] * block.expansion
162
+ for i in range(1, num_blocks[branch_index]):
163
+ layers.append(block(self.num_inchannels[branch_index],
164
+ num_channels[branch_index]))
165
+
166
+ return nn.Sequential(*layers)
167
+
168
+ def _make_branches(self, num_branches, block, num_blocks, num_channels):
169
+ branches = []
170
+
171
+ for i in range(num_branches):
172
+ branches.append(
173
+ self._make_one_branch(i, block, num_blocks, num_channels))
174
+
175
+ return nn.ModuleList(branches)
176
+
177
+ def _make_fuse_layers(self):
178
+ if self.num_branches == 1:
179
+ return None
180
+
181
+ num_branches = self.num_branches
182
+ num_inchannels = self.num_inchannels
183
+ fuse_layers = []
184
+ for i in range(num_branches if self.multi_scale_output else 1):
185
+ fuse_layer = []
186
+ for j in range(num_branches):
187
+ if j > i:
188
+ fuse_layer.append(nn.Sequential(
189
+ nn.Conv2d(num_inchannels[j],
190
+ num_inchannels[i],
191
+ 1,
192
+ 1,
193
+ 0,
194
+ bias=False),
195
+ nn.BatchNorm2d(num_inchannels[i],
196
+ momentum=BN_MOMENTUM),
197
+ nn.Upsample(scale_factor=2**(j-i), mode='nearest')))
198
+ elif j == i:
199
+ fuse_layer.append(None)
200
+ else:
201
+ conv3x3s = []
202
+ for k in range(i-j):
203
+ if k == i - j - 1:
204
+ num_outchannels_conv3x3 = num_inchannels[i]
205
+ conv3x3s.append(nn.Sequential(
206
+ nn.Conv2d(num_inchannels[j],
207
+ num_outchannels_conv3x3,
208
+ 3, 2, 1, bias=False),
209
+ nn.BatchNorm2d(num_outchannels_conv3x3,
210
+ momentum=BN_MOMENTUM)))
211
+ else:
212
+ num_outchannels_conv3x3 = num_inchannels[j]
213
+ conv3x3s.append(nn.Sequential(
214
+ nn.Conv2d(num_inchannels[j],
215
+ num_outchannels_conv3x3,
216
+ 3, 2, 1, bias=False),
217
+ nn.BatchNorm2d(num_outchannels_conv3x3,
218
+ momentum=BN_MOMENTUM),
219
+ nn.ReLU(False)))
220
+ fuse_layer.append(nn.Sequential(*conv3x3s))
221
+ fuse_layers.append(nn.ModuleList(fuse_layer))
222
+
223
+ return nn.ModuleList(fuse_layers)
224
+
225
+ def get_num_inchannels(self):
226
+ return self.num_inchannels
227
+
228
+ def forward(self, x):
229
+ if self.num_branches == 1:
230
+ return [self.branches[0](x[0])]
231
+
232
+ for i in range(self.num_branches):
233
+ x[i] = self.branches[i](x[i])
234
+
235
+ x_fuse = []
236
+ for i in range(len(self.fuse_layers)):
237
+ y = x[0] if i == 0 else self.fuse_layers[i][0](x[0])
238
+ for j in range(1, self.num_branches):
239
+ if i == j:
240
+ y = y + x[j]
241
+ else:
242
+ y = y + self.fuse_layers[i][j](x[j])
243
+ x_fuse.append(self.relu(y))
244
+
245
+ return x_fuse
246
+
247
+
248
+ blocks_dict = {
249
+ 'BASIC': BasicBlock,
250
+ 'BOTTLENECK': Bottleneck
251
+ }
252
+
253
+
254
+ class HighResolutionNet(nn.Module):
255
+
256
+ def __init__(self, cfg, **kwargs):
257
+ super(HighResolutionNet, self).__init__()
258
+
259
+ self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1,
260
+ bias=False)
261
+ self.bn1 = nn.BatchNorm2d(64, momentum=BN_MOMENTUM)
262
+ self.conv2 = nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1,
263
+ bias=False)
264
+ self.bn2 = nn.BatchNorm2d(64, momentum=BN_MOMENTUM)
265
+ self.relu = nn.ReLU(inplace=True)
266
+
267
+ self.stage1_cfg = cfg['MODEL']['EXTRA']['STAGE1']
268
+ num_channels = self.stage1_cfg['NUM_CHANNELS'][0]
269
+ block = blocks_dict[self.stage1_cfg['BLOCK']]
270
+ num_blocks = self.stage1_cfg['NUM_BLOCKS'][0]
271
+ self.layer1 = self._make_layer(block, 64, num_channels, num_blocks)
272
+ stage1_out_channel = block.expansion*num_channels
273
+
274
+ self.stage2_cfg = cfg['MODEL']['EXTRA']['STAGE2']
275
+ num_channels = self.stage2_cfg['NUM_CHANNELS']
276
+ block = blocks_dict[self.stage2_cfg['BLOCK']]
277
+ num_channels = [
278
+ num_channels[i] * block.expansion for i in range(len(num_channels))]
279
+ self.transition1 = self._make_transition_layer(
280
+ [stage1_out_channel], num_channels)
281
+ self.stage2, pre_stage_channels = self._make_stage(
282
+ self.stage2_cfg, num_channels)
283
+
284
+ self.stage3_cfg = cfg['MODEL']['EXTRA']['STAGE3']
285
+ num_channels = self.stage3_cfg['NUM_CHANNELS']
286
+ block = blocks_dict[self.stage3_cfg['BLOCK']]
287
+ num_channels = [
288
+ num_channels[i] * block.expansion for i in range(len(num_channels))]
289
+ self.transition2 = self._make_transition_layer(
290
+ pre_stage_channels, num_channels)
291
+ self.stage3, pre_stage_channels = self._make_stage(
292
+ self.stage3_cfg, num_channels)
293
+
294
+ self.stage4_cfg = cfg['MODEL']['EXTRA']['STAGE4']
295
+ num_channels = self.stage4_cfg['NUM_CHANNELS']
296
+ block = blocks_dict[self.stage4_cfg['BLOCK']]
297
+ num_channels = [
298
+ num_channels[i] * block.expansion for i in range(len(num_channels))]
299
+ self.transition3 = self._make_transition_layer(
300
+ pre_stage_channels, num_channels)
301
+ self.stage4, pre_stage_channels = self._make_stage(
302
+ self.stage4_cfg, num_channels, multi_scale_output=True)
303
+
304
+ # Classification Head
305
+ self.incre_modules, self.downsamp_modules, \
306
+ self.final_layer = self._make_head(pre_stage_channels)
307
+
308
+ self.classifier = nn.Linear(2048, 1000)
309
+
310
+ def _make_head(self, pre_stage_channels):
311
+ head_block = Bottleneck
312
+ head_channels = [32, 64, 128, 256]
313
+
314
+ # Increasing the #channels on each resolution
315
+ # from C, 2C, 4C, 8C to 128, 256, 512, 1024
316
+ incre_modules = []
317
+ for i, channels in enumerate(pre_stage_channels):
318
+ incre_module = self._make_layer(head_block,
319
+ channels,
320
+ head_channels[i],
321
+ 1,
322
+ stride=1)
323
+ incre_modules.append(incre_module)
324
+ incre_modules = nn.ModuleList(incre_modules)
325
+
326
+ # downsampling modules
327
+ downsamp_modules = []
328
+ for i in range(len(pre_stage_channels)-1):
329
+ in_channels = head_channels[i] * head_block.expansion
330
+ out_channels = head_channels[i+1] * head_block.expansion
331
+
332
+ downsamp_module = nn.Sequential(
333
+ nn.Conv2d(in_channels=in_channels,
334
+ out_channels=out_channels,
335
+ kernel_size=3,
336
+ stride=2,
337
+ padding=1),
338
+ nn.BatchNorm2d(out_channels, momentum=BN_MOMENTUM),
339
+ nn.ReLU(inplace=True)
340
+ )
341
+
342
+ downsamp_modules.append(downsamp_module)
343
+ downsamp_modules = nn.ModuleList(downsamp_modules)
344
+
345
+ final_layer = nn.Sequential(
346
+ nn.Conv2d(
347
+ in_channels=head_channels[3] * head_block.expansion,
348
+ out_channels=2048,
349
+ kernel_size=1,
350
+ stride=1,
351
+ padding=0
352
+ ),
353
+ nn.BatchNorm2d(2048, momentum=BN_MOMENTUM),
354
+ nn.ReLU(inplace=True)
355
+ )
356
+
357
+ return incre_modules, downsamp_modules, final_layer
358
+
359
+ def _make_transition_layer(
360
+ self, num_channels_pre_layer, num_channels_cur_layer):
361
+ num_branches_cur = len(num_channels_cur_layer)
362
+ num_branches_pre = len(num_channels_pre_layer)
363
+
364
+ transition_layers = []
365
+ for i in range(num_branches_cur):
366
+ if i < num_branches_pre:
367
+ if num_channels_cur_layer[i] != num_channels_pre_layer[i]:
368
+ transition_layers.append(nn.Sequential(
369
+ nn.Conv2d(num_channels_pre_layer[i],
370
+ num_channels_cur_layer[i],
371
+ 3,
372
+ 1,
373
+ 1,
374
+ bias=False),
375
+ nn.BatchNorm2d(
376
+ num_channels_cur_layer[i], momentum=BN_MOMENTUM),
377
+ nn.ReLU(inplace=True)))
378
+ else:
379
+ transition_layers.append(None)
380
+ else:
381
+ conv3x3s = []
382
+ for j in range(i+1-num_branches_pre):
383
+ inchannels = num_channels_pre_layer[-1]
384
+ outchannels = num_channels_cur_layer[i] \
385
+ if j == i-num_branches_pre else inchannels
386
+ conv3x3s.append(nn.Sequential(
387
+ nn.Conv2d(
388
+ inchannels, outchannels, 3, 2, 1, bias=False),
389
+ nn.BatchNorm2d(outchannels, momentum=BN_MOMENTUM),
390
+ nn.ReLU(inplace=True)))
391
+ transition_layers.append(nn.Sequential(*conv3x3s))
392
+
393
+ return nn.ModuleList(transition_layers)
394
+
395
+ def _make_layer(self, block, inplanes, planes, blocks, stride=1):
396
+ downsample = None
397
+ if stride != 1 or inplanes != planes * block.expansion:
398
+ downsample = nn.Sequential(
399
+ nn.Conv2d(inplanes, planes * block.expansion,
400
+ kernel_size=1, stride=stride, bias=False),
401
+ nn.BatchNorm2d(planes * block.expansion, momentum=BN_MOMENTUM),
402
+ )
403
+
404
+ layers = []
405
+ layers.append(block(inplanes, planes, stride, downsample))
406
+ inplanes = planes * block.expansion
407
+ for i in range(1, blocks):
408
+ layers.append(block(inplanes, planes))
409
+
410
+ return nn.Sequential(*layers)
411
+
412
+ def _make_stage(self, layer_config, num_inchannels,
413
+ multi_scale_output=True):
414
+ num_modules = layer_config['NUM_MODULES']
415
+ num_branches = layer_config['NUM_BRANCHES']
416
+ num_blocks = layer_config['NUM_BLOCKS']
417
+ num_channels = layer_config['NUM_CHANNELS']
418
+ block = blocks_dict[layer_config['BLOCK']]
419
+ fuse_method = layer_config['FUSE_METHOD']
420
+
421
+ modules = []
422
+ for i in range(num_modules):
423
+ # multi_scale_output is only used last module
424
+ if not multi_scale_output and i == num_modules - 1:
425
+ reset_multi_scale_output = False
426
+ else:
427
+ reset_multi_scale_output = True
428
+
429
+ modules.append(
430
+ HighResolutionModule(num_branches,
431
+ block,
432
+ num_blocks,
433
+ num_inchannels,
434
+ num_channels,
435
+ fuse_method,
436
+ reset_multi_scale_output)
437
+ )
438
+ num_inchannels = modules[-1].get_num_inchannels()
439
+
440
+ return nn.Sequential(*modules), num_inchannels
441
+
442
+ def forward(self, x):
443
+ x = self.conv1(x)
444
+ x = self.bn1(x)
445
+ x = self.relu(x)
446
+ x = self.conv2(x)
447
+ x = self.bn2(x)
448
+ x = self.relu(x)
449
+ x = self.layer1(x)
450
+
451
+ x_list = []
452
+ for i in range(self.stage2_cfg['NUM_BRANCHES']):
453
+ if self.transition1[i] is not None:
454
+ x_list.append(self.transition1[i](x))
455
+ else:
456
+ x_list.append(x)
457
+ y_list = self.stage2(x_list)
458
+
459
+ x_list = []
460
+ for i in range(self.stage3_cfg['NUM_BRANCHES']):
461
+ if self.transition2[i] is not None:
462
+ x_list.append(self.transition2[i](y_list[-1]))
463
+ else:
464
+ x_list.append(y_list[i])
465
+ y_list = self.stage3(x_list)
466
+
467
+ x_list = []
468
+ for i in range(self.stage4_cfg['NUM_BRANCHES']):
469
+ if self.transition3[i] is not None:
470
+ x_list.append(self.transition3[i](y_list[-1]))
471
+ else:
472
+ x_list.append(y_list[i])
473
+ y_list = self.stage4(x_list)
474
+
475
+ # Classification Head
476
+ y = self.incre_modules[0](y_list[0])
477
+ for i in range(len(self.downsamp_modules)):
478
+ y = self.incre_modules[i+1](y_list[i+1]) + \
479
+ self.downsamp_modules[i](y)
480
+
481
+ y = self.final_layer(y)
482
+
483
+ if torch._C._get_tracing_state():
484
+ y = y.flatten(start_dim=2).mean(dim=2)
485
+ else:
486
+ y = F.avg_pool2d(y, kernel_size=y.size()
487
+ [2:]).view(y.size(0), -1)
488
+
489
+ y = self.classifier(y)
490
+
491
+ return y
492
+
493
+ def init_weights(self, pretrained='',):
494
+ logger.info('=> init weights from normal distribution')
495
+ for m in self.modules():
496
+ if isinstance(m, nn.Conv2d):
497
+ nn.init.kaiming_normal_(
498
+ m.weight, mode='fan_out', nonlinearity='relu')
499
+ elif isinstance(m, nn.BatchNorm2d):
500
+ nn.init.constant_(m.weight, 1)
501
+ nn.init.constant_(m.bias, 0)
502
+ if os.path.isfile(pretrained):
503
+ pretrained_dict = torch.load(pretrained)
504
+ logger.info('=> loading pretrained model {}'.format(pretrained))
505
+ model_dict = self.state_dict()
506
+ pretrained_dict = {k: v for k, v in pretrained_dict.items()
507
+ if k in model_dict.keys()}
508
+ for k, _ in pretrained_dict.items():
509
+ logger.info(
510
+ '=> loading {} pretrained model {}'.format(k, pretrained))
511
+ model_dict.update(pretrained_dict)
512
+ self.load_state_dict(model_dict)
513
+
514
+
515
+ def get_cls_net(config, **kwargs):
516
+ model = HighResolutionNet(config, **kwargs)
517
+ model.init_weights()
518
+ return model
lib/utils/__pycache__/modelsummary.cpython-310.pyc ADDED
Binary file (3.18 kB). View file
 
lib/utils/__pycache__/modelsummary.cpython-38.pyc ADDED
Binary file (3.24 kB). View file
 
lib/utils/__pycache__/utils.cpython-310.pyc ADDED
Binary file (2.54 kB). View file
 
lib/utils/__pycache__/utils.cpython-38.pyc ADDED
Binary file (2.57 kB). View file
 
lib/utils/modelsummary.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ------------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft
3
+ # Licensed under the MIT License.
4
+ # Written by Bin Xiao (Bin.Xiao@microsoft.com)
5
+ # Modified by Ke Sun (sunk@mail.ustc.edu.cn)
6
+ # ------------------------------------------------------------------------------
7
+
8
+ from __future__ import absolute_import
9
+ from __future__ import division
10
+ from __future__ import print_function
11
+
12
+ import os
13
+ import logging
14
+ from collections import namedtuple
15
+
16
+ import torch
17
+ import torch.nn as nn
18
+
19
+ def get_model_summary(model, *input_tensors, item_length=26, verbose=False):
20
+ """
21
+ :param model:
22
+ :param input_tensors:
23
+ :param item_length:
24
+ :return:
25
+ """
26
+
27
+ summary = []
28
+
29
+ ModuleDetails = namedtuple(
30
+ "Layer", ["name", "input_size", "output_size", "num_parameters", "multiply_adds"])
31
+ hooks = []
32
+ layer_instances = {}
33
+
34
+ def add_hooks(module):
35
+
36
+ def hook(module, input, output):
37
+ class_name = str(module.__class__.__name__)
38
+
39
+ instance_index = 1
40
+ if class_name not in layer_instances:
41
+ layer_instances[class_name] = instance_index
42
+ else:
43
+ instance_index = layer_instances[class_name] + 1
44
+ layer_instances[class_name] = instance_index
45
+
46
+ layer_name = class_name + "_" + str(instance_index)
47
+
48
+ params = 0
49
+
50
+ if class_name.find("Conv") != -1 or class_name.find("BatchNorm") != -1 or \
51
+ class_name.find("Linear") != -1:
52
+ for param_ in module.parameters():
53
+ params += param_.view(-1).size(0)
54
+
55
+ flops = "Not Available"
56
+ if class_name.find("Conv") != -1 and hasattr(module, "weight"):
57
+ flops = (
58
+ torch.prod(
59
+ torch.LongTensor(list(module.weight.data.size()))) *
60
+ torch.prod(
61
+ torch.LongTensor(list(output.size())[2:]))).item()
62
+ elif isinstance(module, nn.Linear):
63
+ flops = (torch.prod(torch.LongTensor(list(output.size()))) \
64
+ * input[0].size(1)).item()
65
+
66
+ if isinstance(input[0], list):
67
+ input = input[0]
68
+ if isinstance(output, list):
69
+ output = output[0]
70
+
71
+ summary.append(
72
+ ModuleDetails(
73
+ name=layer_name,
74
+ input_size=list(input[0].size()),
75
+ output_size=list(output.size()),
76
+ num_parameters=params,
77
+ multiply_adds=flops)
78
+ )
79
+
80
+ if not isinstance(module, nn.ModuleList) \
81
+ and not isinstance(module, nn.Sequential) \
82
+ and module != model:
83
+ hooks.append(module.register_forward_hook(hook))
84
+
85
+ model.eval()
86
+ model.apply(add_hooks)
87
+
88
+ space_len = item_length
89
+
90
+ model(*input_tensors)
91
+ for hook in hooks:
92
+ hook.remove()
93
+
94
+ details = ''
95
+ if verbose:
96
+ details = "Model Summary" + \
97
+ os.linesep + \
98
+ "Name{}Input Size{}Output Size{}Parameters{}Multiply Adds (Flops){}".format(
99
+ ' ' * (space_len - len("Name")),
100
+ ' ' * (space_len - len("Input Size")),
101
+ ' ' * (space_len - len("Output Size")),
102
+ ' ' * (space_len - len("Parameters")),
103
+ ' ' * (space_len - len("Multiply Adds (Flops)"))) \
104
+ + os.linesep + '-' * space_len * 5 + os.linesep
105
+
106
+ params_sum = 0
107
+ flops_sum = 0
108
+ for layer in summary:
109
+ params_sum += layer.num_parameters
110
+ if layer.multiply_adds != "Not Available":
111
+ flops_sum += layer.multiply_adds
112
+ if verbose:
113
+ details += "{}{}{}{}{}{}{}{}{}{}".format(
114
+ layer.name,
115
+ ' ' * (space_len - len(layer.name)),
116
+ layer.input_size,
117
+ ' ' * (space_len - len(str(layer.input_size))),
118
+ layer.output_size,
119
+ ' ' * (space_len - len(str(layer.output_size))),
120
+ layer.num_parameters,
121
+ ' ' * (space_len - len(str(layer.num_parameters))),
122
+ layer.multiply_adds,
123
+ ' ' * (space_len - len(str(layer.multiply_adds)))) \
124
+ + os.linesep + '-' * space_len * 5 + os.linesep
125
+
126
+ details += os.linesep \
127
+ + "Total Parameters: {:,}".format(params_sum) \
128
+ + os.linesep + '-' * space_len * 5 + os.linesep
129
+ details += "Total Multiply Adds (For Convolution and Linear Layers only): {:,} GFLOPs".format(flops_sum/(1024**3)) \
130
+ + os.linesep + '-' * space_len * 5 + os.linesep
131
+ details += "Number of Layers" + os.linesep
132
+ for layer in layer_instances:
133
+ details += "{} : {} layers ".format(layer, layer_instances[layer])
134
+
135
+ return details
lib/utils/utils.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ------------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft
3
+ # Licensed under the MIT License.
4
+ # Written by Bin Xiao (Bin.Xiao@microsoft.com)
5
+ # Modified by Ke Sun (sunk@mail.ustc.edu.cn)
6
+ # ------------------------------------------------------------------------------
7
+
8
+ from __future__ import absolute_import
9
+ from __future__ import division
10
+ from __future__ import print_function
11
+
12
+ import os
13
+ import logging
14
+ import time
15
+ from pathlib import Path
16
+
17
+ import torch
18
+ import torch.optim as optim
19
+
20
+ def create_logger(cfg, cfg_name, phase='train'):
21
+ root_output_dir = Path(cfg.OUTPUT_DIR)
22
+ # set up logger
23
+ if not root_output_dir.exists():
24
+ print('=> creating {}'.format(root_output_dir))
25
+ root_output_dir.mkdir()
26
+
27
+ dataset = cfg.DATASET.DATASET
28
+ model = cfg.MODEL.NAME
29
+ cfg_name = os.path.basename(cfg_name).split('.')[0]
30
+
31
+ final_output_dir = root_output_dir / dataset / cfg_name
32
+
33
+ print('=> creating {}'.format(final_output_dir))
34
+ final_output_dir.mkdir(parents=True, exist_ok=True)
35
+
36
+ time_str = time.strftime('%Y-%m-%d-%H-%M')
37
+ log_file = '{}_{}_{}.log'.format(cfg_name, time_str, phase)
38
+ final_log_file = final_output_dir / log_file
39
+ head = '%(asctime)-15s %(message)s'
40
+ logging.basicConfig(filename=str(final_log_file),
41
+ format=head)
42
+ logger = logging.getLogger()
43
+ logger.setLevel(logging.INFO)
44
+ console = logging.StreamHandler()
45
+ logging.getLogger('').addHandler(console)
46
+
47
+ tensorboard_log_dir = Path(cfg.LOG_DIR) / dataset / model / \
48
+ (cfg_name + '_' + time_str)
49
+ print('=> creating {}'.format(tensorboard_log_dir))
50
+ tensorboard_log_dir.mkdir(parents=True, exist_ok=True)
51
+
52
+ return logger, str(final_output_dir), str(tensorboard_log_dir)
53
+
54
+
55
+ def get_optimizer(cfg, model):
56
+ optimizer = None
57
+ if cfg.TRAIN.OPTIMIZER == 'sgd':
58
+ optimizer = optim.SGD(
59
+ #model.parameters(),
60
+ filter(lambda p: p.requires_grad, model.parameters()),
61
+ lr=cfg.TRAIN.LR,
62
+ momentum=cfg.TRAIN.MOMENTUM,
63
+ weight_decay=cfg.TRAIN.WD,
64
+ nesterov=cfg.TRAIN.NESTEROV
65
+ )
66
+ elif cfg.TRAIN.OPTIMIZER == 'adam':
67
+ optimizer = optim.Adam(
68
+ #model.parameters(),
69
+ filter(lambda p: p.requires_grad, model.parameters()),
70
+ lr=cfg.TRAIN.LR
71
+ )
72
+ elif cfg.TRAIN.OPTIMIZER == 'rmsprop':
73
+ optimizer = optim.RMSprop(
74
+ #model.parameters(),
75
+ filter(lambda p: p.requires_grad, model.parameters()),
76
+ lr=cfg.TRAIN.LR,
77
+ momentum=cfg.TRAIN.MOMENTUM,
78
+ weight_decay=cfg.TRAIN.WD,
79
+ alpha=cfg.TRAIN.RMSPROP_ALPHA,
80
+ centered=cfg.TRAIN.RMSPROP_CENTERED
81
+ )
82
+
83
+ return optimizer
84
+
85
+
86
+ def save_checkpoint(states, is_best, output_dir,
87
+ filename='checkpoint.pth.tar'):
88
+ torch.save(states, os.path.join(output_dir, filename))
89
+ if is_best and 'state_dict' in states:
90
+ torch.save(states['state_dict'],
91
+ os.path.join(output_dir, 'model_best.pth.tar'))
92
+
log/imagenet/cls_hrnet/cls_hrnet_w48_sgd_lr5e-2_wd1e-4_bs32_x100_2023-04-01-01-50/events.out.tfevents.1680285049.DESKTOP-E93KC17 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:49832defcaa3ab4311bfb6e605bd62ff7de78197ec374d95cdccd6951884afa7
3
+ size 58320
log/imagenet/cls_hrnet/cls_hrnet_w48_sgd_lr5e-2_wd1e-4_bs32_x100_2023-04-01-17-49/events.out.tfevents.1680342578.DESKTOP-E93KC17 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4161538afd3fe5721eaf780292358a982978063a12ed16187e809668a2406d39
3
+ size 38720