-
Notifications
You must be signed in to change notification settings - Fork 221
/
evaluate_nasnet.py
77 lines (55 loc) · 2.47 KB
/
evaluate_nasnet.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
import numpy as np
import argparse
from path import Path
from keras.models import Model
from keras.layers import Dense, Dropout
from keras.preprocessing.image import load_img, img_to_array
import tensorflow as tf
from utils.nasnet import NASNetMobile, preprocess_input
from utils.score_utils import mean_score, std_score
parser = argparse.ArgumentParser(description='Evaluate NIMA(Inception ResNet v2)')
parser.add_argument('-dir', type=str, default=None,
help='Pass a directory to evaluate the images in it')
parser.add_argument('-img', type=str, default=[None], nargs='+',
help='Pass one or more image paths to evaluate them')
parser.add_argument('-rank', type=str, default='true',
help='Whether to tank the images after they have been scored')
args = parser.parse_args()
target_size = (224, 224) # NASNet requires strict size set to 224x224
rank_images = args.rank.lower() in ("true", "yes", "t", "1")
# give priority to directory
if args.dir is not None:
print("Loading images from directory : ", args.dir)
imgs = Path(args.dir).files('*.png')
imgs += Path(args.dir).files('*.jpg')
imgs += Path(args.dir).files('*.jpeg')
elif args.img[0] is not None:
print("Loading images from path(s) : ", args.img)
imgs = args.img
else:
raise RuntimeError('Either -dir or -img arguments must be passed as argument')
with tf.device('/CPU:0'):
base_model = NASNetMobile((224, 224, 3), include_top=False, pooling='avg', weights=None)
x = Dropout(0.75)(base_model.output)
x = Dense(10, activation='softmax')(x)
model = Model(base_model.input, x)
model.load_weights('weights/nasnet_weights.h5')
score_list = []
for img_path in imgs:
img = load_img(img_path, target_size=target_size)
x = img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
scores = model.predict(x, batch_size=1, verbose=0)[0]
mean = mean_score(scores)
std = std_score(scores)
file_name = Path(img_path).name.lower()
score_list.append((file_name, mean))
print("Evaluating : ", img_path)
print("NIMA Score : %0.3f +- (%0.3f)" % (mean, std))
print()
if rank_images:
print("*" * 40, "Ranking Images", "*" * 40)
score_list = sorted(score_list, key=lambda x: x[1], reverse=True)
for i, (name, score) in enumerate(score_list):
print("%d)" % (i + 1), "%s : Score = %0.5f" % (name, score))