Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use it in yolov5? #37

Open
WANGCHAO1996 opened this issue Sep 8, 2021 · 8 comments
Open

how to use it in yolov5? #37

WANGCHAO1996 opened this issue Sep 8, 2021 · 8 comments

Comments

@WANGCHAO1996
Copy link

WANGCHAO1996 commented Sep 8, 2021

Is the result after NMS in YoloV5 passed to WBF for secondary screening?@ZFTurbo

@NOBUGW
Copy link

NOBUGW commented Nov 9, 2021

你好,请问你解决了吗?

@ZFTurbo
Copy link
Owner

ZFTurbo commented Nov 9, 2021

I used it in Yolov5. What you need to do is to read all txt files with predictions created by Yolo and gather it in single CSV file or just numpy table. Then you can use WBF.

Here is my code:

# coding: utf-8
__author__ = 'ZFTurbo: https://kaggle.com/zfturbo'

import glob
import os

def convert_yolov5_preds(valid_files_dir, labels_dir, out_file):
    valid_files = glob.glob(valid_files_dir + '*.jpg')
    print('Total image files: {}'.format(len(valid_files)))
    files = glob.glob(labels_dir + '*.txt')
    print('Total labels files: {}'.format(len(files)))
    valid_ids = [os.path.basename(f)[:-4] for f in valid_files]
    out = open(out_file, 'w')
    out.write('image_id,label,conf,x1,x2,y1,y2\n')
    fixes = 0
    for f in files:
        image_id = os.path.basename(f)[:-4]
        in1 = open(f, 'r')
        lines = in1.readlines()
        in1.close()
        valid_ids.remove(image_id)
        for line in lines:
            arr = line.strip().split(' ')
            class_id = arr[0]
            x = float(arr[1])
            y = float(arr[2])
            w = float(arr[3])
            h = float(arr[4])
            x1 = x - (w / 2)
            x2 = x + (w / 2)
            y1 = y - (h / 2)
            y2 = y + (h / 2)
            if x1 < 0:
                fixes += 1
                x1 = 0
            if x2 > 1:
                fixes += 1
                x2 = 1
            if y1 < 0:
                fixes += 1
                y1 = 0
            if y2 > 1:
                fixes += 1
                y2 = 1
            conf = arr[5]
            pred_str = '{},{},{},{:.6f},{:.6f},{:.6f},{:.6f}\n'.format(image_id, str(class_id), conf, x1, x2, y1, y2)
            out.write(pred_str)

    print(len(valid_ids))

    # Output empty IDs
    for image_id in list(valid_ids):
        out.write('{},,,,,,\n'.format(image_id))

    out.close()
    print('Fixes: {}'.format(fixes))
    print('Result was written in: {}'.format(out_file))


if __name__ == '__main__':
    # Location of images
    valid_files_dir = './valid_imgs/'
    # Location of yolo v5 predictions
    labels_dir = 'yolov5x/valid_iou_0.45_02.1/labels/'
    # CSF-file to store results
    out_file = 'yolov5x_full_valid_iou_0.45_0.1.csv'
    convert_yolov5_preds(valid_files_dir, labels_dir, out_file)

@haoxue1215
Copy link

I used it in Yolov5. What you need to do is to read all txt files with predictions created by Yolo and gather it in single CSV file or just numpy table. Then you can use WBF.

Here is my code:

# coding: utf-8
__author__ = 'ZFTurbo: https://kaggle.com/zfturbo'

import glob
import os

def convert_yolov5_preds(valid_files_dir, labels_dir, out_file):
    valid_files = glob.glob(valid_files_dir + '*.jpg')
    print('Total image files: {}'.format(len(valid_files)))
    files = glob.glob(labels_dir + '*.txt')
    print('Total labels files: {}'.format(len(files)))
    valid_ids = [os.path.basename(f)[:-4] for f in valid_files]
    out = open(out_file, 'w')
    out.write('image_id,label,conf,x1,x2,y1,y2\n')
    fixes = 0
    for f in files:
        image_id = os.path.basename(f)[:-4]
        in1 = open(f, 'r')
        lines = in1.readlines()
        in1.close()
        valid_ids.remove(image_id)
        for line in lines:
            arr = line.strip().split(' ')
            class_id = arr[0]
            x = float(arr[1])
            y = float(arr[2])
            w = float(arr[3])
            h = float(arr[4])
            x1 = x - (w / 2)
            x2 = x + (w / 2)
            y1 = y - (h / 2)
            y2 = y + (h / 2)
            if x1 < 0:
                fixes += 1
                x1 = 0
            if x2 > 1:
                fixes += 1
                x2 = 1
            if y1 < 0:
                fixes += 1
                y1 = 0
            if y2 > 1:
                fixes += 1
                y2 = 1
            conf = arr[5]
            pred_str = '{},{},{},{:.6f},{:.6f},{:.6f},{:.6f}\n'.format(image_id, str(class_id), conf, x1, x2, y1, y2)
            out.write(pred_str)

    print(len(valid_ids))

    # Output empty IDs
    for image_id in list(valid_ids):
        out.write('{},,,,,,\n'.format(image_id))

    out.close()
    print('Fixes: {}'.format(fixes))
    print('Result was written in: {}'.format(out_file))


if __name__ == '__main__':
    # Location of images
    valid_files_dir = './valid_imgs/'
    # Location of yolo v5 predictions
    labels_dir = 'yolov5x/valid_iou_0.45_02.1/labels/'
    # CSF-file to store results
    out_file = 'yolov5x_full_valid_iou_0.45_0.1.csv'
    convert_yolov5_preds(valid_files_dir, labels_dir, out_file)

then?

@czczmr
Copy link

czczmr commented Feb 22, 2022

你好,请问你解决了吗?

之后要怎么做呢,从保存好的out_file 中读取数据使用你们的wbf;
请问有从两个csv文件里读取框坐标后使用wbf的demo吗

@czczmr
Copy link

czczmr commented Feb 22, 2022

之后要怎么做呢,从保存好的out_file 中读取数据使用你们的wbf;
请问有从两个csv文件里读取框坐标后使用wbf的demo吗

@zyfn
Copy link

zyfn commented Mar 19, 2022

Please, I have used WBF in Yolov5.Dataset is coco128.Device is GPU-1050ti .But the post processing time of every image is closely 1.5s. The time is normally?

@dapiaoGe
Copy link

Hello friends,csv file i have made,then how to use wbf ?thanks for your reply

@dapiaoGe
Copy link

之后要怎么做呢,从保存好的out_file 中读取数据使用你们的wbf; 请问有从两个csv文件里读取框坐标后使用wbf的demo吗

哥们你现在解决了吗?可以请教一下?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants