在 TF 数据集管道中调用 Keras 标准模型预处理函数

Calling Keras standard model preprocessing functions in TF Dataset pipeline

我正在使用 Keras 附带的一些标准 CNN 模型作为我自己模型的基础 - 假设是 VGG16。到目前为止,我习惯于通过 Keras 图像数据生成器调用相应的预处理函数,如下所示:

ImageDataGenerator(preprocessing_function=vgg16.preprocess_input)  # or any other std. model

现在我想改用 TF Dataset,这样我就可以使用它的 from_tensor_slices() 方法,这使得多 GPU 训练更容易。我为这个新管道想出了以下自定义预处理函数:

@tf.function
def load_images(image_path, label):
    image = tf.io.read_file(image_path)
    image = tf.image.decode_jpeg(image, channels=3)
    image = vgg16.preprocess_input(image)  # Is this call correct?
    image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
    return (image, label)

但我不确定这是否是函数调用的正确顺序,以及在此顺序中调用 vgg16.preprocess_input(image) 的正确位置。我可以称之为 std.像这样的预处理功能,还是我需要将image数据转换成before/after那个?

您可以使用路径和标签创建数据集 from_tensor_slices(),然后使用 map 加载和预处理图像:

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy
from PIL import Image

# Create random images
for i in range(3):
  imarray = numpy.random.rand(100,100,3) * 255
  im = Image.fromarray(imarray.astype('uint8'))
  im.save('result_image{}.jpeg'.format(i))

def load_images(image_path, label):
    image = tf.io.read_file(image_path)
    image = tf.image.decode_jpeg(image, channels=3)
    
    #preprocess_input --> will convert the input images from RGB to BGR, then will zero-center each color channel with respect to the ImageNet dataset, without scaling
    image = tf.keras.applications.vgg16.preprocess_input(image)
    image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
    image /= 255.0 
    return image, label

IMG_SIZE = 64
paths = ['result_image0.jpeg', 'result_image1.jpeg', 'result_image2.jpeg']
labels = [0, 1, 1]

dataset = tf.data.Dataset.from_tensor_slices((paths, labels))
ds = dataset.map(load_images)

image, _ = next(iter(ds.take(1)))
plt.imshow(image)

或者您可以使用 tf.keras.applications.vgg16.preprocess_input 作为模型的一部分。例如:

preprocess = tf.keras.applications.vgg16.preprocess_input

some_input = tf.keras.layers.Input((256, 256, 3))
some_output = tf.keras.layers.Lambda(preprocess)(some_input)
model = tf.keras.Model(some_input, some_output)

model(tf.random.normal((2, 256, 256, 3)))