将层添加到resnet[keras]的顶部:ValueError:输入0与层conv2d_transpose_1不兼容:预期ndim=4,发现ndim=2
我们已经导入了一个在ImageNet上预训练的ResNet50模型,并希望在其顶部添加一些反褶积层以实现语义分割 我们使用google colaboratory,Keras和Tensorflow作为后端将层添加到resnet[keras]的顶部:ValueError:输入0与层conv2d_transpose_1不兼容:预期ndim=4,发现ndim=2,keras,deep-learning,google-colaboratory,resnet,semantic-segmentation,Keras,Deep Learning,Google Colaboratory,Resnet,Semantic Segmentation,我们已经导入了一个在ImageNet上预训练的ResNet50模型,并希望在其顶部添加一些反褶积层以实现语义分割 我们使用google colaboratory,Keras和Tensorflow作为后端 import keras from keras.applications.resnet50 import ResNet50 from keras.layers import Dense, Activation, Conv2DTranspose, Reshape, UpSampling2D fro
import keras
from keras.applications.resnet50 import ResNet50
from keras.layers import Dense, Activation, Conv2DTranspose, Reshape, UpSampling2D
from keras.regularizers import l2
from keras import backend as K;
height = 224 #dimensions of image
width = 224
channel = 3
# Importing the ResNet architecture pretrained on ImageNet
resnet_model = ResNet50(weights = 'imagenet', input_shape=(height, width, channel))
# Removing the classification layer and the last average
resnet_model.layers.pop()
resnet_model.layers.pop()
#resnet_model.summary()
# Upsampling
conv1 = Conv2DTranspose(28, (3,3), strides=(2,2), activation = None, kernel_regularizer=l2(0.))(resnet_model.outputs)
model = Model(inputs=resnet_model.input, outputs=conv1)
我们得到以下错误:
ValueError:输入0与层conv2d_transpose_1不兼容:预期ndim=4,发现ndim=2
我们的resnet模型(没有最后两层)的输出似乎是一个一维向量,但我们希望它是一个三维向量
这是pop之后“resnet_model.summary()”的最终输出部分
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_10 (InputLayer) (None, 224, 224, 3) 0
__________________________________________________________________________________________________
conv1_pad (ZeroPadding2D) (None, 230, 230, 3) 0 input_10[0][0]
__________________________________________________________________________________________________
.
.
.
.
.
__________________________________________________________________________________________________
bn5c_branch2b (BatchNormalizati (None, 7, 7, 512) 2048 res5c_branch2b[0][0]
__________________________________________________________________________________________________
activation_489 (Activation) (None, 7, 7, 512) 0 bn5c_branch2b[0][0]
__________________________________________________________________________________________________
res5c_branch2c (Conv2D) (None, 7, 7, 2048) 1050624 activation_489[0][0]
__________________________________________________________________________________________________
bn5c_branch2c (BatchNormalizati (None, 7, 7, 2048) 8192 res5c_branch2c[0][0]
__________________________________________________________________________________________________
add_160 (Add) (None, 7, 7, 2048) 0 bn5c_branch2c[0][0]
activation_487[0][0]
__________________________________________________________________________________________________
activation_490 (Activation) (None, 7, 7, 2048) 0 add_160[0][0]
==================================================================================================
Total params: 23,587,712
Trainable params: 23,534,592
Non-trainable params: 53,120
__________________________________________________________________________________________________
我们如何解决这个问题?不要这样做:
resnet_model.layers.pop()
Pop对于功能模型来说是没有意义的,因为层不再是连续的,特别是对于使用剩余连接的ResNet。如果您检查pop后,summary()
确认图层已删除,但计算图中仍有这些图层:
>>> resnet_model.output
<tf.Tensor 'fc1000/Softmax:0' shape=(?, 1000) dtype=float32>
通过实例化模型,可以确认输出张量具有预期的形状和语义:
>>> resnet_model.output
<tf.Tensor 'activation_98/Relu:0' shape=(?, 7, 7, 2048) dtype=float32>
>resnet\u model.output
还有一件事,我更喜欢使用
model.output
而不是model.outputs
,因为这个特定的模型只有一个输出。你能打印摘要并检查pop后的最后一层是什么吗?我添加了你要求的内容,感谢你的评论我们实现了你的修改,非常感谢!我们现在将尝试建立一些跳过连接,以改进功能本地化,如果您有一些提示,将非常欢迎。
>>> resnet_model.output
<tf.Tensor 'activation_98/Relu:0' shape=(?, 7, 7, 2048) dtype=float32>