【问题标题】:How do I change the decoder feature type in Tensorflow Datasets?如何更改 Tensorflow 数据集中的解码器特征类型?
【发布时间】:2021-05-24 07:49:06
【问题描述】:

我正在尝试从这里对 tensorflow 图像数据集(例如 cifar 和其他数据集)进行一些扩充: https://www.tensorflow.org/datasets/catalog/

现在我有一个映射函数,实际上来自 SO 的另一个用户帮助我使用了我自己的数据集:

def map_data(inputs):
    image = inputs['image']
    image = tf.numpy_function(func=aug_fn, inp=[image], Tout=tf.float32)
    image = image / 255.0

    labels = inputs['label']
    labels = tf.one_hot(labels, num_classes)

return {'image_input': image, 'label': labels}, labels

现在在迭代数据集时出现此错误:

ValueError: Missing data for input "image_input". You passed a data dictionary with keys ['image', 'label']. Expected the following keys: ['image_input', 'label']

这是有道理的,因为解码器返回 uint8 类型。

但我在文档中找不到有关如何更改它的任何信息或示例。

我能以某种方式访问​​解码器对象的属性吗?

我在 API 中尝试过 https://www.tensorflow.org/datasets/api_docs/python/tfds/decode/Decoder?hl=cs

但它不起作用。

非常感谢您!

【问题讨论】:

    标签: tensorflow keras tensorflow-datasets


    【解决方案1】:

    如果你想使用增强,你应该在模型的顶部使用这些层

        data_augmentation = tf.keras.Sequential([
        tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal'),
        tf.keras.layers.experimental.preprocessing.RandomRotation(0.2),
        tf.keras.layers.experimental.preprocessing.RandomZoom(0.1),
        ])
    

    对于缩放,你使用这条线

    rescale = tf.keras.layers.experimental.preprocessing.Rescaling(1. / 255)
    

    这应该可以在您的模型中顺利运行。这是一个具有增强和重新缩放的模型示例

        model = Sequential([
        layers.experimental.preprocessing.Rescaling(1. / 255, input_shape=(256, 256, 3)),
        layers.experimental.preprocessing.RandomFlip('horizontal'),
        layers.experimental.preprocessing.RandomRotation(0.2),
        layers.experimental.preprocessing.RandomZoom(0.1),
        layers.Conv2D(16, 3, padding='same', activation='relu'),
        layers.MaxPooling2D(),
        layers.Conv2D(32, 3, padding='same', activation='relu'),
        layers.MaxPooling2D(),
        layers.Conv2D(64, 3, padding='same', activation='relu'),
        layers.MaxPooling2D(),
        layers.Flatten(),
        layers.Dense(128, activation='relu'),
        layers.Dense(num_classes)
    ])
    

    【讨论】:

    • 嗨,Amel,谢谢,我知道这些层并且以前使用过它们,但是我用这些层做的很多事情是我不能用这些层做的。不过还是谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    相关资源
    最近更新 更多