def run_test_harness():
    # load dataset
    trainX, trainY, testX, testY = load_dataset()
    # create data generator
    train_datagen = ImageDataGenerator(featurewise_center=True,
                                       horizontal_flip=True,
                                       vertical_flip=True,
                                       rotation_range=90)
    test_datagen = ImageDataGenerator(featurewise_center=True)
    # specify imagenet mean values for centering
    train_datagen.mean = [123.68, 116.779, 103.939]
    test_datagen.mean = [123.68, 116.779, 103.939]
    # prepare iterators
    train_it = train_datagen.flow(trainX, trainY, batch_size=128)
    test_it = test_datagen.flow(testX, testY, batch_size=128)
    # define model
    model = define_model()
    # fit model
    history = model.fit_generator(train_it,
                                  steps_per_epoch=len(train_it),
                                  validation_data=test_it,
                                  validation_steps=len(test_it),
                                  epochs=50,
                                  verbose=0)
    # evaluate model
    loss, fbeta = model.evaluate_generator(test_it,
                                           steps=len(test_it),
                                           verbose=0)
    print('> loss=%.3f, fbeta=%.3f' % (loss, fbeta))
    # learning curves
    summarize_diagnostics(history)
Пример #2
0
def create_generators(conf, _Xtrain, _ytrain, _Xvalid, _yvalid):
    # Create Keras ImageDataGenerator
    aug_datagen = ImageDataGenerator(
        featurewise_center=conf['normalize'] == 'featurewise',
        featurewise_std_normalization=conf['normalize'] == 'featurewise',
        rotation_range=0,
        width_shift_range=0.4,
        height_shift_range=0.0,
        horizontal_flip=True,
        preprocessing_function=get_random_eraser(v_l=-1, v_h=1))
    plain_datagen = ImageDataGenerator(
        featurewise_center=aug_datagen.featurewise_center,
        featurewise_std_normalization=aug_datagen.
        featurewise_std_normalization,
    )
    # Set featurewise normalization mean/std
    if aug_datagen.featurewise_center:
        print(' normalize featurewise')
        aug_datagen.mean, aug_datagen.std = np.mean(_Xtrain), np.std(_Xtrain)
        plain_datagen.mean, plain_datagen.std = aug_datagen.mean, aug_datagen.std
    # Create Generators
    train_generator = MixupGenerator(_Xtrain,
                                     _ytrain,
                                     alpha=1.0,
                                     batch_size=conf['batch_size'],
                                     datagen=aug_datagen)()
    valid_generator = plain_datagen.flow(_Xvalid,
                                         _yvalid,
                                         batch_size=conf['batch_size'],
                                         shuffle=False)
    return train_generator, valid_generator, plain_datagen
def DataGenerator(train_batch, val_batch, IMG_SIZE):
    datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
                                 rescale=1.0/255.0,
                                 rotation_range=10,
                                 horizontal_flip=True,
                                 vertical_flip=False)

    datagen.mean=np.array([103.939, 116.779, 123.68],dtype=np.float32).reshape(1,1,3)

    train_gen = datagen.flow_from_directory('E:\cov\covtrain',
                                            target_size=(IMG_SIZE, IMG_SIZE),
                                            color_mode='rgb', 
                                            class_mode='categorical',
                                            batch_size=train_batch)

    val_gen = datagen.flow_from_directory('E:\cov\covval', 
                                          target_size=(IMG_SIZE, IMG_SIZE),
                                          color_mode='rgb', 
                                          class_mode='categorical',
                                          batch_size=val_batch)

    datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
                                 rescale=1.0/255.0)
    
    datagen.mean=np.array([103.939, 116.779, 123.68],dtype=np.float32).reshape(1,1,3)

    test_gen = datagen.flow_from_directory('E:\cov\covtest', 
                                           target_size=(IMG_SIZE, IMG_SIZE),
                                           color_mode='rgb', 
                                           batch_size=1,
                                           class_mode='categorical',
                                           shuffle=False)
    
    return train_gen, val_gen, test_gen
Пример #4
0
def create_image_gen(HEIGHT, WIDTH, CHANNEL):
    # derive the paths to the training, validation, and testing directories
    trainImagePath = os.path.sep.join([BASE_PATH, BASE_IMAGE_TYPE, TRAIN])
    validImagePath = os.path.sep.join([BASE_PATH, BASE_IMAGE_TYPE, VAL])
    testImagePath = os.path.sep.join([BASE_PATH, BASE_IMAGE_TYPE, TEST])

    # determine the total number of image paths in training, validation and testing directories
    totalTrain = len(list(paths.list_images(trainImagePath)))
    totalVal = len(list(paths.list_images(validImagePath)))
    totalTest = len(list(paths.list_images(testImagePath)))

    # initialize the training data augmentation object
    trainImageAug = ImageDataGenerator(
        rotation_range=30,
        zoom_range=0.15,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.15,
        horizontal_flip=True,
        fill_mode="nearest")
    # initialize the validation/testing data augmentation object (which we'll be adding mean subtraction to)
    valImageAug = ImageDataGenerator()
    # define the ImageNet mean subtraction (in RGB order) and set the
    # the mean subtraction value for each of the data augmentation
    # objects
    mean = np.array([123.68, 116.779, 103.939], dtype="float32")
    trainImageAug.mean = mean
    valImageAug.mean = mean

    TARGET_SIZE = (HEIGHT, WIDTH)
    # initialize the training generator
    trainImageGen = trainImageAug.flow_from_directory(
        trainImagePath,
        class_mode="categorical",
        target_size=TARGET_SIZE,
        color_mode="rgb",
        shuffle=True,
        batch_size=BATCH_SIZE)

    # initialize the validation generator
    valImageGen = valImageAug.flow_from_directory(
        validImagePath,
        class_mode="categorical",
        target_size=TARGET_SIZE,
        color_mode="rgb",
        shuffle=False,
        batch_size=BATCH_SIZE)

    # initialize the testing generator
    testImageGen = valImageAug.flow_from_directory(
        testImagePath,
        class_mode="categorical",
        target_size=TARGET_SIZE,
        color_mode="rgb",
        shuffle=False,
        batch_size=BATCH_SIZE)
    return (trainImageGen, valImageGen, testImageGen), (totalTrain, totalVal, totalTest)
Пример #5
0
def get_generator():
    datagen_args = dict(
        data_format='channels_last',
        # set input mean to 0 over the dataset
        featurewise_center=True,
        # set each sample mean to 0
        samplewise_center=False,
        # divide inputs by std of dataset
        featurewise_std_normalization=True,
        # divide each input by its std
        samplewise_std_normalization=False,
        # apply ZCA whitening
        zca_whitening=False,
        # randomly rotate images in the range (deg 0 to 180)
        rotation_range=0,
        # randomly shift images horizontally
        width_shift_range=0,
        # randomly shift images vertically
        height_shift_range=0,
        # randomly flip images
        horizontal_flip=False,
        # randomly flip images
        vertical_flip=False,

        rescale=1. / 255)

    image_datagen = ImageDataGenerator(**datagen_args)
    image_datagen.mean = np.array([[[0.36654497, 0.35386439, 0.30782658]]])
    image_datagen.std = np.array([[[0.19212837, 0.19031791, 0.18903286]]])
    return image_datagen
Пример #6
0
def run_test_harness():
    model = define_model()
    # create data generator
    datagen = ImageDataGenerator(featurewise_center=True)
    # specify imagenet mean values for centering
    '''
	The model also expects images to be centered. 
	That is, to have the mean pixel values from each channel (red, green, and blue) as calculated on the ImageNet training dataset subtracted from the input. 
	Keras provides a function to perform this preparation for individual photos via the preprocess_input() function. 
	Nevertheless, we can achieve the same effect with the ImageDataGenerator by setting the “featurewise_center” argument to “True” 
	and manually specifying the mean pixel values to use when centering as the mean values from the ImageNet training dataset: [123.68, 116.779, 103.939]
	'''
    datagen.mean = [123.68, 116.779, 103.939]

    # prepare iterators
    train_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',
                                           class_mode='binary',
                                           batch_size=64,
                                           target_size=(224, 224))
    test_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',
                                          class_mode='binary',
                                          batch_size=64,
                                          target_size=(224, 224))
    # fit model
    history = model.fit_generator(train_it,
                                  steps_per_epoch=len(train_it),
                                  validation_data=test_it,
                                  validation_steps=len(test_it),
                                  epochs=10,
                                  verbose=1)
    _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=1)
    print('> %.3f' % (acc * 100.0))
    summarize_diagnostics(history)
Пример #7
0
def predict(model, path_img):
    datagen = ImageDataGenerator(
        rescale=1. / 255,
        featurewise_center=True,
        featurewise_std_normalization=True)
    img_height, img_width = 224, 224
    batch_size = 40
    datagen.mean = np.array([0.485, 0.456, 0.406])
    datagen.std = np.array([0.229, 0.224, 0.225])

    datagen_generator = datagen.flow_from_directory(
        path_img,
        shuffle=False,
        color_mode="rgb",
        target_size=(img_height, img_width),
        batch_size=batch_size,
        class_mode='categorical',
        subset='training')  # set as training data

    Y_pred = model.predict(datagen_generator)
    y_pred = np.argmax(Y_pred, axis=1)

    TEST_DIR = Path(path_img)
    test_files = sorted(list(TEST_DIR.rglob('*.*')))

    data_file = pd.DataFrame(test_files)
    data_file['class'] = y_pred
    return data_file
def save_bottlebeck_features():
    # This base_model is trained on fish detection, we assume that the convolutional features
    # will be more relevant

    base_model_1 = ResNet50(include_top=False, weights='imagenet', input_shape=(3, img_width, img_height))
    base_model = Model(input=base_model_1.input, output=base_model_1.layers[-2].output)
    #print(base_model.summary())

    datagen = ImageDataGenerator(rescale=1., featurewise_center=True)  # (
    datagen.mean = np.array(resnet50_data_mean, dtype=np.float32).reshape(3, 1, 1)
    # generate valid first
    generator = datagen.flow_from_directory(
            validation_data_dir,
            target_size=(img_width, img_height),
            batch_size=32,
            class_mode='categorical',
            shuffle=False)
    bottleneck_features_validation = base_model.predict_generator(generator, nb_validation_samples)
    np.save(open(os.path.join(exp_dir_path, 'bottleneck_features_validation_resnet50_no_enhancement.npy'), 'w'), bottleneck_features_validation)

    # generate train
    generator = datagen.flow_from_directory(
            train_data_dir,
            target_size=(img_width, img_height),
            batch_size=32,
            class_mode='categorical',
            shuffle=False)

    bottleneck_features_train = base_model.predict_generator(generator, nb_train_samples)
    np.save(open(os.path.join(exp_dir_path, 'bottleneck_features_train_resnet50_no_enhancement.npy'), 'w'), bottleneck_features_train)
Пример #9
0
def get_mnist_generator(batch_size=32,
                        zero_mean=False,
                        unit_variance=False,
                        horizontal_flip=True,
                        vertical_flip=False,
                        rotation_range=10,
                        height_shift_range=0,
                        width_shift_range=0.25):
    (X_train, Y_train), (X_val, Y_val), (X_test, Y_test) = get_mnist()

    train_gen = ImageDataGenerator(featurewise_center=zero_mean,
                                   featurewise_std_normalization=unit_variance,
                                   width_shift_range=width_shift_range,
                                   height_shift_range=height_shift_range,
                                   rotation_range=rotation_range,
                                   horizontal_flip=horizontal_flip,
                                   vertical_flip=vertical_flip)
    train_gen.fit(X_train, seed=1234)

    valtest_gen = ImageDataGenerator(
        featurewise_center=zero_mean,
        featurewise_std_normalization=unit_variance)
    valtest_gen.mean = train_gen.mean
    valtest_gen.std = train_gen.std

    train_gen = train_gen.flow(X_train,
                               Y_train,
                               batch_size=batch_size,
                               seed=999)
    val_gen = valtest_gen.flow(X_val, Y_val, batch_size=batch_size, seed=888)
    test_gen = valtest_gen.flow(X_test,
                                Y_test,
                                batch_size=batch_size,
                                seed=777)
    return train_gen, val_gen, test_gen, (valtest_gen.mean, valtest_gen.std)
Пример #10
0
def run_test_harness():
    # define model
    model = define_model()
    # create data generator
    datagen = ImageDataGenerator(
        featurewise_center=True)  # antes: ImageDataGenerator(rescale=1.0/255.0, -> primer modelo
    # width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True)
    # specify imagenet mean values for centering
    datagen.mean = [123.68, 116.779, 103.939]
    # specify imagenet mean values for centering
    # datagen.mean = [123.68, 116.779, 103.939]
    # prepare iterator
    train_it = datagen.flow_from_directory('dataset_pearls_others/train/',
                                           class_mode='binary', batch_size=62, target_size=(224, 224))
    test_it = datagen.flow_from_directory('dataset_pearls_others/test/',
                                          class_mode='binary', batch_size=31, target_size=(224, 224))
    ## bonito
    earlyStopping = EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='min')
    mcp_save = ModelCheckpoint('.mdl_wts.hdf5', save_best_only=True, monitor='val_loss', mode='min')
    reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=7, verbose=1, epsilon=1e-4, mode='min')

    # fit model
    history = model.fit_generator(train_it, steps_per_epoch=len(train_it),
                                  validation_data=test_it, validation_steps=len(test_it), epochs=2, verbose=1, callbacks = [earlyStopping, mcp_save, reduce_lr_loss])

    # save model
    model.save('model_09_12_pearls_others.h5')
    # evaluate model
    _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0)
    print('> %.3f' % (acc * 100.0))
    # learning curves
    summarize_diagnostics(history)
Пример #11
0
def run_test_Transfer(model_x, f_name):
    # define model
    model = model_x()
    # create data generator
    datagen = ImageDataGenerator(featurewise_center=True)
    # specify imagenet mean values for centering
    datagen.mean = [123.68, 116.779, 103.939]
    # prepare iterator
    train_it = datagen.flow_from_directory('Images/Train/',
                                           class_mode='binary',
                                           target_size=(200, 200))
    test_it = datagen.flow_from_directory('Images/Test/',
                                          class_mode='binary',
                                          target_size=(200, 200))
    # fit model
    history = model.fit_generator(train_it,
                                  steps_per_epoch=len(train_it),
                                  validation_data=test_it,
                                  validation_steps=len(test_it),
                                  epochs=5,
                                  verbose=1)
    # evaluate model
    _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0)
    print('> %.3f' % (acc * 100.0))
    # learning curves
    summarize_diagnostics(history, f_name)
Пример #12
0
def run_test_harness():
    # define model
    model = define_model()
    # create data generator
    datagen = ImageDataGenerator(featurewise_center=True)
    # specify imagenet mean values for centering
    datagen.mean = [123.68, 116.779, 103.939]
    # # prepare iterator
    # train_it = datagen.flow_from_directory('D:\\UDAY\\ML_API\\dataset_dogs_vs_cats\\train\\',
    #                                        class_mode='binary', batch_size=64, target_size=(224, 224))
    # test_it = datagen.flow_from_directory('D:\\UDAY\\ML_API\\dataset_dogs_vs_cats\\test\\',
    #                                       class_mode='binary', batch_size=64, target_size=(224, 224))
    # print(train_it)
    # # fit model
    # history = model.fit_generator(train_it, steps_per_epoch=len(train_it),
    #                               validation_data=test_it, validation_steps=len(test_it), epochs=10, verbose=1)
    # # evaluate model
    # _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0)
    # print('> %.3f' % (acc * 100.0))
    # # learning curves
    # summarize_diagnostics(history)

    #final_model
    train_it = datagen.flow_from_directory(
        'D:\\UDAY\\ML_API\\finalize_dogs_vs_cats\\',
        class_mode='binary',
        batch_size=64,
        target_size=(224, 224))
    model.fit_generator(train_it, steps_per_epoch=20, epochs=5, verbose=1)
    model.save('D:\\UDAY\\ML_API\\final_model.h5')
Пример #13
0
def extract_bottleneck_feature():
    datagen = ImageDataGenerator(
        rescale=1.,
        featurewise_center=True,
    )

    datagen.mean = np.array(resnet_mean, dtype=np.float32).reshape(3, 1, 1)

    train_generator = datagen.flow_from_directory(train_root,
                                                  target_size=image_size,
                                                  batch_size=24,
                                                  class_mode='binary',
                                                  shuffle=False)

    validate_generator = datagen.flow_from_directory(validate_root,
                                                     target_size=image_size,
                                                     batch_size=24,
                                                     class_mode='binary',
                                                     shuffle=False)

    base_model = ResNet50(include_top=False, weights='imagenet')
    feature_model = Model(input=base_model.input,
                          output=base_model.layers[-2].output)

    bottleneck_train_feature = feature_model.predict_generator(
        train_generator, train_samples)
    np.save(open('bottleneck_train_feature.npy', 'w'),
            bottleneck_train_feature)

    bottleneck_validate_feature = feature_model.predict_generator(
        validate_generator, validate_samples)
    np.save(open('bottleneck_validate_feature.npy', 'w'),
            bottleneck_validate_feature)
Пример #14
0
def run_test_harness_vgg16(model):
  datagen = ImageDataGenerator(featurewise_center= True)
  datagen.mean = [123.68, 116.779, 103.939]

  print('\nInitializing Training Data Generator\n')
  train_data_gen = datagen.flow_from_directory('train',
                                               class_mode= 'binary',
                                               batch_size= 64,
                                               target_size= (224,224))
  
  print('\nInitializing Testing Data Generator\n')
  test_data_gen = datagen.flow_from_directory('test',
                                              class_mode= 'binary',
                                              batch_size= 64,
                                              target_size= (224,224))
  
  print('\nModel Fitting\n')
  history = model.fit_generator(train_data_gen,
                                steps_per_epoch= len(train_data_gen),
                                validation_data = test_data_gen,
                                validation_steps= len(test_data_gen),
                                epochs= 10
                                )
  
  print('\nEvaluating Model\n')
  _, acc = model.evaluate_generator(test_data_gen,
                                    steps= len(test_data_gen)
                                    )
  
  print('>%.3f'%(acc*100))

  return history
Пример #15
0
def run_test_harness(epochs, fit_generator_verbose, evaluate_generator_verbose):
	# define model
	#fit_generator_verbose = 1
	#evaluate_generator_verbose = 1
	#model = define_model_vgg16_transfer()
	model = transfer_learn_mbv1()

	datagen = ImageDataGenerator(featurewise_center=True)
	# specify imagenet mean values for centering
	datagen.mean = [123.68, 116.779, 103.939]
	batch_size = 64 # - original
	#batch_size = 128
	train_it = datagen.flow_from_directory(processed_dataset_home + 'train/',
			class_mode='binary', batch_size=batch_size, target_size=(224, 224))
	test_it = datagen.flow_from_directory(processed_dataset_home + 'test/',
			class_mode='binary', batch_size=batch_size, target_size=(224, 224))

	# original epochs = 1
	history = model.fit_generator(train_it, steps_per_epoch=len(train_it),
		validation_data=test_it, validation_steps=len(test_it), epochs=epochs, verbose=fit_generator_verbose) # epochs=10

	#model.save(f"final_model.h5") # not yet, I guess

	# evaluate model
	_, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=evaluate_generator_verbose)
	print('> %.3f' % (acc * 100.0))
	# learning curves
	summarize_diagnostics(history)
Пример #16
0
def run_test_harness():
    # define model
    model = define_model()
    # create data generator
    datagen = ImageDataGenerator(featurewise_center=True)
    # specify imagenet mean values for centering
    datagen.mean = [123.68, 116.779, 103.939]
    # prepare iterator
    train_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',
                                           class_mode='binary',
                                           batch_size=64,
                                           target_size=(224, 224))
    test_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',
                                          class_mode='binary',
                                          batch_size=64,
                                          target_size=(224, 224))
    # fit model
    history = model.fit_generator(train_it,
                                  steps_per_epoch=len(train_it),
                                  validation_data=test_it,
                                  validation_steps=len(test_it),
                                  epochs=10,
                                  verbose=1)
    # evaluate model
    _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0)
    print('> %.3f' % (acc * 100.0))
    # learning curves
    summarize_diagnostics(history)
def save_bottlebeck_features():
    #datagen = ImageDataGenerator(rescale=1. / 255)
    datagen = ImageDataGenerator(rescale=1., featurewise_center=True) #(rescale=1./255)
    datagen.mean=np.array([103.939, 116.779, 123.68],dtype=np.float32).reshape(1,1,3)

    # build the ResNet50 network
    model = applications.ResNet50(include_top=False, pooling = 'avg', weights='imagenet')
    #model = applications.(include_top=False, weights='imagenet')

    generator = datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode=None,
        shuffle=False)
    bottleneck_features_train = model.predict_generator(
        generator, nb_train_samples // batch_size)
    np.save(open('bottleneck_features_train.npy', 'wb'),
            bottleneck_features_train)

    generator = datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode=None,
        shuffle=False)
    bottleneck_features_validation = model.predict_generator(
        generator, nb_validation_samples // batch_size)
    np.save(open('bottleneck_features_validation.npy', 'wb'),
            bottleneck_features_validation)
    #model.summary()
    plot_model(model, show_shapes = True,to_file='Model_ResNet_notop.png')
Пример #18
0
def trainCNNAug():
    model = buildModel()

    json_string = model.to_json()
    f = open('model2.json', 'w')
    f.write(json_string)
    f.close()

    trainX = np.load('data\\trainX.npy')
    trainY = np.load('data\\trainY.npy')
    validX = np.load('data\\validX.npy')
    validY = np.load('data\\validY.npy')

    mu = np.mean(trainX, axis=0)
    np.save('mu.npy', mu)

    validX -= mu
    print('loaded')

    datagen = ImageDataGenerator(horizontal_flip=True, featurewise_center=True)
    datagen.mean = mu

    ckpoint = ModelCheckpoint('model2.{epoch:02d}.hdf5',
                              monitor='val_acc',
                              save_best_only=True,
                              verbose=1)
    history = model.fit_generator(datagen.flow(trainX, trainY, batch_size=128),
                                  samples_per_epoch=len(trainX),
                                  nb_epoch=300,
                                  validation_data=(validX, validY),
                                  callbacks=[ckpoint])
    np.save('acc2.npy', np.array(history.history['acc']))
    np.save('val_acc2.npy', np.array(history.history['val_acc']))
Пример #19
0
def db_test_predict():
    from applications_train import get_img_fit_flow
    center = True
    model_file = "/home/gcgic/Downloads/fine_tuning_resnet50_best_lrate0.001_bsize64_epochs1000_1536810325.2233403.h5"
    dataset = "/home/gcgic/Documents/Hemerson/Datasets/UCF11/UCF11_sampled_from_101_VR_Gaussian_H_RGB_sz_99_sg_33_split1_augmented_1/valid"

    model = load_model(model_file)

    if center:
        flow_dir = {'directory': os.path.join(os.path.split(dataset)[0], 'training'), 'target_size': (224, 224),
                'batch_size': 25, 'class_mode': 'categorical'}
        data_aug = {'featurewise_center': True, 'featurewise_std_normalization': True}
        _, stdev, mean = get_img_fit_flow(data_aug, 1, flow_dir)
        datagen = ImageDataGenerator(featurewise_center = True,
                        featurewise_std_normalization = True)
        datagen.mean = mean
        datagen.std = stdev
        tta = TTA_Model(model, 1, mean = mean, std = stdev)
    else:
        datagen = ImageDataGenerator(rescale=1./255)
        tta = TTA_Model(model, 1)


    preds_tta = tta.predict(dataset)

    print("Accuracy using TTA: {}".format(preds_tta))

    generator = datagen.flow_from_directory(dataset, target_size = (224, 224), color_mode='rgb', batch_size = 16, class_mode = 'categorical', shuffle=False)

    model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])

    samples = sum([len(files) for r, d, files in os.walk(dataset)])
    score = model.evaluate_generator(generator, samples // 16 + 1)
    print("Metrics names:       {}".format(model.metrics_names))
    print("Score from evaluate: {}".format(score))
Пример #20
0
def getDataGenerator(datadir, meanstddir, batchSize, shuffle):
    dataGenerator = ImageDataGenerator(featurewise_center=True,
                                       featurewise_std_normalization=True,
                                       horizontal_flip=True)

    mean = np.load(meanstddir + '/mean.npy')
    std = np.load(meanstddir + '/std.npy')

    nImages = 0
    classes = []
    for subdir in sorted(os.listdir(datadir)):
        if os.path.isdir(os.path.join(datadir, subdir)):
            classes.append(subdir)
            nImages += len(os.listdir(os.path.join(datadir, subdir)))

    # need to swap channel
    mean[0, 0, :] = mean[0, 0, ::-1]
    std[0, 0, :] = mean[0, 0, ::-1]

    dataGenerator.mean = mean
    dataGenerator.std = std

    generator = dataGenerator.flow_from_directory(
        datadir,
        target_size=(55, 47),
        batch_size=batchSize,
        shuffle=shuffle,
        class_mode='sparse',
        classes=classes,
        seed=np.random.randint(100000))
    return generator, nImages
Пример #21
0
def run_test():
    model = define_model()
    datagen = ImageDataGenerator(featurewise_center=True)
    datagen.mean = [123.68, 116.779, 103.939]
    #iterators
    train_it = datagen.flow_from_directory(dir + '\\Dataset\\train\\',
                                           class_mode='binary',
                                           batch_size=64,
                                           target_size=(200, 200))
    test_it = datagen.flow_from_directory(dir + '\\Dataset\\test\\',
                                          class_mode='binary',
                                          batch_size=64,
                                          target_size=(200, 200))
    #fit model
    hist = model.fit(train_it,
                     steps_per_epoch=len(train_it),
                     validation_data=test_it,
                     validation_steps=len(test_it),
                     epochs=20,
                     verbose=0)
    #evaluate model
    _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0)
    print('> %.3f' % (acc * 100.0))
    plot_learning_curves(hist)
    model.save('final_model.h5')
Пример #22
0
def run_test_harness():
    # define model
    model = define_model()
    # create data generator
    datagen = ImageDataGenerator(featurewise_center=True)
    # specify imagenet mean values for centering
    datagen.mean = [123.68, 116.779, 103.939]
    # prepare iterator
    train_it = datagen.flow_from_directory(
        'D:\\Documents\\Academics\\semester VI\\ES 654- ML\\assi 3\\assignment-3-alianas23\\dataset\\train\\train\\',
        class_mode='binary',
        batch_size=2,
        target_size=(224, 224))
    test_it = datagen.flow_from_directory(
        'D:\\Documents\\Academics\\semester VI\\ES 654- ML\\assi 3\\assignment-3-alianas23\\dataset\\train\\test\\',
        class_mode='binary',
        batch_size=2,
        target_size=(224, 224))
    # fit model
    history = model.fit_generator(train_it,
                                  steps_per_epoch=len(train_it),
                                  validation_data=test_it,
                                  validation_steps=len(test_it),
                                  epochs=10,
                                  verbose=1)
    # evaluate model
    _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0)
    print('> %.3f' % (acc * 100.0))
    # learning curves
    summarize_diagnostics(history)
def save_bottlebeck_features_no_pooling():
    # Ideally we want use original VGG image prepossessing mean and no scale
    datagen = ImageDataGenerator(rescale=1., featurewise_center=True)  # (rescale=1./255)
    datagen.mean = np.array(resnet50_data_mean, dtype=np.float32).reshape(3, 1, 1)

    base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(3, img_width, img_height))
    base_model_1 = Model(input=base_model.input, output=base_model.layers[-2].output)

    # add the model on top of the convolutional base
    extract_model = Sequential()
    extract_model.add(base_model_1)

    generator = datagen.flow_from_directory(
            train_data_dir,
            target_size=(img_width, img_height),
            batch_size=32,
            class_mode='binary',
            shuffle=False)

    bottleneck_features_train = extract_model.predict_generator(generator, nb_train_samples)
    np.save(open(os.path.join(exp_dir_path, 'training', 'bottleneck_features_train_resnet50_no_pooling_180.npy'), 'w'), bottleneck_features_train)

    generator = datagen.flow_from_directory(
            validation_data_dir,
            target_size=(img_width, img_height),
            batch_size=32,
            class_mode='binary',
            shuffle=False)
    bottleneck_features_validation = extract_model.predict_generator(generator, nb_validation_samples)
    np.save(open(os.path.join(exp_dir_path, 'training', 'bottleneck_features_validation_resnet50_no_pooling_180.npy'), 'w'), bottleneck_features_validation)
Пример #24
0
def train_top_model():

    train_data = np.load(
        open(
            os.path.join(
                exp_dir_path,
                'bottleneck_features_train_resnet50_no_enhancement_avgpool.npy'
            )))
    validation_data = np.load(
        open(
            os.path.join(
                exp_dir_path,
                'bottleneck_features_validation_resnet50_no_enhancement_avgpool.npy'
            )))

    datagen = ImageDataGenerator(rescale=1., featurewise_center=True)
    datagen.mean = np.array(resnet50_data_mean,
                            dtype=np.float32).reshape(3, 1, 1)
    generator = datagen.flow_from_directory(train_data_dir,
                                            target_size=(img_width,
                                                         img_height),
                                            batch_size=32,
                                            class_mode='categorical',
                                            shuffle=False)
    train_labels = np_utils.to_categorical(generator.classes, n_out)

    generator = datagen.flow_from_directory(validation_data_dir,
                                            target_size=(img_width,
                                                         img_height),
                                            batch_size=32,
                                            class_mode='categorical',
                                            shuffle=False)
    validation_labels = np_utils.to_categorical(generator.classes, n_out)

    model = Sequential()
    model.add(Flatten(input_shape=train_data.shape[1:]))
    model.add(Dense(n_out, activation='softmax'))

    # model.compile(optimizer='rmsprop',
    #               loss='categorical_crossentropy',
    #               metrics=['accuracy', 'categorical_crossentropy'])

    lr_list = [0.001 * 0.1**(x) for x in range(3)]
    for lr in lr_list:
        print('lr: %.5f' % lr)
        model.compile(optimizer=optimizers.SGD(lr=lr, momentum=0.9),
                      loss='categorical_crossentropy',
                      metrics=['accuracy', 'categorical_crossentropy'])
        model.fit(train_data,
                  train_labels,
                  nb_epoch=nb_epoch * 10,
                  batch_size=64,
                  class_weight='auto',
                  validation_data=(validation_data, validation_labels))
    # # it's better to save the model...
    model.save_weights(
        os.path.join(exp_dir_path,
                     'bottleneck_fc_model_no_enhancement_avgpool.h5'))
    return model
Пример #25
0
    def image_dataGen(self,
                      directory,
                      target_size,
                      batch_size,
                      data_augmentation=False):
        '''
        产生batch
        :param directory: 'train','validate','test'文件夹路径
        :param batch_size:
        :param target_size: 图像大小
        :return:
        '''

        if data_augmentation:
            # use imgaug instead
            datagen = ImageDataGenerator(
                rescale=1.0 / 255,
                # rotation_range=10,
                # width_shift_range=0.2,
                # height_shift_range=0.2,
                # shear_range=0.1,
                # zoom_range=0.1,
                # horizontal_flip=True,
                fill_mode='nearest',
                preprocessing_function=augment_callback,
                featurewise_std_normalization=True,
                featurewise_center=True)
            # rescaling is before normalization
            datagen.mean = np.array([0.485, 0.456, 0.406])
            datagen.std = np.array([0.229, 0.224, 0.225])
        else:
            datagen = ImageDataGenerator(rescale=1.0 / 255,
                                         featurewise_std_normalization=True,
                                         featurewise_center=True)
            datagen.mean = np.array([0.485, 0.456, 0.406])
            datagen.std = np.array([0.229, 0.224, 0.225])
        data_generator = datagen.flow_from_directory(
            directory,
            classes=self.
            labels,  # in order to mark self.labels[i] as one-hot(i), instead of a random combination
            target_size=target_size,
            batch_size=batch_size,
            class_mode=self.label_types)
        return data_generator
Пример #26
0
def get_feature(model, queries, db):
    img_size = (224, 224)
    test_path = DATASET_PATH + '/test/test_data'

    mean = np.array([144.62598745, 132.1989693, 119.10957842],
                    dtype=np.float32).reshape((1, 1, 3)) / 255.0
    std = np.array([5.71350834, 7.67297079, 8.68071288],
                   dtype=np.float32).reshape((1, 1, 3)) / 255.0

    intermediate_layer_model = Model(inputs=model.layers[0].input,
                                     outputs=model.layers[-1].output)
    test_datagen = ImageDataGenerator(rescale=1. / 255,
                                      dtype='float32',
                                      featurewise_std_normalization=True,
                                      featurewise_center=True)

    query_generator = test_datagen.flow_from_directory(directory=test_path,
                                                       target_size=(224, 224),
                                                       classes=['query'],
                                                       color_mode="rgb",
                                                       batch_size=64,
                                                       class_mode=None,
                                                       shuffle=False)
    test_datagen.mean = mean
    test_datagen.std = std

    query_vecs = intermediate_layer_model.predict_generator(
        query_generator, steps=len(query_generator), verbose=1)

    reference_generator = test_datagen.flow_from_directory(
        directory=test_path,
        target_size=(224, 224),
        classes=['reference'],
        color_mode="rgb",
        batch_size=64,
        class_mode=None,
        shuffle=False)
    test_datagen.mean = mean
    test_datagen.std = std

    reference_vecs = intermediate_layer_model.predict_generator(
        reference_generator, steps=len(reference_generator), verbose=1)

    return queries, query_vecs, db, reference_vecs
Пример #27
0
def generate_data(train_path, valid_path):
    # generate & augment training data
    train_datagen = ImageDataGenerator(rotation_range=30.,
                                       shear_range=0.2,
                                       zoom_range=0.2,
                                       horizontal_flip=True)
    train_datagen.mean = np.array([123.675, 116.28, 103.53],
                                  dtype=np.float32).reshape((3, 1, 1))
    train_data = train_datagen.flow_from_directory(train_path,
                                                   target_size=img_size,
                                                   classes=None)
    # generate training data
    valid_datagen = ImageDataGenerator()
    valid_datagen.mean = np.array([123.675, 116.28, 103.53],
                                  dtype=np.float32).reshape((3, 1, 1))
    valid_data = train_datagen.flow_from_directory(valid_path,
                                                   target_size=img_size,
                                                   classes=None)
    return train_data, valid_data
Пример #28
0
def run_finalize_harness(epochs, fit_generator_verbose):
	#model = define_model_vgg16_transfer()
	model = transfer_learn_mbv1()
	datagen = ImageDataGenerator(featurewise_center=True)
	datagen.mean = [123.68, 116.779, 103.939]
	finalize_it = datagen.flow_from_directory(processed_dataset_home + 'finalize/',
		class_mode='binary', batch_size=64, target_size=(224, 224))
	# epochs = 10
	model.fit_generator(finalize_it, steps_per_epoch=len(finalize_it), epochs=epochs, verbose=fit_generator_verbose)
	model.save(f"final_model.h5") # 82 mb
Пример #29
0
def save_bottlebeck_features():
    # This base_model is trained on fish detection, we assume that the convolutional features
    # will be more relevant
    model = get_model_resnet50_fully_connected_no_pooling()
    model.load_weights(
        './exp_dir/fish_localise/training/fine_tune_all_conv_resnet50.h5')
    base_model_1 = Model(input=model.input, output=model.layers[-2].output)

    # build a classifier model to put on top of the convolutional model
    top_model = Sequential()
    top_model.add(
        AveragePooling2D((7, 7), name='avg_pool', input_shape=(2048, 7, 7)))
    base_model = Sequential()
    base_model.add(base_model_1)
    base_model.add(top_model)
    base_model.add(Flatten())
    print(base_model.summary())

    datagen = ImageDataGenerator(rescale=1., featurewise_center=True)  # (
    datagen.mean = np.array(resnet50_data_mean,
                            dtype=np.float32).reshape(3, 1, 1)
    # rescale=1./255)
    # generate valid first
    generator = datagen.flow_from_directory(validation_data_dir,
                                            target_size=(img_width,
                                                         img_height),
                                            batch_size=32,
                                            class_mode='categorical',
                                            shuffle=False)
    bottleneck_features_validation = base_model.predict_generator(
        generator, nb_validation_samples)
    np.save(
        open(
            os.path.join(exp_dir_path,
                         'bottleneck_features_validation_resnet50.npy'), 'w'),
        bottleneck_features_validation)

    # generate train
    generator = datagen.flow_from_directory(train_data_dir,
                                            target_size=(img_width,
                                                         img_height),
                                            batch_size=32,
                                            class_mode='categorical',
                                            shuffle=False)

    bottleneck_features_train = base_model.predict_generator(
        generator, nb_train_samples)
    np.save(
        open(
            os.path.join(exp_dir_path,
                         'bottleneck_features_train_resnet50.npy'), 'w'),
        bottleneck_features_train)
Пример #30
0
    def generate_dataflow(self, image_size=(224, 224)):

        for it in config.configured_dirs:
            idg = ImageDataGenerator()

            #Remove the mean operation, just using original images
            idg.mean = np.array([103.939, 116.779, 123.68],
                                dtype=np.float32).reshape((3, 1, 1))

            self.__dataflow[it] = idg.flow_from_directory(
                self.get_dir_path(it),
                target_size=image_size,
                classes=config.configured_classes)