【问题标题】:Concatenate Custom Pretrained Model with new Model Keras将自定义预训练模型与新模型 Keras 连接起来
【发布时间】:2020-11-08 22:11:35
【问题描述】:

我将 Sports_1M caffe 模型转换为 Keras,并将其作为预训练模型使用到我的新 Keras 模型中。我还加载了预训练的权重。

我删除了 Pretrained 模型的顶层,最后与 New Model 连接。我不想再次训练加载的预训练模型(只想使用预训练模型的嵌入并用它来训练我的新 Keras 模型)。

代码如下:

from keras.models import model_from_json
from keras import backend as K
K.set_image_dim_ordering('th')

model = model_from_json(open('/content/sports_1M/sports1M_model_new.json', 'r').read())
model.load_weights('/content/sports_1M/sports1M_weights.h5')

我的问题是:

  1. 我应该编译预训练模型然后将其连接起来吗?

     model.compile(loss='mean_squared_error', optimizer='adam')
    
  2. 我怎么知道预训练模型没有再次训练它(我不想要)?

  3. 如何训练整个(串联)架构?

     model2 = Model(model.get_input_at(0),model.get_layer(layer_name).output)
    
     input_shape = (3, 16, 112, 112)
    
     encoded_l = model2(left_input)
    
     prediction = Dense(1,activation='sigmoid')(encoded_l)
    
     Model([left_input,right_input] , prediction)
    
  4. 当我们使用像 VGG 这样的 Inbuild 预训练模型时,我们一般使用VGG(include_top = False , weights = 'imagenet')

我的情况是这样想的

【问题讨论】:

    标签: tensorflow keras deep-learning


    【解决方案1】:

    我得到了答案,我们可以简单地设置layers.trainable = False

    for layer in model.layers:
      layer.trainable = False
    

    【讨论】:

      猜你喜欢
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 2021-01-12
      • 2018-07-31
      • 2018-04-15
      相关资源
      最近更新 更多