tensorflow中的自定义卷积

2024-05-16 09:30:18 发布

您现在位置:Python中文网/ 问答频道 /正文

让我假设我想改变卷积的核心,这样内核的WeigTs会根据内核中的传入值来改变。如何在tensorflow中编写这样一个层

 Kernel -> Image  

|a b c|    |2 3 2|
|d e f| -> |5 4 5|
|g h i|    |5 3 1|

----> because e would be 4, if both were overlapping, the other wheigths should change like f(x):

f(x)=x*wheigt

|4a 4b 4c|
|4d e  4f|
|4g 4h 4i|

所以,至少,改变中间的所有wheights,比如f(x)


Tags: theimage核心iftensorflowbekernel内核
1条回答
网友
1楼 · 发布于 2024-05-16 09:30:18

如果我理解正确,我认为这是您想要的(TF2.x,但在1.x中是相同的):

import tensorflow as tf

# Input data
kernel = tf.constant([[1., 2., 3.],
                      [4., 5., 6.],
                      [7., 8., 9.]], dtype=tf.float32)
img = tf.reshape(tf.range(90, dtype=tf.float32), [1, 5, 6, 3])
# Do separable convolution
kernel_t = tf.tile(kernel[:, :, tf.newaxis, tf.newaxis], [1, 1, 3, 1])
eye = tf.eye(3, 3, batch_shape=[1, 1])  # Pointwise filter does nothing
conv = tf.nn.separable_conv2d(img, kernel_t, eye, strides=[1, 1, 1, 1], padding='SAME')
# Scale convolution result and subtract the scaling for the central value
result = conv * img - kernel[1, 1] * img * (img - 1)

# Check result
kernel_np = kernel.numpy()
img_np = img.numpy()
result_np = result.numpy()
# Coordinates of checked result
i, j, c = 3, 4, 1
# Image value
v = img_np[0, i, j, c]
# Image window aroud value
img_w = img_np[0, i - 1:i + 2, j - 1:j + 2, c]
# Kernel scaled by image value except at center
kernel_scaled = kernel_np * v
kernel_scaled[1, 1] = kernel_np[1, 1]
# Compute output value
val_test = (img_w * kernel_scaled).sum()
# Check against TF calculation
val_result = result_np[0, i, j, c]
print(val_test == val_result)
# True

相关问题 更多 >