如何在使用tf进行预测时获取文件名,keras.preprocessing.image_dataset_from_directory()?

bfhwhh0e  于 2023-04-30  发布在  其他
关注(0)|答案(4)|浏览(204)

Keras引入了TF。keras.preprocessing.image_dataset_from_directory函数,它比以前的ImageDataGenerator更高效。tensorflow 2中的flow_from_directory方法。x.
我正在练习catsvsdogs问题,并使用此函数为我的模型构建数据管道。在训练模型之后,我使用preds = model。predict(test_ds)获取我的测试数据集的预测。我应该如何匹配图片名称的preds?(有发电机。文件名之前,但在新方法中不再存在。)谢谢!

zu0ti5jz

zu0ti5jz1#

扩展@丹尼尔Woolcott和@Almog大卫的答案,文件路径由Tensorflow v2中的image_dataset_from_directory()函数返回。4.已经。不需要改变函数的源代码。
更准确地说,您可以轻松地使用file_paths属性检索路径。
试试这个:

img_folder = "your_image_folder/"

img_generator = keras.preprocessing.image_dataset_from_directory(
    img_folder, 
    batch_size=32, 
    image_size=(224,224)
)

file_paths = img_generator.file_paths
print(file_paths)

打印输出:

your_file_001.jpg
your_file_002.jpg
…
a7qyws3x

a7qyws3x2#

我也遇到过类似的问题。解决方案是获取底层的tf。keras.preprocessing.image_dataset_from_directory函数并将'image_paths'变量添加到return语句。这不会引起计算开销,因为已经检索了文件名。
主函数代码取自GitHub: www.example.com
参见下文:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np

from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.keras.layers.preprocessing import image_preprocessing
from tensorflow.python.keras.preprocessing import dataset_utils
from tensorflow.python.ops import image_ops
from tensorflow.python.ops import io_ops
from tensorflow.python.util.tf_export import keras_export

WHITELIST_FORMATS = ('.bmp', '.gif', '.jpeg', '.jpg', '.png')

## Tensorflow override method to return fname as list as well as dataset

def image_dataset_from_directory(directory,
                                 labels='inferred',
                                 label_mode='int',
                                 class_names=None,
                                 color_mode='rgb',
                                 batch_size=32,
                                 image_size=(256, 256),
                                 shuffle=True,
                                 seed=None,
                                 validation_split=None,
                                 subset=None,
                                 interpolation='bilinear',
                                 follow_links=False):
  
  if labels != 'inferred':
    if not isinstance(labels, (list, tuple)):
      raise ValueError(
          '`labels` argument should be a list/tuple of integer labels, of '
          'the same size as the number of image files in the target '
          'directory. If you wish to infer the labels from the subdirectory '
          'names in the target directory, pass `labels="inferred"`. '
          'If you wish to get a dataset that only contains images '
          '(no labels), pass `label_mode=None`.')
    if class_names:
      raise ValueError('You can only pass `class_names` if the labels are '
                       'inferred from the subdirectory names in the target '
                       'directory (`labels="inferred"`).')
  if label_mode not in {'int', 'categorical', 'binary', None}:
    raise ValueError(
        '`label_mode` argument must be one of "int", "categorical", "binary", '
        'or None. Received: %s' % (label_mode,))
  if color_mode == 'rgb':
    num_channels = 3
  elif color_mode == 'rgba':
    num_channels = 4
  elif color_mode == 'grayscale':
    num_channels = 1
  else:
    raise ValueError(
        '`color_mode` must be one of {"rbg", "rgba", "grayscale"}. '
        'Received: %s' % (color_mode,))
  interpolation = image_preprocessing.get_interpolation(interpolation)
  dataset_utils.check_validation_split_arg(
      validation_split, subset, shuffle, seed)

  if seed is None:
    seed = np.random.randint(1e6)
  image_paths, labels, class_names = dataset_utils.index_directory(
      directory,
      labels,
      formats=WHITELIST_FORMATS,
      class_names=class_names,
      shuffle=shuffle,
      seed=seed,
      follow_links=follow_links)

  if label_mode == 'binary' and len(class_names) != 2:
    raise ValueError(
        'When passing `label_mode="binary", there must exactly 2 classes. '
        'Found the following classes: %s' % (class_names,))

  image_paths, labels = dataset_utils.get_training_or_validation_split(
      image_paths, labels, validation_split, subset)

  dataset = paths_and_labels_to_dataset(
      image_paths=image_paths,
      image_size=image_size,
      num_channels=num_channels,
      labels=labels,
      label_mode=label_mode,
      num_classes=len(class_names),
      interpolation=interpolation)
  if shuffle:
    # Shuffle locally at each iteration
    dataset = dataset.shuffle(buffer_size=batch_size * 8, seed=seed)
  dataset = dataset.batch(batch_size)
  # Users may need to reference `class_names`.
  dataset.class_names = class_names
  return dataset, image_paths

def paths_and_labels_to_dataset(image_paths,
                                image_size,
                                num_channels,
                                labels,
                                label_mode,
                                num_classes,
                                interpolation):
  """Constructs a dataset of images and labels."""
  # TODO(fchollet): consider making num_parallel_calls settable
  path_ds = dataset_ops.Dataset.from_tensor_slices(image_paths)
  img_ds = path_ds.map(
      lambda x: path_to_image(x, image_size, num_channels, interpolation))
  if label_mode:
    label_ds = dataset_utils.labels_to_dataset(labels, label_mode, num_classes)
    img_ds = dataset_ops.Dataset.zip((img_ds, label_ds))
  return img_ds

def path_to_image(path, image_size, num_channels, interpolation):
  img = io_ops.read_file(path)
  img = image_ops.decode_image(
      img, channels=num_channels, expand_animations=False)
  img = image_ops.resize_images_v2(img, image_size, method=interpolation)
  img.set_shape((image_size[0], image_size[1], num_channels))
  return img

其工作方式为:

train_dir = '/content/drive/My Drive/just_monkeying_around/monkey_training'
BATCH_SIZE = 32
IMG_SIZE = (224, 224)

train_dataset, train_paths = image_dataset_from_directory(train_dir,
                                             shuffle=True,
                                             batch_size=BATCH_SIZE,
                                             image_size=IMG_SIZE)

train_paths返回文件字符串列表。

bhmjp9jg

bhmjp9jg3#

Tensorflow 24数据集有一个名为的字段:file_paths因此可以使用它来获取文件路径。
如果您在数据集创建中使用shuffle=True,请注意,您必须在数据集创建代码中禁用此行(方法:image_dataset_from_directory):

if shuffle:
      # Shuffle locally at each iteration
      dataset = dataset.shuffle(buffer_size=batch_size * 8, seed=seed)
irlmq6kh

irlmq6kh4#

datagen = ImageDataGenerator()
test_data = datagen.flow_from_directory('.', classes=['test'])
test_data.filenames

基于此https://datascience.stackexchange.com/a/93308/149241
更多细节可以在这里以及https://kylewbanks.com/blog/loading-unlabeled-images-with-imagedatagenerator-flowfromdirectory-keras

相关问题