Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong loss shape when using keras generators #15

Closed
landoooo opened this issue Nov 16, 2017 · 1 comment
Closed

Wrong loss shape when using keras generators #15

landoooo opened this issue Nov 16, 2017 · 1 comment

Comments

@landoooo
Copy link

landoooo commented Nov 16, 2017

I am facing the following issue when trying to train a Squeezenet with no weights and two classes:

expected loss to have shape (None, 2) but got array with shape (64, 1)

the code has been deeply inspired by this tutorial (https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html)

from keras.preprocessing.image import ImageDataGenerator
from keras import backend as K
from keras_squeezenet import SqueezeNet

img_width, img_height = 227, 227

train_data_dir = 'C:/IMAGES/KERAS/train'
validation_data_dir = 'C:/IMAGES/KERAS/validation'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 2
batch_size = 64

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

model = SqueezeNet(weights=None, classes=2)

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

train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0,
    zoom_range=0,
    horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

model.fit_generator(
    generator=train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size)

In the folders train and validation there are 2 subfolders (OK and NOK), and both classes are well read by the generators. This is the output from them:

Found 40294 images belonging to 2 classes.
Found 1273 images belonging to 2 classes.

Setting classes=1 when declaring the model solves the error of the expected vs real shape of the loss vector but makes the classifier pointless.

Any idea about how to have more than 1 class when using generators?

@landoooo
Copy link
Author

Issue here was the class_mode='binary' used on the data generators. Changing it into categorical made it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant