open-mmlab / mmdetection

OpenMMLab Detection Toolbox and Benchmark
https://mmdetection.readthedocs.io
Apache License 2.0
29.41k stars 9.43k forks source link

Training a model with hyperspectral data #5139

Closed Jolan-Dubrulle closed 3 years ago

Jolan-Dubrulle commented 3 years ago

I am trying to train the Retinanet model on hyperspectral data (with 30 channels) in tif format. To do this, I first set the number of backbone input channels to 30 in the config file by the following line:

model = dict(pretrained=None, bbox_head=dict(num_classes=1), backbone=dict(depth=18, in_channels=30, norm_eval=False))

However, the 'LoadImageFromFile' class (mmdetection/mmdet/datasets/pipelines/loading.py) does not work on my hyperspectral tif images. The reason is that OpenCV cannot read my 30 channel tif files.

This is the error message I get:

imdecode_('imdecode_(''): can't read header: '): can't read header: OpenCV(4.5.1) /tmp/pip-req-build-1syr35c1/opencv/modules/imgcodecs/src/grfmt_tiff.cpp:148: error: (-215:Assertion failed) channels <= 4 in function 'normalizeChannelsNumber'
OpenCV(4.5.1) /tmp/pip-req-build-1syr35c1/opencv/modules/imgcodecs/src/grfmt_tiff.cpp:148: error: (-215:Assertion failed) channels <= 4 in function 'normalizeChannelsNumber'

Traceback (most recent call last):
  File "tools/train.py", line 187, in <module>
    main()
  File "tools/train.py", line 183, in main
    meta=meta)
  File "/home/jolan/mmdetection/mmdet/apis/train.py", line 170, in train_detector
    runner.run(data_loaders, cfg.workflow)
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/runner/epoch_based_runner.py", line 125, in run
    epoch_runner(data_loaders[i], **kwargs)
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/runner/epoch_based_runner.py", line 47, in train
    for i, data_batch in enumerate(self.data_loader):
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 435, in __next__
    data = self._next_data()
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 1085, in _next_data
    return self._process_data(data)
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 1111, in _process_data
    data.reraise()
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/torch/_utils.py", line 428, in reraise
    raise self.exc_type(msg)
AttributeError: Caught AttributeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/jolan/env_mmdetection/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/jolan/env_mmdetection/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/jolan/mmdetection/mmdet/datasets/custom.py", line 193, in __getitem__
    data = self.prepare_train_img(idx)
  File "/home/jolan/mmdetection/mmdet/datasets/custom.py", line 216, in prepare_train_img
    return self.pipeline(results)
  File "/home/jolan/mmdetection/mmdet/datasets/pipelines/compose.py", line 40, in __call__
    data = t(data)
  File "/home/jolan/mmdetection/mmdet/datasets/pipelines/loading.py", line 68, in __call__
    results['img_shape'] = img.shape
AttributeError: 'NoneType' object has no attribute 'shape'

So I decided to create a new class in the mmdetection/mmdet/datasets/pipelines/loading.py file called 'LoadHypImageFromFile' in order to be able to read my hyperspectral tif images. This class is identical to the 'LoadImageFromFile' class except that img = mmcv.imread(filename, backend='tifffile') has been used instead of:

img_bytes = self.file_client.get(filename) img = mmcv.imfrombytes(img_bytes, flag=self.color_type)

Unfortunately, it doesn't work and I get this error message:

Traceback (most recent call last):
  File "tools/train.py", line 187, in <module>
    main()
  File "tools/train.py", line 183, in main
    meta=meta)
  File "/home/jolan/mmdetection/mmdet/apis/train.py", line 170, in train_detector
    runner.run(data_loaders, cfg.workflow)
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/runner/epoch_based_runner.py", line 125, in run
    epoch_runner(data_loaders[i], **kwargs)
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/runner/epoch_based_runner.py", line 47, in train
    for i, data_batch in enumerate(self.data_loader):
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 435, in __next__
    data = self._next_data()
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 1085, in _next_data
    return self._process_data(data)
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 1111, in _process_data
    data.reraise()
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/torch/_utils.py", line 428, in reraise
    raise self.exc_type(msg)
RecursionError: Caught RecursionError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/parallel/collate.py", line 81, in collate
    for key in batch[0]
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/parallel/collate.py", line 81, in <dictcomp>
    for key in batch[0]
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/parallel/collate.py", line 81, in collate
    for key in batch[0]
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/parallel/collate.py", line 81, in <dictcomp>
    for key in batch[0]
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/parallel/collate.py", line 77, in collate
    return [collate(samples, samples_per_gpu) for samples in transposed]
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/parallel/collate.py", line 77, in <listcomp>
    return [collate(samples, samples_per_gpu) for samples in transposed]
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/parallel/collate.py", line 77, in collate
    return [collate(samples, samples_per_gpu) for samples in transposed]
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/parallel/collate.py", line 77, in <listcomp>
.
.
.
    return [collate(samples, samples_per_gpu) for samples in transposed]
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/parallel/collate.py", line 77, in <listcomp>
    return [collate(samples, samples_per_gpu) for samples in transposed]
  File "/home/jolan/env_mmdetection/lib/python3.6/site-packages/mmcv/parallel/collate.py", line 23, in collate
    if not isinstance(batch, Sequence):
  File "/usr/lib/python3.6/abc.py", line 184, in __instancecheck__
    if subclass in cls._abc_cache:
  File "/usr/lib/python3.6/_weakrefset.py", line 75, in __contains__
    return wr in self.data
RecursionError: maximum recursion depth exceeded in comparison

Thanks for your help !

hhaAndroid commented 3 years ago

You may need to check subsequent pipelines, such as Resize,Normalize, etc..

wwcc1107 commented 2 years ago

hi have you solved this problem,i currently wanted to read .tif images(RGB,three channels) in mmdetection

Yanhui-Struggle commented 1 year ago

So hard, I meet this problem too, have you solved this problem?