首页 > 解决方案 > 在 MobileNetV3 上进行迁移学习时 Keras 和 tensorflow 冲突

问题描述

我正在尝试在 Keras 中使用 MobileNetV3 进行迁移学习,但我遇到了一些问题。

from keras.models import Model
from keras.layers import GlobalMaxPooling2D, Dense, Dropout
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
from tensorflow.keras.applications import MobileNetV3Small
import numpy as np
from tqdm import tqdm
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

pretrained_model = MobileNetV3Small(input_shape=(224,224,3),
                                    weights="imagenet",
                                    include_top=False)

# freeze all layers except the last one
for layer in pretrained_model.layers:
    layer.trainable = False
pretrained_model.layers[-1].trainable = True

# combine the model with some extra layers for classification
last_output = pretrained_model.layers[-1].output
x = GlobalMaxPooling2D()(last_output)
x = Dense(128, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(1, activation='sigmoid')(x)
model = Model(pretrained_model.input, x)

当我尝试制作密集层时出现此错误:

TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.

但它通过添加以下代码片段来修复:

from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()

当我包含上面的代码修复时,我在调用时收到此错误model.fit()

FailedPreconditionError: 2 root error(s) found.
  (0) Failed precondition: Could not find variable Conv_1_2/kernel. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status=Not found: Resource localhost/Conv_1_2/kernel/N10tensorflow3VarE does not exist.
     [[{{node Conv_1_2/Conv2D/ReadVariableOp}}]]
     [[_arg_dense_12_target_0_1/_100]]
  (1) Failed precondition: Could not find variable Conv_1_2/kernel. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status=Not found: Resource localhost/Conv_1_2/kernel/N10tensorflow3VarE does not exist.
     [[{{node Conv_1_2/Conv2D/ReadVariableOp}}]]
0 successful operations.
0 derived errors ignored.

如何解决这些问题并训练模型?

标签: tensorflowkerastransfer-learningmobilenet

解决方案


来自评论

不要混合tf.keras和独立keras。它们不兼容。只使用其中之一(从 Frightera 转述)

工作代码如下图

from tensorflow.keras.models import Model
from tensorflow.keras.layers import GlobalMaxPooling2D, Dense, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.applications import MobileNetV3Small
import numpy as np
from tqdm import tqdm
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

pretrained_model = MobileNetV3Small(input_shape=(224,224,3),
                                    weights="imagenet",
                                    include_top=False)

# freeze all layers except the last one
for layer in pretrained_model.layers:
    layer.trainable = False
pretrained_model.layers[-1].trainable = True

# combine the model with some extra layers for classification
last_output = pretrained_model.layers[-1].output
x = GlobalMaxPooling2D()(last_output)
x = Dense(128, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(1, activation='sigmoid')(x)
model = Model(pretrained_model.input, x)

推荐阅读