Fhun
Fhun

Reputation: 105

Simple binary classification by CNN with Keras, But got only 50% acc

Today I try to use fit_generator function for binary classifying plain black and white image but it gives me only 50% accuracy

This is just my coding exercise but I think accuracy should reach 100%. So I am curious what is my mistake.

I do all the code in Google-colaboratory.

here is my code.

Set up

import numpy as np
import random
from matplotlib import pyplot as plt

img_height = 150
img_width = 150
batch_size = 8

class MyDataset(object):

    def __init__(self):
        placeholder = 0

    def generator(self):
        is_black = True
        X, y = [], []
        while True:
            if is_black:
                img = np.full((img_height, img_width, 3), 255)
            else:
                img = np.zeros((img_height, img_width, 3))
            img = img / 255.
            X.append(img)
            y.append(is_black)
            is_black = not is_black

            if len(X) >= batch_size:
                c = list(zip(X, y))
                random.shuffle(c)
                X, y = zip(*c)
                yield np.asarray(X, dtype=np.float32), np.asarray(y, dtype=np.float32)
                X, y = [], []

dataset = MyDataset()
sample_gen = dataset.generator()

Visualize data

Example of inputs for model

X, y = next(sample_gen)

label_dict = {0:'black', 1:'white'}

sample_size = len(X)

fig = plt.figure(figsize=(16, 8))

for sample in range(sample_size):
    img = X[sample]
    lbl = label_dict[y[sample]]

    fig.add_subplot(2, sample_size//2, sample + 1)
    f = plt.imshow(img)
    f.axes.get_xaxis().set_visible(False)
    f.axes.get_yaxis().set_visible(False)
    plt.title(lbl)

plt.show()

Create model

I create a small size model. It has only 9 parameters.

import tensorflow as tf

model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(filters=1, kernel_size=(1,1), padding='same', 
                                 activation='relu', input_shape=(img_height, img_width, 3)))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(img_height//2,img_height//2)))

model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(1, activation='softmax'))

model.summary()

Train model

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

model.fit_generator(
    sample_gen, 
    steps_per_epoch = 100//batch_size , 
    epochs=300)

Result

After 200+ epochs, accuracy still be 0.5.

Epoch 218/300
12/12 [==============================] - 0s 8ms/step - loss: 7.9712 - acc: 0.5000
Epoch 219/300
12/12 [==============================] - 0s 8ms/step - loss: 7.9712 - acc: 0.5000
Epoch 220/300
12/12 [==============================] - 0s 8ms/step - loss: 7.9712 - acc: 0.5000
Epoch 221/300
12/12 [==============================] - 0s 9ms/step - loss: 7.9712 - acc: 0.5000
Epoch 222/300
12/12 [==============================] - 0s 8ms/step - loss: 7.9712 - acc: 0.5000

I already studied a bit about CNN and I am beginner at Keras.

Upvotes: 0

Views: 1488

Answers (1)

dedObed
dedObed

Reputation: 1363

The problem is at the end of your model definition, specificaly here:

model.add(tf.keras.layers.Dense(1, activation='softmax'))

By applying softmax you -- by definition -- enforce it's outputs to sum to one. The only way how a single value can comply, is to become 1 itself. Therefore, no information is propagated through.

To fix it, turn the softmax into a logistic sigmoid, e.g.:

model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

This way, you also can interpret the output of your model as the posterior probability that the data comes from class 1.

Upvotes: 1

Related Questions