我想在与尺寸为[[batch_size,H,W,n_channels]]的矩阵中每个像素的深度通道对应的每个向量上映射一个TensorFlow函数。 换句话说,对于我批量处理的每个尺寸为[[H x W
的图像:我提取了一些具有相同大小H x W]的特征图
这类似于将1x1卷积应用于矩阵;但是,我需要在深度通道上应用更通用的功能,而不是简单的求和运算。
我认为tf.map_fn()
可能是一个选项,我尝试了以下解决方案,在该解决方案中,我递归使用tf.map_fn()
访问与每个像素相关的特征。但是,这种情况似乎不太理想,最重要的是
在尝试反向传播渐变时会引发错误
。您是否知道发生这种情况的原因,以及如何构造代码以避免错误?这是我当前对该函数的实现:
import tensorflow as tf
from tensorflow import layers
def apply_function_on_pixel_features(incoming):
# at first the input is [None, W, H, n_channels]
if len(incoming.get_shape()) > 1:
return tf.map_fn(lambda x: apply_function_on_pixel_features(x), incoming)
else:
# here the input is [n_channels]
# apply some function that applies a transfomration and returns a vetor of the same size
output = my_custom_fun(incoming) # my_custom_fun() doesn't change the shape
return output
和我的代码正文:
H = 128
W = 132
n_channels = 8
x1 = tf.placeholder(tf.float32, [None, H, W, 1])
x2 = layers.conv2d(x1, filters=n_channels, kernel_size=3, padding='same')
# now apply a function to the features vector associated to each pixel
x3 = apply_function_on_pixel_features(x2)
x4 = tf.nn.softmax(x3)
loss = cross_entropy(x4, labels)
optimizer = tf.train.AdamOptimizer(lr)
train_op = optimizer.minimize(loss) # <--- ERROR HERE!
尤其是以下错误:
File "/home/venvs/tensorflowGPU/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2481, in AddOp
self._AddOpInternal(op)
File "/home/venvs/tensorflowGPU/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2509, in _AddOpInternal
self._MaybeAddControlDependency(op)
File "/home/venvs/tensorflowGPU/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2547, in _MaybeAddControlDependency
op._add_control_input(self.GetControlPivot().op)
AttributeError: 'NoneType' object has no attribute 'op'
整个错误堆栈和代码可以在here中找到。感谢您的帮助,
G。
我想在一个尺寸为[batch_size,H,W,n_channels]的矩阵中,在与每个像素的深度通道相对应的每个向量上映射一个TensorFlow函数。换句话说,对于每个图像...
def apply_function_on_pixel_features(incoming, batch_size):
# get input shape:
_, W, H, C = incoming.get_shape().as_list()
incoming_flat = tf.reshape(incoming, shape=[batch_size * W * H, C])
# apply function on every vector of shape [1, C]
out_matrix = my_custom_fun(incoming_flat) # dimension remains unchanged
# go back to the input shape shape [None, W, H, C]
out_shape = tf.convert_to_tensor([batch_size, W, H, C])
out_matrix = tf.reshape(out_matrix, shape=out_shape)
return out_matrix