【发布时间】:2021-01-15 11:04:46
【问题描述】:
我有一个已经训练好的模型,我想对目录中的图像进行二元分类预测。我有超过 100,000 张图像,所以为了提高效率,我想做批量预测。如何对我的图像进行批量预测,获取预测结果,并将图像根据类预测后存储在两个单独的文件夹中?
这是我的代码到目前为止的样子...
model_filepath = r"C:\Users\model_200.h5"
model = tf.keras.models.load_model(model_filepath)
test_dir = r"C:\Users\image_testing_folder"
batch_size = 64
IMG_HEIGHT = 200
IMG_WIDTH = 200
test_image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
test_image_gen = test_image_generator.flow_from_directory(directory=str(test_dir),
batch_size=batch_size,
shuffle=False,
target_size=(IMG_HEIGHT, IMG_WIDTH),
)
predictions = (model.predict(test_image_gen) > 0.5).astype("int32")
predictions
一种解决方案是将预测与图像文件路径联系起来,然后使用 shutil.move() 将原始图像移动到目标文件夹。我该怎么做?有没有比使用 ImageDataGenerator 和 .flow_from_directory 更好的批量预测方法?
【问题讨论】:
标签: python image tensorflow machine-learning classification