简体   繁体   English

keras 结合预训练模型

[英]keras combine pretrained model

I trained a single model and want to combine it with another keras model using the functional api (backend is tensorflow version 1.4)我训练了一个模型,并希望使用功能 api 将它与另一个 keras 模型结合起来(后端是 tensorflow 1.4 版)

My first model looks like this:我的第一个模型如下所示:

import tensorflow.contrib.keras.api.keras as keras

model = keras.models.Sequential()
input = Input(shape=(200,))
dnn = Dense(400, activation="relu")(input)
dnn = Dense(400, activation="relu")(dnn)
output = Dense(5, activation="softmax")(dnn)
model = keras.models.Model(inputs=input, outputs=output)

after I trained this model I save it using the keras model.save() method.在我训练这个模型后,我使用 keras model.save() 方法保存它。 I can also load the model and retrain it without problems.我还可以加载模型并毫无问题地重新训练它。

Now I want to use the output of this model as additional input for a second model:现在我想使用这个模型的输出作为第二个模型的附加输入:

# load first model
old_model = keras.models.load_model(path_to_old_model)

input_1 = Input(shape=(200,))
input_2 = Input(shape=(200,))
output_old_model = old_model(input_2)

merge_layer = concatenate([input_1, output_old_model])
dnn_layer = Dense(200, activation="relu")(merge_layer)
dnn_layer = Dense(200, activation="relu")(dnn_layer)
output = Dense(10, activation="sigmoid")(dnn_layer)
new_model = keras.models.Model(inputs=[input_1, input_2], outputs=output)
new_model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"]
new_model.fit(inputs=[x1,x2], labels=labels, epochs=50, batch_size=32)

when I try this I get the following error message:当我尝试这个时,我收到以下错误消息:

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value dense_1/kernel
 [[Node: dense_1/kernel/read = Identity[T=DT_FLOAT, _class=["loc:@dense_1/kernel"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](dense_1/kernel)]]
 [[Node: model_1_1/dense_3/BiasAdd/_79 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_68_model_1_1/dense_3/BiasAdd", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

I would do this in following steps:我将按照以下步骤执行此操作:

  1. Define function for building a clean model with the same architecture:定义用于构建具有相同架构的干净模型的函数:

     def build_base(): input = Input(shape=(200,)) dnn = Dense(400, activation="relu")(input) dnn = Dense(400, activation="relu")(dnn) output = Dense(5, activation="softmax")(dnn) model = keras.models.Model(inputs=input, outputs=output) return input, output, model
  2. Build two copies of the same model:构建相同模型的两个副本:

     input_1, output_1, model_1 = build_base() input_2, output_2, model_2 = build_base()
  3. Set weights in both models:在两个模型中设置权重:

     model_1.set_weights(old_model.get_weights()) model_2.set_weights(old_model.get_weights())
  4. Now do the rest:现在做剩下的:

     merge_layer = concatenate([input_1, output_2]) dnn_layer = Dense(200, activation="relu")(merge_layer) dnn_layer = Dense(200, activation="relu")(dnn_layer) output = Dense(10, activation="sigmoid")(dnn_layer) new_model = keras.models.Model(inputs=[input_1, input_2], outputs=output)

Let's say you have a pre-trained/saved CNN model called pretrained_model and you want to add a densely connected layers to it, then using the functional API you can write something like this:假设您有一个名为pretrained_model的预训练/保存的 CNN 模型,并且您想向其添加一个密集连接的层,然后使用功能 API 您可以编写如下内容:

from keras import models, layers

kmodel = layers.Flatten()(pretrained_model.output)
kmodel = layers.Dense(256, activation='relu')(kmodel)
kmodel_out = layers.Dense(1, activation='sigmoid')(kmodel)
model = models.Model(pretrained_model.input, kmodel_out)

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

相关问题 预训练的keras模型在android中重现相同的结果 - Pretrained keras model is returing the same result in android keras 在预训练模型上设置可训练标志 - keras setting trainable flag on pretrained model 从keras中的预训练模型加载权重时的层误差 - Error in layers while loading weights from a pretrained model in keras for finetuning 使用预训练的 model 和 keras: AttributeError: 'NoneType' object 没有属性 'shape' - Using pretrained model with keras: AttributeError: 'NoneType' object has no attribute 'shape' 如何将 pretrained.network model 从 Keras 导入到 Matlab? - How to import pretrained network model from Keras to Matlab? `检查失败:cudnnSetTensorNdDescriptor` 当使用预训练的 Keras 模型进行迁移学习时 - `Check failed: cudnnSetTensorNdDescriptor` when transfer learning with pretrained Keras model 为什么我需要在Keras中编译和拟合预训练模型? - Why do I need to compile and fit a pretrained model in Keras? 我会尝试为 keras 预训练 model 更改频道 - I would try change channel for keras pretrained model 在预训练的方式Keras,Tensorflow中使用机器学习模型 - Use Machine Learning Model in Pretrained Manner Keras, Tensorflow Keras 预训练的 Xception 模型总是给出预测“sewing_machine” - Keras pretrained Xception model always gives the prediction 'sewing_machine'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM