Пример #1
0
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()

    # if we are viewing a video and we did not grab a frame, we reach the end of video
    if args.get('video') and not grabbed:
        break
Пример #2
0
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: ',
    progressbar.Percentage(), ' ',
from pyimagesearch.preprocessing import SimplePreprocessor
# # resize the image to a fixed size, ignoring the aspect ratio
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:
Пример #4
0
from pyimagesearch.preprocessing import ImageToArrayPreprocessor
from pyimagesearch.preprocessing import SimplePreprocessor
from pyimagesearch.preprocessing import MeanPreprocessor
from pyimagesearch.preprocessing import CropPreprocessor
from pyimagesearch.io import HDF5DatasetGenerator
from pyimagesearch.utils.ranked import rank5_accuracy
from keras.models import load_model
import numpy as np
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"])
Пример #5
0
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
Пример #6
0
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")

# 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()
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")
    pred = model.predict(crop)
Пример #8
0
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()
Пример #9
0
    return age_preds, gender_preds

# 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 list of image paths
image_paths = [args['image']]

# if input path is directory
if os.path.isdir(args['image']):
    image_paths = sorted(list(paths.list_images(args['image'])))

# 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
for image_path in image_paths:
    # load image fron disk, resize it and convert it to grayscale
    print(f'[INFO] processing {image_path}')
    image = cv2.imread(image_path)
    image = imutils.resize(image, width=1024)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # 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
Пример #10
0
    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_mean['R'], age_mean['G'], age_mean['B'])
    gender_mp = MeanPreprocessor(gender_mean['R'], gender_mean['G'],
                                 gender_mean['B'])
    cp = CropPreprocessor(config.IMAGE_SIZE, config.IMAGE_SIZE)
    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)

    for image_path in np.array(image_paths)[rows]:
        path = os.path.sep.join([config.BASE_PATH, '*', f'{image_path}'])
        path = glob.glob(path)[0]
        # load image fron disk, resize it and convert it to grayscale
        print(f'[INFO] processing {path}')
        image = cv2.imread(path)
        image = imutils.resize(image, width=1024)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Пример #11
0
classNames = [str(x) for x in np.unique(classNames)]
print("[INFO] Names of classes {}...".format(classNames))

from pyimagesearch.preprocessing import ImageToArrayPreprocessor
from pyimagesearch.preprocessing import AspectAwarePreprocessor
from pyimagesearch.preprocessing import PatchPreprocessor
from pyimagesearch.preprocessing import SimplePreprocessor
from pyimagesearch.preprocessing import CropPreprocessor
from pyimagesearch.preprocessing import AddChannelPreprocessor

# 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)

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(imagePaths))
# load the dataset from disk then scale the raw pixel intensities
# to the range [0, 1]
spreproc = "sp_aap_iap"
sdl = SimpleDatasetLoader(preprocessors=[sp, aap, iap])
(data, labels) = sdl.load(imagePaths, verbose=500)
data = data.astype("float") / 255.0
Пример #12
0
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])
Пример #13
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