Skip to content Skip to sidebar Skip to footer

Fitting Array To Datagen Then Passing As Parameter To Keras Classifier

I have a convolutional neural network that i am using to classify cats and dogs, using keras classifier. I had to use custom cross validation, due to how my data is organized, wher

Solution 1:

I think at this point you should just make a custom cross-validation loop, since you want extra flexibility. Then you'll be able to apply any transformation you want. For example, I used this transformation:

img  = tf.image.random_contrast(img, .2, .5)

But you can make it anything you want.

import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras import Sequential
from glob2 import glob
from collections import deque

group1 = glob('group1\\*\\*.jpg')
group2 = glob('group2\\*\\*.jpg')
group3 = glob('group3\\*\\*.jpg')

groups = [group1, group2, group3]

assertall(map(len, groups))

defload(file_path):
    img = tf.io.read_file(file_path)
    img = tf.image.decode_jpeg(img, channels=3)
    img = tf.image.convert_image_dtype(img, tf.float32)
    img = tf.image.resize(img, size=(100, 100))
    img  = tf.image.random_contrast(img, .2, .5)
    label = tf.strings.split(file_path, os.sep)[1]
    label = tf.cast(tf.equal(label, 'dogs'), tf.int32)
    return img, label

accuracies_on_test_set = {}

for i inrange(len(groups)):
    d = deque(groups)
    d.rotate(i)
    train1, train2, test1 = d
    train_ds = tf.data.Dataset.from_tensor_slices(train1 + train2).\
        shuffle(len(train1) + len(train2)).map(load).batch(4)
    test_ds = tf.data.Dataset.from_tensor_slices(test1).\
        shuffle(len(test1)).map(load).batch(4)

    model = Sequential()
    model.add(Conv2D(32, (3, 3), input_shape=(100, 100, 3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Flatten())
    model.add(Dropout(0.25))
    model.add(Dense(1))
    model.add(Activation('sigmoid'))
    model.compile(loss='binary_crossentropy',
                  optimizer='rmsprop',
                  metrics=['mse', 'accuracy'])

    model.fit(train_ds, validation_data=test_ds, epochs=5, verbose=0)
    loss, mse, accuracy = model.evaluate(test_ds, verbose=0)
    accuracies_on_test_set[f'epoch_{i + 1}_accuracy'] = accuracy

print(accuracies_on_test_set)
{'epoch_1_accuracy': 0.915, 'epoch_2_accuracy': 0.95, 'epoch_3_accuracy': 0.9}

The folder structure is this:

group1/
    dogs/
        dog001.jpg
    cats/
        cat001.jpg
group2/
    dogs/
        dog001.jpg
    cats/
        cat001.jpg
group3/
    dogs/
        dog001.jpg
    cats/
        cat001.jpg

Post a Comment for "Fitting Array To Datagen Then Passing As Parameter To Keras Classifier"