Tensorflow 中的图像卷积 [英] Image over Image convolution in Tensorflow

查看:40
本文介绍了Tensorflow 中的图像卷积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设,我有两组图像 A 和 B,每组 11X5x5x3,其中 11 是示例数量,5x5x3 是图像尺寸.
Tensorflow 中是否有一种简单的方法可以将 A_i 中的每个图像应用于 B_i 上的每个图像(即 B_i 起着过滤器的作用,而 A_i 是 tf.conv2d 中的输入)?例如,conv2d(A_1,B_1), conv2d(A_2,B_2),...,conv2d(A_11,B_11)
这里没有权重学习只是想在一个图像上应用卷积在另一个图像上.我试着这样做:

Assume, I have two set of images, A and B, each 11X5x5x3, where 11 is a number of examples and 5x5x3 is an image dimension.
Is there an easy way in Tensorflow to apply convolution for each image in A_i over B_i (i.e. B_i plays a filter role and A_i is an input in tf.conv2d)? For example, conv2d(A_1,B_1), conv2d(A_2,B_2),...,conv2d(A_11,B_11)
No weight learning here just wanted to apply convolution on one image over another. I tried to do it as follow:

# change B to 5x5x3x11 to be compatible with tf convolution.
tf.nn.conv2d(A, B, strides=[1,1,1,1], padding ='SAME' )

但问题在于它在所有 B_i 上的每个 A_i 上应用卷积.我不想要这个,我只想要 A_i 超过 B_j,其中 i==j.当然我可以一个一个做,但是效率不高,需要批量处理.

but the problem with this is that it applies convolution on every A_i over all B_i's. I don't want this, I want A_i only over B_j where i==j. Of course I can do it one by one, but it wouldn't be efficient and I need to do it in a batch mode.

任何评论如何解决这个问题?

Any comment how to solve this problem?

谢谢.J

推荐答案

我不确定这是否是您需要的,因为它不是真正的批处理模式,但您可以使用地图功能:

I am not sure that is what you need because it is not really batch mode but you could use a map function :

A = tf.placeholder(dtype=tf.float32, shape=[None, 5, 5, 3])
B = tf.placeholder(dtype=tf.float32, shape=[None, 5, 5, 3])

output = tf.map_fn(
    lambda inputs : tf.nn.conv2d(
        tf.expand_dims(inputs[0], 0),  # H,W,C -> 1,H,W,C
        tf.expand_dims(inputs[1], 3),  # H,W,C -> H,W,C,1
        strides=[1,1,1,1],
        padding="SAME"
    ),  # Result of conv is 1,H,W,1
   elems=[A,B],
   dtype=tf.float32
)
final_output = output[:, 0, :, :, 0]  # B,1,H,W,1 -> B,H,W

性能将取决于我猜这些微小的分离卷积如何并行化.

Performance will depend on how the tiny separate convolutions will be parallelized I guess.

这篇关于Tensorflow 中的图像卷积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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