title: Simple binary classification by CNN with Keras, But got only 50% acc
body: <p>Today I try to use fit_generator function for binary classifying plain black and white image but it gives me only 50% accuracy</p>
  
  <p>This is just my coding exercise but I think accuracy should reach 100%. So I am curious what is my mistake.</p>
  
  <p>I do all the code in Google-colaboratory.</p>
  
  <p>here is my code.</p>
  
  <p><strong>Set up</strong> </p>
  
  <pre><code>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) &gt;= 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()
  </code></pre>
  
  <p><strong>Visualize data</strong></p>
  
  <p><a href="https://i.stack.imgur.com/Edc0C.png" rel="nofollow noreferrer">Example of inputs for model</a></p>
  
  <pre><code>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()
  </code></pre>
  
  <p><strong>Create model</strong></p>
  
  <p>I create a small size model. It has only 9 parameters.</p>
  
  <pre><code>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()
  </code></pre>
  
  <p><strong>Train model</strong></p>
  
  <pre><code>model.compile(optimizer='rmsprop',
                loss='binary_crossentropy',
                metrics=['accuracy'])
  
  model.fit_generator(
      sample_gen, 
      steps_per_epoch = 100//batch_size , 
      epochs=300)
  </code></pre>
  
  <p><strong>Result</strong></p>
  
  <p>After 200+ epochs, accuracy still be 0.5.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I already studied a bit about CNN and I am beginner at Keras.</p>