0

我使用以下代码创建了一个 CNN 来在 keras 中进行二进制分类:

def neural_network():
  classifier = Sequential()

  # Adding a first convolutional layer
  classifier.add(Convolution2D(48, 3, input_shape = (320, 320, 3), activation = 'relu'))
  classifier.add(MaxPooling2D())

  # Adding a second convolutional layer
  classifier.add(Convolution2D(48, 3, activation = 'relu'))
  classifier.add(MaxPooling2D())


  #Flattening
  classifier.add(Flatten())

  #Full connected
  classifier.add(Dense(256, activation = 'relu'))
  #Full connected
  classifier.add(Dense(256, activation = 'sigmoid'))

  #Full connected
  classifier.add(Dense(1, activation = 'sigmoid'))


  # Compiling the CNN
  classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

  classifier.summary()


  train_datagen = ImageDataGenerator(rescale = 1./255,
                                    shear_range = 0.2,
                                    horizontal_flip = True,
                                    vertical_flip=True,
                                    brightness_range=[0.5, 1.5])

  test_datagen = ImageDataGenerator(rescale = 1./255)

  training_set = train_datagen.flow_from_directory('/content/drive/My Drive/data_sep/train',
                                                  target_size = (320, 320),
                                                  batch_size = 32,
                                                  class_mode = 'binary')

  test_set = test_datagen.flow_from_directory('/content/drive/My Drive/data_sep/validate',
                                              target_size = (320, 320),
                                              batch_size = 32,
                                              class_mode = 'binary')

  es = EarlyStopping(
      monitor="val_accuracy",
      mode="max",
      patience
      baseline=None,
      restore_best_weights=True,
  )

  filepath  = "/content/drive/My Drive/data_sep/weightsbestval.hdf5"
  checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
  callbacks_list = [checkpoint]

  history = classifier.fit(training_set,
                          epochs  = 10,
                          validation_data = test_set,
                          callbacks= es
                          )
  
  best_score = max(history.history['val_accuracy'])

  from sklearn.metrics import classification_report

  predictions =(classifier.predict(test_set) > 0.5).astype("int32")

  newlist = predictions.tolist()
  finallist = []
  for number in newlist:
    finallist.append(number[0])

  predicted_classes = np.asarray(finallist)
  true_classes = test_set.classes
  class_labels = list(test_set.class_indices.keys())
  report = classification_report(true_classes, predicted_classes, target_names=class_labels)
  
  accuracy = metrics.accuracy_score(true_classes, predicted_classes)  
  print(true_classes)
  print(predicted_classes)
  print(class_labels)
  correct = 0
  for i in range(len(true_classes)):
    if (true_classes[i] == predicted_classes[i]): 
      correct = correct + 1
  print(correct)
  print((correct*1.0)/(len(true_classes)*1.0))
  print(report)
  return best_score

当我运行模型时,我通过 model.fit() 获得了 81.90% 的验证准确度,但在完成 model.predict 后,验证准确度为 40%。我添加了一个回调,其中恢复了最佳权重。那么这里可能是什么问题呢?

4

2 回答 2

0

为我解决的问题是我创建了另一个图像数据生成器变量

test2_datagen = ImageDataGenerator(rescale = 1./255)

test2_set = test2_datagen.flow_from_directory('/content/drive/My Drive/data_sep/validate',
                                              target_size = (320, 320),
                                              batch_size = 32,
                                              class_mode = 'binary',
                                              Shuffle = False)

但正如你所见,我设置Shuffle = False了. 如果有人有同样的问题,我会发布这个答案。所以我用于test2_set预测。

于 2020-11-22T12:37:22.157 回答
-1

由于您在此行中保存了最佳模型

  checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')

请在你的代码中加载这个模型,然后预测

from keras.models import load_model  
loaded_model = load_model('data_sep/weightsbestval.hdf5')  

然后

loaded_model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics['accuracy'])
score = loaded_model.evaluate(X_test, Y_test, verbose=0)
print ("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))  

如果您觉得这有用,请投票/标记正确

于 2020-11-22T06:48:29.363 回答