dog-qiuqiu / FastestDet

:zap: A newly designed ultra lightweight anchor free target detection algorithm, weight only 250K parameters, reduces the time consumption by 10% compared with yolo-fastest, and the post-processing is simpler
BSD 3-Clause "New" or "Revised" License
776 stars 133 forks source link

Right way to convert coco dataset #31

Closed dzk9528 closed 1 year ago

dzk9528 commented 1 year ago
import json
import cv2
import os
import matplotlib.pyplot as plt
import shutil
from tqdm import tqdm

def load_images_from_folder(input_path):
    file_names = []
    for filename in tqdm(os.listdir(input_path)):
        file_names.append(filename)
    return file_names

def get_img_ann(image_id):
    img_ann = []
    isFound = False
    for ann in data['annotations']:
        if ann['image_id'] == image_id:
            img_ann.append(ann)
            isFound = True
    if isFound:
        return img_ann
    else:
        return None

def get_img(filename):
    for img in data['images']:
        if img['file_name'] == filename:
            return img

if __name__ == "__main__":
    input_path = "/home/dzhang/data/coco/val2017/"
    output_path = "/home/dzhang/data/coco_fastest_net/val2017"
    annotation_path = "/home/dzhang/data/coco/annotations/instances_val2017.json"
    output_txt_path = "/home/dzhang/data/coco_fastest_net/val2017.txt"
    os.system(f"rm -rf {output_path}")
    os.makedirs(output_path, exist_ok=True)

    with open(annotation_path, 'r') as f:
        data = json.load(f)

    file_names = load_images_from_folder(input_path)
    labeled_file_names = []

    count = 0

    print("Processing labels...")
    for filename in tqdm(file_names):
    # Extracting image 
        img = get_img(filename)
        img_id = img['id']
        img_w = img['width']
        img_h = img['height']

        # Get Annotations for this image
        img_ann = get_img_ann(img_id)

        if img_ann:
            # Opening file for current image
            file_object = open(f"{output_path}/img{count}.txt", "a")

            for ann in img_ann:
                current_category = ann['category_id'] - 1 # As yolo format labels start from 0 
                if current_category > 79:
                    print("Current category larger than 79")
                    continue
                current_bbox = ann['bbox']
                x = current_bbox[0]
                y = current_bbox[1]
                w = current_bbox[2]
                h = current_bbox[3]

                # Finding midpoints
                x_centre = (x + (x+w))/2
                y_centre = (y + (y+h))/2

                # Normalization
                x_centre = x_centre / img_w
                y_centre = y_centre / img_h
                w = w / img_w
                h = h / img_h

                # Limiting upto fix number of decimal places
                x_centre = format(x_centre, '.6f')
                y_centre = format(y_centre, '.6f')
                w = format(w, '.6f')
                h = format(h, '.6f')

                # Writing current object 
                file_object.write(f"{current_category} {x_centre} {y_centre} {w} {h}\n")

            file_object.close()

            # copy image here if successful
            source = os.path.join(input_path, filename)
            destination = f"{output_path}/img{count}.jpg"
            labeled_file_names.append(destination)
            shutil.copy(source, destination)
            count += 1  # This should be outside the if img_ann block.
        else:
            print(f"Image {filename}'s label not found.")
    with open(output_txt_path, 'w') as f:
        for filename in labeled_file_names:
            f.write(f"{filename}\n")
    print("Labels processed successfully.")

I use the above code to transfer coco dataset to standard format, but the evaluation result seems not to be right: Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.028 Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.054 Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.027 Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.006 Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.025 Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.051 Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.031 Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.043 Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.045 Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.013 Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.047 Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.079

Do you know why?

dzk9528 commented 1 year ago

Just figure out that the category id need to be mapped, now the map0.5 is 0.252 now. I will close this issue.