简体   繁体   English

是否可以从 image_dataset_from_directory 获取图像名称?

[英]is it possible to get image name from image_dataset_from_directory?

what I have is a python script to classify images using pre-trained model.我拥有的是一个 python 脚本,用于使用预训练模型对图像进行分类。 first, I read the images using首先,我使用

VALIDATION_DATASET = image_dataset_from_directory(VALIDATION_DIR,
                                                  shuffle=True,
                                                  batch_size=BATCH_SIZE,
                                                  image_size=IMG_SIZE)

after that I divide the validation dataset into validation and test groups as in:之后,我将验证数据集划分为验证组和测试组,如下所示:

VAL_BATCHES = tf.data.experimental.cardinality(VALIDATION_DATASET)
TEST_DATASET = VALIDATION_DATASET.take(VAL_BATCHES // 5)
VALIDATION_DATASET = VALIDATION_DATASET.skip(VAL_BATCHES // 5)

and finally, for optimization I use prefetch :最后,为了优化我使用prefetch

test_dataset = TEST_DATASET.prefetch(buffer_size=AUTOTUNE)

in the testing part I use:在我使用的测试部分:

model_name = r"C:\model\location\pre_trained_model.h5"
model = tf.keras.models.load_model(model_name)

predictions_A = tf.where(tf.nn.sigmoid(model.predict_on_batch(image_batch_A).flatten())< 0.5, 0, 1)
predictions_B = tf.where(tf.nn.sigmoid(model.predict_on_batch(image_batch_B).flatten())< 0.5, 0, 1)
predictions_C = tf.where(tf.nn.sigmoid(model.predict_on_batch(image_batch_C).flatten())< 0.5, 0, 1)
predictions_D = tf.where(tf.nn.sigmoid(model.predict_on_batch(image_batch_D).flatten())< 0.5, 0, 1)

ALLpredictions= np.concatenate((predictions_A,predictions_B,predictions_C, predictions_D ), axis=0)


classificationRPRT = classification_report(ALLlabel_batch, ALLpredictions, target_names=CLASSES_NAMES)
print(classificationRPRT)

because tf choose images randomly, I want to know the name of the images so later I can compare the model result with separate result done manually.因为tf随机选择图像,所以我想知道图像的名称,所以稍后我可以将模型结果与手动完成的单独结果进行比较。

You can instead use a custom loading function with tf.data.Dataset that returns both the image and the file name:您可以改为使用带有tf.data.Dataset的自定义加载函数,该函数返回图像和文件名:

import tensorflow as tf
from glob2 import glob

files = glob('main_folder/*/*.jpg')

def load(path):
    as_string = tf.io.read_file(path)
    as_image = tf.image.decode_image(as_string, channels=3)
    resized = tf.image.resize(as_image, (224, 224))
    normalized = tf.divide(resized, 255)
    return normalized, path

ds = tf.data.Dataset.from_tensor_slices(files).shuffle(32).map(load).batch(8)

This wil return the resized image and also the file name:这将返回调整大小的图像以及文件名:

image_batch, path_batch = next(iter(ds))

在预取之前尝试以下操作:

image_paths = VALIDATION_DATASET.file_paths

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 image_dataset_from_directory 用于多标签分类 - image_dataset_from_directory for multilabel classifcation Tensorflow image_dataset_from_directory 用于输入数据集和 output 数据集 - Tensorflow image_dataset_from_directory for input dataset and output dataset 使用 tensorflow image_dataset_from_directory 时从数据集中获取标签 - Get labels from dataset when using tensorflow image_dataset_from_directory 导入错误:无法从“tensorflow.keras.preprocessing”(未知位置)导入名称“image_dataset_from_directory” - ImportError: cannot import name 'image_dataset_from_directory' from 'tensorflow.keras.preprocessing' (unknown location) 如何从 Keras 中的 image_dataset_from_directory() 从 MapDataset 附加或获取文件名? - How to attach or get filenames from MapDataset from image_dataset_from_directory() in Keras? 丢失 function 与 image_dataset_from_directory 一起使用 - Loss function to use with image_dataset_from_directory 导入错误:无法从“keras.preprocessing”导入名称“image_dataset_from_directory” - ImportError: cannot import name 'image_dataset_from_directory' from 'keras.preprocessing' Keras:`image_dataset_from_directory` 中标签的一次性使用 - Keras: one-hot for labels in `image_dataset_from_directory` image_dataset_from_directory() 如何对数字类名的预测进行排序? - How does image_dataset_from_directory() order the predictions of a numerical class name? 如何将 keras image_dataset_from_directory 与自定义结构一起使用? - How to use keras image_dataset_from_directory with custom structures?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM