ZFTurbo / segmentation_models_3D

Set of models for segmentation of 3D volumes
MIT License
125 stars 26 forks source link

Error during .fit in latest version. #34

Open Pineirin opened 6 months ago

Pineirin commented 6 months ago

Greetings.

I tried using your library to perform a 3D semantic segmentation. I attempted to follow Sreeni's (https://www.youtube.com/watch?v=Dt73QWZQck4) tutorial, but I kept running into this error during the fit.

Exception has occurred: AttributeError module 'keras.api.ops' has no attribute 'pow' File "C:\Users\Pineirin\Desktop\3DCNN\3D_UNET.py", line 72, in model.fit( AttributeError: module 'keras.api.ops' has no attribute 'pow'

I'm using your lates version, after installing the tensorflow, keras and classification_models_3D version defined in your requirements.txt.

I also attempted to use your library myself, as i guess the problem is with the updates, using a chatbot to aid me. But I ran into exactly the same error. I'm using Windows 10 and 11.

Sorry if I'm bothering with an issue that is due to my error, but I feel that if I reach the same error from two different paths it may be an error of the library.

Please let me know if you need any additional information.

Lewis-CJ commented 5 months ago

Not following a tutorial, but a quick fix for this is to change ops.pow to ops.power: https://keras.io/api/ops/numpy/#power-function

ZFTurbo commented 5 months ago

Please let me know if you need any additional information.

Can you please put full error log?

simolb7 commented 5 months ago

i have the same error, how can i fix?

simolb7 commented 5 months ago

Please let me know if you need any additional information.

Can you please put full error log?


AttributeError Traceback (most recent call last) in <cell line: 21>() 19 print(model.output_shape) 20 ---> 21 history=model.fit(train_img_datagen, 22 steps_per_epoch=steps_per_epoch, 23 epochs=100,

4 frames /usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py in error_handler(*args, **kwargs) 120 # To get the full stack trace, call: 121 # keras.config.disable_traceback_filtering() --> 122 raise e.with_traceback(filtered_tb) from None 123 finally: 124 del filtered_tb

/usr/local/lib/python3.10/dist-packages/segmentation_models_3D/base/objects.py in call(self, gt, pr) 128 129 def call(self, gt, pr): --> 130 return self.l1(gt, pr) + self.l2(gt, pr)

/usr/local/lib/python3.10/dist-packages/segmentation_models_3D/base/objects.py in call(self, gt, pr) 116 117 def call(self, gt, pr): --> 118 return self.multiplier * self.loss(gt, pr) 119 120

/usr/local/lib/python3.10/dist-packages/segmentation_models_3D/losses.py in call(self, gt, pr) 198 199 def call(self, gt, pr): --> 200 return F.categorical_focal_loss( 201 gt, 202 pr,

/usr/local/lib/python3.10/dist-packages/segmentation_models_3D/base/functional.py in categorical_focal_loss(gt, pr, gamma, alpha, class_indexes, *kwargs) 284 285 # Calculate focal loss --> 286 loss = - gt (alpha ops.pow((1 - pr), gamma) ops.log(pr)) 287 288 return ops.mean(loss)

AttributeError: module 'keras.api.ops' has no attribute 'pow'

I am working on colab

ZFTurbo commented 5 months ago

Change in: /usr/local/lib/python3.10/dist-packages/segmentation_models_3D/base/functional.py

ops.pow -> ops.power

I did an update in repository too

simolb7 commented 5 months ago

I made your changes, but it s not still working. There are also other 2 lines in functional.py that use ops.pow (row 311-312).

Now i have this error:


TypeError Traceback (most recent call last) in <cell line: 21>() 19 print(model.output_shape) 20 ---> 21 history=model.fit(train_img_datagen, 22 steps_per_epoch=steps_per_epoch, 23 epochs=100,

2 frames /usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py in error_handler(*args, **kwargs) 120 # To get the full stack trace, call: 121 # keras.config.disable_traceback_filtering() --> 122 raise e.with_traceback(filtered_tb) from None 123 finally: 124 del filtered_tb

/usr/local/lib/python3.10/dist-packages/segmentation_models_3D/metrics.py in call(self, gt, pr) 52 53 def call(self, gt, pr): ---> 54 return F.iou_score( 55 gt, 56 pr,

/usr/local/lib/python3.10/dist-packages/segmentation_models_3D/base/functional.py in iou_score(gt, pr, class_weights, class_indexes, smooth, per_image, threshold, *kwargs) 93 # score calculation 94 ---> 95 intersection = ops.sum(gt pr, axis=axes) 96 union = ops.sum(gt + pr, axis=axes) - intersection 97

TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type float64 of argument 'x'.

Thank you

ZFTurbo commented 5 months ago

Can you say which loss function you use? And which metric?

simolb7 commented 5 months ago

Sure!

dice_loss = sm.losses.DiceLoss(class_weights=np.array([wt0, wt1, wt2, wt3])) focal_loss = sm.losses.CategoricalFocalLoss() total_loss = dice_loss + (1 * focal_loss) metrics = ['accuracy', sm.metrics.IOUScore(threshold=0.5)]

ZFTurbo commented 5 months ago

I modified:

https://github.com/ZFTurbo/segmentation_models_3D/blob/master/training_example_torch.py

with your settings:

    dice_loss = sm.losses.DiceLoss()
    focal_loss = sm.losses.CategoricalFocalLoss()
    total_loss = dice_loss + (1 * focal_loss)
    model.compile(optimizer=optim, loss=total_loss, metrics=[sm.metrics.IOUScore])

It works for me. May be problem with backend. Which one do you use?

изображение