opendatalab / DocLayout-YOLO

DocLayout-YOLO: Enhancing Document Layout Analysis through Diverse Synthetic Data and Global-to-Local Adaptive Perception
https://huggingface.co/spaces/opendatalab/DocLayout-YOLO
GNU Affero General Public License v3.0
175 stars 9 forks source link

推理时增大batch_size #14

Closed luciaganlulu closed 2 hours ago

luciaganlulu commented 3 hours ago

您好,请问是直接改这里的batch数,另外source从单个image_path改成所有的图片的PIL.Image列表嘛?https://github.com/opendatalab/DocLayout-YOLO/blob/main/doclayout_yolo/engine/model.py#L431C8-L431C98

JulioZhao97 commented 2 hours ago

您好,是的

  1. 推理batch_size大小可以直接在 https://github.com/opendatalab/DocLayout-YOLO/blob/main/doclayout_yolo/engine/model.py#L431C8-L431C98 修改batch_size的值
  2. demo.py中的input输入为列表,列表中元素可以为PIL.Image, np.arrayimage_path都可以
luciaganlulu commented 2 hours ago

那请问这种方式的话,怎么保存每个输入图片对应的annotated_frame到指定的路径以及从det_res中获取每个输入图片对应的检测结果参数值呢(例如bbox, label, score)?predict这个函数返回的list没太看明白

luciaganlulu commented 1 hour ago

那请问这种方式的话,怎么保存每个输入图片对应的annotated_frame到指定的路径以及从det_res中获取每个输入图片对应的检测结果参数值呢(例如bbox, label, score)?predict这个函数返回的list没太看明白

您好我把det_res打印出来看到了,路径问题知道啦,但是请问如何获取图中每个检测对象的bbox和score呢? 另外det_res里面的save_dir=‘runs/detect/predict’是什么意思呢?没找到这个路径在哪里诶

JulioZhao97 commented 1 hour ago

您好,predict返回的list长度和输入的list相同,其中每一个元素包含了预测的结果,可以参考以下保存每一个图片的结果

image_list = [
        "assets/example/academic.jpg",
        "assets/example/exam_paper.jpg",
        "assets/example/financial.jpg",
        "assets/example/fuzzy_scan.jpg",
    ]

    det_res = model.predict(
        image_list,
        imgsz=args.imgsz,
        conf=args.conf,
        device=device,
    )
    for image_idx, image_path in enumerate(image_list):
        print(det_res[image_idx].boxes.xyxy)
        print(det_res[image_idx].boxes.cls)
        print(det_res[image_idx].boxes.conf)
        annotated_frame = det_res[image_idx].plot(pil=True, line_width=args.line_width, font_size=args.font_size)
        if not os.path.exists(args.res_path):
            os.makedirs(args.res_path)
        output_path = os.path.join(args.res_path, image_path.split("/")[-1].replace(".jpg", "_res.jpg"))
        cv2.imwrite(output_path, annotated_frame)
        print(f"Result saved to {output_path}")
JulioZhao97 commented 1 hour ago

那请问这种方式的话,怎么保存每个输入图片对应的annotated_frame到指定的路径以及从det_res中获取每个输入图片对应的检测结果参数值呢(例如bbox, label, score)?predict这个函数返回的list没太看明白

您好我把det_res打印出来看到了,路径问题知道啦,但是请问如何获取图中每个检测对象的bbox和score呢? 另外det_res里面的save_dir=‘runs/detect/predict’是什么意思呢?没找到这个路径在哪里诶

  1. 结果参数值在det_res[idx].bbox中,分类结果为det_res[idx].bbox.cls,置信度为det_res[idx].bbox.conf,检测框为det_res[idx].bbox.xyxy(或者不同格式)
  2. save_dir只有传入save=True才会保存到runs/detect/predict这个默认路径,可以参考https://docs.ultralytics.com/modes/predict/#inference-arguments
luciaganlulu commented 1 hour ago

好的谢谢,非常感谢,代码这里写得很清楚~ https://github.com/opendatalab/DocLayout-YOLO/blob/main/doclayout_yolo/engine/results.py#L444

luciaganlulu commented 1 hour ago

那请问这种方式的话,怎么保存每个输入图片对应的annotated_frame到指定的路径以及从det_res中获取每个输入图片对应的检测结果参数值呢(例如bbox, label, score)?predict这个函数返回的list没太看明白

您好我把det_res打印出来看到了,路径问题知道啦,但是请问如何获取图中每个检测对象的bbox和score呢? 另外det_res里面的save_dir=‘runs/detect/predict’是什么意思呢?没找到这个路径在哪里诶

  1. 结果参数值在det_res[idx].bbox中,分类结果为det_res[idx].bbox.cls,置信度为det_res[idx].bbox.conf,检测框为det_res[idx].bbox.xyxy(或者不同格式)
  2. save_dir只有传入save=True才会保存到runs/detect/predict这个默认路径,可以参考https://docs.ultralytics.com/modes/predict/#inference-arguments

好的非常感谢~

luciaganlulu commented 1 hour ago

您好,predict返回的list长度和输入的list相同,其中每一个元素包含了预测的结果,可以参考以下保存每一个图片的结果

image_list = [
        "assets/example/academic.jpg",
        "assets/example/exam_paper.jpg",
        "assets/example/financial.jpg",
        "assets/example/fuzzy_scan.jpg",
    ]

    det_res = model.predict(
        image_list,
        imgsz=args.imgsz,
        conf=args.conf,
        device=device,
    )
    for image_idx, image_path in enumerate(image_list):
        print(det_res[image_idx].boxes.xyxy)
        print(det_res[image_idx].boxes.cls)
        print(det_res[image_idx].boxes.conf)
        annotated_frame = det_res[image_idx].plot(pil=True, line_width=args.line_width, font_size=args.font_size)
        if not os.path.exists(args.res_path):
            os.makedirs(args.res_path)
        output_path = os.path.join(args.res_path, image_path.split("/")[-1].replace(".jpg", "_res.jpg"))
        cv2.imwrite(output_path, annotated_frame)
        print(f"Result saved to {output_path}")

请问下,这种方式与os.walk逐个image推理的方式相比的话,区别就在于这种list的方式可以结合batch_size数进行批处理对吗?后者只能batch_size为1。

JulioZhao97 commented 1 hour ago

您好,predict返回的list长度和输入的list相同,其中每一个元素包含了预测的结果,可以参考以下保存每一个图片的结果

image_list = [
        "assets/example/academic.jpg",
        "assets/example/exam_paper.jpg",
        "assets/example/financial.jpg",
        "assets/example/fuzzy_scan.jpg",
    ]

    det_res = model.predict(
        image_list,
        imgsz=args.imgsz,
        conf=args.conf,
        device=device,
    )
    for image_idx, image_path in enumerate(image_list):
        print(det_res[image_idx].boxes.xyxy)
        print(det_res[image_idx].boxes.cls)
        print(det_res[image_idx].boxes.conf)
        annotated_frame = det_res[image_idx].plot(pil=True, line_width=args.line_width, font_size=args.font_size)
        if not os.path.exists(args.res_path):
            os.makedirs(args.res_path)
        output_path = os.path.join(args.res_path, image_path.split("/")[-1].replace(".jpg", "_res.jpg"))
        cv2.imwrite(output_path, annotated_frame)
        print(f"Result saved to {output_path}")

请问下,这种方式与os.walk逐个image推理的方式相比的话,区别就在于这种list的方式可以结合batch_size数进行批处理对吗?后者只能batch_size为1。

根据YOLO的逻辑来说应该是的,您可以测试一下

luciaganlulu commented 13 minutes ago
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
det_res = model.predict(
    image_paths,
    imgsz=1024,
    conf=0.2,
    device="cuda:1"
)

您好,我用的image_list的方式,batchsize设置的8,上面这个代码的话,我想用GPU #1(显存多点),但请问为什么还是用GPU #0呢?

torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 4.55 GiB. GPU 0 has a total capacity of 79.15 GiB of which 3.27 GiB is free.