【问题标题】:Keras Transfer learning with own model使用自己的模型进行 Keras 迁移学习
【发布时间】:2023-03-27 18:43:01
【问题描述】:

编辑:

显然,keras 与 tensorflow.keras 的一些问题导致了我的问题。我按照这个来改变我的进口: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.engine.input_layer.InputLayer>

我正在尝试加载模型并将最后一个密集层与另一个不同输出维度的密集层交换。 这是我到目前为止所得到的:

保存的模型:

def create_model(n_timesteps=828 , n_features=1, n_outputs=7):
    dtype='float32'
    model = Sequential([
        Convolution1D(input_shape=(n_timesteps, n_features), kernel_size=3, filters=128, activation='relu', kernel_regularizer='l2', dtype=dtype, name="conv1"),
        MaxPool1D(pool_size=4, strides=4), # reduce size of the input by 4
        Dropout(0.3, dtype=dtype),
        Flatten(),
        Dense(32, activation='relu',  kernel_regularizer=tf.keras.regularizers.l2(), dtype=dtype, name="dense1"),
        Dropout(0.2, dtype=dtype),
        Dense(n_outputs, activation='softmax', dtype=dtype, name="dense2")
    ])

    return model

以及重新训练的尝试:

def retrain(model_name="cnn_general"):
    model = tf.keras.models.load_model('saved_models\\' + model_name)                        
    model.trainable = False
    model.compile(optimizer='adam', loss='categorical_crossentropy', 
              metrics=['accuracy'])
    print(model.summary())

    output = Dense(1, activation='sigmoid')(model.layers[-1].output)

    retrained_model = Model(inputs=model.inputs, outputs=output)
    print(retrained_model.summary())

我得到错误输出:

Traceback (most recent call last):
  File "C:/.../CNN_heatbeats.py", line 233, in <module>
    retrain((X_train, y_train, X_test,  y_test, X_val, y_val))
  File "C:/.../CNN_heatbeats.py", line 161, in retrain
    output = Dense(10, activation='sigmoid')(model.layers[-1].output)
  File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper
    return func(*args, **kwargs)
  File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\keras\engine\base_layer.py", line 475, in __call__
    previous_mask = _collect_previous_mask(inputs)
  File "C:\Miniconda\envs\tensorflow-gpu-cuda10\lib\site-packages\keras\engine\base_layer.py", line 1441, in _collect_previous_mask
    mask = node.output_masks[tensor_index]
AttributeError: 'Node' object has no attribute 'output_masks'

我只找到了有关 VGG16 等模型的信息,但无法将其应用于我自己的网络。我在 retrain() 函数中所做的是否合适?我怎样才能让它发挥作用?

【问题讨论】:

    标签: python tensorflow keras transfer-learning


    【解决方案1】:

    试过这种方式吗?

    def retrain(model_name="cnn_general"):
        model = tf.keras.models.load_model('saved_models\\' + model_name)                        
        model.trainable = False
        model.compile(optimizer='adam', loss='categorical_crossentropy', 
                  metrics=['accuracy'])
        model.summary()
    
        retrained_model = Sequential()
        for layer in model.layers[:-1]:
            layer.trainable = False
            retrained_model.add(layer)
        retrained_model.add(Dense(1, activation='sigmoid'))
        retrained_model.summary()
    

    【讨论】:

    • 感谢您的回答!我以前试过,它给出了这个: TypeError: The added layer must be an instance of class Layer.找到:
    • 其实我只是在我的机器上试过你的代码,它可以工作。我没有训练模型,我只是使用了 create_model 函数,保存了模型,然后使用了你定义的 retrain_model 函数。那么代码不会产生任何错误。
    • 嗨,再次感谢您的努力!正如我在编辑中所说,这是 keras 与 tensorflow.keras 的问题,所以您没有遇到任何问题也就不足为奇了!
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 2021-06-03
    • 2020-02-11
    • 2018-12-22
    • 1970-01-01
    • 2020-06-02
    • 2017-12-21
    • 1970-01-01
    相关资源
    最近更新 更多