-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
254 lines (222 loc) · 7.59 KB
/
utils.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
import torch
import sys
import numpy as np
import random
import traceback
import argparse
from torchlight import DictAction
import torch.nn as nn
import torch.distributed as dist
import pickle
def init_seed(seed):
torch.cuda.manual_seed_all(seed)
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
# torch.backends.cudnn.enabled = True
# training speed is too slow if set to True
torch.backends.cudnn.deterministic = False
# on cuda 11 cudnn8, the default algorithm is very slow
# unlike on cuda 10, the default works well
torch.backends.cudnn.benchmark = True
def import_class(import_str):
mod_str, _sep, class_str = import_str.rpartition('.')
__import__(mod_str)
try:
return getattr(sys.modules[mod_str], class_str)
except AttributeError:
raise ImportError('Class %s cannot be found (%s)' % (class_str, traceback.format_exception(*sys.exc_info())))
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Unsupported value encountered.')
def has_batchnorms(model):
bn_types = (nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d, nn.SyncBatchNorm)
for name, module in model.named_modules():
if isinstance(module, bn_types):
return True
return False
def list_all_reduce(list_to_reduce):
list_to_reduce_sum = sum(list_to_reduce)
local_count = len(list_to_reduce)
# Convert to tensors for reduction
list_to_reduce_sum_tensor = torch.tensor(list_to_reduce_sum).cuda()
local_count_tensor = torch.tensor(local_count).cuda()
# Use all_reduce to sum these tensors across all GPUs
dist.all_reduce(list_to_reduce_sum_tensor, op=dist.ReduceOp.SUM)
dist.all_reduce(local_count_tensor, op=dist.ReduceOp.SUM)
# Calculate the global mean loss
global_mean = list_to_reduce_sum_tensor.item() / local_count_tensor.item()
return global_mean
def is_dist_avail_and_initialized():
if not dist.is_available():
return False
if not dist.is_initialized():
return False
return True
def get_world_size():
if not is_dist_avail_and_initialized():
return 1
return dist.get_world_size()
def get_parser():
# parameter priority: command line > config > default
parser = argparse.ArgumentParser(
description='GRA Transformer')
parser.add_argument(
'--work-dir',
default='./work_dir/temp',
help='the work folder for storing results')
parser.add_argument('-model_saved_name', default='')
parser.add_argument(
'--config',
default='./config/nturgbd-cross-view/test_bone.yaml',
help='path to the configuration file')
# processor
parser.add_argument(
'--phase', default='train', help='must be train or test')
parser.add_argument(
'--save-score',
type=str2bool,
default=False,
help='if ture, the classification score will be stored')
# gra
parser.add_argument(
'--joint-label',
type=list,
default=[],
help='tells which group each joint belongs to')
# visulize and debug
parser.add_argument(
'--seed', type=int, default=42, help='random seed for pytorch')
parser.add_argument(
'--log-interval',
type=int,
default=100,
help='the interval for printing messages (#iteration)')
parser.add_argument(
'--save-interval',
type=int,
default=1,
help='the interval for storing models (#iteration)')
parser.add_argument(
'--save-epoch',
type=int,
default=10,
help='the start epoch to save model (#iteration)')
parser.add_argument(
'--eval-interval',
type=int,
default=5,
help='the interval for evaluating models (#iteration)')
parser.add_argument(
'--ema',
action="store_true",
default=False,
help='ema weight for eval')
parser.add_argument('--lambda_1', type=float, default=1e-4)
parser.add_argument('--lambda_2', type=float, default=1e-1)
parser.add_argument(
'--print-log',
type=str2bool,
default=True,
help='print logging or not')
parser.add_argument(
'--show-topk',
type=int,
default=[1, 5],
nargs='+',
help='which Top K accuracy will be shown')
# feeder
parser.add_argument(
'--feeder', default='feeder.feeder', help='data loader will be used')
parser.add_argument(
'--num-worker',
type=int,
default=4,
help='the number of worker for data loader')
parser.add_argument(
'--train-feeder-args',
action=DictAction,
default=dict(),
help='the arguments of data loader for training')
parser.add_argument(
'--test-feeder-args',
action=DictAction,
default=dict(),
help='the arguments of data loader for test')
# model
parser.add_argument('--model', default=None, help='the model will be used')
parser.add_argument(
'--model-args',
action=DictAction,
default=dict(),
help='the arguments of model')
parser.add_argument(
'--weights',
default=None,
help='the weights for network initialization')
parser.add_argument(
'--ignore-weights',
type=str,
default=[],
nargs='+',
help='the name of weights which will be ignored in the initialization')
# optim
parser.add_argument(
'--base-lr', type=float, default=0.025, help='initial learning rate')
parser.add_argument(
'--lrscheduler', default='step', choices=['step', 'cosine'])
parser.add_argument(
'--step',
type=int,
default=[110, 120],
nargs='+',
help='the epoch where optimizer reduce the learning rate')
parser.add_argument(
'--device',
type=int,
default=[0,1],
nargs='+',
help='the indexes of GPUs for training or testing')
parser.add_argument('--optimizer', default='SGD', help='type of optimizer')
parser.add_argument(
'--nesterov', type=str2bool, default=False, help='use nesterov or not')
parser.add_argument(
'--momentum', type=float, default=0.9, help='nesterov momentum')
parser.add_argument(
'--batch-size', type=int, default=256, help='training batch size')
parser.add_argument(
'--test-batch-size', type=int, default=256, help='test batch size')
parser.add_argument(
'--start-epoch',
type=int,
default=0,
help='start training from which epoch')
parser.add_argument(
'--num-epoch',
type=int,
default=80,
help='stop training in which epoch')
parser.add_argument(
'--weight-decay',
type=float,
default=0.0005,
help='weight decay for optimizer')
parser.add_argument(
'--lr-decay-rate',
type=float,
default=0.1,
help='decay rate for learning rate')
parser.add_argument('--warm_up_epoch', type=int, default=0)
parser.add_argument('--max_norm', type=float, default=0.)
parser.add_argument('--use_mixup', type=str2bool, default=False)
parser.add_argument('--mixup_prob', type=float, default=0.)
parser.add_argument('--start_eval', type=int, default=0)
# DDP related
parser.add_argument('--local_rank', type=int, default=0)
parser.add_argument('--port', default="12355")
parser.add_argument('--sync_bn', type=str2bool, default=False)
return parser