keras Tensorflow有效NetB0错误:TypeError:调用层"tf. math. truediv"时遇到异常(类型为TFOpLambda)

cs7cruho  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(213)

tensorflow.keras.applications.EfficientNetB0创建要素提取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

图像来自tensorflow 数据集food101,图像形状为(224,224,3),dtype=tf.float32,模型试图利用'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

该模型将在笔记本电脑(离线)上使用以下GPU规格进行训练。

+-----------------------------------------------------------------------------+
| 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 |
+-----------------------------------------------------------------------------+

任何帮助都很感激。提前感谢。
尝试创建模型时。出现上述错误。

8fsztsew

8fsztsew1#

我最近也遇到了这个错误,很明显,把tensorflow版本改回2.4.1版本就解决了这个问题。不幸的是,我不知道这个错误的幕后解释。

相关问题