张量流中数据集管道中的高斯模糊图像 [英] Gaussian blur image in dataset pipeline in tensorflow

查看:98
本文介绍了张量流中数据集管道中的高斯模糊图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望部分数据扩充可以对训练数据应用高斯模糊。

I want part of my data augmentation to apply a Gaussian blur to my training data.

为此,我创建了一个自定义的Initializer类,该类初始化了DepthwiseConv2d具有所需的高斯核。

For that end, I have created a custom Initializer class, which initializes a DepthwiseConv2d to have the desired Gaussian kernel.

但是我收到以下错误:

tensorflow.python.framework.errors_impl.FailedPreconditionError:  {{function_node __inference_Dataset_map_<lambda>_67}} Error while reading resource variable _AnonymousVar0 from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/_AnonymousVar0/class tensorflow::Var does not exist.
     [[{{node depthwise_conv2d/depthwise/ReadVariableOp}}]]
     [[IteratorGetNext]] [Op:__inference_distributed_function_694]

这是一个简单的工作示例:

Here's a simple working example:

import tensorflow as tf


class GaussianInitializer(tf.keras.initializers.Initializer):
    def __init__(self):
        super().__init__()
        self.sigma = 2

    def _gaussian_kernel(self, kernel_size, dtype):
        x = tf.range(-kernel_size // 2 + 1, kernel_size // 2 + 1, dtype=dtype)
        g = tf.math.exp(-(tf.pow(x, 2) / (2 * tf.pow(tf.cast(self.sigma, dtype), 2))))
        g_norm2d = tf.pow(tf.reduce_sum(g), 2)
        return tf.tensordot(g, g, axes=0) / g_norm2d

    def __call__(self, shape, dtype):
        kernel = tf.expand_dims(self._gaussian_kernel(shape[0], dtype), axis=-1)
        return tf.expand_dims(tf.tile(kernel, (1, 1, shape[2])), axis=-1)


def gaussian_blur_img(img):
    blur_layer = tf.keras.layers.DepthwiseConv2D(
        kernel_size=5, padding='same', use_bias=False,
        depthwise_initializer=GaussianInitializer(), dtype=img.dtype
    )
    blur_layer.trainable = False
    return tf.squeeze(blur_layer(tf.expand_dims(img, axis=0)), axis=0)


data = tf.data.Dataset.from_tensor_slices(
    (tf.ones((1, 10, 10, 3)), tf.ones((1, 10, 10, 1)))
).map(lambda x, y: (gaussian_blur_img(x), y)).repeat().batch(10)


x = tf.keras.layers.Input((10, 10, 3))
y = tf.keras.layers.Conv2D(filters=1, kernel_size=1, activation=tf.keras.activations.relu)(x)

model = tf.keras.models.Model(inputs=[x], outputs=[y])
model.compile(loss=tf.losses.binary_crossentropy)
model.fit(data, steps_per_epoch=10, epochs=10)

如何解决此问题?

推荐答案

不确定上面的代码有什么问题,所以我不会接受自己的回答,希望其他人会这样做提供一个更好的解释错误的方法...编辑:由于没有人介入,我选择了自己的答案。

Not sure yet what is wrong with the code above, so I won't accept my own answer in hope someone else would give a better one which does explain what's wrong... due to no one chipping in, I'm choosing my own answer.

但是,我确实设法创建一个工作的高斯模糊滤镜,使用tf.nn而不是tf.keras.layers编写起来更简单:

However, I did manage to create a working Gaussian blur filter, and it's even simpler to write, using tf.nn instead of tf.keras.layers:

def _gaussian_kernel(kernel_size, sigma, n_channels, dtype):
    x = tf.range(-kernel_size // 2 + 1, kernel_size // 2 + 1, dtype=dtype)
    g = tf.math.exp(-(tf.pow(x, 2) / (2 * tf.pow(tf.cast(sigma, dtype), 2))))
    g_norm2d = tf.pow(tf.reduce_sum(g), 2)
    g_kernel = tf.tensordot(g, g, axes=0) / g_norm2d
    g_kernel = tf.expand_dims(g_kernel, axis=-1)
    return tf.expand_dims(tf.tile(g_kernel, (1, 1, n_channels)), axis=-1)


def apply_blur(img):
    blur = _gaussian_kernel(3, 2, 3, img.dtype)
    img = tf.nn.depthwise_conv2d(img[None], blur, [1,1,1,1], 'SAME')
    return img[0]

data = tf.data.Dataset.from_tensor_slices(
    (tf.pad(tf.ones((1, 1, 1, 2)), ((0, 0),(1, 1),(1, 1),(0,1))), tf.ones((1, 3, 3, 1)))
).map(lambda x, y: (apply_blur(x), y)).repeat().batch(10)

按预期工作。

这篇关于张量流中数据集管道中的高斯模糊图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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