我有一个特殊的卷积内核:1)它的尺寸很大(600x600); 2)它是一个稀疏过滤器,主要由0个值和一些1s组成。我想将此内核应用于另一个大图像(2000x2000)。由于过滤器只有0和1,因此在这种特殊情况下的卷积运算等效于以下步骤:
1)计算相对于卷积滤波器中心点的1s坐标; 2)通过每个相对坐标转换图像; 3)将翻译结果加起来。
假设总共有n
个1,那么以上将导致n
的翻译。内存中不能有n
个图像,因为所需的空间为nx2000x2000
,这将导致OOM。我尝试使用while循环来节省内存空间,并且代码如下所示:
def sum_conv(acc, curr):
"""
Apply translation to implement convolution. Summation is done in while loop.
:param acc: (batch, size, size, 1)
:param curr: (batch, 2)
:return:
"""
translation = tf.reshape(curr, [batch_size, 2])
trans_x = tf.expand_dims(translation[:, 1], axis=-1)
trans_y = tf.expand_dims(translation[:, 0], axis=-1)
ones, zeros = tf.ones_like(trans_x), tf.zeros_like(trans_x)
transform = tf.concat([ones, zeros, trans_x, zeros, ones, trans_y, zeros, zeros], axis=-1)
# Image: (batch, size, size, 1)
conv_image = tf.contrib.image.transform(image, transform)
return acc + conv_image
# Points: (num_points, batch, 2)
# Loop on points (i.e. positions of 1s) to get the results of convolution
i0 = tf.constant(0)
conv_image0 = tf.zeros((batch_size, size, size, 1))
c = lambda i, prev: i < num_points
b = lambda i, prev: (i + 1, sum_conv(prev, tf.gather(points, i, axis=0)))
i, conv_images = tf.while_loop(c, b, (i0, conv_image0))
我有两个问题。
1)有人可以帮助我提出上述算法的更简单实现吗?
2)TensorFlow中是否有任何支持稀疏卷积的操作?