def main(): """Compute rank1 and rank5 accuracies """ # load the RGB means for the training set means = json.loads(open(config.DATASET_MEAN).read()) # initialize the image preprocessors simple_preprocessor = SimplePreprocessor(64, 64) mean_preprocessor = MeanPreprocessor(means["R"], means["G"], means["B"]) image_to_array_preprocessor = ImageToArrayPreprocessor() # initialize the testing dataset generator test_gen = HDF5DatasetGenerator( config.TEST_HDF5, 64, preprocessors=[simple_preprocessor, mean_preprocessor, image_to_array_preprocessor], 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(test_gen.generator(), steps=test_gen.num_images // 64, max_queue_size=10) # compute the rank-1 and rank-5 accuracies (rank1, rank5) = rank5_accuracy(predictions, test_gen.database["labels"]) print("[INFO] rank-1: {:.2f}%".format(rank1 * 100)) print("[INFO] rank-5: {:.2f}%".format(rank5 * 100)) # close the database test_gen.close()
def main(): """Display rank-1 and rank-5 accuracies """ # construct the argument parse and parse the arguments args = argparse.ArgumentParser() args.add_argument("-d", "--db", required=True, help="path HDF5 database") args.add_argument("-m", "--model", required=True, help="path to pre-trained model") args = vars(args.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()
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 testing data print("[INFO] predicting on test data...") predictions = model.predict_generator(testGen.generator(), steps=testGen.numImages // 64, max_queue_size=64 * 2) # compute the rank-1 and rank5 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()
__author__ = 'kevin' from pyimagesearch.utils.ranked import rank5_accuracy import argparse import pickle import h5py ap = argparse.ArgumentParser() ap.add_argument("-d", "--db", required=True, help="path to HDF5 database") ap.add_argument("-m", "--model", required=True, help="path to pre-trained model") args = vars(ap.parse_args()) print("[INFO] loading pre-trained model....") model = pickle.loads(open(args["model"], "rb").read()) db = h5py.File(args["db"], "rb") i = int(db["labels"].shape[0] * 0.75) print("[INFO] prediction..") preds = model.predict_proba(db["features"][:i]) (rank1, rank5) = rank5_accuracy(preds, db["labels"][i:]) print("[INFO] rank-1: {:.2f}%".format(rank1 * 100)) print("[INFO] rank-5: {:.2f}%".format(rank5 * 100)) db.close()
batch_size=configs.BATCH_SIZE) # load model and predict data from generator model = load_model(configs.MODEL_PATH) predictions = model.predict(test_gen, steps=test_gen.samples // configs.BATCH_SIZE) # extract ground truth label from generator y_true = [] steps = len(test_gen) or (test_gen.samples // test_gen.batch_size) for idx in np.arange(0, steps): (_, label_batch) = test_gen[idx] y_true.extend(label_batch) # compute rank-1 accuracy (rank1, _) = rank5_accuracy(y_true=y_true, y_pred=predictions) print("[INFO] rank-1: {:.2f}%".format(rank1 * 100)) # re-initialize preprocessor utilizer along with generator test_preprocessor = CustomPreprocessing([cp, ip]) test_aug = ImageDataGenerator(preprocessing_function=mp.preprocess) test_gen = test_aug.flow_from_directory(directory=configs.TEST_IMAGE, batch_size=configs.BATCH_SIZE) # re-initialize ground truth label list y_true = [] predictions = [] # number of unique batch image/labels in generator steps = len(test_gen) # test_gen.samples // configs.BATCH_SIZE
args["batch"], os.getpid()) ] f = open(os.path.sep.join(p), "w") print("[INFO] Saving Classification Report and Confusion Matrix to file...") f.write("Learning Ratio = {}\n".format(args["lr"])) f.write(report) f.write("") f.write(str(confmatrix)) f.close() # 5. Compute the rank-1, rank 3 and rank-5 accuracies print("[INFO] predicting...") from pyimagesearch.utils.ranked import rank5_accuracy, rankn_accuracy (rank1, rank5) = rank5_accuracy(predictions, testY.argmax(axis=1)) (rank1, rankn) = rankn_accuracy(predictions, testY.argmax(axis=1), n=3) # display the rank-1 rank-3 and rank-5 accuracies print("rank-1: {:.2f}%".format(rank1 * 100)) print("rank-3: {:.2f}%".format(rankn * 100)) print("rank-5: {:.2f}%".format(rank5 * 100)) # 6. Saving the ranking to file p = [ args["output"], "{}_ranking_{}_lr:{}_epochs:{}_batch:{}_{}.txt".format( args["network"], args["optimizer"], args["lr"], args["epochs"], args["batch"], os.getpid()) ] print("[INFO] Saving Ranking information to file...") f = open(os.path.sep.join(p), "w")
# initialize testing datasets generation test_gen = HDF5DatasetGenerator(configs.TEST_HDF5, feature_ref_name="data", batch_size=configs.BATCH_SIZE, preprocessors=[sp, mp, ip]) # load model model = load_model(configs.MODEL_PATH) # predict test generator predictions = model.predict(test_gen.generate(passes=1), steps=test_gen.num_images // configs.BATCH_SIZE) # compute and display rank-1 accuracy (rank1, _) = rank5_accuracy(y_true=test_gen.db["labels"], y_pred=predictions) print("[INFO] rank-1: {:.2f}%".format(rank1 * 100)) # close generator test_gen.close() # initialize progressbar for new model evaluation widgets = [ "Evaluating: ", progressbar.Percentage(), " ", progressbar.Bar(), " ", progressbar.ETA() ] pbar = progressbar.ProgressBar(widgets=widgets, max_value=test_gen.num_images // configs.BATCH_SIZE).start()
def main(): """Evaluate AlexNet on Cats vs. Dogs """ # load the RGB means for the training set means = json.loads(open(config.DATASET_MEAN).read()) # initialize the image preprocessors simple_preprocessor = SimplePreprocessor(227, 227) mean_preprocessor = MeanPreprocessor(means["R"], means["G"], means["B"]) crop_preprocessor = CropPreprocessor(227, 227) image_to_array_preprocessor = 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)...") test_gen = HDF5DatasetGenerator(config.TEST_HDF5, 64, preprocessors=[ simple_preprocessor, mean_preprocessor, image_to_array_preprocessor ], classes=2) predictions = model.predict_generator(test_gen.generator(), steps=test_gen.num_images // 64, max_queue_size=10) # compute the rank-1 and rank-5 accuracies (rank1, _) = rank5_accuracy(predictions, test_gen.database["labels"]) print("[INFO] rank-1: {:.2f}%".format(rank1 * 100)) test_gen.close() # re-initialize the testing set generator, this time excluding the `SimplePreprocessor` test_gen = HDF5DatasetGenerator(config.TEST_HDF5, 64, preprocessors=[mean_preprocessor], classes=2) predictions = [] # initialize the progress bar widgets = [ "Evaluating: ", progressbar.Percentage(), " ", progressbar.Bar(), " ", progressbar.ETA() ] progress_bar = progressbar.ProgressBar(maxval=test_gen.num_images // 64, widgets=widgets).start() # loop over a single pass of the test data # passes=1 to indicate the testing data only needs to be looped over once for (i, (images, _)) in enumerate(test_gen.generator(passes=1)): # loop over each of the individual images for image in images: # apply the crop preprocessor to the image to generate 10 # separate crops, then convert them from images to arrays crops = crop_preprocessor.preprocess(image) crops = np.array( [image_to_array_preprocessor.preprocess(c) for c in crops], dtype="float32") # make predictions on the crops and then average them # together to obtain the final prediction pred = model.predict(crops) predictions.append(pred.mean(axis=0)) # update the progress bar progress_bar.update(i) # compute the rank-1 accuracy progress_bar.finish() print("[INFO] predicting on test data (with crops)...") (rank1, _) = rank5_accuracy(predictions, test_gen.database["labels"]) print("[INFO] rank-1: {:.2f}%".format(rank1 * 100)) test_gen.close()
# 加载训练好的模型 print("[INFO] loading model ...") model = load_model(config.MODEL_PATH) # 初始化测试数据集生成器,并进行预测 print("[INFO] predicting on test data (no crops)...") testGen = HDF.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) #计算rank-1和rank-5准确度 (rank1, _) = rank5_accuracy(predictions, testGen.db['labels']) print("[INFO] rank-1: {:.2f}%".format(rank1 * 100)) testGen.close() # 重新初始化生成器 # 'SimplePreprocessor' testGen = HDF.HDF5DatasetGenerator(config.TEST_HDF5, 64, preprocessors=[mp], classes=2) predictions = [] # 初始化进度条 widgets = [ 'Evaluating: ', progressbar.Percentage(), " ",
# 4. Saving the classification report and confussion matrix to file p = [args["output"], "clasification_report_logistic_regression_model.txt"] f = open(os.path.sep.join(p), "w") print("[INFO] Saving Classification Report and Confusion Matrix to file...") f.write(report) f.write("") f.write(str(confmatrix)) f.close() # 5. Compute the rank-1, rank 3 and rank-5 accuracies print("[INFO] predicting...") from pyimagesearch.utils.ranked import rank5_accuracy, rankn_accuracy (rank1, rank5) = rank5_accuracy(to_categorical(predictions), testY.argmax(axis=1)) (rank1, rankn) = rankn_accuracy(to_categorical(predictions), testY.argmax(axis=1), n=3) # display the rank-1 rank-3 and rank-5 accuracies print("rank-1: {:.2f}%".format(rank1 * 100)) print("rank-3: {:.2f}%".format(rankn * 100)) print("rank-5: {:.2f}%".format(rank5 * 100)) # 6. Saving the ranking to file p = [args["output"], "ranking_report_logistic_regression_model.txt"] print("[INFO] Saving Ranking information to file...") f = open(os.path.sep.join(p), "w") f.write("rank-1: {:.2f}%".format(rank1 * 100)) f.write("rank-3: {:.2f}%".format(rankn * 100))
# load means of R, G, B means = json.loads(open(config.DATASET_MEAN).read()) # initialize preprocessors sp, mp, cp, iap = SimplePreprocessor(227, 227), MeanPreprocessor( means['R'], means['G'], means['B']), CropPreprocessor(227, 227), ImageToArrayPreprocessor() # initialize test generator for evaluting without cropping test_gen = HDF5DatasetGenerator(config.TEST_HDF5, preprocessors=[sp, mp, iap], batch_size=128) print('[INFO] evaluating model without cropping...') preds = model.predict_generator(test_gen.generator(), steps=test_gen.num_images // 128) rank_1, _ = rank5_accuracy(preds, test_gen.db['labels']) print(f'[INFO] rank-1: f{rank_1*100:.2f}') # close test_gen test_gen.close() # initialize test generator for evaluting with cropping test_gen = HDF5DatasetGenerator(config.TEST_HDF5, preprocessors=[mp], batch_size=128) preds = [] # construct progressbar widgets = [ 'Evaluating: ', progressbar.Percentage(), ' ',
print("[INFO] loading pre-trained model...") checkpointsPath = os.path.sep.join([args["checkpoints"], args["prefix"]]) (symbol, argParams, auxParams) = mx.model.load_checkpoint(checkpointsPath, args["epoch"]) # construct the model model = mx.mod.Module(symbol=symbol, context=[mx.gpu(0)]) model.bind(data_shapes=testIter.provide_data, label_shapes=testIter.provide_label) model.set_params(argParams, auxParams) # initialize the list of predictions and targets print("[INFO] evaluating model...") predictions = [] targets = [] # loop over the predictions in batches for (preds, _, batch) in model.iter_predict(testIter): # convert the batch of predictions and labels to NumPy # arrays preds = preds[0].asnumpy() labels = batch.label[0].asnumpy().astype("int") # update the predictions and targets lists, respectively predictions.extend(preds) targets.extend(labels) # apply array slicing to the targets since mxnet will return the # next full batch size rather than the *actual* number of labels targets = targets[:len(predictions)] # compute the rank-1 and rank-5 accuracies (rank1, rank5) = rank5_accuracy(predictions, targets) print("[INFO] rank-1: {:.2f}%".format(rank1 * 100)) print("[INFO] rank-5: {:.2f}%".format(rank5 * 100))
#encoding:utf-8 from pyimagesearch.utils.ranked import rank5_accuracy import argparse import pickle import h5py # 解析命令行参数 ap = argparse.ArgumentParser() ap.add_argument('-d', '--db', required=True, help='path HDF5 databases') ap.add_argument('-m', '--model', required=True, help='path to pre-trained model') args = vars(ap.parse_args()) # 加载模型 print("[INFO] loading pre-trained model...") model = pickle.loads(open(args['model'], 'rb').read()) db = h5py.File(args['db'], 'r') i = int(db['labels'].shape[0] * 0.75) # 继续宁预测 print("[INFO] predicting....") preds = model.predict_proba(db['features'][i:]) (rank1, rank5) = rank5_accuracy(preds, db['labels'][i:]) # 结果打印 print("[INFO] rank-1:{:.2f}%".format(rank1 * 100)) print("[INFO] rank-5:{:.2f}%".format(rank5 * 100)) db.close()
import h5py import pickle from pyimagesearch.utils.ranked import rank5_accuracy import numpy as np import argparse # construct argument parser and parse the argument ap = argparse.ArgumentParser() ap.add_argument('-d', '--db', required=True, help='path to HDF5 database') ap.add_argument('-m', '--model', required=True, help='path to estimator cpickle') args = vars(ap.parse_args()) # open HDF5 database abd partition data by index provided that database is shuffled prior to writing to disk print('[INFO] loading HDF5 database...') db = h5py.File(args['db'], 'r') i = int(len(db['labels'])*0.75) # load model and predict print('[INFO] loading model...') model = pickle.loads(open(args['model'], 'rb').read()) preds = model.predict_proba(db['features'][i:]) # display rank_1 and rank_5 rank_1, rank_5 = rank5_accuracy(preds, db['labels'][i:]) print(f'[INFO] rank-1: {rank_1*100:.2f}') print(f'[INFO] rank-5: {rank_5*100:.2f}')