python - 无法将大小为 1665179 的数组 reshape 为形状 (512,512,3,3)

标签 python tensorflow deep-learning object-detection yolo

用于检测的脚本。

权重文件是 yolov4 coco 预训练模型,可以在这里找到。( https://drive.google.com/file/d/1cewMfusmPjYWbrnuJRuKhPMwRe_b9PaT/view )

import time
from absl import app, flags, logging
from absl.flags import FLAGS
import core.utils as utils
from core.yolov4 import YOLOv4, YOLOv3, YOLOv3_tiny, decode
from PIL import Image
from core.config import cfg
import cv2
import numpy as np
import tensorflow as tf

flags.DEFINE_string('framework', 'tf', '(tf, tflite')
flags.DEFINE_string('weights', './data/yolov4.weights',
                    'path to weights file')
flags.DEFINE_integer('size', 608, 'resize images to')
flags.DEFINE_boolean('tiny', False, 'yolo or yolo-tiny')
flags.DEFINE_string('model', 'yolov4', 'yolov3 or yolov4')
flags.DEFINE_string('image', './data/kite.jpg', 'path to input image')
flags.DEFINE_string('output', 'result.png', 'path to output image')

def main(_argv):
    if FLAGS.tiny:
        STRIDES = np.array(cfg.YOLO.STRIDES_TINY)
        ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_TINY, FLAGS.tiny)
    else:
        STRIDES = np.array(cfg.YOLO.STRIDES)
        if FLAGS.model == 'yolov4':
            ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS, FLAGS.tiny)
        else:
            ANCHORS = utils.get_anchors(cfg.YOLO.ANCHORS_V3, FLAGS.tiny)
    NUM_CLASS = len(utils.read_class_names(cfg.YOLO.CLASSES))
    XYSCALE = cfg.YOLO.XYSCALE
    input_size = FLAGS.size
    image_path = FLAGS.image

    original_image = cv2.imread(image_path)
    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
    original_image_size = original_image.shape[:2]

    image_data = utils.image_preprocess(np.copy(original_image), [input_size, input_size])
    image_data = image_data[np.newaxis, ...].astype(np.float32)
    if FLAGS.framework == 'tf':
        input_layer = tf.keras.layers.Input([input_size, input_size, 3])
        if FLAGS.tiny:
            feature_maps = YOLOv3_tiny(input_layer, NUM_CLASS)
            bbox_tensors = []
            for i, fm in enumerate(feature_maps):
                bbox_tensor = decode(fm, NUM_CLASS, i)
                bbox_tensors.append(bbox_tensor)
            model = tf.keras.Model(input_layer, bbox_tensors)
            utils.load_weights_tiny(model, FLAGS.weights)
        else:
            if FLAGS.model == 'yolov3':
                feature_maps = YOLOv3(input_layer, NUM_CLASS)
                bbox_tensors = []
                for i, fm in enumerate(feature_maps):
                    bbox_tensor = decode(fm, NUM_CLASS, i)
                    bbox_tensors.append(bbox_tensor)
                model = tf.keras.Model(input_layer, bbox_tensors)
                utils.load_weights_v3(model, FLAGS.weights)
            elif FLAGS.model == 'yolov4':
                feature_maps = YOLOv4(input_layer, NUM_CLASS)
                bbox_tensors = []
                for i, fm in enumerate(feature_maps):
                    bbox_tensor = decode(fm, NUM_CLASS, i)
                    bbox_tensors.append(bbox_tensor)
                model = tf.keras.Model(input_layer, bbox_tensors)

                if FLAGS.weights.split(".")[len(FLAGS.weights.split(".")) - 1] == "weights":
                    utils.load_weights(model, FLAGS.weights)
                else:
                    model.load_weights(FLAGS.weights).expect_partial()

        model.summary()
        pred_bbox = model.predict(image_data)
    else:
        # Load TFLite model and allocate tensors.
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        # Get input and output tensors.
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
        interpreter.set_tensor(input_details[0]['index'], image_data)
        interpreter.invoke()
        pred_bbox = [interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details))]

    if FLAGS.model == 'yolov4':
        pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES, XYSCALE)
    else:
        pred_bbox = utils.postprocess_bbbox(pred_bbox, ANCHORS, STRIDES)
    bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.25)
    bboxes = utils.nms(bboxes, 0.213, method='nms')

    image = utils.draw_bbox(original_image, bboxes)
    image = Image.fromarray(image)
    image.show()
    # image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
    # cv2.imwrite(FLAGS.output, image)

if __name__ == '__main__':
    try:
        app.run(main)
    except SystemExit:
        pass

错误日志:

Traceback (most recent call last):
  File "detect.py", line 100, in <module>
    app.run(main)
  File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 299, in run
    _run_main(main, args)
  File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 250, in _run_main
    sys.exit(main(argv))
  File "detect.py", line 68, in main
    utils.load_weights(model, FLAGS.weights)
  File "/content/tensorflow-yolov4/core/utils.py", line 114, in load_weights
    conv_weights = conv_weights.reshape(conv_shape).transpose([2, 3, 1, 0])
ValueError: cannot reshape array of size 1665179 into shape (512,512,3,3)

您可以使用 github 重现此错误: https://github.com/hunglc007/tensorflow-yolov4-tflite/blob/master/detect.py并尝试使用预先训练的权重进行演示检测

最佳答案

我有类似的 reshape 错误,但我在使用此存储库自定义 tflite 转换模型的 Android 代码示例中得到了它。此评论解决了我的问题,

https://github.com/hunglc007/tensorflow-yolov4-tflite/issues/147#issuecomment-666736983 .

在配置文件中,coco.names 是硬编码的。

https://github.com/hunglc007/tensorflow-yolov4-tflite/blob/31b6d91503ecf57aa409c67b97f1596ee4ffdefc/core/config.py#L14

应该使用包含自定义训练类的所需 yolov4 obj.names 进行更改。在这里,在前面的代码中有 traffic.names

https://github.com/hunglc007/tensorflow-yolov4-tflite/commit/31b6d91503ecf57aa409c67b97f1596ee4ffdefc#diff-97ed6d1d0787c0eb92725a8c161e43c9b5281d1f671cc27d91809406ff084368R14

关于python - 无法将大小为 1665179 的数组 reshape 为形状 (512,512,3,3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62406263/

相关文章:

python - Keras:骰子系数损失函数为负且随epochs增加

tensorflow - 我的模型训练中 val_acc 大幅下降,原因是什么?

python - Django 教程应用程序,第 2 部分。为什么缺少 - 在 makemigrations 命令后添加字段问题到选择?

python - 如何替换矩阵中对应于另一个矩阵的值?

c++ - 在 C++ 中运行经过训练的 tensorflow 模型

python - DNN分类器 : 'DataFrame' object has no attribute 'dtype'

deep-learning - 如何对pytorch中的变量应用指数移动平均衰减?

python - 必须使用某种集合调用索引 : assign column name to dataframe

python - 跨数据库加入sqlalchemy

python - Keras 值错误 : Error when checking target: expected dense_1 to have 3 dimensions