Reputation: 475
My code were working perfectly for months but today I realized it's not working anymore. In my code, I just copy-pasted this keras code example : https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/#example-efficientnetb0-for-stanford-dogs
So "my" code looks like this :
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"]
)
However, today when I run this cell in my colab, I get a lot of warnings like this :
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
And this error :
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.
I think google colab updated keras and tensorflow, now they are both version 2.5.0
How can I make my code works again ?
Upvotes: 0
Views: 707
Reputation: 17219
You should not mix tf 2.x
and standalone keras
. You should import your libraries as follows, thus you won't get any issue.
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)
Upvotes: 1
Reputation: 475
So after some research, I realized it comes from the imports :
from tensorflow.keras.applications import EfficientNetB0
I don't know why but it does not throw any error, but breaks the entire code. Instead, I have to import :
from keras.applications.efficientnet import EfficientNetB0
And it works perfectly.
Upvotes: 0