【问题标题】:yolo_head when evaluates model gives: Attribute Error: list object has no attribute 'dtype'yolo_head 在评估模型时给出:属性错误:列表对象没有属性“dtype”
【发布时间】:2020-06-09 13:40:43
【问题描述】:

对不起,我是新手,但我无法在任何地方找到答案。 我正在尝试在我的计算机上实现 yolo 汽车检测,但我收到此错误并且不知道该怎么做。

这里是代码

sess = tf.keras.backend.get_session()


##detect 80 classes, usng 5 anchor boxes; 729x1280 images which are processed to 608x608 images

class_names = read_classes("model_data/coco_classes.txt")
anchors = read_anchors("model_data/yolo_anchors.txt")
image_shape = (720., 1280.)

#load the model
yolo_model = load_model("model_data/yolo.h5", compile = False)


#convert output of the model to usable bounding box tensors
yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))```

这是错误

Traceback (most recent call last):
  File "yolo.py", line 232, in <module>
    yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))
  File "C:\Users\Desktop\PYTHON\tensor\cardetect\yad2k\models\keras_yolo.py", line 113, in yolo_head
    conv_index = K.cast(conv_index, K.dtype(feats))
  File "C:\Users\Desktop\PYTHON\tensor\lib\site-packages\keras\backend\tensorflow_backend.py", line 905, in dtype
    return x.dtype.base_dtype.name
AttributeError: 'list' object has no attribute 'dtype'

这里是yolo_head的代码,不知道重要吗

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

我不知道我是否遗漏了一些关键细节,但在任何地方都找不到答案,我尝试了一些方法,但无济于事。告诉我是否需要添加更多信息,我将不胜感激。

我可能还应该补充一点,这是导入部分:

import argparse
import os
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
import scipy.io
import scipy.misc
import numpy as np
import pandas as pd
import PIL
import tensorflow as tf
from keras import backend as K
from keras.layers import Input, Lambda, Conv2D
from keras.models import load_model, Model
from yolo_utils import read_classes, read_anchors, generate_colors, preprocess_image, draw_boxes, scale_boxes
from yad2k.models.keras_yolo import yolo_head, yolo_boxes_to_corners, preprocess_true_boxes, yolo_loss, yolo_body
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

如您所见,我禁用了 tensorflow 2 作为 V1 工作,因为这是一些旧代码。我怀疑我的 keras 和 tensorflow 版本是问题所在,但如果我可以避免降级,或者有人可以向我解释问题所在,那就太好了,这样我就可以以某种方式解决它。这段代码在 Jupyter 书中有效,所以我不知道为什么它现在让我失望了。

【问题讨论】:

    标签: deep-learning attributeerror yolo dtype


    【解决方案1】:

    我最近遇到了同样的问题。首先,我假设您的代码和权重(yolo.h5)是从 Andrew Ng 博士的课程“yolo car detection”中复制的。我尝试打印 yolo.output,它就像:

    [<tf.Tensor 'conv2d_59/BiasAdd:0' shape=(?, ?, ?, 255) dtype=float32>]
    

    在这里找到解决方案 yolo_head_solution 基本上他把改变的专长变成了专长[0]

    其次,这是我满足的条件。我从 coursera 复制代码,然后从暗网下载权重。并且变量 yolo_model.output 与前一个不同,即

    [<tf.Tensor 'conv2d_59/BiasAdd:0' shape=(?, ?, ?, 255) dtype=float32>, <tf.Tensor 'conv2d_67/BiasAdd:0' shape=(?, ?, ?, 255) dtype=float32>, <tf.Tensor 'conv2d_75/BiasAdd:0' shape=(?, ?, ?, 255) dtype=float32>]
    

    所以解决办法是改yolo_eval(the code is from here):

    def yolo_eval(yolo_outputs,
              anchors,
              num_classes,
              image_shape,
              max_boxes=20,
              score_threshold=.6,
              iou_threshold=.5):
    """Evaluate YOLO model on given input and return filtered boxes."""
    num_layers = len(yolo_outputs)
    anchor_mask = [[6,7,8], [3,4,5], [0,1,2]] if num_layers==3 else [[3,4,5], [1,2,3]] # default setting
    input_shape = K.shape(yolo_outputs[0])[1:3] * 32
    boxes = []
    box_scores = []
    for l in range(num_layers):
        #here called yolo_head
        _boxes, _box_scores = yolo_boxes_and_scores(yolo_outputs[l],
            anchors[anchor_mask[l]], num_classes, input_shape, image_shape)
        boxes.append(_boxes)
        box_scores.append(_box_scores)
    boxes = K.concatenate(boxes, axis=0)
    box_scores = K.concatenate(box_scores, axis=0)
    
    mask = box_scores >= score_threshold
    max_boxes_tensor = K.constant(max_boxes, dtype='int32')
    boxes_ = []
    scores_ = []
    classes_ = []
    for c in range(num_classes):
        # TODO: use keras backend instead of tf.
        class_boxes = tf.boolean_mask(boxes, mask[:, c])
        class_box_scores = tf.boolean_mask(box_scores[:, c], mask[:, c])
        nms_index = tf.image.non_max_suppression(
            class_boxes, class_box_scores, max_boxes_tensor, iou_threshold=iou_threshold)
        class_boxes = K.gather(class_boxes, nms_index)
        class_box_scores = K.gather(class_box_scores, nms_index)
        classes = K.ones_like(class_box_scores, 'int32') * c
        boxes_.append(class_boxes)
        scores_.append(class_box_scores)
        classes_.append(classes)
    boxes_ = K.concatenate(boxes_, axis=0)
    scores_ = K.concatenate(scores_, axis=0)
    classes_ = K.concatenate(classes_, axis=0)
    
    return boxes_, scores_, classes_
    

    【讨论】:

    • 很好,终于有答案了,迫不及待想试试这个,我会用结果回复。我的 Yolo.h5 权重不是来自他的课程,因为由于某种原因我无法下载它们,所以我只是从网上下载了一些权重,认为它应该仍然有效,因为它用于相同的目的。 PARAMS 有点不同,我认为还有 20000 多个,认为这是个问题吗?我的意思是所有 coursera 代码都来自 YOLO 暗网网站,所以认为它应该仍然可以工作。我会试试你的解决方案,看看会发生什么。也许你知道我在哪里可以下载 Andrews 体重文件,因为 Coursera 不让我。
    猜你喜欢
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多