我使用 Colab 进行编码并收到此错误:
AttributeError: module 'keras.api.backend' has no attribute 'clip'.
我尝试升级 TensorFlow 和 Keras,但仍然遇到相同的错误。在第一个 epoch 拟合模型时出现此错误。 我该如何解决它?
import segmentation_models as sm
model_vgg16=sm.Unet(backbone_name=backbone,input_shape=(256,256,3),classes=4,activation="softmax",encoder_weights="imagenet",decoder_use_batchnorm=True,encoder_freeze=False )
model_vgg16.summary()
"""# loss and metrics"""
loss="categorical_crossentropy"
dice_loss=sm.losses.DiceLoss()
focal_loss=sm.losses.CategoricalFocalLoss()
focal_dice_loss=sm.losses.categorical_focal_dice_loss
metric=[sm.metrics.IOUScore(threshold=0.5)]
"""# compile"""
lr=0.001
model_vgg16.compile(optimizer=keras.optimizers.Adam(learning_rate=lr),
loss=[focal_dice_loss],
metrics=[metric])
history = model_vgg16.fit(preprocessed_x_train, ytrain_categorical, epochs=20,validation_data=(preprocessed_x_val,y_val_categorical),batch_size=32)
错误:
Epoch 1/20
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-76-887dcd97e6be> in <cell line: 1>()
----> 1 history = model_vgg16.fit(preprocessed_x_train, ytrain_categorical, epochs=20,
2 validation_data=(preprocessed_x_val,y_val_categorical),
3 batch_size=32)
3 frames
/usr/local/lib/python3.10/dist-packages/segmentation_models/base/functional.py in categorical_focal_loss(gt, pr, gamma, alpha, class_indexes, **kwargs)
276
277 # clip to prevent NaN's and Inf's
--> 278 pr = backend.clip(pr, backend.epsilon(), 1.0 - backend.epsilon())
279
280 # Calculate focal loss
AttributeError: module 'keras.api.backend' has no attribute 'clip'
! pip install keras==2.10.0 tensorflow==2.10.0