简体   繁体   English

Keras:替换模型的输入层不会更新模型,摘要等其他层的输出形状

[英]Keras: Replacing inputlayer of a model doesn't update the outputshapes, of other layers in model.summary, etc

I am using Keras with TensorFlow backend. 我在TensorFlow后端上使用Keras。 I want to use a pretrained U-Net model and replace the input layer with another one. 我想使用预训练的U-Net模型,并用另一个模型替换输入层。 I trained the model on images of size (256,256). 我在尺寸(256,256)的图像上训练了模型。 When I am predicting a bigger scene I want to manipulate the input such that the UNet does what it does, just on another image size such that I don't have to cute the image or anything. 当我预测更大的场景时,我想操纵输入,以便UNet可以完成它的工作,只是在另一个图像尺寸上,这样我就不必对图像进行可爱处理。 Here is my code: 这是我的代码:

model = load(model_path)
model.layers.pop(0)
new_input = Input(shape = (512,512))
model = Model(new_input,model(new_input_layer))

Now when I am using 现在当我使用

print(model.summary())

it outputs 它输出

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         (None, 512, 512, 3)     0
_________________________________________________________________
model_1 (Model)              multiple                  211825
=================================================================

and if I am doing 如果我在做

model.layers[1].summary()

I get 我懂了

____________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connectedto
=========================================================================
conv2d_1 (Conv2D)             (None, 256, 256, 16)    1216     input_1[0][0]
__________________________________________________________________________
batch_normalization_1       (None, 256, 256, 16)      64      conv2d_1[0][0]

and so on. 等等。 Typically the output shape of conv_2d_1 should be (None,512,512,16), but it wasn't updated properly (and for other layers as well). 通常, conv_2d_1的输出形状应为(None,512,512,16),但未正确更新(以及其他层也未正确更新)。 Further, when I am using 此外,当我使用

model.layers[1].layers[0].output_shape

I get the same result as in the summary. 我得到与摘要相同的结果。

When I am doing a prediction with the adapted model everything works fine with respect to the output. 当我使用调整后的模型进行预测时,就输出而言一切正常。 But if the image size is bigger than (512,512), for example like (4096,4096) I'm running into memory problems/ allocation problems with respect to gpu. 但是,如果图像大小大于(512,512),例如(4096,4096),我会遇到有关gpu的内存问题/分配问题。 Therefore I want to calculate the memory that is needed, to predict the image and cut it, if it is too big. 因此,我想计算所需的内存,以预测图像并将其剪切(如果太大)。 But to write a function that does this for me I need the correct informations about the outputs shapes. 但是要编写一个对我有用的函数,我需要有关输出形状的正确信息。
Does anyone have any suggestions? 有没有人有什么建议? Maybe I should replace the input layer in another way?! 也许我应该以另一种方式替换输入层? Maybe I can update the model somehow? 也许我可以以某种方式更新模型? Or does some keras function already exist, which calculates the needed memory?(I didn't found any) Thanks for your attention! 还是已经存在一些计算所需内存的keras函数?(我没有找到)感谢您的关注! :) :)

There is a better alternative to what you are trying to achieve. 除了您要实现的目标之外,还有更好的选择。 The CNN layers can handle arbitrary shape if you specify shape=(None,None,3) says some height and width with 3 channels. 如果您指定shape=(None,None,3)表示3个通道的高度和宽度,则CNN层可以处理任意形状。 You can train the original model like that and don't have to adjust tensor / image shapes when you predict. 您可以像这样训练原始模型,而无需在预测时调整张量/图像形状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM