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

AssertionError for img_norm_cfg #4

Open
kvnptl opened this issue Oct 30, 2023 · 3 comments
Open

AssertionError for img_norm_cfg #4

kvnptl opened this issue Oct 30, 2023 · 3 comments

Comments

@kvnptl
Copy link

kvnptl commented Oct 30, 2023

Hello,

Getting an error while running an inference python tools/test.py configs/mt_detr/mt_detr_c+l+r.py checkpoint/model/mt_detr_c+l+r.pth.

Traceback (most recent call last):
  File "tools/test.py", line 251, in <module>
    main()
  File "tools/test.py", line 216, in main
    outputs = single_gpu_test(model, data_loader, args.show, args.show_dir,
  File "/home/user/user/mt_detr/mmdet/apis/test.py", line 36, in single_gpu_test
    imgs = tensor2imgs(img_tensor, **img_metas[0]['img_norm_cfg'])
  File "/home/user/anaconda3/envs/mt_detr/lib/python3.8/site-packages/mmcv/image/misc.py", line 32, in tensor2imgs
    assert len(mean) == 3
AssertionError

It seems like it is defined for 3 modalities, so it has 3 values. For Camera Only, it works fine.

img_norm_cfg = dict(
    mean=[123.675, 116.28, 103.53, 123.675, 116.28, 103.53,123.675, 116.28, 103.53]
    ,std=[58.395, 57.12, 57.375, 58.395, 57.12, 57.375, 58.395, 57.12, 57.375], to_rgb=False)

I tried with the same values as Camera Only but encountered the below error. So later in the code, it expects in the above order only. So, there is no meaning to changing it.

Traceback (most recent call last):
  File "tools/test.py", line 251, in <module>
    main()
  File "tools/test.py", line 216, in main
    outputs = single_gpu_test(model, data_loader, args.show, args.show_dir,
  File "/home/user/user/mt_detr/mmdet/apis/test.py", line 25, in single_gpu_test
    for i, data in enumerate(data_loader):
  File "/home/user/anaconda3/envs/mt_detr/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 521, in __next__
    data = self._next_data()
  File "/home/user/anaconda3/envs/mt_detr/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1203, in _next_data
    return self._process_data(data)
  File "/home/user/anaconda3/envs/mt_detr/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1229, in _process_data
    data.reraise()
  File "/home/user/anaconda3/envs/mt_detr/lib/python3.8/site-packages/torch/_utils.py", line 434, in reraise
    raise exception
cv2.error: Caught error in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/user/anaconda3/envs/mt_detr/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/user/anaconda3/envs/mt_detr/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 "/home/user/anaconda3/envs/mt_detr/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 "/home/user/user/mt_detr/mmdet/datasets/custom.py", line 192, in __getitem__
    return self.prepare_test_img(idx)
  File "/home/user/user/mt_detr/mmdet/datasets/custom.py", line 235, in prepare_test_img
    return self.pipeline(results)
  File "/home/user/user/mt_detr/mmdet/datasets/pipelines/compose.py", line 40, in __call__
    data = t(data)
  File "/home/user/user/mt_detr/mmdet/datasets/pipelines/test_time_aug.py", line 106, in __call__
    data = self.transforms(_results)
  File "/home/user/user/mt_detr/mmdet/datasets/pipelines/compose.py", line 40, in __call__
    data = t(data)
  File "/home/user/user/mt_detr/mmdet/datasets/pipelines/transforms.py", line 665, in __call__
    results[key] = mmcv.imnormalize(results[key], self.mean, self.std,
  File "/home/user/anaconda3/envs/mt_detr/lib/python3.8/site-packages/mmcv/image/photometric.py", line 22, in imnormalize
    return imnormalize_(img, mean, std, to_rgb)
  File "/home/user/anaconda3/envs/mt_detr/lib/python3.8/site-packages/mmcv/image/photometric.py", line 42, in imnormalize_
    cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)  # inplace
cv2.error: OpenCV(4.8.1) /io/opencv/modules/imgproc/src/color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function 'cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3, 4>; VDcn = cv::impl::{anonymous}::Set<3, 4>; VDepth = cv::impl::{anonymous}::Set<0, 2, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = cv::impl::<unnamed>::NONE; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Invalid number of channels in input image:
>     'VScn::contains(scn)'
> where
>     'scn' is 9

Any help would be appreciated.

Thanks,
K

@shedoflevi
Copy link

Hello, I met the same problem this afternoon. I used the config mt_detr_c+l+r+t.py and the length was 12, so I think it was a product of 3 feature channels of four pictures from 4 modalities and your intuition was right. I don't know how to solve it, too. I'm trying to solve this problem by compressing the number of extra channels back to 3, but I'm not sure whether it will get good effect. I'll appreciate if you could share your experience after you get the solution. Thanks!

@kvnptl
Copy link
Author

kvnptl commented Oct 31, 2023

Hi, I kinda avoided this error and got my inference running.

As this error is about AssertionError, and it can only handle either number of channels 1 or 3. So in the /home/user/anaconda3/envs/mt_detr/lib/python3.8/site-packages/mmcv/image/misc.py file, I modified the assertion condition. Now it looks like this

channels = tensor.size(1)
assert (channels == len(mean) == len(std))
# assert len(mean) == 3 #commented
# assert len(std) == 3  #commented

It is just a temporary workaround for me, not a solution.

@shedoflevi
Copy link

Hi, I tried your method and it worked! Thank you for your help and wishing you all the best in your future work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants