-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.py
104 lines (82 loc) · 3.19 KB
/
eval.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
import os
import yaml
import argparse
import tensorflow as tf
from common import set_seed
from common import get_logger
from common import get_session
from dataloader import set_dataset
from dataloader import DataLoader
from models import set_model
def get_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("--stamp", type=str, default='resnet50')
parser.add_argument("--dataset", type=str, default='imagenet',
choices=['cifar100', 'tinyimagenet', 'imagenet', 'cub', 'stanforddogs', 'mit67'])
parser.add_argument('--snapshot', type=str, default=None)
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("--gpus", type=str, default='-1')
return parser.parse_args()
def main():
temp_args = get_arguments()
assert temp_args.snapshot is not None, 'snapshot must be selected!'
set_seed()
args = argparse.ArgumentParser().parse_args(args=[])
tmp = yaml.full_load(
open(f'{temp_args.result_path}/'
f'{temp_args.dataset}/'
f'{temp_args.stamp}/'
'model_desc.yml', 'r'))
for k, v in tmp.items():
setattr(args, k, v)
args.snapshot = temp_args.snapshot
args.src_path = temp_args.src_path
args.data_path = temp_args.data_path
args.result_path = temp_args.result_path
args.gpus = temp_args.gpus
args.batch_size = 1
get_session(args)
logger = get_logger("MyLogger")
for k, v in vars(args).items():
logger.info(f"{k} : {v}")
##########################
# Dataset
##########################
_, valset = set_dataset(args.dataset, args.classes, args.data_path)
validation_steps = len(valset)
logger.info("TOTAL STEPS OF DATASET FOR EVALUATION")
logger.info("=========== VALSET ===========")
logger.info(f" --> {validation_steps}")
##########################
# Model & Generator
##########################
model = set_model(args.backbone, args.dataset, args.classes)
model.load_weights(args.snapshot)
logger.info(f"Load weights at {args.snapshot}")
model.compile(
loss=args.loss,
batch_size=args.batch_size,
optimizer=tf.keras.optimizers.SGD(args.lr, momentum=.9),
metrics=[
tf.keras.metrics.TopKCategoricalAccuracy(k=1, name='acc1'),
tf.keras.metrics.TopKCategoricalAccuracy(k=5, name='acc5')],
xe_loss=tf.keras.losses.categorical_crossentropy,
cls_loss=tf.keras.losses.KLD,
cls_lambda=args.loss_weight,
temperature=args.temperature)
val_generator = DataLoader(
loss='crossentropy',
mode='val',
datalist=valset,
dataset=args.dataset,
classes=args.classes,
batch_size=args.batch_size,
shuffle=False).dataloader()
##########################
# Evaluation
##########################
print(model.evaluate(val_generator, steps=validation_steps, return_dict=True))
if __name__ == '__main__':
main()