Exemplo n.º 1
0
# construct the training image generator for data augmentation
aug = ImageDataGenerator(rotation_range=20, width_shift_range=0.2,
	height_shift_range=0.2, shear_range=0.15, zoom_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
sp = SimplePreprocessor(64, 64)
mp = MeanPreprocessor(means["R"], means["G"], means["B"])
iap = ImageToArrayPreprocessor()

# initialize the training and validation dataset generators
bs = 64
trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, batchSize=bs, aug=aug,
	preprocessors=[sp, mp, iap], classes=config.NUM_CLASSES)
valGen = HDF5DatasetGenerator(config.VAL_HDF5, batchSize=bs,
	preprocessors=[sp, mp, iap], 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...")
	opt = Adam(lr=1e-3)
	model = DeeperGoogLeNet.build(width=64, height=64, depth=3,
		classes=config.NUM_CLASSES, reg=0.0002)
	model.compile(loss="categorical_crossentropy", optimizer=opt,
		metrics=["accuracy"])
# otherwise, load the checkpoint from disk
else:
	print("[INFO] loading {}...".format(args["model"]))
Exemplo n.º 2
0
# 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 generators, then make predictions on
# the testing data
print("[INFO] predicting on test data (no crops)...")
bs = 8
testGen = HDF5DatasetGenerator(config.TEST_HDF5,
                               batchSize=bs,
                               preprocessors=[sp, mp, iap],
                               classes=2)
predictions = model.predict_generator(testGen.generator(),
                                      steps=testGen.numImages // bs,
                                      max_queue_size=bs * 2)

# compute the rank-1 and rank-5 accuracies
(rank1, _) = rank5_accuracy(predictions, testGen.db["labels"])
print("[INFO] rank-1: {:.2f}%".format(rank1 * 100))
testGen.close()

# re-initialzie the testing set generator excluding the SimplePreprocessor
testGen = HDF5DatasetGenerator(config.TEST_HDF5,
                               batchSize=bs,
                               preprocessors=[mp],
                               classes=2)
Exemplo n.º 3
0
args = vars(ap.parse_args())

# construct the training and testing image generators for data augmentation
# then initialize the image preprocessor
trainAug = ImageDataGenerator(rotation_range=10,
                              zoom_range=0.1,
                              horizontal_flip=True,
                              rescale=1 / 255.0,
                              fill_mode="nearest")
valAug = ImageDataGenerator(rescale=1 / 255.0)
iap = ImageToArrayPreprocessor()

# initialize the training and validation dataset generators
trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5,
                                config.BATCH_SIZE,
                                aug=trainAug,
                                preprocessors=[iap],
                                classes=config.NUM_CLASSES)
valGen = HDF5DatasetGenerator(config.VAL_HDF5,
                              config.BATCH_SIZE,
                              aug=valAug,
                              preprocessors=[iap],
                              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("Compiling model...")
    model = EmotionVGGNet.build(width=48,
                                height=48,
                                depth=1,
# --model is the path to the specific model checkpoint to load
ap = argparse.ArgumentParser()
ap.add_argument("-m",
                "--model",
                type=str,
                help="path to model checkpoint to load")
args = vars(ap.parse_args())

# initialize the testing data generator and image preprocessor
testAug = ImageDataGenerator(rescale=1 / 255.0)
iap = ImageToArrayPreprocessor()

# initialize the testing dataset generator
testGen = HDF5DatasetGenerator(config.TEST_HDF5,
                               config.BATCH_SIZE,
                               aug=testAug,
                               preprocessors=[iap],
                               classes=config.NUM_CLASSES)

# load the model from disk
print("Loading {}...".format(args["model"]))
model = load_model(args["model"])

# evaluate the network
(loss,
 acc) = model.evaluate_generator(testGen.generator(),
                                 steps=testGen.numImages // config.BATCH_SIZE,
                                 max_queue_size=config.BATCH_SIZE * 2)

print("Accuracy: {:.2f}".format(acc * 100))
                         zoom_range=0.15,
                         width_shift_range=0.2,
                         height_shift_range=0.2,
                         shear_range=0.15,
                         horizontal_flip=True,
                         fill_mode="nearest")
iap = ImageToArrayPreprocessor()
pp = PatchPreprocessor(227, 227)
sp = SimplePreprocessor(227, 227)
mp = MeanPreprocessor(means["R"], means["G"], means["B"])

#initialize the dataset generators
#initialize the dataset generators
trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5,
                                4,
                                aug=aug,
                                preprocessors=[pp, mp, iap],
                                classes=3)
valGen = HDF5DatasetGenerator(config.VAL_HDF5,
                              4,
                              aug=aug,
                              preprocessors=[sp, mp, iap],
                              classes=3)

#compile the model and do al the deep learning
optimizer = Adam(lr=1e-3)
model = AlexNet.build(width=227, height=227, depth=3, reg=0.0002, classes=3)
model.compile(loss="binary_crossentropy",
              optimizer=optimizer,
              metrics=["accuracy"])
Exemplo n.º 6
0
#importing the necessasry libnrariea
from config import dogs_vs_cats_config as config
from utilities.preprocessing import CropPreprocessor
from utilities.preprocessing import MeanPreprocessor, ImageToArrayPreprocessor
from utilities.io import HDF5DatasetGenerator
from sklearn.metrics import classification_report
from keras.models import load_model
import json
import numpy as np

#brings in from the meanPreprocessor
means = json.loads(open(config.DATASET_MEAN).read())
mp = MeanPreprocessor(means['R'], means['G'], means['B'])
iap = ImageToArrayPreprocessor(227, 227)
cp = CropPreprocessor(227, 227)
model = load_model(config.MODEL_PATH)

#making a generator obhject
testGen = HDF5DatasetGenerator(config.TEST_HDF5,
                               32,
                               preprocessors=[mp],
                               classes=2)
predictions = []
for (images, labels) in testGen.generator(passes=1):
    for image in images:
        crops = cp.preprocess(image)
        crops = np.array([iap.preprocess(c) for c in crops], dtype="float32")
        pred = model.predict(crops)
        predictions.append(pred.mean(axis=0))