繁体   English   中英

如何对 tf.data.Dataset 的图像应用预处理?

[英]How to apply pre-processing to images of a tf.data.Dataset?

如果我理解正确,而不是像这样将完整的数据集加载到 memory 中:

images = []
file_list = glob.glob('path/to/images/*.jpg')
for file in file_list:
    images.append(img_to_array(load_img(file, target_size=input_shape)))

images = np.stack(images, axis=0)
images = preprocess(images)

# classify the image
print("[INFO] classifying image with '{}'...".format(used_model))
predictions = model.predict(images)
decoded_predictions = imagenet_utils.decode_predictions(predictions)

应该使用 tensorflows 数据实用程序以获得更好的 memory 管理和性能:

images = tf.keras.utils.image_dataset_from_directory(file_path, image_size=input_shape, labels=None)
AUTOTUNE = tf.data.AUTOTUNE
images = images.prefetch(buffer_size=AUTOTUNE)

# this line will now crash
images = preprocess(images)


# classify the image
print("[INFO] classifying image with '{}'...".format(used_model))
predictions = model.predict(images)
decoded_predictions = imagenet_utils.decode_predictions(predictions)

如上面的代码所写,我知道有不同的数据结构,它们不能用于相同的代码。 我的问题是:如何对我的数据进行预处理? 对应的教程好像都在讲训练,而我想做简单的推理。

附加问题:如果数据来自 S3 存储桶(脚本在 Airflow-DAG 中运行),将如何完成?

您可以使用tf.data.Dataset.map对图像或图像批次应用预处理。 这是一个例子:

import tensorflow as tf
import pathlib

dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)

batch_size = 32

train_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  seed=123,
  image_size=(180, 180),
  batch_size=batch_size)

scale_layer = tf.keras.layers.Rescaling(1./255)

def preprocess(images, labels):
  images = tf.image.resize(scale_layer(images),[120, 120], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
  return images, labels

train_ds = train_ds.map(preprocess)

在你的例子中,你只有图像,所以你可以忽略这里的标签。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM