我正在使用 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)在此順序中呼叫的正確位置。我可以稱之為標準嗎?預處理功能如此,還是我需要image在此之前/之后轉換資料?
uj5u.com熱心網友回復:
您可以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)))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/368283.html
