import numpy as np import argparse import os ap = argparse.ArgumentParser() ap.add_argument("-d", "--dataset", required=True) ap.add_argument("-m", "--model", required=True) args = vars(ap.parse_args()) print("[INFO] Loading") imagePaths = list(paths.list_images(args["dataset"])) classNames = [pt.split(os.path.sep)[-2] for pt in imagePaths] classNames = [str(x) for x in np.unique(classNames)] aap = AspectAwarePreprocessor(64, 64) iap = ImageToArrayPreprocessor() sdl = SimpleDatasetLoader(preprocessors=[aap, iap]) (data, labels) = sdl.load(imagePaths, verbose=500) data = data.astype("float") / 255.0 (trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.25, random_state=42) trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1,
le = pickle.loads(open(config.LABEL_ENCODER_PATH, "rb").read()) rows = open(config.TEST_MX_LIST).read().strip().split("\n") rows = np.random.choice(rows, size=args["sample_size"]) print("[INFO] loading pre-trained model") checkpointsPath = os.path.sep.join([args["checkpoints"], args["prefix"]]) model = mx.model.FeedForward.load(checkpointsPath, args["epoch"]) model = mx.model.FeedForward(ctx=[mx.gpu(0)], symbol=model.symbol, arg_params=model.arg_params, aux_params=model.aux_params) sp = AspectAwarePreprocessor(width=224, height=224) mp = MeanPreprocessor(config.R_MEAN, config.G_MEAN, config.B_MEAN) iap = ImageToArrayPreprocessor(dataFormat="channels_first") for row in rows: (target, imagePath) = row.split("\t")[1:] target = int(target) 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) preds = model.predict(image)[0] idxs = np.argsort(preds)[::-1][:5] print("[INFO] actual={}".format(le.inverse_transform(target)))
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) 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) 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") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(deploy.DLIB_LANDMARK_PATH) fa = FaceAligner(predictor) imagePaths = [args["image"]] if os.path.isdir(args["image"]): imagePaths = sorted(list(paths.list_files(args["image"]))) for imagePath in imagePaths: print("[INFO] processing {}".format(imagePath)) image = cv2.imread(imagePath) image = imutils.resize(image, width=800)
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) 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() rows = open(config.TEST_MX_LIST).read().strip().split("\n") rows = np.random.choice(rows, size=args["sample_size"]) for row in rows: (_, gtLabel, imagePath) = row.strip().split("\t") image = cv2.imread(imagePath) ageImage = iap.preprocess(ageMP.preprocess(sp.preprocess(image))) genderImage = iap.preprocess(genderMP.preprocess(sp.preprocess(image))) ageImage = np.expand_dims(ageImage, axis=0) genderImage = np.expand_dims(genderImage, axis=0) agePreds = ageModel.predict(ageImage)[0]