Python Tensorflow图像\u数据集\u来自输入数据集和输出数据集的\u目录
我试图学习图像自动编码,但我不能使用输入和输出图像来训练模型 例: 输入图像文件夹:“…/Pictures/input”Python Tensorflow图像\u数据集\u来自输入数据集和输出数据集的\u目录,python,tensorflow,image-processing,keras,autoencoder,Python,Tensorflow,Image Processing,Keras,Autoencoder,我试图学习图像自动编码,但我不能使用输入和输出图像来训练模型 例: 输入图像文件夹:“…/Pictures/input” 输出图像文件夹:“…/Pictures/output” 但我有一个错误的说法: 使用数据集作为输入时不支持`y`参数 在训练模型时,如何使用自己的输入图像和输出图像?您可以使用tf.data.Dataset来获得更大的灵活性。从我读到的,image\u dataset\u From\u directory不支持除整数以外的任何自定义标签 试试这个: import os imp
输出图像文件夹:“…/Pictures/output” 但我有一个错误的说法: 使用数据集作为输入时不支持`y`参数
在训练模型时,如何使用自己的输入图像和输出图像?您可以使用
tf.data.Dataset
来获得更大的灵活性。从我读到的,image\u dataset\u From\u 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))
感谢您的回复,但是我的输入和输出目录不同,所以我不能为bothOK传递相同的图像。我在编辑答案时考虑到了这个限制。然后,您只需要获得x和y的匹配文件列表。只需用对您的任务有意义的文件列表替换
os.listdir()
。确保它与相应的y正确排序。当我使用新代码块时,我得到以下错误OP\u REQUIRES在整个文件中失败\u read\u ops.cc:116:未找到:NewRandomAccessFile未能创建/打开:000128\u 17.png:系统找不到指定的文件
(顺便说一句,我将decode\u jpeg改为decode\u png)这可能是因为'000128_17.png'
没有文件夹名。我用glob2.glob
为您做了一个例子,它解决了这个问题。谢谢,我可以像重新缩放、旋转等那样扩展这个数据集吗image\u dataset\u from\u 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 ]]]
model.fit(ds, epochs=5)