from config import tiny_imagenet_config as config from pyimagesearch.preprocessing import imagetoarraypreprocessor as ITA from pyimagesearch.preprocessing import simplespreprocessor as SP from pyimagesearch.preprocessing import meanpreprocessor as MP from pyimagesearch.io import hdf5datasetgenerator as HDFG from pyimagesearch.utils.ranked import rank5_accuracy 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 = SP.SimplePreprocessor(64,64) mp = MP.MeanPreprocessor(means['R'],means['G'],means['B']) iap = ITA.ImageToArrayPreprocessor() # initialize the testing dataset generator testGen = HDFG.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=64 * 2) # compute the rank-1 and rank-5 accuracies (rank1,rank5) = rank5_accuracy(predictions,testGen.db['labels'])
ap = argparse.ArgumentParser() ap.add_argument("-d", "--dataset", required=True, help="path to input dataset") ap.add_argument("-m", "--model", required=True, help="path to output model") ap.add_argument("-o", "--output", required=True, help="path to the output image files") args = vars(ap.parse_args()) classLabels = ["cat", "dog", "panda"] print("[INFO] sampling images...") imagePaths = np.array(list(paths.list_images(args["dataset"]))) idxs = np.random.randint(0, len(imagePaths), size=(10,)) imagePaths = imagePaths[idxs] sp = simplepreprocessor.SimplePreprocessor(32, 32) iap = imagetoarraypreprocessor.ImageToArrayPreprocessor() sdl = simpledatasetloader.SimpleDatasetLoader(preprocessors=[sp, iap]) (data, labels) = sdl.load(imagePaths) data = data.astype("float") / 255.0 print("[INFO] loading pre-trained network...") model = load_model(args["model"]) print("[INFO] predicting...") preds = model.predict(data, batch_size=32).argmax(axis=1) for (i, imagePath) in enumerate(imagePaths): image = cv2.imread(imagePath) cv2.putText(image, "Label: {}".format(classLabels[preds[i]]), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) cv2.imwrite("{}/image_{}.jpg".format(args["output"]), i), image)
from pyimagesearch.io import hdf5datasetgenerator as hdf5dg from keras.preprocessing.image import ImageDataGenerator from keras.models import load_model import argparse # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-m", "--model", type=str, help="path to model checkpoint to load") args = vars(ap.parse_args()) # initialize the testing data generator and image preprocessor testAug = ImageDataGenerator(rescale=1 / 255.0) iap = imagetoap.ImageToArrayPreprocessor() # initialize the testing dataset generator testGen = hdf5dg.HDF5DatasetGenerator(config.TEST_HDF5, config.BATCH_SIZE, aug=testAug, preprocessors=[iap], classes=config.NUM_CLASSES) # load the model from disk print("[INFO] loading {}...".format(args["model"])) model = load_model(args["model"]) # evaluate the network (loss, acc) = model.evaluate_generator(testGen.generator(),
from pyimagesearch.preprocessing import croppreprocessor as CP from pyimagesearch.io import hdf5datasetgenerator as HDF from pyimagesearch.utils.ranked import rank5_accuracy from keras.models import load_model import numpy as np import progressbar import json # 加载RGB均值数据 means = json.loads(open(config.DATASET_MEAN).read()) # 初始化预处理 sp = SP.SimplePreprocessor(227, 227) mp = MP.MeanPreprocessor(means['R'], means['G'], means['B']) cp = CP.CropPreprocessor(227, 227) iap = IAP.ImageToArrayPreprocessor() # 加载训练好的模型 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准确度
ap.add_argument("-s", "--start-epoch", type=int, default=0, help="epoch to restart training at") args = vars(ap.parse_args()) # construct the training and testing image generators for data # augmentation, then initialize the image preprocessor trainAug = ImageDataGenerator(rotation_range=10, zoom_range=0.1, horizontal_flip=True, rescale=1 / 255.0, fill_mode="nearest") valAug = ImageDataGenerator(rescale=1 / 255.0) iap = image2arrayProc.ImageToArrayPreprocessor() trainGen = hdf5DG.HDF5DatasetGenerator(config.TRAIN_HDF5, config.BATCH_SIZE, aug=trainAug, preprocessors=[iap], classes=config.NUM_CLASSES) valGen = hdf5DG.HDF5DatasetGenerator(config.VAL_HDF5, config.BATCH_SIZE, aug=valAug, preprocessors=[iap], classes=config.NUM_CLASSES) if args["model"] is None: print("[INFO] compiling model...") model = EVGG.EmotionVGGNet.build(width=48,