-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.py
206 lines (174 loc) · 7.93 KB
/
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
"""Copyright (c) Facebook, Inc. and its affiliates.
All rights reserved.
This source code is licensed under the license found in the
LICENSE file in the root directory of this source tree.
Portions of the source code are from the OLTR project which
notice below and in LICENSE in the root directory of
this source tree.
Copyright (c) 2019, Zhongqi Miao
All rights reserved.
"""
import os
import argparse
import pprint
from data import dataloader
from run_networks import model
import warnings
import yaml
from utils import source_import, get_value
data_root = {'ImageNet': './dataset/ImageNet',
'Places': './dataset/Places-LT',
'iNaturalist18': '/checkpoint/bykang/iNaturalist18',
'CIFAR10': './dataset/CIFAR10',
'CIFAR100': './dataset/CIFAR100',
}
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', default=None, type=str)
parser.add_argument('--test', default=False, action='store_true')
parser.add_argument('--batch_size', type=int, default=None)
parser.add_argument('--test_open', default=False, action='store_true')
parser.add_argument('--output_logits', default=False)
parser.add_argument('--model_dir', type=str, default=None)
parser.add_argument('--save_feat', type=str, default='')
# KNN testing parameters
parser.add_argument('--knn', default=False, action='store_true')
parser.add_argument('--feat_type', type=str, default='cl2n')
parser.add_argument('--dist_type', type=str, default='l2')
# Learnable tau
parser.add_argument('--val_as_train', default=False, action='store_true')
args = parser.parse_args()
def update(config, args):
# Change parameters
config['model_dir'] = get_value(config['model_dir'], args.model_dir)
config['training_opt']['batch_size'] = \
get_value(config['training_opt']['batch_size'], args.batch_size)
# Testing with KNN
if args.knn and args.test:
training_opt = config['training_opt']
classifier_param = {
'feat_dim': training_opt['feature_dim'],
'num_classes': training_opt['num_classes'],
'feat_type': args.feat_type,
'dist_type': args.dist_type,
'log_dir': training_opt['log_dir']}
classifier = {
'def_file': './models/KNNClassifier.py',
'params': classifier_param,
'optim_params': config['networks']['classifier']['optim_params']}
config['networks']['classifier'] = classifier
return config
# ============================================================================
# LOAD CONFIGURATIONS
with open(args.cfg) as f:
config = yaml.load(f)
config = update(config, args)
test_mode = args.test
test_open = args.test_open
if test_open:
test_mode = True
output_logits = args.output_logits
training_opt = config['training_opt']
relatin_opt = config['memory']
dataset = training_opt['dataset']
if not os.path.isdir(training_opt['log_dir']):
os.makedirs(training_opt['log_dir'])
print('Loading dataset from: %s' % data_root[dataset.rstrip('_LT')])
pprint.pprint(config)
def split2phase(split):
if split == 'train' and args.val_as_train:
return 'train_val'
else:
return split
if not test_mode:
sampler_defs = training_opt['sampler']
if sampler_defs:
if sampler_defs['type'] == 'ClassAwareSampler':
sampler_dic = {
'sampler': source_import(sampler_defs['def_file']).get_sampler(),
'params': {'num_samples_cls': sampler_defs['num_samples_cls']}
}
elif sampler_defs['type'] in ['MixedPrioritizedSampler',
'ClassPrioritySampler']:
sampler_dic = {
'sampler': source_import(sampler_defs['def_file']).get_sampler(),
'params': {k: v for k, v in sampler_defs.items() \
if k not in ['type', 'def_file']}
}
elif sampler_defs['type'] == 'MetaSampler': # Add option for Meta Sampler
learner = source_import(sampler_defs['def_file']).get_learner()(
num_classes=training_opt['num_classes'],
init_pow=sampler_defs.get('init_pow', 0.0),
freq_path=sampler_defs.get('freq_path', None)
).cuda()
sampler_dic = {
'batch_sampler': True,
'sampler': source_import(sampler_defs['def_file']).get_sampler(),
'params': {'meta_learner': learner, 'batch_size': training_opt['batch_size']}
}
else:
sampler_dic = None
splits = ['train', 'train_plain', 'val']
if dataset not in ['iNaturalist18', 'ImageNet']:
splits.append('test')
data = {x: dataloader.load_data(data_root=data_root[dataset.rstrip('_LT')],
dataset=dataset, phase=split2phase(x),
batch_size=training_opt['batch_size'],
sampler_dic=sampler_dic,
num_workers=training_opt['num_workers'],
cifar_imb_ratio=training_opt['cifar_imb_ratio'] if 'cifar_imb_ratio' in training_opt else None)
for x in splits}
if sampler_defs and sampler_defs['type'] == 'MetaSampler': # todo: use meta-sampler
cbs_file = './data/ClassAwareSampler.py'
cbs_sampler_dic = {
'sampler': source_import(cbs_file).get_sampler(),
'params': {'is_infinite': True}
}
# use Class Balanced Sampler to create meta set
data['meta'] = dataloader.load_data(data_root=data_root[dataset.rstrip('_LT')],
dataset=dataset, phase='train' if 'CIFAR' in dataset else 'val',
batch_size=sampler_defs.get('meta_batch_size', training_opt['batch_size'], ),
sampler_dic=cbs_sampler_dic,
num_workers=training_opt['num_workers'],
cifar_imb_ratio=training_opt['cifar_imb_ratio'] if 'cifar_imb_ratio' in training_opt else None,
meta=True)
training_model = model(config, data, test=False, meta_sample=True, learner=learner)
else:
training_model = model(config, data, test=False)
training_model.train()
else:
warnings.filterwarnings("ignore", "(Possibly )?corrupt EXIF data",
UserWarning)
print('Under testing phase, we load training data simply to calculate \
training data number for each class.')
if 'iNaturalist' in training_opt['dataset']:
splits = ['train', 'val']
test_split = 'val'
else:
splits = ['train', 'val', 'test']
test_split = 'test'
if 'ImageNet' == training_opt['dataset']:
splits = ['train', 'val']
test_split = 'val'
if args.knn or True:
splits.append('train_plain')
data = {x: dataloader.load_data(data_root=data_root[dataset.rstrip('_LT')],
dataset=dataset, phase=x,
batch_size=training_opt['batch_size'],
sampler_dic=None,
test_open=test_open,
num_workers=training_opt['num_workers'],
shuffle=False,
cifar_imb_ratio=training_opt['cifar_imb_ratio'] if 'cifar_imb_ratio' in training_opt else None)
for x in splits}
training_model = model(config, data, test=True)
# training_model.load_model()
training_model.load_model(args.model_dir)
if args.save_feat in ['train_plain', 'val', 'test']:
saveit = True
test_split = args.save_feat
else:
saveit = False
training_model.eval(phase=test_split, openset=test_open, save_feat=saveit)
if output_logits:
training_model.output_logits(openset=test_open)
print('ALL COMPLETED.')