Giter VIP home page Giter VIP logo

Comments (8)

ZFTurbo avatar ZFTurbo commented on July 23, 2024 1

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)

from weighted-boxes-fusion.

NOBUGW avatar NOBUGW commented on July 23, 2024

你好,请问你解决了吗?

from weighted-boxes-fusion.

haoxue1215 avatar haoxue1215 commented on July 23, 2024

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?

from weighted-boxes-fusion.

czczmr avatar czczmr commented on July 23, 2024

你好,请问你解决了吗?

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

from weighted-boxes-fusion.

czczmr avatar czczmr commented on July 23, 2024

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

from weighted-boxes-fusion.

zyfn avatar zyfn commented on July 23, 2024

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?

from weighted-boxes-fusion.

dapiaoGe avatar dapiaoGe commented on July 23, 2024

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

from weighted-boxes-fusion.

dapiaoGe avatar dapiaoGe commented on July 23, 2024

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

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

from weighted-boxes-fusion.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.