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 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()
# load model from disk custom_objects = None age_path = deploy.AGE_NETWORK_PATH gender_path = deploy.GENDER_NETWORK_PATH gender_model = load_model(gender_path) agh = AgeGenderHelper(config, deploy) one_off_mappings = agh.build_oneoff_mappings(age_le) one_off = OneOffAccuracy(one_off_mappings) custom_objects = {'one_off_accuracy': one_off.one_off_accuracy} age_model = load_model(age_path, custom_objects=custom_objects) # initialize image preprocessors sp = SimplePreprocessor(256, 256, inter=cv2.INTER_CUBIC) age_mp = MeanPreprocessor(age_means['R'], age_means['G'], age_means['B']) gender_mp = MeanPreprocessor(gender_means['R'], gender_means['G'], gender_means['B']) cp = CropPreprocessor(227, 227) iap = ImageToArrayPreprocessor() # initialize dlib's face detector (HOG-based), then create facial landmark predictor and face aligner detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(deploy.DLIB_LANDMARK_PATH) fa = FaceAligner(predictor) # keep looping while True: # get the current frame grabbed, frame = camera.read()
from pyimagesearch.preprocessing import CropPreprocessor # extract a random crop from the image with the target width # and height from pyimagesearch.preprocessing import AddChannelPreprocessor # subtitutes G and B channel for medianblur and Canny edge map from pyimagesearch.preprocessing import MeanPreprocessor # subtract the means for each channel # initialize the image preprocessors aap = AspectAwarePreprocessor(224, 224) iap = ImageToArrayPreprocessor() pp = PatchPreprocessor(224, 224) sp = SimplePreprocessor(224, 224) cp = CropPreprocessor(224, 224) acp = AddChannelPreprocessor(10, 20) mp = MeanPreprocessor(1, 1, 1) if args["model"] in ("inception", "xception"): aap = AspectAwarePreprocessor(299, 299) iap = ImageToArrayPreprocessor() pp = PatchPreprocessor(299, 299) sp = SimplePreprocessor(299, 299) cp = CropPreprocessor(299, 299) acp = AddChannelPreprocessor(10, 20) from pyimagesearch.datasets import SimpleDatasetLoader # load the image (data) and extract the class label assuming # that our path has the following format: # /path/to/dataset/{class}/{image}.jpg # print("[INFO] loading {}".format(imagePath))
ageModel = mx.model.FeedForward.load(agePath, deploy.AGE_EPOCH) genderModel = mx.model.FeedForward.load(genderPath, deploy.GENDER_EPOCH) # now that the networks are loaded, we need to compile them print("[INFO] compiling models...") ageModel = mx.model.FeedForward(ctx=[mx.gpu(0)], symbol=ageModel.symbol, arg_params=ageModel.arg_params, aux_params=ageModel.aux_params) genderModel = mx.model.FeedForward(ctx=[mx.gpu(0)], symbol=genderModel.symbol, arg_params=genderModel.arg_params, aux_params=genderModel.aux_params) # initialize the image pre-processors sp = SimplePreprocessor(width=227, height=227, inter=cv2.INTER_CUBIC) ageMP = MeanPreprocessor(ageMeans["R"], ageMeans["G"], ageMeans["B"]) genderMP = MeanPreprocessor(genderMeans["R"], genderMeans["G"], genderMeans["B"]) iap = ImageToArrayPreprocessor() # load a sample of testing images rows = open(config.TEST_MX_LIST).read().strip().split("\n") rows = np.random.choice(rows, size=args["sample_size"]) # loop over the rows for row in rows: # unpack the row (_, gtLabel, imagePath) = row.strip().split("\t") image = cv2.imread(imagePath) # pre-process the image, one for the age model and another for
# now that the networks are loaded, we need to compile them print("[INFO] compiling models...") ageModel = mx.model.FeedForward(ctx=[mx.gpu(0)], symbol=ageModel.symbol, arg_params=ageModel.arg_params, aux_params=ageModel.aux_params) genderModel = mx.model.FeedForward(ctx=[mx.gpu(0)], symbol=genderModel.symbol, arg_params=genderModel.arg_params, aux_params=genderModel.aux_params) # initialize the image pre-processors sp = SimplePreprocessor(width=256, height=256, inter=cv2.INTER_CUBIC) cp = CropPreprocessor(width=227, height=227, horiz=True) ageMP = MeanPreprocessor(ageMeans["R"], ageMeans["G"], ageMeans["B"]) genderMP = MeanPreprocessor(genderMeans["R"], genderMeans["G"], genderMeans["B"]) iap = ImageToArrayPreprocessor(dataFormat="channels_first") # initialize dlib's face detector (HOG-based), then create the # the facial landmark predictor and face aligner detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(deploy.DLIB_LANDMARK_PATH) fa = FaceAligner(predictor) # if a video path was not supplied, grab the reference to the webcam if not args.get("video", False): camera = VideoStream(src=0, usePiCamera=False).start() # otherwise, load the video
def upload_file(): file = request.files['image'] image_path = os.path.sep.join([UPLOAD_FOLDER, file.filename]) file.save(image_path) # image_url = uploader.upload(image_path) # image = AgeGenderHelper.url_to_image(image_url['url']) # initialize dlib's face detector (HOG-based), then create facial landmark predictor and face aligner detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(deploy.DLIB_LANDMARK_PATH) fa = FaceAligner(predictor) # initialize image preprocessors sp, cp, iap = SimplePreprocessor( 256, 256, inter=cv2.INTER_CUBIC), CropPreprocessor( config.IMAGE_SIZE, config.IMAGE_SIZE, horiz=False), ImageToArrayPreprocessor() # loop over image paths # load image fron disk, resize it and convert it to grayscale print(f'[INFO] processing {file.filename}') image = cv2.imread(image_path) image = imutils.resize(image, width=1024) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) clone = image.copy() # detect faces in grayscale image rects = detector(gray, 1) # loop over face detections for rect in rects: # determine facial landmarks for face region, then align face shape = predictor(gray, rect) face = fa.align(image, gray, rect) # draw bounding box around face x, y, w, h = face_utils.rect_to_bb(rect) cv2.rectangle(clone, (x, y), (x + w, y + h), (0, 255, 0), 2) if config.DATASET == 'IOG': # load Label Encoder and mean files print('[INFO] loading label encoders and mean files...') age_le = pickle.loads(open(deploy.AGE_LABEL_ENCODER, 'rb').read()) gender_le = pickle.loads( open(deploy.GENDER_LABEL_ENCODER, 'rb').read()) age_means = json.loads(open(deploy.AGE_MEAN).read()) gender_means = json.loads(open(deploy.GENDER_MEAN).read()) # initialize image preprocessors age_mp = MeanPreprocessor(age_means['R'], age_means['G'], age_means['B']) gender_mp = MeanPreprocessor(gender_means['R'], gender_means['G'], gender_means['B']) age_preds, gender_preds = predict(face, sp, age_mp, gender_mp, cp, iap, deploy.AGE_NETWORK_PATH, deploy.GENDER_NETWORK_PATH, age_le, gender_le) elif config.DATASET == 'ADIENCE': # age_preds_cross, gender_preds_cross = [], [] i = 0 # load Label Encoder and mean files print( f'[INFO] loading label encoders and mean files for cross validation {i}...' ) age_le = pickle.loads( open(deploy.AGE_LABEL_ENCODERS[i], 'rb').read()) gender_le = pickle.loads( open(deploy.GENDER_LABEL_ENCODERS[i], 'rb').read()) age_means = json.loads(open(deploy.AGE_MEANS[i]).read()) gender_means = json.loads(open(deploy.GENDER_MEANS[i]).read()) # initialize image preprocessors age_mp = MeanPreprocessor(age_means['R'], age_means['G'], age_means['B']) gender_mp = MeanPreprocessor(gender_means['R'], gender_means['G'], gender_means['B']) age_preds, gender_preds = predict(face, sp, age_mp, gender_mp, cp, iap, deploy.AGE_NETWORK_PATHS[i], deploy.GENDER_NETWORK_PATHS[i], age_le, gender_le) # age_preds_cross.append(age_pred) # gender_preds_cross.append(gender_pred) # age_preds, gender_preds = np.mean(age_preds_cross, axis = 0), np.mean(gender_preds_cross, axis = 0) clone = AgeGenderHelper.visualize_video(age_preds, gender_preds, age_le, gender_le, clone, (x, y)) # path = image_path.split('.') # pred_path = '.'.join([f'{path[0]}_predict', path[1]]) # pred_filename = pred_path.split(os.path.sep)[-1] pred_path = '.'.join([f"{image_path.split('.')[0]}_1", 'jpg']) cv2.imwrite(pred_path, clone) # image_url = uploader.upload(pred_path) gc.collect() K.clear_session() return render_template('index.html', filename=pred_path.split(os.path.sep)[-1])
import os import pdb aug = 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") 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)
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()
rows = np.random.choice(rows, size=args["sample_size"]) # load our pre-trained model print("[INFO] loading pre-trained model...") checkpointsPath = os.path.sep.join([args["checkpoints"], args["prefix"]]) model = mx.model.FeedForward.load(checkpointsPath, args["epoch"]) # compile the model model = mx.model.FeedForward(ctx=[mx.gpu(0)], symbol=model.symbol, arg_params=model.arg_params, aux_params=model.aux_params) # initialize the image pre-processors sp = AspectAwarePreprocessor(width=224, height=224) mp = MeanPreprocessor(config.R_MEAN, config.G_MEAN, config.B_MEAN) iap = ImageToArrayPreprocessor(dataFormat="channels_first") # loop over the testing images for row in rows: # grab the target class label and the image path from the row (target, imagePath) = row.split("\t")[1:] target = int(target) # load the image from disk and pre-process it by resizing the # image and applying the pre-processors image = cv2.imread(imagePath) orig = image.copy() orig = imutils.resize(orig, width=min(500, orig.shape[1])) image = iap.preprocess(mp.preprocess(sp.preprocess(image))) image = np.expand_dims(image, axis=0)
from pyimagesearch.utils.ranked import rank5_accuracy from keras.models import load_model import progressbar import numpy as np import os 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()
import json from shutil import rmtree from config import affectnet_config as config from pyimagesearch.preprocessing import AspectAwarePreprocessor, ImageToArrayPreprocessor, SimplePreprocessor, MeanPreprocessor, CropPreprocessor from keras.models import load_model # In[3]: images = glob('/path/to/images/jpg/A/*/*.jpg') print('Found {} images'.format(len(images))) # In[4]: 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() #load the pretrained network model = load_model(config.MODEL_PATH) # In[5]: predictions = [] for image in tqdm(images[:100]): face = face_get(image) crop = sp.preprocess(face) crop = mp.preprocess(crop) crop = np.expand_dims(crop, axis=0) #crop = np.array([iap.preprocess(c) for c in crop], dtype = "float32")
# draw bounding box around face clone = image.copy() x, y, w, h = face_utils.rect_to_bb(rect) cv2.rectangle(clone, (x, y), (x+w, y+h), (0, 255, 0), 2) if config.DATASET == 'IOG': # load Label Encoder and mean files print('[INFO] loading label encoders and mean files...') age_le = pickle.loads(open(deploy.AGE_LABEL_ENCODER, 'rb').read()) gender_le = pickle.loads(open(deploy.GENDER_LABEL_ENCODER, 'rb').read()) age_means = json.loads(open(deploy.AGE_MEAN).read()) gender_means = json.loads(open(deploy.GENDER_MEAN).read()) # initialize image preprocessors age_mp = MeanPreprocessor(age_means['R'], age_means['G'], age_means['B']) gender_mp = MeanPreprocessor(gender_means['R'], gender_means['G'], gender_means['B']) age_preds, gender_preds = predict(face, sp, age_mp, gender_mp, cp, iap, deploy.AGE_NETWORK_PATH, deploy.GENDER_NETWORK_PATH, age_le, gender_le) # visualize age and gender predictions age_canvas = AgeGenderHelper.visualize_age(age_preds, age_le) gender_canvas = AgeGenderHelper.visualize_gender(gender_preds, gender_le) elif config.DATASET == 'ADIENCE': # age_preds_cross, gender_preds_cross = [], [] i = 0 # load Label Encoder and mean files print(f'[INFO] loading label encoders and mean files for cross validation {i}...') age_le = pickle.loads(open(deploy.AGE_LABEL_ENCODERS[i], 'rb').read())
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()
help='epoch to restart training at') args = vars(ap.parse_args()) # construct training image generator for data augmentation aug = ImageDataGenerator(rotation_range=18, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, zoom_range=0.15, horizontal_flip=True) # load RGB means for training set means = json.loads(open(config.DATASET_MEAN).read()) # initialize image preprocessors sp, mp, iap = SimplePreprocessor(64, 64), MeanPreprocessor( means['R'], means['G'], means['B']), ImageToArrayPreprocessor() # initialize training and validation dataset generators train_gen = HDF5DatasetGenerator(config.TRAIN_HDF5, 64, preprocessors=[sp, mp, iap], aug=aug, classes=config.NUM_CLASSES) val_gen = HDF5DatasetGenerator(config.VAL_HDF5, 64, preprocessors=[sp, mp, iap], aug=aug, classes=config.NUM_CLASSES) # if there is no specific model checkpoint supplied, initialize network and compile model if args['model'] is None:
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 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()
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, )
import argparse import pandas as pd # construct argument parser and parse the argument ap = argparse.ArgumentParser() ap.add_argument('-s', '--submit', required=True, help='path to submission file') args = vars(ap.parse_args()) # 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: ',
class GenderRecognizer: # load the label encoders and mean files print("[INFO] loading label encoders and mean files...") ageLE = pickle.loads(open(deploy.AGE_LABEL_ENCODER, "rb").read()) genderLE = pickle.loads(open(deploy.GENDER_LABEL_ENCODER, "rb").read()) ageMeans = json.loads(open(deploy.AGE_MEANS).read()) genderMeans = json.loads(open(deploy.GENDER_MEANS).read()) # load the models from disk print("[INFO] loading models...") agePath = os.path.sep.join([deploy.AGE_NETWORK_PATH, deploy.AGE_PREFIX]) genderPath = os.path.sep.join( [deploy.GENDER_NETWORK_PATH, deploy.GENDER_PREFIX]) ageModel = mx.model.FeedForward.load(agePath, deploy.AGE_EPOCH) genderModel = mx.model.FeedForward.load(genderPath, deploy.GENDER_EPOCH) # now that the networks are loaded, we need to compile them print("[INFO] compiling models...") ageModel = mx.model.FeedForward(ctx=[mx.gpu(0)], symbol=ageModel.symbol, arg_params=ageModel.arg_params, aux_params=ageModel.aux_params) genderModel = mx.model.FeedForward(ctx=[mx.gpu(0)], symbol=genderModel.symbol, arg_params=genderModel.arg_params, aux_params=genderModel.aux_params) # initialize the image pre-processors sp = SimplePreprocessor(width=256, height=256, inter=cv2.INTER_CUBIC) cp = CropPreprocessor(width=227, height=227, horiz=True) ageMP = MeanPreprocessor(ageMeans["R"], ageMeans["G"], ageMeans["B"]) genderMP = MeanPreprocessor(genderMeans["R"], genderMeans["G"], genderMeans["B"]) iap = ImageToArrayPreprocessor(dataFormat="channels_first") def __init__(self): # initialize dlib's face detector (HOG-based), then create the # the facial landmark predictor and face aligner self.detector = dlib.get_frontal_face_detector() self.predictor = dlib.shape_predictor(deploy.DLIB_LANDMARK_PATH) self.fa = FaceAligner(self.predictor) def recognize(self, face): # resize the face to a fixed size, then extract 10-crop # patches from it face = self.sp.preprocess(face) patches = self.cp.preprocess(face) # allocate memory for the age and gender patches agePatches = np.zeros((patches.shape[0], 3, 227, 227), dtype="float") genderPatches = np.zeros((patches.shape[0], 3, 227, 227), dtype="float") # loop over the patches for j in np.arange(0, patches.shape[0]): # perform mean subtraction on the patch agePatch = self.ageMP.preprocess(patches[j]) genderPatch = self.genderMP.preprocess(patches[j]) agePatch = self.iap.preprocess(agePatch) genderPatch = self.iap.preprocess(genderPatch) # update the respective patches lists agePatches[j] = agePatch genderPatches[j] = genderPatch # make predictions on age and gender based on the extracted # patches agePreds = self.ageModel.predict(agePatches) genderPreds = self.genderModel.predict(genderPatches) # compute the average for each class label based on the # predictions for the patches agePreds = agePreds.mean(axis=0) genderPreds = genderPreds.mean(axis=0) # visualize the age and gender predictions age = GenderRecognizer.visAge(agePreds, self.ageLE) gender = GenderRecognizer.visGender(genderPreds, self.genderLE) image_to_show_path_GA = "images/{}/{}".format(gender, age) return image_to_show_path_GA @staticmethod def visAge(agePreds, le): # initialize the canvas and sort the predictions according # to their probability idxs = np.argsort(agePreds)[::-1] # construct the text for the prediction #ageLabel = le.inverse_transform(j) # Python 2.7 ageLabel = le.inverse_transform(idxs[0]).decode("utf-8") ageLabel = ageLabel.replace("_", "-") ageLabel = ageLabel.replace("-inf", "+") age = "{}".format(ageLabel) return age @staticmethod def visGender(genderPreds, le): idxs = np.argsort(genderPreds)[::-1] gender = le.inverse_transform(idxs[0]) gender = "Male" if gender == 0 else "Female" text_gender = "{}".format(gender) return text_gender
import json from keras.models import load_model from config import tiny_imagenet_config as config from pyimagesearch.io import HDF5DatasetGenerator 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(),
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