-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrank_accuracy.py
38 lines (31 loc) · 1.32 KB
/
rank_accuracy.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
# import packages
from config import tiny_imagenet_config as config
from pipeline.preprocessing import ImageToArrayPreprocessor
from pipeline.preprocessing import SimplePreprocessor
from pipeline.preprocessing import MeanPreprocessor
from pipeline.utils.ranked import rank5_accuracy
from pipeline.io import HDF5DatasetGenerator
from keras.models import load_model
import json
# load the RGB means for the training set
means = json.loads(open(config.DATASET_MEAN).read())
# initialize the image preprocessors
sp = SimplePreprocessor(64, 64)
mp = MeanPreprocessor(means["R"], means["G"], means["B"])
iap = ImageToArrayPreprocessor()
# initialize the testing dataset generator
testGen = HDF5DatasetGenerator(config.TEST_HDF5, 64, preprocessors = [sp, mp, iap],
classes = config.NUM_CLASSES)
# load the pre-trained network
print("[INFO] loading model...")
model = load_model(config.MODEL_PATH)
# make predictions on the testing data
print("[INFO] predicting on test data...")
predictions = model.predict_generator(testGen.generator(),
steps = testGen.numImages // 64, max_queue_size = 10)
# compute the rank-1 and rank-5 accuracies
(rank1, rank5) = rank5_accuracy(predictions, testGen.db["labels"])
print("[INFO] rank-1: {:.2f}%".format(rank1 * 100))
print("[INFO] rank-5: {:.2f}%".format(rank5 * 100))
# close the database
testGen.close()