首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Keras EfficientNet迁移学习代码示例不起作用

Keras EfficientNet迁移学习代码示例不起作用
EN

Stack Overflow用户
提问于 2021-05-26 18:23:13
回答 2查看 241关注 0票数 0

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

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

代码语言:javascript
运行
复制
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中运行这个cell时,我收到了很多这样的警告:

代码语言:javascript
运行
复制
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

还有这个错误:

代码语言:javascript
运行
复制
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版本

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-26 19:52:13

您不应该将tf 2.x和独立的keras混用。你应该像下面这样导入你的库,这样你就不会有任何问题。

代码语言:javascript
运行
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2021-05-26 19:41:36

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

代码语言:javascript
运行
复制
from tensorflow.keras.applications import EfficientNetB0

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

代码语言:javascript
运行
复制
from keras.applications.efficientnet import EfficientNetB0

而且它工作得很完美。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67703023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档