来自 tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory 的 tf.data.Dataset? [英] tf.data.Dataset from tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory?

查看:57
本文介绍了来自 tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory 的 tf.data.Dataset?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建tf.data.Dataset 来自 <代码>tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory?

我正在考虑 tf.data.Dataset.from_generator,但不清楚如何为它获取 output_types 关键字参数,给定返回类型:

I'm considering tf.data.Dataset.from_generator, but it's unclear how to acquire the output_types keyword argument for it, given the return type:

A DirectoryIterator 产生 (x, y) 元组,其中 x 是一个包含一批形状为 的图像的 numpy 数组(batch_size, *target_size, channels)y 是对应标签的 numpy 数组.

A DirectoryIterator yielding tuples of (x, y) where x is a numpy array containing a batch of images with shape (batch_size, *target_size, channels) and y is a numpy array of corresponding labels.

推荐答案

Both batch_xbatch_y 属于 K.floatx() 类型,所以必须默认为 tf.float32.

Both batch_x and batch_y in ImageDataGenerator are of type K.floatx(), so must be tf.float32 by default.

如何使用 Keras 生成器已经讨论过类似的问题使用 tf.data API.让我从那里复制粘贴答案:

Similar question was discussed already at How to use Keras generator with tf.data API. Let me copy-paste the answer from there:

def make_generator():
    train_datagen = ImageDataGenerator(rescale=1. / 255)
    train_generator = 
    train_datagen.flow_from_directory(train_dataset_folder,target_size=(224, 224), class_mode='categorical', batch_size=32)
    return train_generator

train_dataset = tf.data.Dataset.from_generator(make_generator,(tf.float32, tf.float32))

作者在图形范围方面遇到了另一个问题,但我想这与您的问题无关.

The author faced another issue with the graph scope, but I guess it is unrelated to your question.

或者作为单衬:

tf.data.Dataset.from_generator(lambda:
    ImageDataGenerator().flow_from_directory('folder_path'),(tf.float32, tf.float32))

这篇关于来自 tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory 的 tf.data.Dataset?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆