这个职位似乎表明,我想要完成的事情是不可能的。然而,我不相信这一点--考虑到我已经做了什么,我不明白为什么我想要做的事情不能实现……
我有两个图像数据集,其中一个具有形状图像(480,720,3),而另一个具有形状图像(540,960,3)。
我使用以下代码初始化了一个模型:
input = Input(shape=(480, 720, 3), name='image_input')
initial_model = VGG16(weights='imagenet', include_top=False)
for layer in initial_model.layers:
layer.trainable = False
x = Flatten()(initial_model(input))
x = Dense(1000, activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(0.5)(x)
x = Dense(1000, activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(0.5)(x)
x = Dense(14, activation='linear')(x)
model = Model(inputs=input, outputs=x)
model.compile(loss='mse', optimizer='adam', metrics=['mae'])现在,我已经在前一个数据集上训练了这个模型,我想弹出输入张量层,并使用一个新的输入张量,其形状与后一个数据集的图像维数相匹配。
model = load_model('path/to/my/trained/model.h5')
old_input = model.pop(0)
new_input = Input(shape=(540, 960, 3), name='image_input')
x = model(new_input)
m = Model(inputs=new_input, outputs=x)
m.save('transfer_model.h5')从而产生此错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/aicg2/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 2506, in save
save_model(self, filepath, overwrite, include_optimizer)
File "/home/aicg2/.local/lib/python2.7/site-packages/keras/models.py", line 106, in save_model
'config': model.get_config()
File "/home/aicg2/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 2322, in get_config
layer_config = layer.get_config()
File "/home/aicg2/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 2370, in get_config
new_node_index = node_conversion_map[node_key]
KeyError: u'image_input_ib-0'在我链接的那篇文章中,maz说有一种尺寸不匹配可以阻止改变模型的输入层--如果是这样的话,我如何将一个(480,720,3)输入层放在期望(224,224,3)图像的VGG16模型前面?
我认为一个更有可能的问题是,我以前的模型的输出与我根据fchollet在这个职位中的意思提供的内容不同。我在语法上很困惑,但我相信整个x = Layer()(x)段是从输入->输出逐层构建的,简单地在前面抛出一个不同的输入就会破坏它。
但我真的不知道..。
有人能告诉我如何完成我想做的事情吗?或者,如果不可能的话,向我解释为什么不?
发布于 2018-02-02 15:41:17
您可以通过使用新的输入形状VGG16创建一个新的new_shape模型实例,并对所有层权重进行复制。代码大致是
new_model = VGG16(weights=None, input_shape=new_shape, include_top=False)
for new_layer, layer in zip(new_model.layers[1:], model.layers[1:]):
new_layer.set_weights(layer.get_weights())发布于 2019-10-24 21:01:02
对于kerassurgeon来说,这应该是非常容易的。首先,您需要安装库;取决于您是通过TensorFlow (TF2.0和更高版本)或Keras作为一个单独的库使用Keras,它需要以不同的方式安装。
对于TF中的Keras:pip install tfkerassurgeon (https://github.com/Raukk/tf-keras-surgeon)。对于独立的Keras:pip install kerassurgeon (https://github.com/BenWhetton/keras-surgeon)
若要替换输入(例如,使用TF2.0;当前尚未测试的代码):
from tensorflow import keras # or import keras for standalone version
from tensorflow.keras.layers import Input
model = load_model('path/to/my/trained/model.h5')
new_input = Input(shape=(540, 960, 3), name='image_input')
# or kerassurgeon for standalone Keras
from tfkerassurgeon import delete_layer, insert_layer
model = delete_layer(model.layers[0])
# inserts before layer 0
model = insert_layer(model.layers[0], new_input)发布于 2019-10-29 05:14:38
这就是我在Keras模型中改变输入大小的方式。我有两个CNN模型,一个有输入大小[None, None, 3],另一个有输入大小[512,512,3]。两种型号的重量相同。利用set_weights(model.get_weights()),可以将模型1的权重转移到模型2。
inputs = Input((None, None, 3))
.....
model = Model(inputs=[inputs], outputs=[outputs])
model.compile(optimizer='adam', loss='mean_squared_error')
model.load_weights('my_model_name.h5')
inputs2 = Input((512, 512, 3))
....
model2 = Model(inputs=[inputs2], outputs=[outputs])
model2.compile(optimizer='adam', loss='mean_squared_error')
model2.set_weights(model.get_weights())https://datascience.stackexchange.com/questions/21734
复制相似问题