def training(aug, means_path, train_hdf5_path, val_hdf5_path, fig_path, json_path, label_encoder_path, best_weight_path, checkpoint_path, cross_val=None): # load RGB means means = json.loads(open(means_path).read()) # initialize image preprocessors sp, mp, pp, iap = SimplePreprocessor(227, 227), MeanPreprocessor(means['R'], means['G'], means['B']), PatchPreprocessor(227, 227), ImageToArrayPreprocessor() # initialize training and validation image generator train_gen = HDF5DatasetGenerator(train_hdf5_path, config.BATCH_SIZE, preprocessors=[pp, mp, iap], aug=aug, classes=config.NUM_CLASSES) val_gen = HDF5DatasetGenerator(val_hdf5_path, config.BATCH_SIZE, preprocessors=[sp, mp, iap], aug=aug, classes=config.NUM_CLASSES) metrics = ['accuracy'] if config.DATASET_TYPE == 'age': le = pickle.loads(open(label_encoder_path, 'rb').read()) agh = AgeGenderHelper(config, deploy) one_off_mappings = agh.build_oneoff_mappings(le) one_off = OneOffAccuracy(one_off_mappings) metrics.append(one_off.one_off_accuracy) # construct callbacks callbacks = [TrainingMonitor(fig_path, json_path=json_path, start_at=args['start_epoch']), EpochCheckpoint(checkpoint_path, every=5, start_at=args['start_epoch']), ModelCheckpointsAdvanced(best_weight_path, json_path=json_path, start_at=args['start_epoch'])] #, LearningRateScheduler(decay) if cross_val is None: print('[INFO] compiling model...') else: print(f'[INFO] compiling model for cross validation {cross_val}...') if args['start_epoch'] == 0: if not os.path.exists(checkpoint_path): os.makedirs(checkpoint_path) model = AgeGenderNet.build(227, 227, 3, config.NUM_CLASSES, reg=5e-4) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=metrics) else: model_path = os.path.sep.join([checkpoint_path, f"epoch_{args['start_epoch']}.hdf5"]) print(f"[INFO] loading {model_path}...") if config.DATASET_TYPE == 'age': model = load_model(model_path, custom_objects={'one_off_accuracy': one_off.one_off_accuracy}) elif config.DATASET_TYPE == 'gender': model = load_model(model_path) # update learning rate print(f'[INFO] old learning rate: {K.get_value(model.optimizer.lr)}') K.set_value(model.optimizer.lr, INIT_LR) print(f'[INFO] new learning rate: {K.get_value(model.optimizer.lr)}') # train the network if cross_val is None: print('[INFO] training the network...') else: print(f'[INFO] training the network for cross validation {cross_val}...') model.fit_generator(train_gen.generator(), steps_per_epoch=train_gen.num_images//config.BATCH_SIZE, validation_data=val_gen.generator(), validation_steps=val_gen.num_images//config.BATCH_SIZE, epochs=MAX_EPOCH-args['start_epoch'], verbose=2, callbacks=callbacks) # close dataset train_gen.close() val_gen.close()
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()
width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, fill_mode="nearest") means = json.loads(open(config.DATASET_MEAN).read()) sp = SimplePreprocessor(227, 227) pp = PatchPreprocessor(227, 227) mp = MeanPreprocessor(means['R'], means['G'], means['B']) iap = ImageToArrayPreprocessor() trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, 128, aug=aug, preprocessors=[pp, mp, iap], classes=2) valGen = HDF5DatasetGenerator(config.VAL_HDF5, 128, preprocessors=[sp, mp, iap], classes=2) print("[INFO] compiling model...") opt = Adam(lr=1e-3) #pdb.set_trace() model = AlexNet.build(width=227, height=227, depth=3, classes=2)
def main(): """Train Resnet on TinyImageNet dataset """ # construct the argument parse and parse the arguments args = argparse.ArgumentParser() args.add_argument("-m", "--model", required=True, help="path to output model") args.add_argument("-o", "--output", required=True, help="path to output directory (logs, plots, etc.)") args = vars(args.parse_args()) # construct the training image generator for data augmentation aug = ImageDataGenerator( rotation_range=18, zoom_range=0.15, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, fill_mode="nearest", ) # 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 training and validation dataset generators train_gen = HDF5DatasetGenerator( config.TRAIN_HDF5, 64, augmentation=aug, preprocessors=[ simple_preprocessor, mean_preprocessor, image_to_array_preprocessor ], classes=config.NUM_CLASSES, ) val_gen = HDF5DatasetGenerator( config.VAL_HDF5, 64, preprocessors=[ simple_preprocessor, mean_preprocessor, image_to_array_preprocessor ], classes=config.NUM_CLASSES, ) # construct the set of callbacks fig_path = os.path.sep.join([args["output"], "{}.png".format(os.getpid())]) json_path = os.path.sep.join( [args["output"], "{}.json".format(os.getpid())]) callbacks = [ TrainingMonitor(fig_path, json_path=json_path), LearningRateScheduler(poly_decay) ] # initialize the optimizer and model (ResNet-56) print("[INFO] compiling model...") model = ResNet.build(64, 64, 3, config.NUM_CLASSES, (3, 4, 6), (64, 128, 256, 512), reg=0.0005, dataset="tiny_imagenet") opt = SGD(lr=INIT_LR, momentum=0.9) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the network print("[INFO] training network...") model.fit_generator( train_gen.generator(), steps_per_epoch=train_gen.num_images // 64, validation_data=val_gen.generator(), validation_steps=val_gen.num_images // 64, epochs=NUM_EPOCHS, max_queue_size=10, callbacks=callbacks, verbose=1, ) # save the network to disk print("[INFO] serializing network...") model.save(args["model"]) # close the databases train_gen.close() val_gen.close()
import progressbar import json means = json.loads(open(config.DATASET_MEAN).read()) sp = SimplePreprocessor(227, 227) mp = MeanPreprocessor(means["R"], means["G"], means["B"]) cp = CropPreprocessor(227, 227) iap = ImageToArrayPreprocessor() print("[INFO] loading model ...") model = load_model(config.MODEL_PATH) 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) (rank1, _) = rank5_accuracy(predictions, testGen.db["labels"]) print("[INFO] rank-1: {:.2f}%".format(rank1 * 100)) testGen.close() testGen = HDF5DatasetGenerator(config.TEST_HDF5, 64, preprocessors=[mp], classes=2) predictions = []
from pyimagesearch.nn.conv import SRCNN from keras.optimizers import Adam import matplotlib.pyplot as plt import numpy as np def super_res_generator(input_datagen, target_datagen): # start infinite loop for training data while True: # grab next input images and target outputs, discarding class labels input_data, target_data = next(input_datagen)[0], next(target_datagen)[0] yield input_data, target_data # initialize input images and target output images generators iap = ImageToArrayPreprocessor() inputs = HDF5DatasetGenerator(config.INPUT_DB, config.BATCH_SIZE, preprocessors=[iap]) targets = HDF5DatasetGenerator(config.OUTPUT_DB, config.BATCH_SIZE, preprocessors=[iap]) # initialize model and optimizer print('[INFO] compiling model...') opt = Adam(decay=1e-3/config.NUM_EPOCHS) model = SRCNN.build(width=config.INPUT_DIM, height=config.INPUT_DIM, depth=3) model.compile(opt, loss='mse') # train model using generator H = model.fit_generator(super_res_generator(inputs.generator(), targets.generator()), epochs=config.NUM_EPOCHS, verbose=2, steps_per_epoch=inputs.num_images//config.BATCH_SIZE) # save model to file print('[INFO] serializing model...') model.save(config.MODEL_PATH)
type=int, default=0, help="epoch to restart training at") args = vars(ap.parse_args()) trainAug = ImageDataGenerator(rotation_range=10, zoom_range=0.1, rescale=1 / 255.0, fill_mode='nearest') valAug = ImageDataGenerator(rescale=1 / 255.0) iap = ImageToArrayPreprocessor() trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, config.BATCH_SIZE, aug=trainAug, preprocessors=[iap], binarize=False, classes=config.NUM_CLASSES, max_label_length=config.MAX_LENGTH) valGen = HDF5DatasetGenerator(config.VAL_HDF5, config.BATCH_SIZE, aug=valAug, preprocessors=[iap], binarize=False, classes=config.NUM_CLASSES, max_label_length=config.MAX_LENGTH) adam = Adam(lr=0.001) if args['model'] is None: print("[info] compiling model..") model = CRNN.build(width=config.WIDTH,
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 = ImageToArrayPreprocessor() # initialize the training and validation dataset generators trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, config.BATCH_SIZE, aug=trainAug, preprocessors=[iap], classes=config.NUM_CLASSES) valGen = HDF5DatasetGenerator(config.VAL_HDF5, config.BATCH_SIZE, aug=valAug, preprocessors=[iap], classes=config.NUM_CLASSES) # if there is no specific model checkpoint supplied, then initialize # the network and compile the model if args["model"] is None: print("[INFO] compiling model...") model = EmotionVGGNet.build(width=48, height=48, depth=1,
shear_range=0.15, horizontal_flip=True, fill_mode="nearest") # 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 training and validation dataset generators trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, 64, aug=aug, preprocessors=[sp, mp, iap], classes=config.NUM_CLASSES) valGen = HDF5DatasetGenerator(config.VAL_HDF5, 64, preprocessors=[sp, mp, iap], classes=config.NUM_CLASSES) # if there is no specific model checkpoint supplied, then initialize # the network and compile the model if args["model"] is None: print("[INFO] compiling model...") model = DeeperGoogLeNet.build(width=64, height=64, depth=3, classes=config.NUM_CLASSES,
from pyimagesearch.io import HDF5DatasetGenerator from pyimagesearch.preprocessing import SimplePreprocessor, MeanPreprocessor, PatchPreprocessor from config import plant_seedlings_config as config from pyimagesearch.nn.conv import AlexNet from keras.preprocessing.image import ImageDataGenerator import json import pickle mean = json.loads(open(config.DATASET_MEAN).read()) le = pickle.loads(open(config.LABEL_MAPPINGS, 'rb').read()) sp, mp, pp = SimplePreprocessor(224, 224), MeanPreprocessor(mean['R'], mean['G'], mean['B']), PatchPreprocessor(224, 224) aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.15, zoom_range=0.1, horizontal_flip=True) train_gen = HDF5DatasetGenerator(config.TRAIN_HDF5, preprocessors=[pp, mp], aug=aug, batch_size=64, num_classes=len(le.classes_)) val_gen = HDF5DatasetGenerator(config.VAL_HDF5, preprocessors=[sp, mp], aug=aug, batch_size=64, num_classes=len(le.classes_)) model = AlexNet.build(224, 224, 3, len(le.classes_)) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(train_gen.generator(), steps_per_epoch=train_gen.num_images//64, epochs=100, verbose=2, )
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()
def main(): """Train ResNet """ # construct the argument parse and parse the arguments args = argparse.ArgumentParser() args.add_argument("-c", "--checkpoints", required=True, help="path to output checkpoint directory") args.add_argument("-m", "--model", type=str, help="path to *specific* model checkpoint to load") args.add_argument("-s", "--start-epoch", type=int, default=0, help="epoch to restart training at") args = vars(args.parse_args()) # construct the training image generator for data augmentation aug = ImageDataGenerator( rotation_range=18, zoom_range=0.15, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, fill_mode="nearest", ) # 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 training and validation dataset generators train_gen = HDF5DatasetGenerator( config.TRAIN_HDF5, 64, augmentation=aug, preprocessors=[ simple_preprocessor, mean_preprocessor, image_to_array_preprocessor ], classes=config.NUM_CLASSES, ) val_gen = HDF5DatasetGenerator( config.VAL_HDF5, 64, preprocessors=[ simple_preprocessor, mean_preprocessor, image_to_array_preprocessor ], classes=config.NUM_CLASSES, ) # if there is no specific model checkpoint supplied, then initialize # the network and compile the model if args["model"] is None: print("[INFO] compiling model...") model = ResNet.build(64, 64, 3, config.NUM_CLASSES, (3, 4, 6), (64, 128, 256, 512), reg=0.0005, dataset="tiny_imagenet") opt = SGD(lr=1e-1, momentum=0.9) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # otherwise, load the checkpoint from disk else: print("[INFO] loading {}...".format(args["model"])) model = load_model(args["model"]) # update the learning rate print("[INFO] old learning rate: {}".format( K.get_value(model.optimizer.lr))) K.set_value(model.optimizer.lr, 1e-5) print("[INFO] new learning rate: {}".format( K.get_value(model.optimizer.lr))) # construct the set of callbacks callbacks = [ EpochCheckpoint(args["checkpoints"], every=5, start_at=args["start_epoch"]), TrainingMonitor(config.FIG_PATH, json_path=config.JSON_PATH, start_at=args["start_epoch"]), ] # train the network model.fit_generator( train_gen.generator(), steps_per_epoch=train_gen.num_images // 64, validation_data=val_gen.generator(), validation_steps=val_gen.num_images // 64, epochs=50, max_queue_size=10, callbacks=callbacks, verbose=1, ) # close the databases train_gen.close() val_gen.close()
import json # load model model = load_model(config.MODEL_PATH) # 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 = []
def super_res_generator(inputDataGen, targetDataGen): # start an infinite loop for the training data while True: # grab the next input images and target outputs, discarding # the class labels (which are irrelevant) inputData = next(inputDataGen)[0] targetData = next(targetDataGen)[0] # yield a tuple of the input data and target data yield (inputData, targetData) # initialize the input images and target output images generators inputs = HDF5DatasetGenerator(config.INPUTS_DB, config.BATCH_SIZE) targets = HDF5DatasetGenerator(config.OUTPUTS_DB, config.BATCH_SIZE) # initialize the model and optimizer print("[INFO] compiling model...") opt = Adam(lr=0.001, decay=0.001 / config.NUM_EPOCHS) model = SRCNN.build(width=config.INPUT_DIM, height=config.INPUT_DIM, depth=3) model.compile(loss="mse", optimizer=opt) # train the model using our generators H = model.fit_generator(super_res_generator(inputs.generator(), targets.generator()), steps_per_epoch=inputs.numImages // config.BATCH_SIZE, epochs=config.NUM_EPOCHS, verbose=1)
def main(): """Train AlexNet on Dogs vs Cats """ # construct the training image generator for data augmentation augmentation = ImageDataGenerator( rotation_range=20, zoom_range=0.15, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, fill_mode="nearest", ) # 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) patch_preprocessor = PatchPreprocessor(227, 227) mean_preprocessor = MeanPreprocessor(means["R"], means["G"], means["B"]) image_to_array_preprocessor = ImageToArrayPreprocessor() # initialize the training and validation dataset generators train_gen = HDF5DatasetGenerator( config.TRAIN_HDF5, 128, augmentation=augmentation, preprocessors=[patch_preprocessor, mean_preprocessor, image_to_array_preprocessor], classes=2, ) val_gen = HDF5DatasetGenerator( config.VAL_HDF5, 128, preprocessors=[simple_preprocessor, mean_preprocessor, image_to_array_preprocessor], classes=2, ) # initialize the optimizer print("[INFO] compiling model...") opt = Adam(lr=1e-3) model = AlexNet.build(width=227, height=227, depth=3, classes=2, regularization=0.0002) model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) # construct the set of callbacks path = os.path.sep.join([config.OUTPUT_PATH, "{}.png".format(os.getpid())]) callbacks = [TrainingMonitor(path)] # train the network model.fit_generator( train_gen.generator(), steps_per_epoch=train_gen.num_images // 128, validation_data=val_gen.generator(), validation_steps=val_gen.num_images // 128, epochs=75, max_queue_size=10, callbacks=callbacks, verbose=1, ) # save the model to file print("[INFO] serializing model...") model.save(config.MODEL_PATH, overwrite=True) # close the HDF5 datasets train_gen.close() val_gen.close()
# 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 = ImageToArrayPreprocessor() # initialize the testing dataset generator testGen = 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(), steps=testGen.numImages // config.BATCH_SIZE, max_queue_size=config.BATCH_SIZE * 2) print("[INFO] accuracy: {:.2f}".format(acc * 100)) # close the testing database
# load RGB means for json means = json.loads(open(config.DATASET_MEAN).read()) # initialize image preprocessors mp, cp, iap = MeanPreprocessor(means['R'], means['G'], means['B']), CropPreprocessor( 227, 227), ImageToArrayPreprocessor() # load model print('[INFO] loading model...') model = load_model(config.MODEL_PATH) # initialize dataset generator test_gen = HDF5DatasetGenerator(config.PUBLIC_TEST_HDF5, batch_size=64, preprocessors=[mp]) preds = [] # initialize progressbar widgets = [ 'Evaluating: ', progressbar.Percentage(), ' ', progressbar.Bar(), ' ', progressbar.ETA() ] pbar = progressbar.ProgressBar(maxval=test_gen.num_images // 64, widgets=widgets) # loop over single pass of test data for i, (images, labels) in enumerate(test_gen.generator(passes=1)):
means = json.loads(open("output/dogs_vs_cats_mean.json").read()) # initialize the image preprocessors sp = SimplePreprocessor(227, 227) mp = MeanPreprocessor(means["R"], means["G"], means["B"]) cp = CropPreprocessor(227, 227) iap = ImageToArrayPreprocessor() # load the pretrained network print("[INFO] loading model...") model = load_model("output/alexnet_dogs_vs_cats.model") # initialize the testing dataset generator, then make predictions on # the testing data print("[INFO] predicting on test data (no crops)...") testGen = HDF5DatasetGenerator("../datasets/kaggle_dogs_vs_cats/hdf5/test.hdf5", 64, preprocessors=[sp, mp, iap], classes=2) predictions = model.predict_generator(testGen.generator(), steps=testGen.numImages // 64, max_q_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
horizontal_flip=True, fill_mode="nearest") aug2 = ImageDataGenerator(preprocessing_function=xception.preprocess_input) # # 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(config.TRAIN_HDF5, "r") # print(db["label_names"], len(db["label_names"])) sp = SimplePreprocessor(299, 299) iap = ImageToArrayPreprocessor() # initialize the training and validation dataset generators trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, 32, aug=aug, preprocessors=[sp, iap], classes=config.NUM_CLASSES, set="train") valGen = HDF5DatasetGenerator(config.TRAIN_HDF5, 32, aug=aug2, preprocessors=[sp, iap], classes=config.NUM_CLASSES, set="val") # construct the set of callbacks path = os.path.sep.join([config.OUTPUT_PATH, "{}.png".format(os.getpid())]) callbacks = [TrainingMonitor(path)] # build model xception_model = xception.Xception(input_shape=(299, 299, 3),
# construct training image generator for data augmentation aug = ImageDataGenerator(rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, zoom_range=0.15, horizontal_flip=True) # load RGB mean for training set means = json.loads(open(config.DATASET_MEAN, 'r').read()) # initialize image preprocessors sp = SimplePreprocessor(227, 227) pp = PatchPreprocessor(227, 227) mp = MeanPreprocessor(means['R'], means['G'], means['B']) iap = ImageToArrayPreprocessor() # initialize training and validation dataset generators print('[INFO] loading dataset...') train_gen = HDF5DatasetGenerator(config.TRAIN_HDF5, batch_size=64*config.G, preprocessors=[pp, mp, iap], aug=aug, classes=2) val_gen = HDF5DatasetGenerator(config.VAL_HDF5, batch_size=64*config.G, preprocessors=[sp, mp, iap], classes=2) # initialize the optimizer if config.G <= 1: print(f'[INFO] compiling model with 1 GPU...') model = AlexNet.build(width=227, height=227, depth=3, classes=2, reg=0.0002) else: print(f'[INFO] compiling model with {config.G} GPU...') # opt = Adam(lr=1e-3) with tf.device('/cpu:0'): model = AlexNet.build(width=227, height=227, depth=3, classes=2, reg=0.0002) model = multi_gpu_model(model, gpus=config.G) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # construct set of callbacks
from pyimagesearch.utils.ranked import rank5_accuracy from pyimagesearch.preprocessing import MeanPreprocessor from pyimagesearch.preprocessing import SimplePreprocessor from pyimagesearch.preprocessing import ImageToArrayPreprocessor # 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 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))
fill_mode="nearest") # load the RGB means for the training set means = json.loads(open(config.DATASET_MEAN).read()) # initialize the image preprocessors sp = SimplePreprocessor(IMAGE_WIDTH, IMAGE_HEIGHT) pp = PatchPreprocessor(IMAGE_WIDTH, IMAGE_HEIGHT) mp = MeanPreprocessor(means["R"], means["G"], means["B"]) iap = ImageToArrayPreprocessor() # initialize the training and validation dataset generators if NUM_CLASSES: trainGen = HDF5DatasetGenerator(TRAIN_HDF5, config.BATCH_SIZE, aug=aug, preprocessors=[sp, mp, iap], classes=NUM_CLASSES) valGen = HDF5DatasetGenerator(config.TEST_HDF5, config.BATCH_SIZE, preprocessors=[sp, mp, iap], classes=NUM_CLASSES) else: trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, config.BATCH_SIZE, aug=aug, preprocessors=[sp, mp, iap], classes=8) valGen = HDF5DatasetGenerator(config.TEST_HDF5, config.BATCH_SIZE, preprocessors=[sp, mp, iap],
args = vars(ap.parse_args()) print('loading model...') model = CRNN.build(width=config.WIDTH, height=config.HEIGHT, depth=1, classes=config.NUM_CLASSES, training=0) model.load_weights(args["model"]) testAug = ImageDataGenerator(rescale=1 / 255.0) iap = ImageToArrayPreprocessor() testGen = HDF5DatasetGenerator(args["images"], config.BATCH_SIZE, aug=testAug, preprocessors=[iap], binarize=False, classes=config.NUM_CLASSES) dic = {} dic[0] = ' ' with open('dic.txt', encoding="utf-8") as dict_file: for i, line in enumerate(dict_file): if i == 0: continue (key, value) = line.strip().split('\t') dic[int(key)] = value dict_file.close() acc = 0 total = 0
def calculate_score(means_path, label_encoder_path, best_weight_path, test_hdf5_path, cross_val=None, preds_cross=None, labels_cross=None, is_mapped=False): # load RGB means for training set means = json.loads(open(means_path).read()) # load LabelEncoder le = pickle.loads(open(label_encoder_path, 'rb').read()) # initialize image preprocessors sp, mp, cp, iap = SimplePreprocessor( config.IMAGE_SIZE, config.IMAGE_SIZE), MeanPreprocessor( means['R'], means['G'], means['B']), CropPreprocessor( config.IMAGE_SIZE, config.IMAGE_SIZE), ImageToArrayPreprocessor() custom_objects = None agh = AgeGenderHelper(config, deploy) if config.DATASET_TYPE == 'age': one_off_mappings = agh.build_oneoff_mappings(le) one_off = OneOffAccuracy(one_off_mappings) custom_objects = {'one_off_accuracy': one_off.one_off_accuracy} # load model print(f'[INFO] loading {best_weight_path}...') model = load_model(best_weight_path, custom_objects=custom_objects) # initialize testing dataset generator, then predict if cross_val is None: print( f'[INFO] predicting in testing data (no crops){config.SALIENCY_INFO}...' ) else: print( f'[INFO] predicting in testing data (no crops) for cross validation {cross_val}{config.SALIENCY_INFO}...' ) test_gen = HDF5DatasetGenerator(test_hdf5_path, batch_size=config.BATCH_SIZE, preprocessors=[sp, mp, iap], classes=config.NUM_CLASSES) preds = model.predict_generator(test_gen.generator(), steps=test_gen.num_images // config.BATCH_SIZE) # compute rank-1 and one-off accuracies labels = to_categorical( test_gen.db['labels'][0:config.BATCH_SIZE * (test_gen.num_images // config.BATCH_SIZE)], num_classes=config.NUM_CLASSES) preds_mapped = preds.argmax(axis=1) if is_mapped == True: preds_mapped = agh.build_mapping_to_iog_labels()[preds_mapped] if cross_val is None: print( '[INFO] serializing all images classified incorrectly for testing dataset...' ) prefix_path = os.path.sep.join( [config.WRONG_BASE, config.DATASET_TYPE]) agh.plot_confusion_matrix_from_data(config, labels.argmax(axis=1), preds_mapped, le=le, save_path=os.path.sep.join([ config.OUTPUT_BASE, f'cm_{config.DATASET_TYPE}.png' ])) else: print( f'[INFO] serializing all images classified incorrectly for cross validation {cross_val} of testing dataset...' ) prefix_path = os.path.sep.join( [config.WRONG_BASE, f'Cross{cross_val}', config.DATASET_TYPE]) preds_cross.extend(preds_mapped.tolist()) labels_cross.extend(labels.argmax(axis=1).tolist()) if os.path.exists(prefix_path): shutil.rmtree(prefix_path) os.makedirs(prefix_path) for i, (pred, label) in enumerate(zip(preds_mapped, labels.argmax(axis=1))): if pred != label: image = test_gen.db['images'][i] if config.DATASET_TYPE == 'age': real_label, real_pred = le.classes_[label], le.classes_[pred] real_label = real_label.replace('_', '-') real_label = real_label.replace('-inf', '+') real_pred = real_pred.replace('_', '-') real_pred = real_pred.replace('-inf', '+') elif config.DATASET_TYPE == 'gender': real_label = 'Male' if label == 0 else 'Female' real_pred = 'Male' if pred == 0 else 'Female' cv2.putText(image, f'Actual: {real_label}, Predict: {real_pred}', (15, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2) cv2.imwrite(os.path.sep.join([prefix_path, f'{i:05d}.jpg']), image) score = accuracy_score(labels.argmax(axis=1), preds_mapped) print(f'[INFO] rank-1: {score:.4f}') score_one_off = None if config.DATASET_TYPE == 'age': score_one_off = one_off.one_off_compute( labels, to_categorical(preds_mapped, num_classes=config.NUM_CLASSES)) print(f'[INFO] one-off: {score_one_off:.4f}') test_gen.close() # re-initialize testing generator, now excluding SimplePreprocessor test_gen = HDF5DatasetGenerator(test_hdf5_path, config.BATCH_SIZE, preprocessors=[mp], classes=config.NUM_CLASSES) preds = [] labels = to_categorical(test_gen.db['labels'], num_classes=config.NUM_CLASSES) print('[INFO] predicting in testing data (with crops)...') # initialize progress bar widgets = [ 'Evaluating: ', progressbar.Percentage(), ' ', progressbar.Bar(), ' ', progressbar.ETA() ] pbar = progressbar.ProgressBar(maxval=math.ceil(test_gen.num_images / config.BATCH_SIZE), widgets=widgets).start() for i, (images, _) in enumerate(test_gen.generator(passes=1)): for image in images: crops = cp.preprocess(image) crops = np.array([iap.preprocess(c) for c in crops]) pred = model.predict(crops) preds.append(pred.mean(axis=0)) pbar.update(i) pbar.finish() test_gen.close() # compute rank-1 accuracy preds_mapped = np.argmax(preds, axis=1) if is_mapped == True: preds_mapped = agh.build_mapping_to_iog_labels()[preds_mapped] score_crops = accuracy_score(labels.argmax(axis=1), preds_mapped) print(f'[INFO] rank-1: {score_crops:.4f}') score_one_off_crops = None if config.DATASET_TYPE == 'age': score_one_off_crops = one_off.one_off_compute( labels, to_categorical(preds_mapped, num_classes=config.NUM_CLASSES)) print(f'[INFO] one-off: {score_one_off_crops:.4f}') return score, score_one_off, score_crops, score_one_off_crops