我正在使用Python 3.7.7,Tensorflow 2.1.0和Functional Api通过以下摘要定义编码器:
Model: "encoder"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 200, 200, 1)] 0
_________________________________________________________________
conv1_1 (Conv2D) (None, 200, 200, 64) 1664
_________________________________________________________________
conv1_2 (Conv2D) (None, 200, 200, 64) 102464
_________________________________________________________________
pool1 (MaxPooling2D) (None, 100, 100, 64) 0
_________________________________________________________________
conv2_1 (Conv2D) (None, 100, 100, 96) 55392
_________________________________________________________________
conv2_2 (Conv2D) (None, 100, 100, 96) 83040
_________________________________________________________________
pool2 (MaxPooling2D) (None, 50, 50, 96) 0
_________________________________________________________________
conv3_1 (Conv2D) (None, 50, 50, 128) 110720
_________________________________________________________________
conv3_2 (Conv2D) (None, 50, 50, 128) 147584
_________________________________________________________________
pool3 (MaxPooling2D) (None, 25, 25, 128) 0
_________________________________________________________________
conv4_1 (Conv2D) (None, 25, 25, 256) 295168
_________________________________________________________________
conv4_2 (Conv2D) (None, 25, 25, 256) 1048832
_________________________________________________________________
pool4 (MaxPooling2D) (None, 12, 12, 256) 0
_________________________________________________________________
conv5_1 (Conv2D) (None, 12, 12, 512) 1180160
_________________________________________________________________
conv5_2 (Conv2D) (None, 12, 12, 512) 2359808
_________________________________________________________________
global_average_pooling2d (Gl (None, 512) 0
=================================================================
Total params: 5,384,832
Trainable params: 5,384,832
Non-trainable params: 0
_________________________________________________________________
None
我从U-Net网络使用以下代码获取编码器:
u_net: Model = get_unet_uncompiled(img_shape = (200, 200, 1))
encoder_input = Model(inputs=u_net.layers[0].input, outputs=u_net.layers[14].output)
encoder_output = GlobalAveragePooling2D()(encoder_input.layers[-1].output)
encoder = Model(encoder_input.input, encoder_output, name='encoder')
print(encoder.summary())
我想将其输出合并为(None, 12, 12, 512)
的形状。
我已经尝试过了:
u_net: Model = get_unet_uncompiled(img_shape = (200, 200, 1))
encoder_input = Model(inputs=u_net.layers[0].input, outputs=u_net.layers[14].output)
encoder_output = GlobalAveragePooling2D()(encoder_input.layers[-1].output)
up = UpSampling2D(size=(12, 12))(encoder_output)
encoder = Model(encoder_input.input, up, name='encoder')
但是我有这个错误:
ValueError: Input 0 of layer up_sampling2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 512]
我不知道自己在做什么。也许UpSampling2D
在这里是错误的。
但是,如何将其转换为形状(None, 12, 12, 512)
?
答案 0 :(得分:1)
Deconvolution/ Upsamping操作形成与正常卷积相同的连通性,但方向相反。对于Upsampling in Keras输入形状是
具有以下形状的4D张量:-如果data_format为“ channels_last” :(批量大小,行,列,通道)
但是,您给输出的global_average_pooling2d层是2D张量([None,512]),因此会引发错误。 如果您想在此处使用Upsamping,则应删除Global Average Pooling层,然后进行upsamping。 This是在Keras中从头实现的UNet的示例。
答案 1 :(得分:1)
您可以使用Lambda层:
import tensorflow as tf
inputs = tf.random.uniform((100, 512),0, 1, dtype=tf.int32)
layer = tf.keras.layers.Lambda(lambda x: tf.tile(tf.reshape(x, (100, 1, 1, 512)),
(1, 12, 12, 1)))
print(layer(inputs).shape)
TensorShape([100, 12, 12, 512])