<jupyter_start><jupyter_text>Labelmg to COCO formatLabelmg by default creates files in the **Pascal VOC** format. Most of the latest pipelines areexpecting the labels in COCO format.1. Pascal VOC format -> coordinates are represented as `(left_top, right_bottom)`2. Labelmg tool produces Pascal voc format.3. COCO expects all the file names should be in number format4. COCO files<jupyter_code>train_dir = "/home/haridas/projects/mystique/data/train_and_test-2020-Jun-05-coco/train/"
test_dir = "/home/haridas/projects/mystique/data/train_and_test-2020-Jun-05-coco/test/"
template_test = "/home/haridas/projects/mystique/data/train_and_test-2020-Jun-05-coco/templates_test_data_coco/"
tree = ET.parse(f"{template_test}/1.xml")
root = tree.getroot()
fn_child = root.find("filename")
fn_child.text = "1.jpg"
# print(ET.tostring(root).decode('utf8'))

def renamefn_to_intfn(data_dir, start=1000):
    """
    @param data_dir: Pascal VOC format generated by labelmg.
    @param start: File name start point.
    """
    get_fn = lambda x: ".".join(x.split(".")[:-1])

    pp = Path(data_dir)
    for fn in glob.glob(f"{data_dir}/*.xml"):
        p = Path(fn)
        root = ET.parse(fn).getroot()
        fn_child = root.find("filename")
        path_child = root.find("path")
        img_fn = fn_child.text
        
        if not get_fn(p.name).isdigit():
            bname = ".".join(p.name.split(".")[:-1])
            png = Path(pp / f"{img_fn}")
            assert png.exists()
            
            imgfn_split = img_fn.split(".")
            img, img_ext = ".".join(imgfn_split[:-1]), imgfn_split[-1]
            
            p.rename(pp / f"{start}.xml")
            png.rename(pp / f"{start}.{img_ext}")
            
            # Update the filename reference in new xml 
            fn_child.text = f"{start}.{img_ext}"
            path_child.text = f"{pp/str(start)}.{img_ext}"
            
            with open(pp/f"{start}.xml", 'w') as f:
                f.write(ET.tostring(root).decode("utf8"))
            
            start += 1
renamefn_to_intfn(template_test)<jupyter_output><empty_output><jupyter_text>Coco Category CheckEnsure the Dataset has correct labels and category ID mapping across train/val/test datasets.<jupyter_code>train_ann_file = "/home/haridas/projects/mystique/data/train_and_test-2020-Jun-05-coco/train_coco_updated.json"
val_ann_file = "/home/haridas/projects/mystique/data/train_and_test-2020-Jun-05-coco/test_coco_updated.json"
test_ann_file = "/home/haridas/projects/mystique/data/train_and_test-2020-Jun-05-coco/templates_test_data_coco_updated.json"

def check_category_id(ann_file):
    ann = json.loads(open(ann_file).read())
    cat_map = {i["id"] : i["name"] for i in ann["categories"]}
    print({(k, cat_map[k]): v for k, v in Counter([i["category_id"] for i in ann["annotations"]]).items()})
    return ann["categories"]

check_category_id(train_ann_file)
check_category_id(val_ann_file)
check_category_id(test_ann_file)
ann.keys()
Counter([i["category_id"] for i in ann["annotations"]])<jupyter_output><empty_output><jupyter_text>Label statistics<jupyter_code>train_df = pd.read_csv("/home/haridas/projects/mystique/data/train_and_test-2020-Jun-05-coco/train_label.csv")
train_df.groupby("filename").count().describe()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup