Esempio n. 1
0
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()
Esempio n. 2
0
# 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)
else:
Esempio n. 3
0
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)

print("[INFO] compiling model...")
# grab the width and height of the image then use these
# 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
Esempio n. 5
0
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, )
Esempio n. 6
0
    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()
Esempio n. 7
0
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()
Esempio n. 8
0
imagePaths = list(paths.list_images(args["dataset"]))
classNames = [pt.split(os.path.sep)[-2] for pt in imagePaths]
classNames = [str(x) for x in np.unique(classNames)]
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)