coursera(deep specialization course): CNN week-3 assignment car detection with YOLO

Viewed 182

I have been struggling with Attribute Error: 'list' object has no attribute 'dtype'. This is from the coursera assignment, everything works fine in coursera jupyter notebook but when i run it in in my local machine then i started getting this error dont know why? could anyone help me with this out.

here in below code we are Converting output of the model to usable bounding box tensors.

yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-90-d69bb71a2d56> in <module>
----> 1 yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))

~\object detection\yad2k\models\keras_yolo.py in yolo_head(feats, anchors, num_classes)
    107     conv_index = K.transpose(K.stack([conv_height_index, conv_width_index]))
    108     conv_index = K.reshape(conv_index, [1, conv_dims[0], conv_dims[1], 1, 2])
--> 109     conv_index = K.cast(conv_index, K.dtype(feats))
    110 
    111     feats = K.reshape(feats, [-1, conv_dims[0], conv_dims[1], num_anchors, num_classes + 5])

~\anaconda3\envs\tensorflow_env\lib\site-packages\tensorflow\python\util\dispatch.py in wrapper(*args, **kwargs)
    199     """Call target, and fall back on dispatchers if there is a TypeError."""
    200     try:
--> 201       return target(*args, **kwargs)
    202     except (TypeError, ValueError):
    203       # Note: convert_to_eager_tensor currently raises a ValueError, not a

~\anaconda3\envs\tensorflow_env\lib\site-packages\tensorflow\python\keras\backend.py in dtype(x)
   1369 
   1370   """
-> 1371   return x.dtype.base_dtype.name
   1372 
   1373 

AttributeError: 'list' object has no attribute 'dtype'
def yolo_head(feats, anchors, num_classes):
    """Convert final layer features to bounding box parameters.

    Parameters
    ----------
    feats : tensor
        Final convolutional layer features.
    anchors : array-like
        Anchor box widths and heights.
    num_classes : int
        Number of target classes.

    Returns
    -------
    box_xy : tensor
        x, y box predictions adjusted by spatial location in conv layer.
    box_wh : tensor
        w, h box predictions adjusted by anchors and conv spatial resolution.
    box_conf : tensor
        Probability estimate for whether each box contains any object.
    box_class_pred : tensor
        Probability distribution estimate for each box over class labels.
    """
    num_anchors = len(anchors)
    # Reshape to batch, height, width, num_anchors, box_params.
    anchors_tensor = K.reshape(K.variable(anchors), [1, 1, 1, num_anchors, 2])
    # Static implementation for fixed models.
    # TODO: Remove or add option for static implementation.
    # _, conv_height, conv_width, _ = K.int_shape(feats)
    # conv_dims = K.variable([conv_width, conv_height])

    # Dynamic implementation of conv dims for fully convolutional model.
    conv_dims = K.shape(feats)[1:3]  # assuming channels last
    # In YOLO the height index is the inner most iteration.
    conv_height_index = K.arange(0, stop=conv_dims[0])
    conv_width_index = K.arange(0, stop=conv_dims[1])
    conv_height_index = K.tile(conv_height_index, [conv_dims[1]])

    # TODO: Repeat_elements and tf.split doesn't support dynamic splits.
    # conv_width_index = K.repeat_elements(conv_width_index, conv_dims[1], axis=0)
    conv_width_index = K.tile(K.expand_dims(conv_width_index, 0), [conv_dims[0], 1])
    conv_width_index = K.flatten(K.transpose(conv_width_index))
    conv_index = K.transpose(K.stack([conv_height_index, conv_width_index]))
    conv_index = K.reshape(conv_index, [1, conv_dims[0], conv_dims[1], 1, 2])
    conv_index = K.cast(conv_index, K.dtype(feats))
    
    feats = K.reshape(feats, [-1, conv_dims[0], conv_dims[1], num_anchors, num_classes + 5])
    conv_dims = K.cast(K.reshape(conv_dims, [1, 1, 1, 1, 2]), K.dtype(feats))

    # Static generation of conv_index:
    # conv_index = np.array([_ for _ in np.ndindex(conv_width, conv_height)])
    # conv_index = conv_index[:, [1, 0]]  # swap columns for YOLO ordering.
    # conv_index = K.variable(
    #     conv_index.reshape(1, conv_height, conv_width, 1, 2))
    # feats = Reshape(
    #     (conv_dims[0], conv_dims[1], num_anchors, num_classes + 5))(feats)

    box_confidence = K.sigmoid(feats[..., 4:5])
    box_xy = K.sigmoid(feats[..., :2])
    box_wh = K.exp(feats[..., 2:4])
    box_class_probs = K.softmax(feats[..., 5:])

    # Adjust preditions to each spatial grid point and anchor size.
    # Note: YOLO iterates over height index before width index.
    box_xy = (box_xy + conv_index) / conv_dims
    box_wh = box_wh * anchors_tensor / conv_dims

    return box_confidence, box_xy, box_wh, box_class_probs```
1 Answers

What is the type of your input to the model? You should cast it into a NumPy array first.

a = [1, 2, 3]
a.dtype

This will raise your error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-b3cdf8323901> in <module>
----> 1 a.dtype

AttributeError: 'list' object has no attribute 'dtype'

After casting to a NumPy array, it'll work correctly:

import numpy as np
a = np.array(a)
a.dtype # dtype('int64')
Related