Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SizeMismatchError When trying to train a model using a custom dataset #194

Closed
vylly opened this issue Oct 29, 2019 · 9 comments
Closed

SizeMismatchError When trying to train a model using a custom dataset #194

vylly opened this issue Oct 29, 2019 · 9 comments

Comments

@vylly
Copy link

vylly commented Oct 29, 2019

I'm trying to train a model using a custom dataset (I made sure to have a COCO style json file for my dataset) but when I'm trying to train a model using it I receive a SizeMismatchError.

Here is the code for what I'm doing :

cfg = get_cfg()

cfg.merge_from_file("../detectron2/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
cfg.DATASETS.TRAIN = ("flowchart",)
cfg.DATASETS.TEST = ()   
cfg.DATALOADER.NUM_WORKERS = 2
cfg.MODEL.WEIGHTS = "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl"  # initialize from model zoo
cfg.SOLVER.IMS_PER_BATCH = 1
cfg.SOLVER.BASE_LR = 0.00025
cfg.SOLVER.MAX_ITER = 300   
cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128  
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 7 # my dataset has 7 classes

os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
trainer = DefaultTrainer(cfg) 
trainer.resume_or_load(resume=False)
trainer.train()

And here is the log :

SizeMismatchError: Caught SizeMismatchError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/william/.conda/envs/detectron2-env/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/william/.conda/envs/detectron2-env/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/william/.conda/envs/detectron2-env/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/william/detectron2/detectron2/data/common.py", line 39, in __getitem__
    data = self._map_func(self._dataset[cur_idx])
  File "/home/william/detectron2/detectron2/utils/serialize.py", line 23, in __call__
    return self._obj(*args, **kwargs)
  File "/home/william/detectron2/detectron2/data/dataset_mapper.py", line 73, in __call__
    utils.check_image_size(dataset_dict, image)
  File "/home/william/detectron2/detectron2/data/detection_utils.py", line 73, in check_image_size
    "mismatch (W,H), got {}, expect {}".format(image_wh, expected_wh)
detectron2.data.detection_utils.SizeMismatchError: mismatch (W,H), got (2080, 1560), expect (4160, 3120)

The size mismatch error is always a factor of 2 but when printing the size of an image and its reference in the json they actually match :

d = DatasetCatalog.get('flowchart')
print(d[0]['width'])
print(d[0]['height'])

1303
2000
from PIL import Image
im = Image.open(d[0]["file_name"])
print(im.size)

(1303, 2000)

Anybody encountered this issue ?

@vylly
Copy link
Author

vylly commented Oct 29, 2019

That was my bad on this one, miscommunicating with the coworkers on how the dataset & json was build.

@vylly vylly closed this as completed Oct 29, 2019
@XiaoJiNu
Copy link

@vylly hi, I miss the same problem but what makes the problem? Whether the size of the image in json is not right?

@vylly
Copy link
Author

vylly commented Oct 29, 2019

@XiaoJiNu Yes exactly, in my case the issue was that we modified some of the image sizes without updating the json file so there was a difference between what was written in the json and the actual size of the images

@morestart
Copy link

morestart commented Feb 13, 2020

hi this is my error , do you know how to solve it?

detectron2.data.detection_utils.SizeMismatchError: Mismatched (W,H) for image /content/data/Images/137.jpg, got (3264, 2448), expect (2448, 3264)

@joeljosephjin
Copy link

I too am getting the same exact error. And on rerunning the code several times, it sometimes passes without showing this error.

hi this is my error , do you know how to solve it?

detectron2.data.detection_utils.SizeMismatchError: Mismatched (W,H) for image /content/data/Images/137.jpg, got (3264, 2448), expect (2448, 3264)

@morestart
Copy link

I too am getting the same exact error. And on rerunning the code several times, it sometimes passes without showing this error.

hi this is my error , do you know how to solve it?

detectron2.data.detection_utils.SizeMismatchError: Mismatched (W,H) for image /content/data/Images/137.jpg, got (3264, 2448), expect (2448, 3264)

i use rectLabel to generate the coco json file, it worked.I never received this error again

@Jeyakumar-johnny-jonathan

Mismatched (W,H) for image /content/Mask_RCNNtrain/IMG_0855.jpeg, got (4032, 3024), expect (3024, 4032)

@CarlBhy
Copy link

CarlBhy commented Mar 19, 2021

i have solved this question,You can take a look through this link!
#2278 (comment)

@Sendeman
Copy link

Mismatched (W,H) for image /content/Mask_RCNNtrain/IMG_0855.jpeg, got (4032, 3024), expect (3024, 4032)

Hi, in my case it was due to exif metadata which rotates the image. detectron2 uses Pillow to load in the data during shape checkin which does the auto rotation, cv2.imread() however doesnt. To fix this take a look at this answer! https://stackoverflow.com/questions/19786301/python-remove-exif-info-from-images

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants