with open(config.DATASET_MEAN, 'r') as f: means = json.load(f) simple_preprocessor = SimplePreprocessor(64, 64) mean_preprocessor = MeanPreprocessor(means['R'], means['G'], means['B']) image_to_array_preprocessor = ImageToArrayPreprocessor() test_generator = HDF5DatasetGenerator(config.TEST_HDF5, 64, preprocessors=[ simple_preprocessor, mean_preprocessor, image_to_array_preprocessor ], classes=config.NUM_CLASSES) print('[INFO] Loading model...') model = load_model(config.MODEL_PATH) print('[INFO] Predicting on test data...') predictions = model.predict_generator(test_generator.generator(), steps=test_generator.num_images // 64, max_queue_size=10) rank1, rank5 = rank5_accuracy(predictions, test_generator.db['labels']) print(f'[INFO] Rank-1: {rank1 * 100:.2f}%') print(f'[INFO] Rank-5: {rank5 * 100:.2f}%') test_generator.close()
import argparse import pickle import h5py # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-d", "--db", required=True, help="path HDF5 database") ap.add_argument("-m", "--model", required=True, help="path to pre-trained model") args = vars(ap.parse_args()) # load the pre-trained model print("[INFO] loading pre-trained model...") model = pickle.loads(open(args["model"], "rb").read()) # open the HDF5 database for reading then determine the index of # the training and testing split, provided that this data was # already shuffled *prior* to writing it to disk db = h5py.File(args["db"], "r") i = int(db["labels"].shape[0] * 0.75) # make predictions on the testing set then compute the rank-1 # and rank-5 accuracies print("[INFO] predicting...") preds = model.predict_proba(db["features"][i:]) (rank1, rank5) = rank5_accuracy(preds, db["labels"][i:]) # display the rank-1 and rank-5 accuracies print("[INFO] rank-1: {:.2f}%".format(rank1 * 100)) print("[INFO] rank-5: {:.2f}%".format(rank5 * 100)) # close the database db.close()
import json # load RGB channel mean values means = json.loads(open(config.DATASET_MEAN_PATH).read()) # initialization the image preprocessors sp = SimplePreprocessor(64, 64) mp = MeanPreprocessor(means["R"], means["G"], means["B"]) iap = ImageToArrayPreprocessor() # initialize 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 prediction on test data print("[INFO] predicting on test data........") preds = model.predict_generator(testGen.generator(), steps=(testGen.numImages / 64)) # compute rank-1 and rank-5 accuracies (rank1, rank5) = rank5_accuracy(preds, testGen.db["labels"].generator()) print("[INFO] Rank - 1 : {:.2f}%".format(rank1 * 100)) print("[INFO] Rank - 5 : {:.2f}%".format(rank5 * 100)) testGen.close()
iap = ImageToArrayPreprocessor() # load the pretrained network print("[INFO] loading model...") model = load_model(config.MODEL_PATH) # initialize the testing dataset generator, then make predictions on # the testing data print("[INFO] predicting on test data (no crops)...") testGen = HDF5DatasetGenerator(config.TEST_HDF5, 64, preprocessors=[sp, mp, iap], classes=2) predictions = model.predict_generator(testGen.generator(), steps=testGen.numImages // 64, max_queue_size=64 * 2) # compute the rank-1 and rank-5 accuracies (rank1, _) = rank5_accuracy(predictions, testGen.db["labels"]) print("[INFO] rank-1: {:.2f}%".format(rank1 * 100)) testGen.close() # re-initialize the testing set generator, this time excluding the # ‘SimplePreprocessor‘ testGen = HDF5DatasetGenerator(config.TEST_HDF5, 64, preprocessors=[mp], classes=2) predictions = [] # initialize the progress bar widgets = [ "Evaluating: ", progressbar.Percentage(), " ", progressbar.Bar(), " ", progressbar.ETA()
from utils.ranked import rank5_accuracy argument_parser = argparse.ArgumentParser() argument_parser.add_argument('-d', '--db', required=True, help='Path to the HDF5 database.') argument_parser.add_argument('-m', '--model', required=True, help='Path to pre-trained model.') arguments = vars(argument_parser.parse_args()) print('[INFO] Loading pre-trained model...') with open(arguments['model'], 'rb') as f: model = pickle.loads(f.read()) db = h5py.File(arguments['db'], 'r') i = int(db['labels'].shape[0] * 0.75) print('[INFO] Predicting...') predictions = model.predict_proba(db['features'][i:]) rank1, rank5 = rank5_accuracy(predictions, db['labels'][i:]) print(f'[INFO] rank-1: {rank1 * 100:.2f}%') print(f'[INFO] rank-5: {rank5 * 100:.2f}%') db.close()