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 main(): """Train ShallowNet on animals dataset. """ # construct the argument parser and parse the arguments args = argparse.ArgumentParser() args.add_argument("-d", "--dataset", required=True, help="path to input dataset") args = vars(args.parse_args()) # grab the list of images that we'll be describing print("[INFO] loading images...") image_paths = list(paths.list_images(args["dataset"])) # initialize the image preprocessors simple_preprocessor = SimplePreprocessor(32, 32) image_to_array_preprocessor = ImageToArrayPreprocessor() # load the dataset from disk then scale the raw pixel intensities to the range [0, 1] dataset_loader = SimpleDatasetLoader(preprocessors=[simple_preprocessor, image_to_array_preprocessor]) (data, labels) = dataset_loader.load(image_paths, verbose=500) data = data.astype("float") / 255.0 # partition the data into training and testing splits using 75% of # the data for training and the remaining 25% for testing (train_x, test_x, train_y, test_y) = train_test_split(data, labels, test_size=0.25, random_state=42) # convert the labels from integers to vectors train_y = LabelBinarizer().fit_transform(train_y) test_y = LabelBinarizer().fit_transform(test_y) # initialize the optimizer and model print("[INFO] compiling model...") opt = SGD(lr=0.005) model = ShallowNet.build(width=32, height=32, depth=3, classes=3) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the network print("[INFO] training network...") model_fit = model.fit(train_x, train_y, validation_data=(test_x, test_y), batch_size=32, epochs=100, verbose=1) # evaluate the network print("[INFO] evaluating network...") predictions = model.predict(test_x, batch_size=32) print( classification_report(test_y.argmax(axis=1), predictions.argmax(axis=1), target_names=["cat", "dog", "panda"]) ) # plot the training loss and accuracy plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 100), model_fit.history["loss"], label="train_loss") plt.plot(np.arange(0, 100), model_fit.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 100), model_fit.history["acc"], label="train_acc") plt.plot(np.arange(0, 100), model_fit.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend() plt.show()
def pre_process_data(dimensions,image_paths): # construct the image generator for data augmentation aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest") # initialize the image preprocessors aap = AspectAwarePreprocessor(dimensions,dimensions) iap = ImageToArrayPreprocessor() # load the dataset from disk then scale the raw pixel intensities to # the range [0, 1] sdl = SimpleDatasetLoader(preprocessors=[aap, iap]) (data, labels) = sdl.load(imagePaths, verbose=500) data = data.astype("float") / 255.0 return data, labels
def main(): """Load pre-trained model from disk """ # construct the argument parse and parse the arguments args = argparse.ArgumentParser() args.add_argument("-d", "--dataset", required=True, help="path to input dataset") args.add_argument("-m", "--model", required=True, help="path to pre-trained model") args = vars(args.parse_args()) # initialize the class labels class_labels = ["cat", "dog", "panda"] # grab the list of images in the dataset then randomly sample indexes into the image paths list print("[INFO] sampling images...") image_paths = np.array(list(paths.list_images(args["dataset"]))) idxs = np.random.randint(0, len(image_paths), size=(10, )) image_paths = image_paths[idxs] # initialize the image preprocessors simple_preprocessor = SimplePreprocessor(32, 32) image_to_array_preprocessor = ImageToArrayPreprocessor() # load the dataset from disk then scale the raw pixel intensities to the range [0, 1] dataset_loader = SimpleDatasetLoader( preprocessors=[simple_preprocessor, image_to_array_preprocessor]) (data, _) = dataset_loader.load(image_paths) data = data.astype("float") / 255.0 # load the pre-trained network print("[INFO] loading pre-trained network...") model = load_model(args["model"]) # make predictions on the images print("[INFO] predicting...") preds = model.predict(data, batch_size=32).argmax(axis=1) # loop over the sample images for (i, image_path) in enumerate(image_paths): # load the example image, draw the prediction, and display it to our screen image = cv2.imread(image_path) cv2.putText(image, "Label: {}".format(class_labels[preds[i]]), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) cv2.imshow("Image", image) cv2.waitKey(0)
def separate_data(imagePaths, classNames): print("[INFO] Separating data.......................") iap = ImageToArrayPreprocessor() aap = AspectAwarePreprocessor(WIDTH, HEIGHT) sdl = SimpleDatasetLoader(preprocessors=[aap, iap]) (data, labels) = sdl.load(imagePaths, verbose=500) data = data.astype("float") / 255.0 # partition the data into training and testing splits using 75% of # the data for training and the remaining 25% for testing (trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.25, random_state=42) # convert the labels from integers to vectors trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) if len(classNames) == 2: trainY = np.hstack((trainY, 1 - trainY)) testY = np.hstack((testY, 1 - testY)) return [trainX, testX, trainY, testY]
from config import tiny_imagenet_config as config from pyimagesearch.preprocessing import ImageToArrayPreprocessor, MeanPreprocessor, SimplePreprocessor from pyimagesearch.utils.ranked import rank5_accuracy from pyimagesearch.io import HDF5DatasetGenerator from keras.models import load_model import json # load RGB means for the 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['G']), ImageToArrayPreprocessor() # initialize testing dataset generator test_gen = HDF5DatasetGenerator(config.TEST_HDF5, 64, preprocessors=[sp, mp, iap], classes=config.NUM_CLASSES) # load pre-trained network print('[INFO] loading network...') model = load_model(config.MODEL_PATH) print('[INFO] predicting on test data...') preds = model.predict_generator(test_gen.generator(), steps=test_gen.num_images//64) # compute rank-1 and rank-5 accuracies rank_1, rank_5 = rank5_accuracy(preds, test_gen.db['labels']) print(f'[INFO] rank-1: {rank_1*100:.2f}') print(f'[INFO] rank-5: {rank_5*100:.2f}') # close dataset test_gen.close()
# construct argument parser and parse the argument ap = argparse.ArgumentParser() ap.add_argument('-d', '--dataset', required=True, help='path to input dataset') ap.add_argument('-o', '--output', required=True, help='path to output for training monitor') args = vars(ap.parse_args()) # grab the list of image paths print('[INFO] loading images...') image_paths = list(paths.list_images(args['dataset'])) # initialize the preprocessors aap, iap = AspectAwarePreprocessor(64, 64), ImageToArrayPreprocessor() # load images and scale it to range [0, 1] sdl = SimpleDatasetLoader(preprocessors=[aap, iap]) data, labels = sdl.load(image_paths, verbose=500) data = data.astype('float') / 255. # partition data and label trainX, testX, trainY, testY = train_test_split(data, labels, test_size=0.25, random_state=42) # convert label to vector le = LabelBinarizer() trainY = le.fit_transform(trainY)
import numpy as np import matplotlib.pyplot as plt from imutils import paths # Construct argument parser and parse argument ap = argparse.ArgumentParser() ap.add_argument('-d', '--dataset', required=True, help='path to input dataset') ap.add_argument('-m', '--model', required=True, help='path to output model') args = vars(ap.parse_args()) # grab the images list print('[INFO] loading images...') image_paths = list(paths.list_images(args['dataset'])) # initialize preprocessors sp, iap = SimplePreprocessor(32, 32), ImageToArrayPreprocessor() # load dataset and scale raw image to range [0, 1] sdl = SimpleDatasetLoader([sp, iap]) data, labels = sdl.load(image_paths, verbose=500) data = data.astype('float') / 255 # partition data trainX, testX, trainY, testY = train_test_split(data, labels, test_size=0.25, random_state=42) # Convert labels to vector lb = LabelBinarizer() trainY = lb.fit_transform(trainY)
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()
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
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 # load the RGB means for the training set means = json.loads(open(config.DATASET_MEAN).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(config.MODEL_PATH) # initialize the testing dataset generator, then make predictions on # the testing data 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) # compute the rank-1 and rank-5 accuracies (rank1, _) = rank5_accuracy(predictions, testGen.db["labels"])
def train(self, epochs, batch_size=128, sample_interval=50): # Load the dataset # cached_images = '/data/bathy_training/cache_images_ohara_07.npz' # # cached bathymetry # cached_bpatches = '/data/bathy_training/cache_raw_bpatches_ohara_07.npz' # all_bpatches = '/data/bathy_training/all_raw_bpatches_ohara_07.npz' # # load dataset # data = np.load(cached_images) # Ximg_train = data['xtrain'] # data = np.load(cached_bpatches) # Xbathy_train = data['xtrain'] # Xbathy_train_means = np.mean(Xbathy_train,axis=(1,2)) # print("shape Xbathy_train_means ", Xbathy_train_means.shape) # # for k in np.arange(Xbathy_train.shape[0]): # Xbathy_train[k,:,:,0] = Xbathy_train[k,:,:,0] - Xbathy_train_means[k] # # data = np.load(all_bpatches) # Xbathy_all = data['xtrain'] # Xbathy_all_means = np.mean(Xbathy_all,axis=(1,2)) # for k in np.arange(Xbathy_all.shape[0]): # Xbathy_all[k,:,:,0] = Xbathy_all[k,:,:,0] - Xbathy_all_means[k] # Configure input # X_train = (X_train.astype(np.float32) - 127.5) / 127.5 # X_train = np.expand_dims(X_train, axis=3) # y_train = y_train.reshape(-1, 1) # Adversarial ground truths valid = np.ones((batch_size, 1)) fake = np.zeros((batch_size, 1)) # to plot metrics d_loss_hist = [] g_loss_hist = [] # initialize image preprocessors #crfp = CropRotFlipPreprocessor(128, 128, horiz=True, rots=True) patchp = PatchPreprocessor(128, 128) iap = ImageToArrayPreprocessor() # initialize the training and validation dataset generators trainGen = HDF5DatasetGeneratorMulti(config.TRAIN_HDF5, 32, aug=None, preprocessors=[patchp, iap]) # construct the set of callbacks ##path = os.path.sep.join([config.OUTPUT_PATH, "{}.png".format( ##os.getpid())]) ##callbacks = [TrainingMonitor(path)] # data generator #dataGenerator = ImageDataGenerator(horizontal_flip = True, vertical_flip = True) #batchIterator = dataGenerator.flow((Ximg_train,[Xbathy_train,Xbathy_train_means]), batch_size=batch_size) #batchIterator = trainGen.generator() for epoch in range(epochs): # --------------------- # Train Discriminator # --------------------- # Select a random half batch of images print("num Images {}".format(trainGen.numImages)) #idx = np.random.randint(0, trainGen.numImages, batch_size) idx = random.sample(range(0, trainGen.numImages), batch_size) print("idx {}".format(idx)) #imgs, bpatches, bp_means = Ximg_train[idx], Xbathy_train[idx], Xbathy_train_means[idx] imgs, bpatches, bp_means = trainGen.get_batch_by_indeces(idx) print("shapes inputs {}, {}, {}".format(imgs.shape, bpatches.shape, bp_means.shape)) actual_batch_size = imgs.shape[0] # Sample noise as generator input noise = np.random.normal(0, 1, (actual_batch_size, self.latent_dim)) #noise = np.random.uniform(-1, 1, (actual_batch_size, self.latent_dim)) # Generate a half batch of new images gen_imgs = self.generator.predict([noise, bpatches, bp_means]) # Train the discriminator d_loss_real = self.discriminator.train_on_batch( [imgs, bpatches, bp_means], valid[:actual_batch_size]) d_loss_fake = self.discriminator.train_on_batch( [gen_imgs, bpatches, bp_means], fake[:actual_batch_size]) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) # --------------------- # Train Generator # --------------------- # Condition on labels ## this is selecting random patches from a box around dive, presumably representative bathy noise = np.random.normal(0, 1, (actual_batch_size, self.latent_dim)) imgs, bpatches, bp_means = trainGen.get_random_batch() #idx = np.random.randint(0, Xbathy_all.shape[0], batch_size) #random_bathy = Xbathy_all[idx] #random_bathy_means = Xbathy_all_means[idx] #sampled_labels = np.random.randint(0, 10, batch_size).reshape(-1, 1) # Train the generator g_loss = self.combined.train_on_batch([noise, bpatches, bp_means], valid) # Plot the progress print("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100 * d_loss[1], g_loss)) d_loss_hist.append(d_loss[0]) g_loss_hist.append(g_loss) # If at save interval => save generated image samples # FIXME THIS ASSUMES A FIXED RANDOM ORDER if epoch % sample_interval == 0: self.sample_images(epoch, trainGen) # plotting the metrics plt.plot(d_loss_hist) plt.plot(g_loss_hist) plt.title('Model loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Discriminator', 'Adversarial'], loc='best') plt.show() plt.savefig("metrics_cgan/metrics.png") plt.close()
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
def main(): """Run image classification """ # construct the argument parse and parse the arguments args = argparse.ArgumentParser() args.add_argument("-d", "--dataset", required=True, help="path to input dataset") args = vars(args.parse_args()) # grab the list of images that we'll be describing, then extract # the class label names from the image paths print("[INFO] loading images...") image_paths = list(paths.list_images(args["dataset"])) class_names = [pt.split(os.path.sep)[-2] for pt in image_paths] class_names = [str(x) for x in np.unique(class_names)] # initialize the image preprocessors aspect_aware_preprocessor = AspectAwarePreprocessor(64, 64) image_to_array_preprocessor = ImageToArrayPreprocessor() # load the dataset from disk then scale the raw pixel intensities to the range [0, 1] sdl = SimpleDatasetLoader( preprocessors=[aspect_aware_preprocessor, image_to_array_preprocessor]) (data, labels) = sdl.load(image_paths, verbose=500) data = data.astype("float") / 255.0 # partition the data into training and testing splits using 75% of # the data for training and the remaining 25% for testing (train_x, test_x, train_y, test_y) = train_test_split(data, labels, test_size=0.25, random_state=42) # convert the labels from integers to vectors train_y = LabelBinarizer().fit_transform(train_y) test_y = LabelBinarizer().fit_transform(test_y) # construct the image generator for data augmentation aug = ImageDataGenerator( rotation_range=30, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest", ) # initialize the optimizer and model print("[INFO] compiling model...") opt = SGD(lr=0.05) model = MiniVGGNet.build(width=64, height=64, depth=3, classes=len(class_names)) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the network print("[INFO] training network...") model_fit = model.fit_generator( aug.flow(train_x, train_y, batch_size=32), validation_data=(test_x, test_y), steps_per_epoch=len(train_x) // 32, epochs=100, verbose=1, ) # evaluate the network print("[INFO] evaluating network...") predictions = model.predict(test_x, batch_size=32) print( classification_report(test_y.argmax(axis=1), predictions.argmax(axis=1), target_names=class_names)) # plot the training loss and accuracy plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 100), model_fit.history["loss"], label="train_loss") plt.plot(np.arange(0, 100), model_fit.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 100), model_fit.history["acc"], label="train_acc") plt.plot(np.arange(0, 100), model_fit.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend() plt.show()
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()
# 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 # the gender model ageImage = iap.preprocess(ageMP.preprocess( sp.preprocess(image)))
print('[INFO] moving image to label folder.....') im = IM.MoveImageToLabel(dataPath=args['dataset']) im.makeFolder() im.move() print("[INFO] loading images...") imagePaths = [ x for x in list(paths.list_images(args['dataset'])) if x.split(os.path.sep)[-2] != 'jpg' ] classNames = [pt.split(os.path.sep)[-2] for pt in imagePaths] classNames = [str(x) for x in np.unique(classNames)] aap = AAP.AspectAwarePreprocesser(64, 64) iap = IAP.ImageToArrayPreprocess() sdl = 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=43) trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) print("[INFO] compiling model....") opt = SGD(lr=0.05)
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() # initialize test generator for evaluting with cropping
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"]) print("[INFO] rank-1: {:.2f}%".format(rank1 * 100))
def main(): """Fine tune VGG16 """ # construct the argument parse and parse the arguments args = argparse.ArgumentParser() args.add_argument("-d", "--dataset", required=True, help="path to input dataset") args.add_argument("-m", "--model", required=True, help="path to output model") args = vars(args.parse_args()) # construct the image generator for data augmentation augmentation = ImageDataGenerator( rotation_range=30, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest", ) # grab the list of images that we'll be describing, then extract # the class label names from the image paths print("[INFO] loading images...") image_paths = list(paths.list_images(args["dataset"])) class_names = [pt.split(os.path.sep)[-2] for pt in image_paths] class_names = [str(x) for x in np.unique(class_names)] # initialize the image preprocessors aspect_aware_preprocessor = AspectAwarePreprocessor(224, 224) image_to_array_preprocessor = ImageToArrayPreprocessor() # load the dataset from disk then scale the raw pixel intensities to the range [0, 1] simple_dataset_loader = SimpleDatasetLoader( preprocessors=[aspect_aware_preprocessor, image_to_array_preprocessor]) (data, labels) = simple_dataset_loader.load(image_paths, verbose=500) data = data.astype("float") / 255.0 # partition the data into training and testing splits using 75% of # the data for training and the remaining 25% for testing (train_x, test_x, train_y, test_y) = train_test_split(data, labels, test_size=0.25, random_state=42) # convert the labels from integers to vectors train_y = LabelBinarizer().fit_transform(train_y) test_y = LabelBinarizer().transform(test_y) # load the VGG16 network, ensuring the head FC layer sets are left off base_model = VGG16(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3))) # initialize the new head of the network, a set of FC layers followed by a softmax classifier head_model = FCHeadNet.build(base_model, len(class_names), 256) # place the head FC model on top of the base model -- this will # become the actual model we will train model = Model(inputs=base_model.input, outputs=head_model) # loop over all layers in the base model and freeze them so they # will *not* be updated during the training process for layer in base_model.layers: layer.trainable = False # compile our model (this needs to be done after our setting our layers to being non-trainable print("[INFO] compiling model...") opt = RMSprop(lr=0.001) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the head of the network for a few epochs (all other layers are frozen) -- this will # allow the new FC layers to start to become initialized with actual "learned" values # versus pure random print("[INFO] training head...") model.fit_generator( augmentation.flow(train_x, train_y, batch_size=32), validation_data=(test_x, test_y), epochs=25, steps_per_epoch=len(train_x) // 32, verbose=1, ) # evaluate the network after initialization print("[INFO] evaluating after initialization...") predictions = model.predict(test_x, batch_size=32) print( classification_report(test_y.argmax(axis=1), predictions.argmax(axis=1), target_names=class_names)) # now that the head FC layers have been trained/initialized, lets # unfreeze the final set of CONV layers and make them trainable for layer in base_model.layers[15:]: layer.trainable = True # for the changes to the model to take affect we need to recompile # the model, this time using SGD with a *very* small learning rate print("[INFO] re-compiling model...") opt = SGD(lr=0.001) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the model again, this time fine-tuning *both* the final set # of CONV layers along with our set of FC layers print("[INFO] fine-tuning model...") model.fit_generator( augmentation.flow(train_x, train_y, batch_size=32), validation_data=(test_x, test_y), epochs=100, steps_per_epoch=len(train_x) // 32, verbose=1, ) # evaluate the network on the fine-tuned model print("[INFO] evaluating after fine-tuning...") predictions = model.predict(test_x, batch_size=32) print( classification_report(test_y.argmax(axis=1), predictions.argmax(axis=1), target_names=class_names)) # save the model to disk print("[INFO] serializing model...") model.save(args["model"])
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:
# 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)
if num != config.NUM_CLASSES: results_str += chars[num] return results_str ap = argparse.ArgumentParser() ap.add_argument("-m", "--model", help = "path to pre-trained model") ap.add_argument("-i", "--images", help = "path to image folder ") 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"]) iap = ImageToArrayPreprocessor() 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 paths = os.listdir(args["images"])
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()
# 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(), ' ',
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 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()
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
ap = argparse.ArgumentParser() ap.add_argument("-m", "--model", help="path to pre-trained model") ap.add_argument("-i", "--images", help="path to hdf5 image file ") 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')
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 else: # camera = VideoStream(src=args["video"], usePiCamera=False).start() # camera = VideoStream(src="./blackorwhite.mp4", usePiCamera=False).start()