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 main(): """Train a k-NN classifier. """ # 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("-k", "--neighbors", type=int, default=1, help="# of nearest neighbors for classification") args.add_argument( "-j", "--jobs", type=int, default=-1, help="# of jobs for k-NN distance (-1 uses all available cores)") 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 preprocessor, load the dataset from disk, # and reshape the data matrix preprocessor = SimplePreprocessor(32, 32) loader = SimpleDatasetLoader(preprocessors=[preprocessor]) (data, labels) = loader.load(image_paths, verbose=500) data = data.reshape((data.shape[0], 3072)) # show some information on memory consumption of the images print("[INFO] features matrix: {:.1f}MB".format(data.nbytes / (1024 * 1024.0))) # encode the labels as integers label_encoder = LabelEncoder() labels = label_encoder.fit_transform(labels) # 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) # train and evaluate a k-NN classifier on the raw pixel intensities print("[INFO] evaluating k-NN classifier...") model = KNeighborsClassifier(n_neighbors=args["neighbors"], n_jobs=args["jobs"]) model.fit(train_x, train_y) print( classification_report(test_y, model.predict(test_x), target_names=label_encoder.classes_))
def training(aug, means_path, train_hdf5_path, val_hdf5_path, fig_path, json_path, label_encoder_path, best_weight_path, checkpoint_path, cross_val=None): # load RGB means means = json.loads(open(means_path).read()) # initialize image preprocessors sp, mp, pp, iap = SimplePreprocessor(227, 227), MeanPreprocessor(means['R'], means['G'], means['B']), PatchPreprocessor(227, 227), ImageToArrayPreprocessor() # initialize training and validation image generator train_gen = HDF5DatasetGenerator(train_hdf5_path, config.BATCH_SIZE, preprocessors=[pp, mp, iap], aug=aug, classes=config.NUM_CLASSES) val_gen = HDF5DatasetGenerator(val_hdf5_path, config.BATCH_SIZE, preprocessors=[sp, mp, iap], aug=aug, classes=config.NUM_CLASSES) metrics = ['accuracy'] if config.DATASET_TYPE == 'age': le = pickle.loads(open(label_encoder_path, 'rb').read()) agh = AgeGenderHelper(config, deploy) one_off_mappings = agh.build_oneoff_mappings(le) one_off = OneOffAccuracy(one_off_mappings) metrics.append(one_off.one_off_accuracy) # construct callbacks callbacks = [TrainingMonitor(fig_path, json_path=json_path, start_at=args['start_epoch']), EpochCheckpoint(checkpoint_path, every=5, start_at=args['start_epoch']), ModelCheckpointsAdvanced(best_weight_path, json_path=json_path, start_at=args['start_epoch'])] #, LearningRateScheduler(decay) if cross_val is None: print('[INFO] compiling model...') else: print(f'[INFO] compiling model for cross validation {cross_val}...') if args['start_epoch'] == 0: if not os.path.exists(checkpoint_path): os.makedirs(checkpoint_path) model = AgeGenderNet.build(227, 227, 3, config.NUM_CLASSES, reg=5e-4) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=metrics) else: model_path = os.path.sep.join([checkpoint_path, f"epoch_{args['start_epoch']}.hdf5"]) print(f"[INFO] loading {model_path}...") if config.DATASET_TYPE == 'age': model = load_model(model_path, custom_objects={'one_off_accuracy': one_off.one_off_accuracy}) elif config.DATASET_TYPE == 'gender': model = load_model(model_path) # update learning rate print(f'[INFO] old learning rate: {K.get_value(model.optimizer.lr)}') K.set_value(model.optimizer.lr, INIT_LR) print(f'[INFO] new learning rate: {K.get_value(model.optimizer.lr)}') # train the network if cross_val is None: print('[INFO] training the network...') else: print(f'[INFO] training the network for cross validation {cross_val}...') model.fit_generator(train_gen.generator(), steps_per_epoch=train_gen.num_images//config.BATCH_SIZE, validation_data=val_gen.generator(), validation_steps=val_gen.num_images//config.BATCH_SIZE, epochs=MAX_EPOCH-args['start_epoch'], verbose=2, callbacks=callbacks) # close dataset train_gen.close() val_gen.close()
def main(): """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 main(): """Run various regularization techniques. """ # 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 image paths print("[INFO] loading images...") image_paths = list(paths.list_images(args["dataset"])) # initialize the image preprocessor, load the dataset from disk, # and reshape the data matrix preprocessor = SimplePreprocessor(32, 32) loader = SimpleDatasetLoader(preprocessors=[preprocessor]) (data, labels) = loader.load(image_paths, verbose=500) data = data.reshape((data.shape[0], 3072)) # encode the labels as integers label_encoder = LabelEncoder() labels = label_encoder.fit_transform(labels) # 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=5) # loop over our set of regularizers for regularizer in (None, "l1", "l2"): # train a SGD classifier using a softmax loss function and the # specified regularization function for 10 epochs print("[INFO] training model with `{}` penalty".format(regularizer)) model = SGDClassifier(loss="log", penalty=regularizer, max_iter=10, learning_rate="constant", tol=1e-3, eta0=0.01, random_state=42) model.fit(train_x, train_y) # evaluate the classifier acc = model.score(test_x, test_y) print("[INFO] `{}` penalty accuracy: {:.2f}%".format( regularizer, acc * 100))
classNames = [pt.split(os.path.sep)[-2] for pt in imagePaths] 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
from keras.optimizers import Adam import json 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)
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
import load_cell import RPi.GPIO as gpio sw = 7 gpio.setwarnings(False) gpio.setmode(gpio.BOARD) gpio.setup(sw,gpio.IN) # initialize the class labels classLabels = ["beans","brinjal","chilli","tomato"] #initialise the serial port port = serial.Serial("/dev/ttyUSB1",baudrate=9600, timeout=1) # initialize the image preprocessors sp = SimplePreprocessor(100, 100) iap = ImageToArrayPreprocessor() # load the pre-trained network print("[INFO] loading pre-trained network...") port.write(bytes(("loading pre-trained network..."+"\r\n"),'UTF-8')) model = load_model("veges.hdf5") # loop over the sample images while True: inp = gpio.input(sw) print("waiting for switch press") if(inp ==1): print("switch pressed") cap = cv2.VideoCapture(0)
# dimensions to define the corners of the image based 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
import cv2 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)
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(): """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()
age_mean = json.loads(open(deploy.AGE_MEAN).read()) gender_mean = json.loads(open(deploy.GENDER_MEAN).read()) # load model from disk 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
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") # 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()
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=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)
from pyimagesearch.io import HDF5DatasetGenerator 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()
from sklearn.metrics import f1_score # construct the training image generator for data augmentation aug = ImageDataGenerator(rotation_range=10, zoom_range=0.05, width_shift_range=0.05, height_shift_range=0.05, shear_range=0.05, horizontal_flip=True, fill_mode="nearest") # load the RGB means for the training set means = json.loads(open(config.DATASET_MEAN).read()) # initialize the image preprocessors sp = SimplePreprocessor(IMAGE_WIDTH, IMAGE_HEIGHT) pp = PatchPreprocessor(IMAGE_WIDTH, IMAGE_HEIGHT) mp = MeanPreprocessor(means["R"], means["G"], means["B"]) iap = ImageToArrayPreprocessor() # initialize the training and validation dataset generators if NUM_CLASSES: trainGen = HDF5DatasetGenerator(TRAIN_HDF5, config.BATCH_SIZE, aug=aug, preprocessors=[sp, mp, iap], classes=NUM_CLASSES) valGen = HDF5DatasetGenerator(config.TEST_HDF5, config.BATCH_SIZE, preprocessors=[sp, mp, iap], classes=NUM_CLASSES)
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()
# 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()
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 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)
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
# rank_accuracy.py 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...")
rotation_range=30, zoom_range=0.2, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.1, horizontal_flip=True, fill_mode="nearest") aug2 = ImageDataGenerator(preprocessing_function=xception.preprocess_input) # # open the HDF5 database for reading then determine the index of # # the training and testing split, provided that this data was # # already shuffled *prior* to writing it to disk # db = h5py.File(config.TRAIN_HDF5, "r") # print(db["label_names"], len(db["label_names"])) sp = SimplePreprocessor(299, 299) iap = ImageToArrayPreprocessor() # initialize the training and validation dataset generators trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, 32, aug=aug, preprocessors=[sp, iap], classes=config.NUM_CLASSES, set="train") valGen = HDF5DatasetGenerator(config.TRAIN_HDF5, 32, aug=aug2, preprocessors=[sp, iap], classes=config.NUM_CLASSES, set="val")
from pyimagesearch.datasets import SimpleDatasetLoader from imutils import paths import argparse # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-d", "--dataset", required=True, help="path to input dataset") args = vars(ap.parse_args()) # grab the list of image paths print("[INFO] loading images...") imgPaths = list(paths.list_images(args["dataset"])) # initialize the image preprocessor, load the dataset from disk, # and reshape the data matrix sp = SimplePreprocessor(32, 32) sdl = SimpleDatasetLoader(preprocessors=[sp]) (data, labels) = sdl.load(imagePaths=imgPaths, verbose=500) data = data.reshape((data.shape[0], 3072)) # encode the labels as integers le = LabelEncoder() labels = le.fit_transform(labels) # 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=5)
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()