Encountered the following error when creating a Feature Extraction EfficientNetB0 model from tensorflow.keras.applications.EfficientNetB0
.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-a777a6ba699b> in <module>
----> 1 model_00 = create_feature_extraction_model()
2 model_00.summary()
<ipython-input-22-b1a80357611c> in create_feature_extraction_model()
1 def create_feature_extraction_model():
----> 2 base_model = tf.keras.applications.EfficientNetB0(include_top=False)
3
4 base_model.trainable = False
5
~/anaconda3/envs/ml/lib/python3.8/site-packages/keras/applications/efficientnet.py in EfficientNetB0(include_top, weights, input_tensor, input_shape, pooling, classes, classifier_activation, **kwargs)
546 classifier_activation='softmax',
547 **kwargs):
--> 548 return EfficientNet(
549 1.0,
550 1.0,
~/anaconda3/envs/ml/lib/python3.8/site-packages/keras/applications/efficientnet.py in EfficientNet(width_coefficient, depth_coefficient, default_size, dropout_rate, drop_connect_rate, depth_divisor, activation, blocks_args, model_name, include_top, weights, input_tensor, input_shape, pooling, classes, classifier_activation)
332 # original implementation.
333 # See https://github.com/tensorflow/tensorflow/issues/49930 for more details
--> 334 x = x / tf.math.sqrt(IMAGENET_STDDEV_RGB)
335
336 x = layers.ZeroPadding2D(
~/anaconda3/envs/ml/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb
~/anaconda3/envs/ml/lib/python3.8/site-packages/keras/layers/core/tf_op_layer.py in handle(self, op, args, kwargs)
105 isinstance(x, keras_tensor.KerasTensor)
106 for x in tf.nest.flatten([args, kwargs])):
--> 107 return TFOpLambda(op)(*args, **kwargs)
108 else:
109 return self.NOT_SUPPORTED
~/anaconda3/envs/ml/lib/python3.8/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
TypeError: Exception encountered when calling layer "tf.math.truediv" (type TFOpLambda).
`x` and `y` must have the same dtype, got tf.float16 != tf.float32.
Call arguments received by layer "tf.math.truediv" (type TFOpLambda):
• x=tf.Tensor(shape=(None, None, None, 3), dtype=float16)
• y=tf.Tensor(shape=(3,), dtype=float32)
• name=None
The images are from tensorflow_datasets food101
. The Image Shapes are (224, 224, 3) with dtype=tf.float32
. And The model is trying to take advantage of Mixed Precision with 'mixed_float16'
.
def create_feature_extraction_model():
base_model = tf.keras.applications.EfficientNetB0(include_top=False)
base_model.trainable = False
inputs = tf.keras.layers.Input(shape=IMG_SHAPE, name='input_layer')
x = base_model(inputs, training=False)
x = tf.keras.layers.GlobalAvgPool2D()(x)
x = tf.keras.layers.Dense(units=num_classes)(x)
outputs = tf.keras.layers.Softmax(dtype=tf.float32, name='output_layer')(x)
model = tf.keras.models.Model(inputs, outputs, name='food101_model')
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
The model is to be trained on a Laptop (offline) with the following GPU specs.
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 520.61.05 Driver Version: 520.61.05 CUDA Version: 11.8 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... On | 00000000:01:00.0 Off | N/A |
| N/A 56C P0 13W / N/A | 7MiB / 4096MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 6850 G /usr/lib/xorg/Xorg 4MiB |
+-----------------------------------------------------------------------------+
Any help appreciated. Thanks in advance.
When trying to create model. The above Error occured.
Got the same error with TF 2.9.2, however not going for the mixed_precision policy "fixed" it.