我最近一直在使用TF2.0。我训练了一个简单的CNN模型(用Keras序列API)来对图像进行二值分类。我使用tf.data.Dataset从磁盘加载映像。实际上,该模型得到了很好的精度,列车binary_accuracy: 0.9831和验证binary_accuracy: 0.9494。
尝试使用model.evaluate()对模型进行评估。它的二进制精度为0.9460。但是当我尝试使用predict_classes()手工计算二进制精度时,我得到了大约0.384。我不知道是怎么回事。请帮帮我。
我添加了用于编译和训练模型的代码。还有评估我的模型的代码。
train_data = tf.data.Dataset.from_tensor_slices((tf.constant(train_x),tf.constant(train_y)))
val_data = tf.data.Dataset.from_tensor_slices((tf.constant(val_x),tf.constant(val_y)))
train_data = train_data.map(preproc).shuffle(buffer_size=100).batch(BATCH_SIZE)
val_data = val_data.map(preproc).shuffle(buffer_size=100).batch(BATCH_SIZE)
model.compile(optimizer=Adam(learning_rate=0.0001),
loss='binary_crossentropy',
metrics=[tf.keras.metrics.BinaryAccuracy()])
checkpointer = ModelCheckpoint(filepath='weights.hdf5', verbose=1, save_best_only=True)
time1 = time.time()
history = model.fit(train_data.repeat(),
epochs=EPOCHS,
steps_per_epoch=STEPS_PER_EPOCH,
validation_data=val_data.repeat(),
validation_steps=VAL_STEPS,
callbacks=[checkpointer])
29/29 [==============================] - 116s 4s/step - loss: 0.0634 - binary_accuracy: 0.9826 - val_loss: 0.1559 - val_binary_accuracy: 0.9494现在用看不见的数据进行测试
test_data = tf.data.Dataset.from_tensor_slices((tf.constant(unseen_faces),tf.constant(unseen_labels)))
test_data = test_data.map(preproc).batch(BATCH_SIZE)
model.evaluate(test_data)
9/9 [==============================] - 19s 2s/step - loss: 0.1689 - binary_accuracy: 0.9460同样的模型,当我尝试用相同数据集的model.predict_classes计算精度时,预测结果与评价报告相去甚远。二进制精度约为38%。
编辑1:训练时使用的预处理功能
def preproc(file_path,label):
img = tf.io.read_file(file_path)
img = tf.image.decode_jpeg(img)
img = (tf.cast(img, tf.float32)/127.5) - 1
return tf.image.resize(img,(IMAGE_HEIGHT,IMAGE_WIDTH)),label手动预测代码
from sklearn.metrics import classification_report
#Testing preprocessing function
def preproc_test(file_path):
img = tf.io.read_file(file_path)
img = tf.image.decode_jpeg(img)
img = (tf.cast(img, tf.float32)/127.5) - 1
return tf.image.resize(img,(IMAGE_HEIGHT,IMAGE_WIDTH))
unseen_faces = []
unseen_labels = []
for im_path in glob.glob('dataset/data/*'):
unseen_faces.append(im_path)
if 'real' in i:
unseen_labels.append(0)
else:
unseen_labels.append(1)
unseen_faces = list(map(preproc_test,unseen_faces))
unseen_faces = tf.stack(unseen_faces)
predicted_labels = model.predict_classes(unseen_faces)
print(classification_report(unseen_labels,predicted_labels,[0,1]))
precision recall f1-score support
0 0.54 0.41 0.47 34
1 0.41 0.54 0.47 26
accuracy 0.47 60
macro avg 0.48 0.48 0.47 60
weighted avg 0.48 0.47 0.47 60发布于 2019-11-09 19:49:55
您的模型在training和testing期间都做得很好。评估的准确性是建立在预测的基础上的,所以你在使用model.predict_classes()时可能犯了一些逻辑上的错误。在评估模型时,请检查是否使用了经过训练的模型权重,而不是任何随机初始化的模型。
evaluate:模型将把培训数据的这一部分分开,不对其进行培训,并将在每个时代结束时评估损失和对这些数据的模型度量。model.evaluate()是用来评估你受过训练的模型的。它的输出是准确或丢失的,而不是对输入数据的预测。
predict:为输入样本生成输出预测。model.predict()实际上是根据输入数据预测的,其输出是目标值。
P.S.:对于二进制分类问题,精度<=50%比随机猜测差。
发布于 2020-02-13 23:04:15
在我的例子中,这是因为我的真实的形状和预测的结果是不同的。我是通过(x_train, y_train), (x_test, y_test) = cifar10.load_data()加载数据的,其中y_train是形状(50000,1)的二维元数据,而model.predict_classes的预测是shape (50000,)。如果我用np.mean(pred==y_train)直接比较它们,我就会得到0.1的结果,这是不正确的。相反,np.mean(pred==np.squeeze(y_train))给出了正确的结果。
https://stackoverflow.com/questions/58782488
复制相似问题