-
Notifications
You must be signed in to change notification settings - Fork 12
/
evaluate_imagenet.py
63 lines (53 loc) · 1.5 KB
/
evaluate_imagenet.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
import argparse
from dataset import load, Split
from nfnet import NFNet, nfnet_params
NUM_CLASSES = 1000
NUM_IMAGES = 50000
def parse_args():
"""Parse command line arguments."""
ap = argparse.ArgumentParser()
ap.add_argument(
"-v",
"--variant",
default="F0",
type=str,
help="model variant",
)
ap.add_argument(
"-b",
"--batch_size",
default=25,
type=int,
help="test batch size",
)
return ap.parse_args()
def main(args):
steps_per_epoch = NUM_IMAGES // args.batch_size
test_imsize = nfnet_params[args.variant]["test_imsize"]
eval_preproc = "resize_crop_32"
model = NFNet(
num_classes=1000,
variant=args.variant,
)
model.build((1, test_imsize, test_imsize, 3))
model.compile(
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[
tf.keras.metrics.SparseCategoricalAccuracy(name="top_1_acc"),
tf.keras.metrics.SparseTopKCategoricalAccuracy(
k=5, name="top_5_acc"
),
],
)
ds_test = load(
Split(4),
is_training=False,
batch_dims=(args.batch_size,), # dtype=tf.bfloat16,
image_size=(test_imsize, test_imsize),
eval_preproc=eval_preproc,
)
model.load_weights(f"{args.variant}_NFNet/{args.variant}_NFNet")
model.evaluate(ds_test, steps=steps_per_epoch)
if __name__ == "__main__":
args = parse_args()
main(args)