输出形状与实际形状的差值

2024-10-03 15:23:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用预先训练过的ResNet50(包含在keras.应用) 为此,我使用了模型的get_layer方法。这样地。在

input = Input(  (224, 224 , 3) )
resnet = ResNet50(weights='imagenet', pooling=max, include_top = False , input_tensor=input  )
firstconv = resnet.get_layer("conv1")

然后我打印出firstconv的形状:

^{pr2}$

我得到了预期的结果(根据模型.摘要() ) : (?,112,112,64)

然后在后面的代码中,我调用输入张量上的firstconv层:

x = firstconv( inputs )

然后我打印这个x的形状,得到(?)?,109,109,64)之后,代码因形状冲突而中断。在

我想知道为什么会发生这种情况,以及我如何解决它。我仍然在学习keras和deep learning,也许我尝试访问Resnet50内部层的方式有问题。在

提前谢谢

我使用从pip安装的ubuntu14.04、keras2.1.4和tensorflow1.6作为Keras的后端。在

编辑:

        self.input = Input(  (224, 224 , 3) )

        self.resnet = ResNet50(weights='imagenet', pooling=max, include_top = False , input_tensor=self.input  )

        for layer in self.resnet.layers:
            layer.trainable = False 
        self.firstconv = self.resnet.get_layer("conv1")
        print(" first convt output ") 
        # this outputs (? , 112 , 112 , 3) , the desired shape
        print( self.firstconv.output_shape )
        ... later on the code 
        x = self.firstconv( self.input) 
        print( x.shape ) 
        # this outputs ( ? , 109 , 109 , 64 ), but the expected shape is ( ? , 112 , 112 , 64 )

就是这样。在代码之间我什么也不做self.firstconv公司或输入


Tags: the代码模型selflayerfalseinputget
2条回答

我发现这个案子有点奇怪。在

我在两台不同的机器上工作resnet.摘要()在第一台(在家里)中,它告诉我restnet模型的第一层是conv1,所以我把这个信息记在心里,准备在另一台机器上工作,后来经过多次尝试,我终于在第二台机器上意识到了这一点Resnet的第一层是conv1d_pad,该层接收(224224224,3)输入并将其转换为(230,230,3),这就是畸形问题的根源。我仍然不知道为什么会有这种差异,但我能够克服这个问题。在

ResNet50示例代码

model = ResNet50(weights = "imagenet",
                 include_top = False,
                 input_shape = (224,224, 3))
for layer in model.layers:
    layer.trainable = False
x = model.output
# flatten outputs of resnet
x = Flatten()(x)
# add a fully connected layer
predictions = Dense(1, activation = "sigmoid")(x)
# build the model
model_final = Model(inputs = model.input, outputs = predictions)

您的描述对于如何重现该问题和代码的用途有点模糊。这里的示例代码给出了使用ResNet50处理输入和输出的正确方法。在

相关问题 更多 >