Reputation: 311
When i shifted from 2 classes to 4 classes in a CNN model. I got errors. A lot of people helped me solve them, like I needed to change loss to sparse_categorical_crossentropy
and last layer activation function.
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
history = model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples//batch_size,
epochs=epochs,
validation_data = validation_generator,
validation_steps = validation_generator.samples // batch_size,
)
I am confused as I had to change the loss function from binary_crossentropy
to categorical_crossenpropy
. I want to know whether I should change the class_mode from binary to categorical for 4 classes or should I leave it as binary.
train_datagen=ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip = True,
#vertical_flip = True,
validation_split=0.2,
brightness_range=[0.5, 1.5]
)
#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,
shuffle=True,
class_mode='binary',
subset='training'
)
validation_generator = train_datagen.flow_from_directory(
train_data_dir, # same directory as training data
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary',
subset='validation'
#validation_data_dir,
#target_size=(img_width,img_height),
#batch_size=batch_size,
#class_mode='binary'
)
Upvotes: 2
Views: 3851
Reputation: 36604
I think reading the documentation would be a good starting point. It would answer all your questions, and then some.
class_mode: One of "categorical", "binary", "sparse", "input", or None. Default: "categorical". Determines the type of label arrays that are returned: - "categorical" will be 2D one-hot encoded labels, - "binary" will be 1D binary labels, "sparse" will be 1D integer labels, - "input" will be images identical to input images (mainly used to work with autoencoders). - If None, no labels are returned (the generator will only yield batches of image data, which is useful to use with model.predict_generator()). Please note that in case of class_mode None, the data still needs to reside in a subdirectory of directory for it to work correctly.
So you should use categorical_crossentropy
as loss function if you choose categorical
for class_mode
, and sparse_categorical_crossentropy
if you choose sparse
.
Upvotes: 3
Reputation: 2079
Of course, you should change it. If you have only 2 classes use class_mode='binary'
in flow_from_directory
function and binary_crossentropy
as loss. In case you have more than 2 classes, use class_mode='categorical'
and categorical_crossentropy
Upvotes: 2