简体   繁体   中英

Why isn't my CNN model for a Binary Classification not learning?

I have been given 10000 images of shape (100,100), representing detection of particles, I have then created 10000 empty images of shape (100,100) and mixed them together. I have given each respective type labels of 0 and 1, seen in the code here:

Labels = np.append(np.ones(10000),np.zeros(empty_sheets.shape[0]))

images_scale1 = np.zeros(s)   #scaling each image so that it has a maximum number of 1
#scaling each image so that it has a maximum number of 1
l = s[0]
for i in range(l):
    images_scale1[i] = images[i]/np.amax(images[i])

empty_sheets_noise1 = add_noise(empty_sheets,0)

scale1noise1 = np.concatenate((images_scale1,empty_sheets_noise1),axis=0)
y11 = Labels

scale1noise1s, y11s = shuffle(scale1noise1, y11)

scale1noise1s_train, scale1noise1s_test, y11s_train, y11s_test = train_test_split(
                                                    scale1noise1s, y11,     test_size=0.25)

#reshaping image arrays so that they can be passed through CNN
scale1noise1s_train = scale1noise1s_train.reshape(scale1noise1s_train.shape[0],100,100,1)
scale1noise1s_test  = scale1noise1s_test.reshape(scale1noise1s_test.shape[0],100,100,1)
y11s_train = y11s_train.reshape(y11s_train.shape[0],1)
y11s_test = y11s_test.reshape(y11s_test.shape[0],1)

Then to set up my model I create a new function:

def create_model():
    #initiates new model
    model = keras.models.Sequential()
    model.add(keras.layers.Conv2D(64, (3,3),activation='relu',input_shape=(100,100,1)))
    model.add(keras.layers.MaxPooling2D((2, 2)))
    model.add(keras.layers.Dropout(0.2))
    model.add(keras.layers.Flatten())
    model.add(keras.layers.Dense(32))
    model.add(keras.layers.Dense(64))
    model.add(keras.layers.Dense(1,activation='sigmoid'))
    return model

estimators1m1 = create_model()
estimators1m1.compile(optimizer='adam', metrics=['accuracy', tf.keras.metrics.Precision(),
                                  tf.keras.metrics.Recall()], loss='binary_crossentropy')

history = estimators1m1.fit(scale1noise1s_train, y11s_train, epochs=3, 
                    validation_data=(scale1noise1s_test, y11s_test))

which produces the following:

Epoch 1/3 469/469 [==============================] - 62s 131ms/step - loss: 0.6939 - accuracy: 0.4917 - precision_2: 0.4905 - recall_2: 0.4456 - val_loss: 0.6933 - val_accuracy: 0.5012 - val_precision_2: 0.5012 - val_recall_2: 1.0000 Epoch 2/3 469/469 [==============================] - 63s 134ms/step - loss: 0.6889 - accuracy: 0.5227 - precision_2: 0.5209 - recall_2: 0.5564 - val_loss: 0.6976 - val_accuracy: 0.4994 - val_precision_2: 0.5014 - val_recall_2: 0.2191 Epoch 3/3 469/469 [==============================] - 59s 127ms/step - loss: 0.6527 - accuracy: 0.5783 - precision_2: 0.5764 - recall_2: 0.5887 - val_loss: 0.7298 - val_accuracy: 0.5000 - val_precision_2: 0.5028 - val_recall_2: 0.2131

I have tried more epochs and I still only manage to get 50% accuracy which is useless as its just predicting the same things constantly.

There can be many reasons why your model is not working. One that seems more likely is that the model is under-fitting as both accuracy on training set and validation set is low meaning that the neural network is unable to capture the pattern in the data. Hence you should consider building little more complex model by adding more layer at the same time avoiding over-fitting with techniques like dropout. You should also get best parameters by doing hyperparameter tuning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM