【问题标题】:Keras EfficientNet transfer learning code example not workingKeras EfficientNet 迁移学习代码示例不起作用
【发布时间】:2021-08-14 14:03:45
【问题描述】:

我的代码已经运行了好几个月,但今天我意识到它不再运行了。在我的代码中,我只是复制粘贴了这个 keras 代码示例:https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/#example-efficientnetb0-for-stanford-dogs

所以“我的”代码看起来像这样:

import tensorflow as tf
import keras
from keras.layers import *
from keras import Sequential
from keras.layers.experimental import preprocessing
from keras import layers
from tensorflow.keras.applications import EfficientNetB0

img_augmentation = Sequential(
    [
        preprocessing.RandomRotation(factor=0.15),
        preprocessing.RandomTranslation(height_factor=0.1, width_factor=0.1),
        preprocessing.RandomFlip(),
        preprocessing.RandomContrast(factor=0.1),
    ],
    name="img_augmentation",
)

inputs = layers.Input(shape=(224, 224, 3))
x = img_augmentation(inputs)
outputs = EfficientNetB0(include_top=True, weights=None, classes=5)(x)

model = tf.keras.Model(inputs, outputs)
model.compile(
    optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]
)

但是,今天当我在我的 colab 中运行这个单元时,我收到了很多这样的警告:

WARNING:tensorflow:
The following Variables were used a Lambda layer's call (tf.compat.v1.nn.fused_batch_norm_422), but
are not present in its tracked objects:
  <tf.Variable 'top_bn/gamma:0' shape=(1280,) dtype=float32>
  <tf.Variable 'top_bn/beta:0' shape=(1280,) dtype=float32>
It is possible that this is intended behavior, but it is more likely
an omission. This is a strong indication that this layer should be
formulated as a subclassed Layer rather than a Lambda layer

还有这个错误:

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.

我认为 google colab 更新了 keras 和 tensorflow,现在它们都是 2.5.0 版本

我怎样才能让我的代码再次工作?

【问题讨论】:

    标签: tensorflow keras google-colaboratory transfer-learning


    【解决方案1】:

    您不应混合使用 tf 2.x 和独立的 keras。您应该按如下方式导入您的库,因此您不会遇到任何问题。

    import tensorflow as tf
    from tensorflow.keras.layers import *
    from tensorflow.keras import Sequential
    from tensorflow.keras.layers.experimental import preprocessing
    from tensorflow.keras import layers
    from tensorflow.keras.applications import EfficientNetB0
    
    img_augmentation = Sequential(
        [
            preprocessing.RandomRotation(factor=0.15),
            preprocessing.RandomTranslation(height_factor=0.1, width_factor=0.1),
            preprocessing.RandomFlip(),
            preprocessing.RandomContrast(factor=0.1),
        ],
        name="img_augmentation",
    )
    
    inputs = layers.Input(shape=(224, 224, 3))
    x = img_augmentation(inputs)
    outputs = EfficientNetB0(include_top=True, weights=None, classes=5)(x)
    
    model = tf.keras.Model(inputs, outputs)
    

    【讨论】:

      【解决方案2】:

      所以经过一番研究,我意识到它来自进口:

      from tensorflow.keras.applications import EfficientNetB0
      

      我不知道为什么,但它没有抛出任何错误,而是破坏了整个代码。相反,我必须导入:

      from keras.applications.efficientnet import EfficientNetB0
      

      而且效果很好。

      【讨论】:

        猜你喜欢
        • 2021-08-09
        • 2020-05-13
        • 1970-01-01
        • 1970-01-01
        • 2020-09-07
        • 2020-10-04
        • 2021-06-03
        • 2021-02-01
        • 2019-09-19
        相关资源
        最近更新 更多