【发布时间】:2020-04-04 18:11:20
【问题描述】:
我希望我的部分数据增强将高斯模糊应用于我的训练数据。
为此,我创建了一个自定义 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]
这是一个简单的工作示例:
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)
我怎样才能解决这个问题?
【问题讨论】:
标签: tensorflow dataset