简体   繁体   English

Tensorflow 元素数量可变的数据集

[英]Tensorflow dataset with variable number of elements

I need a dataset structured to handle a variable number of input images (a set of images) to regress against an integer target variable.我需要一个结构化的数据集来处理可变数量的输入图像(一组图像)以针对 integer 目标变量进行回归。

The code I am using to source the images is like this:我用来获取图像的代码是这样的:

import tensorflow as tf
from tensorflow import convert_to_tensor


def read_image_tf(path: str) -> tf.Tensor:
    image = tf.keras.utils.load_img(path)
    return tf.keras.utils.img_to_array(image)

def read_image_list(x, y):
    return tf.map_fn(read_image_tf, x), y


paths_list = [['image_1', 'image_2', 'image_3'], ['image_6'], ['image_4', 'image_5', 'image_8', 'image_19']]

x = tf.ragged.constant(paths_list)
y = tf.constant([1,2,3])

dataset = tf.data.Dataset.from_tensor_slices((x, y))
dataset = dataset.map(lambda x,y: read_image_list(x,y))

This code breaks with TypeError ( TypeError: path should be path-like or io.BytesIO, not <class 'tensorflow.python.framework.ops.Tensor'> ), as it seems that the map operation is not extracting the paths correctly from the original RaggedTensor .此代码因 TypeError( TypeError: path should be path-like or io.BytesIO, not <class 'tensorflow.python.framework.ops.Tensor'> )而中断,因为map操作似乎没有从中正确提取路径原始的RaggedTensor I have also tried to use a generator with similar results.我也尝试过使用具有类似结果的生成器。 Any help would be much appreciated任何帮助将非常感激

Maybe something like this:也许是这样的:

import tensorflow as tf

def read_image_tf(path: str) -> tf.Tensor:
    img = tf.io.read_file(path)
    return tf.io.decode_png(img, channels=3) # more generic: tf.io.decode_image

def read_image_list(x, y):
    return tf.map_fn(read_image_tf, x, dtype=tf.uint8), y

paths_list = [['/content/image1.png', '/content/image1.png', '/content/image1.png'], ['/content/image1.png'], ['/content/image1.png', '/content/image1.png', '/content/image1.png', '/content/image1.png']]

x = tf.ragged.constant(paths_list)
y = tf.constant([1,2,3])

dataset = tf.data.Dataset.from_tensor_slices((x, y))
dataset = dataset.map(lambda x, y: read_image_list(x, y))

for x, y in dataset:
  print(x.shape, y)
(3, 100, 100, 3) tf.Tensor(1, shape=(), dtype=int32)
(1, 100, 100, 3) tf.Tensor(2, shape=(), dtype=int32)
(4, 100, 100, 3) tf.Tensor(3, shape=(), dtype=int32)

You can also convert x back to a ragged tensor if you want.如果需要,您还可以将x转换回参差不齐的张量。

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

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