简体   繁体   English

为什么我的用于二进制分类的 CNN model 没有学习?

[英]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.我得到了 10000 个形状 (100,100) 的图像,代表粒子的检测,然后我创建了 10000 个形状 (100,100) 的空图像并将它们混合在一起。 I have given each respective type labels of 0 and 1, seen in the code here:我已经给出了 0 和 1 的每个类型标签,在此处的代码中可以看到:

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:然后设置我的 model 我创建一个新的 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纪元 1/3 469/469 [===============================] - 62 秒 131 毫秒/步 - 损失:0.6939 - 准确度: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/步 - 损失:0.6889 - 准确度: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 [===============================] - 59 秒 127 毫秒/步 - 损失:0.6527 -准确度: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.我已经尝试了更多的时期,但我仍然只能获得 50% 的准确率,这是无用的,因为它只是不断地预测相同的事情。

There can be many reasons why your model is not working.您的 model 无法工作的原因可能有很多。 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.似乎更有可能的是 model 欠拟合,因为训练集和验证集的准确度都很低,这意味着神经网络无法捕捉数据中的模式。 Hence you should consider building little more complex model by adding more layer at the same time avoiding over-fitting with techniques like dropout.因此,您应该考虑构建更复杂的 model,同时通过添加更多层来避免像 dropout 等技术的过度拟合。 You should also get best parameters by doing hyperparameter tuning.您还应该通过进行超参数调整来获得最佳参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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