fea*_*eer 2 dataset tensorflow
我希望我的部分数据增强将高斯模糊应用于我的训练数据。
为此,我创建了一个自定义 Initializer 类,该类初始化 DepthwiseConv2d 以获得所需的高斯内核。
但我收到以下错误:
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]
Run Code Online (Sandbox Code Playgroud)
这是一个简单的工作示例:
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)
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
还不确定上面的代码有什么问题,所以我不会接受我自己的答案,希望其他人能给出一个更好的答案来解释问题所在......编辑:由于没有人插手,我选择我自己的答案。
但是,我确实设法创建了一个工作高斯模糊过滤器,编写起来更简单,使用 tf.nn 而不是 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)
Run Code Online (Sandbox Code Playgroud)
按预期工作。
归档时间: |
|
查看次数: |
4467 次 |
最近记录: |