我想从keras和tensorflow API实现自定义层。 该层将其输出卷积与来自输入张量通道的n个矩阵(由n_index arg提供)连接。 (最后一个频道) 用Ones张量进行快速测试似乎可以工作,但是在我的测试模型中却行不通。 非常感谢。
tf版本= 2.2.0 keras版本= 2.4.3
from keras.layers import Layer, Conv2D
import tensorflow as tf
class PseudoCoordConv(Layer):
def __init__(self,
n_index: int,
filters: int,
kernel_size=(3, 3),
padding='same',
activation='relu',
kernel_initializer=k_init,
bias_initializer=b_init,
**kwargs):
super(PseudoCoordConv, self).__init__(**kwargs)
self.filters = filters
self.kernel_size = kernel_size
self.padding = padding
self.activation = activation
self.kernel_initializer = kernel_initializer
self.bias_initializer = bias_initializer
self.n_index = n_index
self.conv_layer = Conv2D(filters=filters,
kernel_size=kernel_size,
strides=(1, 1),
dilation_rate=(1, 1),
padding=padding,
activation=activation,
use_bias=True,
kernel_initializer=kernel_initializer,
bias_initializer=bias_initializer)
self.built = True
def call(self, inputs):
coord_maps = []
for index in range(self.n_index):
coord_map = inputs[:, :, :, index]
coord_map = tf.expand_dims(coord_map, axis=-1)
coord_maps.append(coord_map)
coord_maps = tf.concat(coord_maps, axis=-1)
x = self.conv_layer(inputs)
return tf.concat([x, coord_maps], axis=-1)
def compute_output_shape(self, input_shape):
return tf.TensorShape[*input_shape, self.filters + self.n_index]
def get_config(self):
config = super(PseudoCoordConv, self).get_config()
config.update({'filters': self.filters,
'kernel_size': self.kernel_size,
'padding': self.padding,
'activation': self.activation,
'kernel_initializer': self.kernel_initializer,
'bias_initializer': self.bias_initializer,
'n_index': self.n_index})
return config
快速测试:
if __name__ == '__main__':
x = tf.ones((32, 64, 64, 3))
layer = PseudoCoordConv(2, 1)
y = layer(x)
print(y.shape)
: tf.Tensor(
[[[[0.09264521 1. 1. ]
[0. 1. 1. ]]
[[0.07449672 1. 1. ]
[0.04539756 1. 1. ]]]], shape=(1, 2, 2, 3), dtype=float32
我的测试模型:
def pseudoCoordConv_test(input_shape: tuple):
input_layer = Input(name='input_layer', shape=input_shape)
x = PseudoCoordConv(n_index=2, filters=1)(input_layer)
return Model(inputs=input_layer, outputs=x)
.compile和.fit后出现错误
: InvalidArgumentError: Incompatible shapes: [1,64,64,3] vs. [1,64,64]
Errors may have originated from an input operation.
Function call stack:
train_function