Course 4, Week 3, Part 1: yolo_filter_boxes function problem "TypeError: 'tuple' object is not callable"

I have a problem with the first function in Week3 assignment. I get this error and I can not figure out what did I wrong in box_classes .I used tf.math.argmax function and it seems to be right. The error is copied below. Can anyone help please, thank you in advance.

TypeError Traceback (most recent call last)
in
4 boxes = tf.random.normal([19, 19, 5, 4], mean=1, stddev=4, seed = 1)
5 box_class_probs = tf.random.normal([19, 19, 5, 80], mean=1, stddev=4, seed = 1)
----> 6 scores, boxes, classes = yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold = 0.5)
7 print("scores[2] = " + str(scores[2].numpy()))
8 print("boxes[2] = " + str(boxes[2].numpy()))

in yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold)
40 scores = tf.boolean_mask=(box_class_scores,filtering_mask)
41 boxes = tf.boolean_mask=(boxes,filtering_mask)
—> 42 classes = tf.boolean_mask(box_classes,filtering_mask)
43 ### END CODE HERE
44

TypeError: ‘tuple’ object is not callable

In line 40 of the above trace, you are assigning tf.boolean_mask the value of a tuple. Why are there two equal signs in that line of code? Then in the next line when you try to use it as a function, it throws an exception.

Note that it looks like you haven’t re-executed the actual yolo_filter_boxes cell since the last time you changed the code.

1 Like

The equal sings probably got there by mistake when i tried to rewrite the last part ,but when I erase them the error is still the same.

TypeError Traceback (most recent call last)
in
4 boxes = tf.random.normal([19, 19, 5, 4], mean=1, stddev=4, seed = 1)
5 box_class_probs = tf.random.normal([19, 19, 5, 80], mean=1, stddev=4, seed = 1)
----> 6 scores, boxes, classes = yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold = 0.5)
7 print("scores[2] = " + str(scores[2].numpy()))
8 print("boxes[2] = " + str(boxes[2].numpy()))

in yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold)
38 # Step 4: Apply the mask to box_class_scores, boxes and box_classes
39 ## (≈ 3 lines)
—> 40 scores = tf.boolean_mask(box_class_scores,filtering_mask)
41 boxes = tf.boolean_mask(boxes,filtering_mask)
42 classes = tf.boolean_mask(box_classes,filtering_mask)

@paulinpaloalto has definitely identified a source of trouble. While it is legal Python to have two assignment operators (equal signs) in one line of code, it isn’t what is needed or appropriate here. It also has unintended side effects.

    #this is fine
b = a = 8
print(b)

==> 8

    #this code is also fine
import numpy as np
import tensorflow as tf

print(type(tf.boolean_mask))
<class 'function'>

z = np.ones((3,1))
mask_applied = tf.boolean_mask(z, (True,True,True))
print(mask_applied)

tf.Tensor(
[[1.]
 [1.]
 [1.]], shape=(3, 1), dtype=float64)
    #this code runs, but doesn't do what is intended
not_as_intended = tf.boolean_mask = 8
print(not_as_intended)
print(tf.boolean_mask)
print(type(tf.boolean_mask))

==> 8
==> 8
==> <class ‘int’>

The type of the variable tf.boolean_mask has been changed.

And once its type has been changed from class function to anything else, you can’t treat it as if it were a function and invoke, or call, it. Further, the type won’t be reset until the kernel is restarted and you import tensorflow again.

   #function call that worked above no longer does
try_to_apply_mask_again = tf.boolean_mask(z, (True,True,True))
print(try_to_apply_mask_again)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/v3/cqv82ph57nq2c4sggjqqlm180000gn/T/ipykernel_7931/3912514312.py in <module>
----> 1 try_to_apply_mask_again = tf.boolean_mask(z, (True,True,True))
      2 print(try_to_apply_mask_again)

TypeError: 'int' object is not callable
1 Like

thanks a lot, the problem is solved thanks to your help. I restarted the kernel and it passed the test.

1 Like