无法将大小为 1665179 的数组重塑为形状 (512,512,3,3)

2024-05-12

该脚本用于进行检测。

权重文件是 yolov4 coco 预训练模型,可以在这里找到。(https://drive.google.com/file/d/1cewMfusmPjYWbrnuJRuKhPMwRe_b9PaT/view 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 https://github.com/hunglc007/tensorflow-yolov4-tflite/blob/master/detect.py并尝试使用预先训练的权重进行演示检测


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

https://github.com/hunglc007/tensorflow-yolov4-tflite/issues/147#issuecomment-666736983 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 https://github.com/hunglc007/tensorflow-yolov4-tflite/blob/31b6d91503ecf57aa409c67b97f1596ee4ffdefc/core/config.py#L14

应将其更改为所需的 yolov4obj.names包含定制培训课程。这里,在前面的代码中有traffic.names.

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法将大小为 1665179 的数组重塑为形状 (512,512,3,3) 的相关文章

随机推荐

  • 正确配置JDK环境变量后仍然找不到java命令

    我在 Windows 虚拟机启动时安装 JDK 使用 cloudinit 用户数据将 PowerShell 脚本传输到 Windows 计算机 然后运行该脚本来安装 JDK softwares Get ItemProperty HKLM S
  • XcodeColors 在 XCode 5 中不起作用

    我尝试安装XcodeColors在 XCode 5 中 但不幸的是 它不能与我从 XCode 4 6 获得的旧插件一起使用 下一步 我检查了 github 网站 在那里我看到了以下拉取请求 提供了 XCode 5 的工作版本 https g
  • 如何在 StatusIcon 上显示文本而不是设置图标?

    我是 Gtk 和 Python 的初学者开发人员 我正在尝试创建一个 gtk StatusIcon 显示文本字符串来代替图标 我如何使用 PixBuf 或任何其他方式来完成此任务 Thanks 使用 Python 和 GTK3 的示例 fr
  • Android 中的 JDBC 连接

    有没有人在 android 中尝试过 JDBC 连接 因为在 Android 2 3 中支持 JDBC 我必须在没有 Web 服务的情况下连接 Mysql 我已经提出申请 但它给了我错误 public class MysqlConnect
  • 为什么这会破坏 UILabel adjustmentFontSizeToFitWidth?

    iOS 7 Xcode 5 使用 UILabel 此代码可以工作 自动调整文本大小以适应 self testLabel numberOfLines 0 self testLabel lineBreakMode NSLineBreakByWo
  • C# 中的可选参数

    我在下面的代码中使用可选参数 但这显示错误 不允许使用默认参数说明符 任何人都可以帮助我 先生 public void getno int pvalu string pname 看起来有些答案中存在一些错误信息 C 4 中引入了可选参数 因
  • EDI AS2 HTTP 跟踪?

    我们正在研究 AS2 实现 并希望能够构建有意义的测试用例以与 SoapUI 或 Postman 一起使用 为了做到这一点 我们有两种方法 只是尝试从现有客户端进行 tcp 转储 跟踪调用 从普通 EDI 文档开始手动构建一些简单的调用 或
  • Install-Package:无法解析远程名称:“packages.nuget.org”[关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 无缘无故地
  • 可视化对象检测图时 TensorBoard 挂起

    我需要可视化 TensorFlow 对象检测模型的结构 我正在尝试在 Colab 中使用 TensorBoard 并使用下面的代码 当 TensorBoard 加载日志时 它会卡在 命名空间层次结构 查找相似子图 步骤上 pip insta
  • 如何将视图动态添加到已在 xml 布局中声明的relativelayout?

    我宣布了一个RelativeLayout在 xml 布局文件中 现在我想添加Views从代码到现有布局 我添加了一个Button通过代码动态地更改为现有布局 如下所示 rLayout RelativeLayout findViewById
  • 通过 HTTPS 加载页面但请求不安全的 XMLHttpRequest 端点

    我有一个页面 上面有一些 D3 javascript 该页面位于 HTTPS 网站内 但证书是自签名的 当我加载页面时 我的 D3 可视化效果不显示 并且出现错误 混合内容 页面位于 https integration jsite com
  • 为什么 lodash 将我的数组转换为对象?

    我是 lodash 的新手 创建了一个函数 该函数从值为 null 或空白的对象中删除键 但是当我传递包含某些部分作为数组的对象时 它会删除数组并将其转换为对象 下面是我尝试过的代码 mixin removeFalsies this rem
  • 如何使用 django 过滤器 icontains 获取多个字段

    我正在尝试将查询搜索与所有模型字段进行比较 但我不知道如何在多个字段中执行此操作 这是我的代码 expense Expense objects filter user request user id order by date q requ
  • 如何在java中压缩/解压tar.gz文件

    谁能告诉我在java中压缩和解压缩tar gzip文件的正确方法我一直在搜索 但我能找到的最多的是zip或gzip 单独 我写了一个包装器公共压缩 http commons apache org compress called jarchi
  • Firebase如何获取用户详细信息?

    我在我的应用程序中使用 Firebase 身份验证 并使用电子邮件和密码注册用户 当用户使用自己的帐户登录时 我也想获取其他用户的详细信息 与登录用户分开 我怎样才能得到这些信息 电子邮件 显示名称和 ID 特定于身份验证系统 等值可从Fi
  • 当 OpenGL 中同时绑定 1D 和 2D 纹理时,正确的行为是什么?

    假设你有这样的东西 glBindTexture GL TEXTURE 2D my2dTex glBindTexture GL TEXTURE 1D my1dTex glBegin 正确的 OpenGL 行为是什么 要绘制一维纹理 二维纹理还
  • pip install ecos 错误,显示“需要 Microsoft Visual C++ 14.0”。 [复制]

    这个问题在这里已经有答案了 我正在尝试使用 pip install 在我的 anaconda 中安装 fancyimpute 但错误显示由于 需要 Microsoft Visual C 14 0 而无法安装 ecos 提供的链接已过期 有谁
  • Term::ReadLine - 我需要点击向上箭头两次才能检索历史记录

    我正在使用 Term ReadLine 并遇到一个奇怪的问题 我需要点击向上箭头两次才能从 addhistory 中检索项目 这是我正在使用的脚本 use Term ReadLine my term Term ReadLine gt new
  • RxJS - 我需要取消订阅吗

    如果我有这样的事情 class MyComponent constructor this interval Observbale interval 1000 const c new MyComponent const subscriptio
  • 无法将大小为 1665179 的数组重塑为形状 (512,512,3,3)

    该脚本用于进行检测 权重文件是 yolov4 coco 预训练模型 可以在这里找到 https drive google com file d 1cewMfusmPjYWbrnuJRuKhPMwRe b9PaT view https dri