-1
$\begingroup$

I am preparing a binary classifier. Initially, I used the following parameters based on the well-known cat and dog classifier example;

train_datagen = ImageDataGenerator(rescale=1./255)
validation_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
# Data Generator for Training data
train_generator = train_datagen.flow_from_directory(train_dir, target_size=(224, 224), batch_size=32, class_mode='binary')    
# Data Generator for Validation data
validation_generator = validation_datagen.flow_from_directory(validation_dir, target_size= (224, 224), batch_size=16, class_mode='binary', shuffle=False)                                                              
# Data Generator for Test data
test_generator = validation_datagen.flow_from_directory(test_dir, target_size=(224, 224), batch_size=16, class_mode='binary', shuffle=False)                                                  
# Compile the model
model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc'])

and the model was;

# Create a sequential model
model = models.Sequential()
# Add the vgg convolutional base model
model.add(vgg_conv)
# Add new layers
model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(1, activation='sigmoid'))

I use VGG16 with imagenet weights by adding these layers at the end of the model. Cat and dog classifier works with good accuracy and I was sure this model is going to give me very good results for the initial steps. However, when I checked the confusion matrix I saw that all the images are classified as they belong to category 0. Then I replaced all "binaries" with "categorical" and used a softmax activator with 2 units. All the rest is the same and now I obtain very good results. I do not understand how this happens? Why the first configuration works very well with another dataset but gives me garbage results with my own dataset? It is better to try as much as possible but it takes a bit less than a day with my dataset. I also do not understand how a binary classifier works with only one unit at the output layer since the actual categories are two.

$\endgroup$
2
  • 1
    $\begingroup$ This question could be on-topic if it were edited to ask the key question: "What loss function should I use?". The code would also be unnecessary. $\endgroup$ May 1 '19 at 17:23
  • $\begingroup$ @PhilipRaeisghasem, as a practical "how to", code is always interesting ;-) $\endgroup$ May 3 at 20:50
1
$\begingroup$

This is simply because they have different loss functions. Categorical cross entropy measures the logarithmic loss for every neuron in the output layer. For binary crossentropy, we have 1 neuron in the output layer. In this case, you should use binary cross entropy.

For Cats and dogs classification ,you have two classes Cats and Dogs. You should use categorical crossentropy.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.