在Keras中更改输入大小 [英] Change the input size in Keras
问题描述
我已经用Keras训练了一个全卷积神经网络。我使用了Functional API,并将输入层定义为 Input(shape =(128,128,3))
,与训练集中的图像大小相对应。 / p>
但是,我想在可变大小的图像上使用经过训练的模型(这是可以的,因为网络是完全卷积的)。为此,我需要将输入层更改为 Input(shape =(None,None,3))
。解决问题的明显方法是直接用输入形状(None,None,3)
训练模型,但是我在需要的地方使用了自定义损失函数来指定训练图像的大小。
我试图定义一个新的输入层并将其分配给我的模型,如下所示:
来自keras.engine的
import InputLayer
input_layer = InputLayer(input_shape =(None,None,3),name = input)
model.layers [0] = input_layer
这实际上会更改输入层的大小相应地,但是下面的层仍然期望(128,128,filters)
输入。
是否可以立即更改所有输入值?
创建一个新模型,除了输入形状不同外,其他模型完全相同。和转移权重:
newModel.set_weights(oldModel.get_weights())
如果出现任何问题,则可能不是完全卷积(例如:包含Flatten层)。
I have trained a fully convolutional neural network with Keras. I have used the Functional API and have defined the input layer as Input(shape=(128,128,3))
, corresponding to the size of the images in my training set.
However, I want to use the trained model on images of variable sizes (which should be ok because the network is fully convolutional). To do this, I need to change my input layer to Input(shape=(None,None,3))
. The obvious way to solve the problem would have been to train my model directly with an input shape of (None,None,3)
but I use a custom loss function where I need to specify the size of my training images.
I have tried to define a new input layer and assign it to my model like this :
from keras.engine import InputLayer
input_layer = InputLayer(input_shape=(None, None, 3), name="input")
model.layers[0] = input_layer
This actually changes the size of the input layers accordingly but the following layers still expect (128,128,filters)
inputs.
Is there a way to change all of the inputs values at once ?
Create a new model, exactly the same, except for new input shape; and tranfer weights:
newModel.set_weights(oldModel.get_weights())
If anything goes wrong, then it might not be fully convolutional (ex: contains a Flatten layer).
这篇关于在Keras中更改输入大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!