Skip to content

Validation fails when image names are not int convertible. #67

@Abdul-Mukit

Description

@Abdul-Mukit

First of all, thank you very much for this much-needed initiative.

Describe the bug

It is assumed in this repo that the image file names will always be int convertible. That is not true. If image names in the validation split are not convertible to int, validation fails. e.g.
instead of tests/data/images/val/000000151480.jpg if the name was tests/data/images/val/000000151480_.jpg and instances_val.json was updated accordingly, validation will fail.

This is the initial error message:

Error executing job with overrides: ['task=train', 'model=v9-s', 'dataset=mock', 'task.epoch=1']
Traceback (most recent call last):
  File "/home/abdul/projects/YOLO/yolo/lazy.py", line 39, in main
    solver.solve(dataloader)
  File "/home/abdul/projects/YOLO/yolo/tools/solver.py", line 148, in solve
    mAPs = self.validator.solve(self.validation_dataloader, epoch_idx=epoch_idx)
  File "/home/abdul/projects/YOLO/yolo/tools/solver.py", line 252, in solve
    predict_json.extend(predicts_to_json(img_paths, predicts, rev_tensor))
  File "/home/abdul/projects/YOLO/yolo/utils/model_utils.py", line 154, in predicts_to_json
    "image_id": int(Path(img_path).stem),
ValueError: invalid literal for int() with base 10: '000000151480_'

I am open to making PRs. Will appreciate input on how to solve this.

To Reproduce

Steps to reproduce the behavior:

  1. Rename tests/data/images/val/000000151480.jpg to tests/data/images/val/000000151480_.jpg.
  2. Edit tests/data/annotations/instances_val.json to change "file_name": "000000151480.jpg", to "file_name": "000000151480_.jpg".
  3. Try training using python yolo/lazy.py task=train model=v9-s dataset=mock task.epoch=1.
  4. See error

Expected behavior

In COCO datasets' */annotations/*.json files, the images are supposed to have int ids and string file_names. A string can have any character and the logic of this codebase should accommodate for it. Instead of using filename as a id it should use the actual id of the image as mentioned in the .json file.

Screenshots

The problem is the it was assumed in "image_id": int(Path(img_path).stem), that the stem will always be convertible to int.

Even if the int is removed to make it "image_id": Path(img_path).stem. The solution is fundamentally wrong as image_id is not filename. In the instances_val.json that particular image already had an int id assigned to it. Both id (int) and filename (string) can be completely random and may not have any similarity
image

Instead of taking the correct id, the file name was used as the id. This was done as the ModelValidator.solve()'s dataloader doesn't return the actual ids but only the img_paths.
image

Thus we run into another assert error:

Error executing job with overrides: ['task=train', 'model=v9-s', 'dataset=mock', 'task.epoch=1']
Traceback (most recent call last):
  File "/home/abdul/projects/YOLO/yolo/lazy.py", line 39, in main
    solver.solve(dataloader)
  File "/home/abdul/projects/YOLO/yolo/tools/solver.py", line 148, in solve
    mAPs = self.validator.solve(self.validation_dataloader, epoch_idx=epoch_idx)
  File "/home/abdul/projects/YOLO/yolo/tools/solver.py", line 260, in solve
    result = calculate_ap(self.coco_gt, predict_json)
  File "/home/abdul/projects/YOLO/yolo/utils/solver_utils.py", line 12, in calculate_ap
    coco_dt = coco_gt.loadRes(pd_path)
  File "/home/abdul/projects/YOLO/.venv/lib/python3.10/site-packages/pycocotools/coco.py", line 327, in loadRes
    assert set(annsImgIds) == (set(annsImgIds) & set(self.getImgIds())), \
AssertionError: Results do not correspond to current coco set

System Info (please complete the following ## information):

  • OS: [e.g. Ubuntu 22.04]
  • Python Version: [3.10]
  • PyTorch Version: [ 2.4.0]
  • CUDA/cuDNN/MPS Version: []
  • YOLO Model Version: [e.g. YOLOv9-s]

Additional context

My dataset was produced directly from CVAT with int image_id and random string for filenames. I have used these datasets before with other DNN libraries, without having this issue before. I am confident that I am not breaking any of COCO's dataset conventions. This "filename is int and same as id", assumption is very limiting. In many industry applications, images are time stamped like <time>_<date>_<location>.jpg. Moreover, while editing datasets using libraries like datumaro images ids mentioned in .json can certainly change anytime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions