0

I'm new to keras and deep learning. I'have tried to use data augmentation for for training my model, but not sure if i'm doing it the right way. Can anyone assure me it my approach is correct? here is my code:

train_path = 'Digital_Mamo/OPTIMAM'      # Relative Path
valid_path = 'Digital_Mamo/InBreast'
test_path = 'Digital_Mamo/BCDR'


valid_batches = ImageDataGenerator().flow_from_directory(valid_path, target_size=(224, 224), classes=['Benign', 'Malignant'], batch_size=9)
test_batches = ImageDataGenerator().flow_from_directory(test_path, target_size=(224, 224), classes=['Benign', 'Malignant'], batch_size=7)

datagen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1,
       height_shift_range=0.1, shear_range=0.15, zoom_range=0.1,
       channel_shift_range=10., horizontal_flip=True)

train_batches = datagen.flow_from_directory(
        train_path,
        target_size=(224, 224),
        batch_size=10,
        classes=['Benign','Malignant'])


vgg16_model= load_model('Fetched_VGG.h5')

# transform the model to Sequential
model= Sequential()
for layer in vgg16_model.layers[:-1]:
    model.add(layer)

model.summary()

# Freezing the layers (Oppose weights to be updated)
for layer in model.layers:
    layer.trainable = False


model.add(Dense(2, activation='softmax', name='predictions'))


### Compile the model
model.compile(Adam(lr=.0001), loss='categorical_crossentropy', metrics=['accuracy'])

# train the model
model.fit_generator(train_batches, steps_per_epoch=28, validation_data=valid_batches, validation_steps=3, epochs=5, verbose=2)

#test
predictions = model.predict_generator(test_batches, steps=3, verbose=0)
4

2 回答 2

0

It's a correct way, but you can use steps_per_epoch=len(train_batches) and validation_steps=len(val_batches) for easier life when you add more data though. Also you can just include test in valid since it won't help anything even you're using them separately.

Edit:

As @Matias point out that you shouldn't use augmentation on validate. So it's not totally wrong as I said in comment but not really correct.

于 2019-09-11T03:54:01.853 回答
0

Actually this is not correct, because the way you coded this, you are applying data augmentation to the validation and test sets, and you should only apply augmentation to the training set.

You would need to create a second instance of ImageDataGenerator for the validation and test sets, without any augmentations set.

于 2019-09-11T05:42:23.457 回答