[英]How can I apply a TensorFlow 2D Convolution (tf.nn.conv2d) to a single (non-batch) 2D image?
I would like to use the function tf.nn.conv2d()
on a single image example, but the TensorFlow documentation seems to only mention applying this transformation to a batch of images. 我想在单个图像示例上使用函数
tf.nn.conv2d()
,但是TensorFlow文档似乎只提到将这种转换应用于一批图像。
The docs mention that the input image must be of shape [batch, in_height, in_width, in_channels]
and the kernel must be of shape [filter_height, filter_width, in_channels, out_channels]
. 文档提到输入图像必须为
[batch, in_height, in_width, in_channels]
形状,内核必须为[filter_height, filter_width, in_channels, out_channels]
形状。 However, what is the most straightforward way to achieve 2D convolution with input shape [in_height, in_width, in_channels]
? 但是,用输入形状
[in_height, in_width, in_channels]
实现2D卷积的最直接方法是什么?
Here is an example of the current approach, where img
has shape (height, width, channels): 这是当前方法的示例,其中
img
具有形状(高度,宽度,通道):
img = tf.random_uniform((10,10,3)) # a single image
img = tf.nn.conv2d([img], kernel)[0] # creating a batch of 1, then indexing the single example
I am reshaping the input as follows: 我正在重塑输入,如下所示:
[in_height, in_width, in_channels]->[1, in_height, in_width, in_channels]->[in_height, in_width, in_channels]
This feels like an unnecessary and costly operation when I am only interested in transforming one example. 当我仅对转换一个示例感兴趣时,这感觉像是不必要且昂贵的操作。
Is there a simple/standard way to do this that doesn't involve reshaping? 有没有简单/标准的方法可以完成此操作,而无需进行整形?
AFAIK there is no way around it. AFAIK无法解决。 It seems ( here and here ) that the first operation creates a copy (someone correct me if I'm wrong).
似乎( 在这里和这里 )第一个操作创建了一个副本(如果我错了,请纠正我)。 You may use
tf.expand_dims
instead though, it's IMO more readable because of it's verbosity. 但是,您可以改用
tf.expand_dims
,因为它的冗长性使IMO更具可读性。
On the other hand, taking 0
element from the tensor should not perform a copy in this case and is almost free. 另一方面,从张量取
0
元素在这种情况下不应该执行复制,并且几乎是免费的。
Most importantly , except for a little inconvenience with syntax (eg [0]
) those operations definitely are not costly , especially in the context of performing convolution. 最重要的是 ,除了语法上的一些不便(例如
[0]
),这些操作绝对不会花费很多 ,尤其是在执行卷积的情况下。
BTW. 顺便说一句。 Other ready alternative layers like the ones in
tf.keras
, require batch as first dimension as well. 其他可用的替代层(例如
tf.keras
层)也需要批量作为第一维。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.