Tensorflow 2 图像批量预测返回结果

我有一个已经训练好的模型,我想对目录中的图像进行二元分类预测。我有超过 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 更好的方法来进行批量预测?


慕勒3428872
浏览 68回答 1
1回答

沧海一幻觉

您可以创建自定义数据集,以便还可以轻松检索文件名:import tensorflow as tffrom tensorflow.keras.layers import *from tensorflow.keras import Sequentialfrom glob2 import globfrom shutil import copyimport numpy as npfiles = glob('group1\\*\\*.jpg')imsize = 64def load(file_path):    img = tf.io.read_file(file_path)    img = tf.image.decode_png(img, channels=3)    img = tf.image.convert_image_dtype(img, tf.float32)    img = tf.image.resize(img, size=(imsize, imsize))    return img, file_pathds = tf.data.Dataset.from_tensor_slices(files).\    take(100).\    shuffle(100).\    map(load).batch(4)model = Sequential()model.add(Conv2D(8, (3, 3), input_shape=(imsize, imsize, 3), activation='relu'))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Flatten())model.add(Dense(units=32, activation='relu'))model.add(Dropout(0.5))model.add(Dense(units=2, activation='sigmoid'))model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])model.build(input_shape=(imsize, imsize, 3))categories = np.array(['cats', 'dogs'])target_dir = 'newpics'for cat in categories:    os.makedirs(os.path.join(target_dir, cat), exist_ok=True)for images, filenames in ds:    preds = model(images)    targets = categories[np.argmax(preds, axis=1)]    for file, destination in zip(filenames, targets):        copy(file.numpy().decode(), os.path.join(target_dir, destination,                                os.path.basename(file.numpy().decode())                                ))        print(file.numpy().decode(), '-->', os.path.join(target_dir, destination,                                os.path.basename(file.numpy().decode())                                ))group1\cats\cat.4051.jpg --> newpics\cats\cat.4051.jpggroup1\cats\cat.4091.jpg --> newpics\dogs\cat.4091.jpggroup1\cats\cat.4055.jpg --> newpics\cats\cat.4055.jpggroup1\cats\cat.4041.jpg --> newpics\cats\cat.4041.jpggroup1\cats\cat.4090.jpg --> newpics\cats\cat.4090.jpggroup1\cats\cat.4071.jpg --> newpics\dogs\cat.4071.jpggroup1\cats\cat.4082.jpg --> newpics\cats\cat.4082.jpggroup1\cats\cat.4037.jpg --> newpics\cats\cat.4037.jpggroup1\cats\cat.4005.jpg --> newpics\cats\cat.4005.jpg您需要更改的只是全局模式和文件夹。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python