CNN 中的 8 个预定义二进制卷积滤波器 - Tensorflow
8 Predefined binary convolutional filters in CNN - Tensorflow
我正在尝试在 Tensorflow 中执行以下操作:
I need this
为此我到目前为止做了以下工作:
def new_weights(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.05))
# From here down is another function
shape = [3, 3, 1, 8,]
H = [
[[0, 1, 0,],[0, -1, 0,],[0, 0, 0,],],
[[0, 0, 1,],[0, -1, 0,],[0, 0, 0,],],
[[0, 0, 0,],[0, -1, 1,],[0, 0, 0,],],
[[0, 0, 0,],[0, -1, 0,],[0, 0, 1,],],
[[0, 0, 0,],[0, -1, 0,],[0, 1, 0,],],
[[0, 0, 0,],[0, -1, 0,],[1, 0, 0,],],
[[0, 0, 0,],[1, -1, 0,],[0, 0, 0,],],
[[1, 0, 0,],[0, -1, 0,],[0, 0, 0,],]
]
anchor_weights = tf.reshape(tf.cast(H, tf.float32), shape)
layer = tf.nn.conv2d(input=input,
filter=anchor_weights,
strides=[1, 1, 1, 1],
padding='SAME')
layer = tf.nn.max_pool(value=layer,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME')
layer = tf.nn.relu(layer)
feature_maps = layer
shape = [1, 1, 8, 1]
v = tf.cast(new_weights(shape), tf.float32)
# To put all together
layer = tf.nn.conv2d(input=feature_maps,
filter=v,
strides=[1, 1, 1, 1],
padding="SAME")
但是当我打印 anchor_weights 和 feature_maps 时,我会这样做作为回应。
anchor_weights e feature_maps
对我来说,我需要的东西似乎是完全错误的,就像我给你看的第一张图片一样。
我不知道如何解决这个问题,有什么想法吗?
我认为您在 H
数组中的元素顺序有误。您将 H
填充为 [8,3,3]
张量。然后你使用 tf.reshape
,这会改变张量的形状,但不会交换元素。你需要这样的东西:
H = [
[
[[0, 0, 0, 0, 0, 0, 0, 1]],
[[1, 0, 0, 0, 0, 0, 0, 0]],
[[0, 1, 0, 0, 0, 0, 0, 0]]
],
[
[[ 0, 0, 0, 0, 0, 0, 1, 0]],
[[-1,-1,-1,-1,-1,-1,-1,-1]],
[[ 0, 0, 1, 0, 0, 0, 0, 0]]
],
[
[[0, 0, 0, 0, 0, 1, 0, 0]],
[[0, 0, 0, 0, 1, 0, 0, 0]],
[[0, 0, 1, 0, 0, 0, 0, 0]]
]
]
anchor_weights = tf.cast(H, tf.float32)
我正在尝试在 Tensorflow 中执行以下操作:
I need this
为此我到目前为止做了以下工作:
def new_weights(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.05))
# From here down is another function
shape = [3, 3, 1, 8,]
H = [
[[0, 1, 0,],[0, -1, 0,],[0, 0, 0,],],
[[0, 0, 1,],[0, -1, 0,],[0, 0, 0,],],
[[0, 0, 0,],[0, -1, 1,],[0, 0, 0,],],
[[0, 0, 0,],[0, -1, 0,],[0, 0, 1,],],
[[0, 0, 0,],[0, -1, 0,],[0, 1, 0,],],
[[0, 0, 0,],[0, -1, 0,],[1, 0, 0,],],
[[0, 0, 0,],[1, -1, 0,],[0, 0, 0,],],
[[1, 0, 0,],[0, -1, 0,],[0, 0, 0,],]
]
anchor_weights = tf.reshape(tf.cast(H, tf.float32), shape)
layer = tf.nn.conv2d(input=input,
filter=anchor_weights,
strides=[1, 1, 1, 1],
padding='SAME')
layer = tf.nn.max_pool(value=layer,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME')
layer = tf.nn.relu(layer)
feature_maps = layer
shape = [1, 1, 8, 1]
v = tf.cast(new_weights(shape), tf.float32)
# To put all together
layer = tf.nn.conv2d(input=feature_maps,
filter=v,
strides=[1, 1, 1, 1],
padding="SAME")
但是当我打印 anchor_weights 和 feature_maps 时,我会这样做作为回应。
anchor_weights e feature_maps
对我来说,我需要的东西似乎是完全错误的,就像我给你看的第一张图片一样。
我不知道如何解决这个问题,有什么想法吗?
我认为您在 H
数组中的元素顺序有误。您将 H
填充为 [8,3,3]
张量。然后你使用 tf.reshape
,这会改变张量的形状,但不会交换元素。你需要这样的东西:
H = [
[
[[0, 0, 0, 0, 0, 0, 0, 1]],
[[1, 0, 0, 0, 0, 0, 0, 0]],
[[0, 1, 0, 0, 0, 0, 0, 0]]
],
[
[[ 0, 0, 0, 0, 0, 0, 1, 0]],
[[-1,-1,-1,-1,-1,-1,-1,-1]],
[[ 0, 0, 1, 0, 0, 0, 0, 0]]
],
[
[[0, 0, 0, 0, 0, 1, 0, 0]],
[[0, 0, 0, 0, 1, 0, 0, 0]],
[[0, 0, 1, 0, 0, 0, 0, 0]]
]
]
anchor_weights = tf.cast(H, tf.float32)