0 Vote C1_W3_Assignment # UNQ_C4 - dice_coefficient

I am trying to solve the UNQ_C4, but I am not able to do it.

I solved the UNQ_C3 and when trying to generalize that solution for the multiple classes I am not succeeding, I have really looked for documentation on how to program this and I have not found it, and during the course no examples were given, that is why I am blocked at this moment and I can’t continue with this last lab.

Could someone give me some indication of where to look for possible solutions on how to implement this?

The result I am getting is the following:

Test Case 1:

Pred:

[[1. 0.]
[0. 1.]]

Label:

[[1. 1.]
[0. 0.]]

Dice coefficient: 0.6805555555555556


Test Case 2:

Pred:

[[1. 0.]
[0. 1.]]

Label:

[[1. 1.]
[0. 1.]]

Dice coefficient: 0.875


Test Case 3:

Pred:

class = 0
[[1. 0.]
[0. 1.]]

class = 1
[[1. 0.]
[0. 1.]]

Label:

class = 0
[[1. 1.]
[0. 0.]]

class = 1
[[1. 1.]
[0. 1.]]

Dice coefficient: 0.7777777777777777

Error: Wrong output for Test Case 1. One possible reason for error: make sure epsilon = 1
Error: Wrong output for Test Case 2. One possible reason for error: make sure epsilon = 1
Error: Wrong output for Test Case 3. One possible reason for error: make sure epsilon = 1
4 Tests passed
3 Tests failed

And the expected result is:

Expected output:

Test Case 1:

Pred:

[[1. 0.]
[0. 1.]]

Label:

[[1. 1.]
[0. 0.]]

Dice coefficient: 0.6


Test Case 2:

Pred:

[[1. 0.]
[0. 1.]]

Label:

[[1. 1.]
[0. 1.]]

Dice coefficient: 0.8333333333333334


Test Case 3:

Pred:

class = 0
[[1. 0.]
[0. 1.]]

class = 1
[[1. 0.]
[0. 1.]]

Label:

class = 0
[[1. 1.]
[0. 0.]]

class = 1
[[1. 1.]
[0. 1.]]

Dice coefficient: 0.7166666666666667

1 Like

Hi @Marcelo_Dotti,

Can you share your lab ID with me ? In the assignment, when you click the top right “Help” button, a panel will open and your lab ID will be shown at the bottom.
I shall take a look.

When you reply back, kindly tag me in the post so that I’m notified.

Thanks,
Mubsi

Hello @Mubsi

I am getting similar problems with the below error output

Test Case 1:

Pred:

[[1. 0.]
[0. 1.]]

Label:

[[1. 1.]
[0. 0.]]

Soft Dice Loss: 0.4


Test Case 2:

Pred:

[[0.5 0. ]
[0. 0.5]]

Label:

[[1. 1.]
[0. 0.]]

Soft Dice Loss: 0.5


Test Case 3:

Pred:

[[1. 0.]
[0. 1.]]

Label:

[[1. 1.]
[0. 1.]]

Soft Dice Loss: 0.16666666666666663


Test Case 4:

Pred:

[[1. 0.8]
[0. 1. ]]

Label:

[[1. 1.]
[0. 1.]]

Soft Dice Loss: 0.02941176470588236


Test Case 5:

Pred:

class = 0
[[0.5 0. ]
[0. 0.5]]

class = 1
[[1. 0.8]
[0. 1. ]]

Label:

class = 0
[[1. 1.]
[0. 0.]]

class = 1
[[1. 1.]
[0. 1.]]

Soft Dice Loss: 0.2647058823529411


Test Case 6:

Soft Dice Loss: 0.4375

Error: Wrong output for Test Case 2. One possible reason for error: make sure epsilon = 1
Error: Wrong output for Test Case 4. One possible reason for error: make sure epsilon = 1
Error: Wrong output for Test Case 5. One possible reason for error: make sure epsilon = 1
9 Tests passed
3 Tests failed

AssertionError Traceback (most recent call last)
in ()
6 ### do not edit anything below
7 sess = K.get_session()
----> 8 soft_dice_loss_test(soft_dice_loss, epsilon, sess)

~/work/W3A1/public_tests.py in soft_dice_loss_test(target, epsilon, sess)
412 ]
413
→ 414 multiple_test_dice(test_cases, target, sess)
415
416 ##############################################

~/work/W3A1/test_utils.py in multiple_test_dice(test_cases, target, sess)
183 print(‘\033[92m’, success," Tests passed")
184 print(‘\033[91m’, len(test_cases) - success, " Tests failed")
→ 185 raise AssertionError(“Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.”.format(target.name))

AssertionError: Not all tests were passed for soft_dice_loss. Check your equations and avoid using global variables inside the function.

My lab ID is ihmvjxwbijns

1 Like

Hi @Eric5,

Your equation for dice_denominator is incorrect. Your are not considering taking power with 2.

Best,
Mubsi

1 Like

Thanks so much, that was very helpful @Mubsi

1 Like

Hi. I have correctly defined multiple dice coefficient but getting errors after submission. Test Case 1:

Pred:

[[1. 0.]
[0. 1.]]

Label:

[[1. 1.]
[0. 0.]]

Dice coefficient: 0.6


Test Case 2:

Pred:

[[1. 0.]
[0. 1.]]

Label:

[[1. 1.]
[0. 1.]]

Dice coefficient: 0.8333333333333334


Test Case 3:

Pred:

class = 0
[[1. 0.]
[0. 1.]]

class = 1
[[1. 0.]
[0. 1.]]

Label:

class = 0
[[1. 1.]
[0. 0.]]

class = 1
[[1. 1.]
[0. 1.]]

Dice coefficient: 0.7166666666666667

All tests passed.
Expected output:
Test Case 1:

Pred:

[[1. 0.]
[0. 1.]]

Label:

[[1. 1.]
[0. 0.]]

Dice coefficient: 0.6


Test Case 2:

Pred:

[[1. 0.]
[0. 1.]]

Label:

[[1. 1.]
[0. 1.]]

Dice coefficient: 0.8333333333333334


Test Case 3:

Pred:

class = 0
[[1. 0.]
[0. 1.]]

class = 1
[[1. 0.]
[0. 1.]]

Label:

class = 0
[[1. 1.]
[0. 0.]]

class = 1
[[1. 1.]
[0. 1.]]

Dice coefficient: 0.7166666666666667
All tests passed. Function “keras.utils.to_categorical” not found in Code Cell UNQ_C1.
Function “np.moveaxis” not found in Code Cell UNQ_C1.
Code Cell UNQ_C1: Function ‘get_sub_volume’ is correct.
Code Cell UNQ_C2: Function ‘standardize’ is correct.
Code Cell UNQ_C3: Function ‘single_class_dice_coefficient’ is correct.
Code Cell UNQ_C4: Function ‘dice_coefficient’ is incorrect. Check implementation.
Code Cell UNQ_C5: Function ‘soft_dice_loss’ is correct.
Code Cell UNQ_C6: Function ‘compute_class_sens_spec’ is correct.
@Mubsi @anyone can you please help

1 Like

Hi @MUHAMMAD_SAAD786,

As per the output of the autograder, you have to use these functions in your Ex 1. Please go through the instructions of Ex 1 on where and how they apply.

Thanks,
Mubsi

For Ex 4, I’ll direct message you.

Hi @MUHAMMAD_SAAD786,

I have already mentioned above how you can fix your Ex 1.

Now, for your Ex 4, please note the following:

  • You are NEVER allowed to change the function parameters that are provided to you.

This is Ex 4 taken from your notebook:

def dice_coefficient(y_true, y_pred, axis=(1, 2, 3),
                   epsilon=1):

And this is the default function provided by us:

def dice_coefficient(y_true, y_pred, axis=(1, 2, 3), 
                   epsilon=0.00001):

As you can see, you changed the value for epsilon.

Please understand, when it is said to make sure epsilon should be equal 1, it is meant for this code cell only, and the epsilon variable provided inside of it, NOT the function parameter.

So the reason you pass the unit tests and fail the grader test for Ex 4 is because, for unit tests the epsilon is expected to be 1. But for the grader, it is expected to be a different value. But since you hardcoded the value of epsilon as 1, you passed the tests in the notebook, but failed the grader.

So keep in mind not make such a mistake again, as throughout the specialisation, unit tests and grader tests expect different values, your code should be generic, not hard coded.

Fix all these mistakes and you should be able to get the full grade.

Best,
Mubsi