输入数据集和输出数据集的 Tensorflow image_dataset_from_directory
Tensorflow image_dataset_from_directory for input dataset and output dataset
我正在尝试学习图像自动编码,但我无法使用输入和输出图像来训练模型
例如:
输入图像文件夹:“.../Pictures/Input”
输出图像文件夹:“.../Pictures/Output”
#get input images from data_dir_input
ds_input = tf.keras.preprocessing.image_dataset_from_directory(
data_dir_input,
seed=123,
image_size=(img_height, img_width),
label_mode=None,
batch_size=batch_size)
#get output images from data_dir_output
ds_output = tf.keras.preprocessing.image_dataset_from_directory(
data_dir_output,
seed=123,
image_size=(img_height, img_width),
label_mode=None,
batch_size=batch_size)
# --------- model init etc --------------
# ...
model.fit(x=ds_input, y=ds_output, batch_size=32, epochs=50)
但是我这样说时出错:
`y` argument is not supported when using dataset as input
如何在训练模型时使用自己的输入图像和输出图像?
您可以使用 tf.data.Dataset
以获得更大的灵活性。据我了解,image_dataset_from_directory
不支持除整数以外的任何自定义标签。
试试这个:
import os
import tensorflow as tf
os.chdir(r'c:/users/user/Pictures')
from glob2 import glob
x_files = glob('inputs/*.jpg')
y_files = glob('targets/*.jpg')
files_ds = tf.data.Dataset.from_tensor_slices((x_files, y_files))
def process_img(file_path):
img = tf.io.read_file(file_path)
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
img = tf.image.resize(img, size=(28, 28))
return img
files_ds = files_ds.map(lambda x, y: (process_img(x), process_img(y))).batch(1)
original, target = next(iter(files_ds))
<tf.Tensor: shape=(1, 28, 28, 3), dtype=float32, numpy=
array([[[[0.357423 , 0.3325731 , 0.20412168],
[0.36274514, 0.21940777, 0.17623049],
[0.34821934, 0.13921566, 0.06858743],
...,
[0.25486213, 0.27446997, 0.2520612 ],
[0.04925931, 0.26666668, 0.07619007],
[0.48167226, 0.5287311 , 0.520888 ]]]
那么您就不需要将 y
传递给 fit()
调用。您将可以这样使用它:
model.fit(ds, epochs=5)
我正在尝试学习图像自动编码,但我无法使用输入和输出图像来训练模型
例如:
输入图像文件夹:“.../Pictures/Input”
输出图像文件夹:“.../Pictures/Output”
#get input images from data_dir_input
ds_input = tf.keras.preprocessing.image_dataset_from_directory(
data_dir_input,
seed=123,
image_size=(img_height, img_width),
label_mode=None,
batch_size=batch_size)
#get output images from data_dir_output
ds_output = tf.keras.preprocessing.image_dataset_from_directory(
data_dir_output,
seed=123,
image_size=(img_height, img_width),
label_mode=None,
batch_size=batch_size)
# --------- model init etc --------------
# ...
model.fit(x=ds_input, y=ds_output, batch_size=32, epochs=50)
但是我这样说时出错:
`y` argument is not supported when using dataset as input
如何在训练模型时使用自己的输入图像和输出图像?
您可以使用 tf.data.Dataset
以获得更大的灵活性。据我了解,image_dataset_from_directory
不支持除整数以外的任何自定义标签。
试试这个:
import os
import tensorflow as tf
os.chdir(r'c:/users/user/Pictures')
from glob2 import glob
x_files = glob('inputs/*.jpg')
y_files = glob('targets/*.jpg')
files_ds = tf.data.Dataset.from_tensor_slices((x_files, y_files))
def process_img(file_path):
img = tf.io.read_file(file_path)
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
img = tf.image.resize(img, size=(28, 28))
return img
files_ds = files_ds.map(lambda x, y: (process_img(x), process_img(y))).batch(1)
original, target = next(iter(files_ds))
<tf.Tensor: shape=(1, 28, 28, 3), dtype=float32, numpy=
array([[[[0.357423 , 0.3325731 , 0.20412168],
[0.36274514, 0.21940777, 0.17623049],
[0.34821934, 0.13921566, 0.06858743],
...,
[0.25486213, 0.27446997, 0.2520612 ],
[0.04925931, 0.26666668, 0.07619007],
[0.48167226, 0.5287311 , 0.520888 ]]]
那么您就不需要将 y
传递给 fit()
调用。您将可以这样使用它:
model.fit(ds, epochs=5)