-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.py
178 lines (149 loc) · 7.26 KB
/
common.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
import os
import sys
import yaml
import random
import logging
import argparse
import numpy as np
import pandas as pd
import tensorflow as tf
from datetime import datetime
def check_arguments(args):
assert args.src_path is not None, 'src_path must be entered.'
assert args.data_path is not None, 'data_path must be entered.'
assert args.result_path is not None, 'result_path must be entered.'
return args
def get_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("--task", type=str, default='v1',
choices=['v1', 'v2', 'lincls'])
parser.add_argument("--freeze", action='store_true')
parser.add_argument("--backbone", type=str, default='resnet50')
parser.add_argument("--batch_size", type=int, default=256)
parser.add_argument("--classes", type=int, default=1000)
parser.add_argument("--img_size", type=int, default=224)
parser.add_argument("--dim", type=int, default=128)
parser.add_argument("--num_negative", type=int, default=65536)
parser.add_argument("--momentum", type=float, default=.999)
parser.add_argument("--weight_decay", type=float, default=0.)
parser.add_argument("--use_bias", action='store_true')
parser.add_argument("--mlp", action='store_true') # v2
parser.add_argument("--shuffle_bn", action='store_true')
parser.add_argument("--steps", type=int, default=0)
parser.add_argument("--epochs", type=int, default=200)
parser.add_argument("--lr", type=float, default=.03)
parser.add_argument("--temperature", type=float, default=0.07)
parser.add_argument("--brightness", type=float, default=0.,
help='0.4')
parser.add_argument("--contrast", type=float, default=0.,
help='0.4')
parser.add_argument("--saturation", type=float, default=0.,
help='0.4')
parser.add_argument("--hue", type=float, default=0.,
help='v1: 0.4 / v2: 0.1') # v1 / v2
parser.add_argument("--checkpoint", action='store_true')
parser.add_argument("--history", action='store_true')
parser.add_argument("--tensorboard", action='store_true')
parser.add_argument("--tb_interval", type=int, default=0)
parser.add_argument("--tb_histogram", type=int, default=0)
parser.add_argument("--lr_mode", type=str, default='exponential',
choices=['constant', 'exponential', 'cosine'],
help="v1 : exponential | v2 : cosine")
parser.add_argument("--lr_value", type=float, default=.1)
parser.add_argument("--lr_interval", type=str, default='120,160')
parser.add_argument('--src_path', type=str, default='.')
parser.add_argument('--data_path', type=str, default=None)
parser.add_argument('--result_path', type=str, default='./result')
parser.add_argument('--snapshot', type=str, default=None)
parser.add_argument("--gpus", type=str, default='-1')
parser.add_argument("--summary", action='store_true')
parser.add_argument("--resume", action='store_true')
parser.add_argument("--ignore-search", type=str, default='')
return check_arguments(parser.parse_args())
def set_seed(SEED=42):
os.environ['PYTHONHASHSEED'] = str(SEED)
random.seed(SEED)
np.random.seed(SEED)
tf.random.set_seed(SEED)
def get_logger(name):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
screen_handler = logging.StreamHandler(stream=sys.stdout)
screen_handler.setFormatter(formatter)
logger.addHandler(screen_handler)
return logger
def get_session(args):
assert int(tf.__version__.split('.')[0]) >= 2.0
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpus
if args.gpus != '-1':
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
print(e)
def create_stamp():
weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
temp = datetime.now()
return "{:02d}{:02d}{:02d}_{}_{:02d}_{:02d}_{:02d}".format(
temp.year % 100,
temp.month,
temp.day,
weekday[temp.weekday()],
temp.hour,
temp.minute,
temp.second,
)
def search_same(args):
search_ignore = ['checkpoint', 'history', 'tensorboard',
'tb_interval', 'snapshot', 'summary',
'src_path', 'data_path', 'result_path',
'resume', 'stamp', 'gpus', 'ignore_search']
if len(args.ignore_search) > 0:
search_ignore += args.ignore_search.split(',')
initial_epoch = 0
stamps = os.listdir(f'{args.result_path}/{args.task}')
for stamp in stamps:
try:
desc = yaml.full_load(
open(f'{args.result_path}/{args.task}/{stamp}/model_desc.yml', 'r'))
except:
continue
flag = True
for k, v in vars(args).items():
if k in search_ignore:
continue
if v != desc[k]:
# if stamp == '210120_Wed_05_19_52':
# print(stamp, k, desc[k], v)
flag = False
break
if flag:
args.stamp = stamp
df = pd.read_csv(
os.path.join(
args.result_path,
f'{args.task}/{args.stamp}/history/epoch.csv'))
if len(df) > 0:
if int(df['epoch'].values[-1]+1) == args.epochs:
print(f'{stamp} Training already finished!!!')
return args, -1
elif np.isnan(df['loss'].values[-1]) or np.isinf(df['loss'].values[-1]):
print('{} | Epoch {:04d}: Invalid loss, terminating training'.format(stamp, int(df['epoch'].values[-1]+1)))
return args, -1
else:
ckpt_list = sorted(
[d.split('.index')[0] for d in os.listdir(
f'{args.result_path}/{args.task}/{args.stamp}/checkpoint') if 'index' in d])
if len(ckpt_list) > 0:
args.snapshot = f'{args.result_path}/{args.task}/{args.stamp}/checkpoint/{ckpt_list[-1]}'
initial_epoch = int(ckpt_list[-1].split('_')[0])
else:
print('{} Training already finished!!!'.format(stamp))
return args, -1
break
return args, initial_epoch