CNN Week 3 Assignment 1 Exercise 1

# UNQ_C1 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
# GRADED FUNCTION: yolo_filter_boxes

Test produces the error below. Where did I go wrong? --Sorry for not knowing how to format the error message with color.
(I noticed that threshold is .6 in the function definition, but .5 internally in the tests; why the difference? could that be the source of error?)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-2cab1199ef30> in <module>
      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()))

<ipython-input-6-630888cb84a6> in yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold)
     35     # same dimension as box_class_scores, and be True for the boxes you want to keep (with probability >= threshold)
     36     ## (≈ 1 line)
---> 37     filtering_mask = box_class_scores >= threshold
     38 
     39     # Step 4: Apply the mask to box_class_scores, boxes and box_classes

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py in greater_equal(x, y, name)
   4053       _result = pywrap_tfe.TFE_Py_FastPathExecute(
   4054         _ctx._context_handle, tld.device_name, "GreaterEqual", name,
-> 4055         tld.op_callbacks, x, y)
   4056       return _result
   4057     except _core._NotOkStatusException as e:

TypeError: Cannot convert 0.5 to EagerTensor of dtype int64

Hello @EduardoChicago

Welcome back! I have edited your post for formatting, and you might edit it to see what I have added.

First, the 0.6 in the function definition gives it a default value and it will be used if the threshold argument is not supplied when calling the function. Since we supplied threshold = 0.5, that value is used instead of 0.6. This should not cause a problem.

If we read the traceback, it was a type error that happened in the arrowed line:
image

When tensorflow compares two numbers, it needs them to be in the same type. I would guess that box_class_scores was somehow typed to be int64 and consequently Tensorflow wanted threshold to be int64 too. However, the latter is not possible because 0.5 can’t become an integer.

Now, threshold has to be a float, and box_class_scores should be float too. Therefore, you would need to first verify the current type of box_class_scores, and if it was indeed an int type, you would then need to trace back and figure out why it ended up that way. To check for a tensorflow variable’s type, you might add a print line after getting box_class_scores, and just remember to remove the print line after all debugging work or it may interfere with the autograder at submission.

    ### START CODE HERE
    # Step 1: Compute box scores
    ##(≈ 1 line)
    box_scores = None

    # Step 2: Find the box_classes using the max box_scores, keep track of the corresponding score
    ##(≈ 2 lines)
    # IMPORTANT: set axis to -1
    box_classes = None
    box_class_scores = None
    tf.print('dtype', 'box_class_scores', box_class_scores.dtype) #ADDED, remove this line after debugging
    
    # Step 3: Create a filtering mask based on "box_class_scores" by using "threshold". The mask should have the
    # same dimension as box_class_scores, and be True for the boxes you want to keep (with probability >= threshold)
    ## (≈ 1 line)
    filtering_mask = None
    
    # Step 4: Apply the mask to box_class_scores, boxes and box_classes
    ## (≈ 3 lines)
    scores = None
    boxes = None
    classes = None
    ### END CODE HERE

Good luck debugging!

Cheers,
Raymond

Replying to Raymond. I understand the nature of the error but still would not know how to fix it. Indeed, box_class_scores are integers. They are generated by tf.math.argmax and tf.math.reduce_max working on box_scores, which are floats. I tried to impose a float type to the argmax output, but it is not allowed (dtypes can only be int32 and int64). How to make box_class_scores float?
Many thanks. (After I understand this issue, I will ask you how to format correctly my topics).

But the point is box_class_scores should not be integers in the first place. Think about what you’re trying to achieve here. argmax gives you the index of the element that is largest, which will be an integer. So you use that to compute box_classes. But the actual score is the value at that position in the original scores array, which is not an integer, right? You don’t take reduce_max of argmax, you take reduce_max of the actual scores.

1 Like

If you have a tensor consisting of integers and you want to convert it to floats, that is easy:

x = tf.cast(x, tf.float32)

But I doubt that will help in this instance. You’ll most likely end up with an incorrect answer of the correct type. :nerd_face:

Hi Paul. I think I got it. thanks!