facebookresearch / detr

End-to-End Object Detection with Transformers
Apache License 2.0
13.42k stars 2.42k forks source link

Do not know how to train a custom object detector #536

Closed JNaranjo-Alcazar closed 2 years ago

JNaranjo-Alcazar commented 2 years ago

I am trying to train a 4-class object detection. I am not sure how to change the code in order to accomplish this. If I copy:

    #model = torch.hub.load('facebookresearch/detr', 'detr_resnet50', pretrained=False, num_classes=4)
    #checkpoint = torch.hub.load_state_dict_from_url(
    #        url='https://dl.fbaipublicfiles.com/detr/detr-r50-e632da11.pth',
    #        map_location='cpu',
    #        check_hash=True)
    #del checkpoint["model"]["class_embed.weight"]
    #del checkpoint["model"]["class_embed.bias"]
    #model.load_state_dict(checkpoint["model"], strict=False)

The criterion is not created. I do know how to create the criterion and the model in order to train a 4-class object detector. I have created a dataset in COCO format as expected by this repo.

Instructions To Reproduce the Issue:

  1. what changes you made (git diff) or what code you wrote

In model/detr.py

    # For more details on this, check the following discussion
    # https://github.com/facebookresearch/detr/issues/108#issuecomment-650269223
    #num_classes = 20 if args.dataset_file != 'coco' else 91
    #if args.dataset_file == "coco_panoptic":
        # for panoptic, we just add a num_classes that is large enough to hold
        # max_obj_id + 1, but the exact value doesn't really matter
    #    num_classes = 250
    num_classes = 5
  1. what exact command you run:

python main.py --epochs 2 --coco_path /data/dataset_COCO_format/

  1. what you observed (including full logs):
    Start training
    Traceback (most recent call last):
    File "main.py", line 262, in <module>
    main(args)
    File "main.py", line 210, in main
    train_stats = train_one_epoch(
    File "/detr_app/detr/engine.py", line 28, in train_one_epoch
    for samples, targets in metric_logger.log_every(data_loader, print_freq, header):
    File "/detr_app/detr/util/misc.py", line 223, in log_every
    for obj in iterable:
    File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 578, in __next__
    data = self._next_data()
    File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1273, in _next_data
    return self._process_data(data)
    File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1299, in _process_data
    data.reraise()
    File "/opt/conda/lib/python3.8/site-packages/torch/_utils.py", line 461, in reraise
    raise exception
    IndexError: Caught IndexError in DataLoader worker process 0.
    Original Traceback (most recent call last):
    File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py", line 300, in _worker_loop
    data = fetcher.fetch(index)
    File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 49, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
    File "/opt/conda/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 49, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
    File "/detr_app/detr/datasets/coco.py", line 27, in __getitem__
    img, target = self.prepare(img, target)
    File "/detr_app/detr/datasets/coco.py", line 88, in __call__
    classes = classes[keep]
    IndexError: The shape of the mask [0] at index 0 does not match the shape of the indexed tensor [1] at index 0

Environment:

Docker image from repository

ShriramGithub7 commented 2 years ago

Did you get to know how to do it?

JNaranjo-Alcazar commented 2 years ago

Yes, it was labeling problem... all categories were set to -1

keithajy commented 1 year ago

hello @JNaranjo-Alcazar , is it possible for me to take a look at your codes? i am trying to train my own custom object detector using detr too and im currently stuck.