【发布时间】:2021-12-24 06:27:01
【问题描述】:
我正在按照本教程训练模型将图像分类为 2 类:https://www.tensorflow.org/tutorials/images/classification
在model.fit() 之后,我想使用包含未包含在训练或验证集中的图像的测试集来评估模型预测的准确性。测试集包含 2 个文件夹,其中包含相应类别的图像。
├── test_data/
│ ├── class1/
│ ├── class2/
我想使用混淆矩阵找到每个类的召回率、精度和准确率。 但是,我是深度学习和 Tensorflow 的新手。我不知道如何获得每个班级的混淆矩阵。我也不确定我将图像传递给模型的方式是否正确。
以下是我目前使用模型预测新数据的实现。
# get the list of class names in the training set
train_class_names = train_ds.class_names
# load the test data
test_data_dir = pathlib.Path('test_data/')
test_data_list = list(test_data_dir.glob('*/*.jpg'))
test_ds = tf.keras.utils.image_dataset_from_directory(
test_data_dir,
image_size=(img_height, img_width),
batch_size=batch_size)
predicted_img = []
# for every image in the test_data folder, pass it to the model to predict its class
for path in test_data_list:
img = tf.keras.utils.load_img(
path, target_size=(img_height, img_width)
)
img_array = tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
test_class_name = path.parent.name
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
# append the image, predicted class and actual class to a list
# so that I can print them out to see if the prediction is correct
predicted_img.append([img, train_class_names[np.argmax(score)], test_class_name])
【问题讨论】:
标签: python tensorflow confusion-matrix