facebookresearch / facebookresearch/detectron2
Error while using a custom dataset mapper for 7-channel images
- Dominant language
- Python
- Stars
- 34.7k
- Forks
- 7.9k
- PR merge metrics
- No merged PRs in 30d
Description
Hello
Im trying to train an instance segmentation model using 7-channel images.
As asked in [#2062](https://github.com/facebookresearch/detectron2/issues/2062)
Of course I followed the Data loading tutorial and take care of the size of the MEAN and STD vectors
According to the tutorial I simply changed the library for the image reading, not PIL but TIFF, but I'm still not able to make it work
The error says:
>File "c:\users\uni bonn\detectron2\detectron2\data\common.py", line 235, in __iter__
> w, h = d["width"], d["height"]
> KeyError: 'width'
I suspect the problem is that the mapping function, in the 'Instances' attribute is returning the wrong value of the Dict, probably only the annotations but not the width info that is in the same level of the file_name.
This is the data format:
`{
"test0": {
"file_name": "0.tif",
"height": 512,
"width": 512,
"image_id": "0",
"annotations": [
{
"bbox": [
134.0,
43.0,
9.0,
9.0
],
"bbox_mode": "BoxMode.XYWH_ABS",
"category_id": 0,
"segmentation": [
[
[
139,
56
],
[...`
For now I'm not trying to do any augmentation. Just making the net able to receive the image as input for training, so I used the code as in teh tutorial.
## Expected behavior:
Training normaly running
The code and error goes at follows:
## Instructions To Reproduce the Issue:
```
imgSize = 512
def custom_mapper(dataset_dict):
dataset_dict = copy.deepcopy(dataset_dict) # it will be modified by code below
# can use other ways to read image
image = tiff.imread(dataset_dict["file_name"])
# See "Data Augmentation" tutorial for details usage
auginput = T.AugInput(image)
transform = T.Resize((800, 800))(auginput)
image = torch.from_numpy(auginput.image.transpose(2, 0, 1))
annos = [
utils.transform_instance_annotations(annotation, [transform], image.shape[1:])
for annotation in dataset_dict.pop("annotations")
]
return {
# create the format that the model expects
"image": image,
"instances": utils.annotations_to_instances(annos, image.shape[1:])
}
class Trainer(DefaultTrainer):
@classmethod
def build_train_loader(cls, cfg):
return build_detection_train_loader(cfg, mapper=custom_mapper)
def setup():
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.DATASETS.TRAIN = ("circles_train",)
cfg.DATASETS.TEST = ()
cfg.DATALOADER.NUM_WORKERS = 2
cfg.SOLVER.IMS_PER_BATCH = 2
cfg.SOLVER.BASE_LR = 0.00025
cfg.SOLVER.MAX_ITER = 400
cfg.SOLVER.STEPS = []
cfg.MODEL.WEIGHTS = ""
cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 512
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1
cfg.INPUT.MIN_SIZE_TRAIN = imgSize
cfg.INPUT.MAX_SIZE_TRAIN = imgSize
cfg.INPUT.MASK_FORMAT = "polygon"
cfg.MODEL.PIXEL_MEAN = [1, 1, 1, 1, 1, 1, 1]
cfg.MODEL.PIXEL_STD = [1, 1, 1, 1, 1, 1, 1]
return cfg
cfg = setup()
trainer = Trainer(cfg)
trainer.resume_or_load(resume=False)
trainer.train()
```
> [06/21 14:28:36 d2.data.build]: Removed 0 images with no usable annotations. 5000 images left.
> [06/21 14:28:36 d2.data.build]: Using training sampler TrainingSampler
> [06/21 14:28:36 d2.data.common]: Serializing 5000 elements to byte tensors and concatenating them all ...
> [06/21 14:28:36 d2.data.common]: Serialized dataset takes 25.49 MiB
> [06/21 14:28:36 d2.engine.train_loop]: Starting training from iteration 0
> ERROR [06/21 14:28:59 d2.engine.train_loop]: Exception during training:
> Traceback (most recent call last):
> File "c:\users\uni bonn\detectron2\detectron2\engine\train_loop.py", line 149, in train
> self.run_step()
> File "c:\users\uni bonn\detectron2\detectron2\engine\defaults.py", line 494, in run_step
> self._trainer.run_step()
> File "c:\users\uni bonn\detectron2\detectron2\engine\train_loop.py", line 267, in run_step
> data = next(self._data_loader_iter)
> File "c:\users\uni bonn\detectron2\detectron2\data\common.py", line 235, in __iter__
> w, h = d["width"], d["height"]
> KeyError: 'width'
> [06/21 14:28:59 d2.engine.hooks]: Total training time: 0:00:22 (0:00:00 on hooks)
> [06/21 14:28:59 d2.utils.events]: iter: 0 lr: N/A max_mem: 1247M
> ...
> File c:\users\uni bonn\detectron2\detectron2\data\common.py:235, in AspectRatioGroupedDataset.__iter__(self)
> 233 def __iter__(self):
> 234 for d in self.dataset:
> --> 235 w, h = d["width"], d["height"]
> 236 bucket_id = 0 if w > h else 1
> 237 bucket = self._buckets[bucket_id]
> KeyError: 'width
## Environment:
sys.platform win32
Python 3.9.12 (main, Apr 4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]
numpy 1.21.5
detectron2 0.6 @c:\users\uni bonn\detectron2\detectron2
Compiler MSVC 193131107
CUDA compiler CUDA 11.7
detectron2 arch flags c:\users\uni bonn\detectron2\detectron2\_C.cp39-win_amd64.pyd; cannot find cuobjdump
DETECTRON2_ENV_MODULE
PyTorch 1.11.0 @C:\Users\Uni Bonn\anaconda3\envs\Felipe\lib\site-packages\torch
PyTorch debug build False
GPU available Yes
GPU 0,1 NVIDIA TITAN RTX (arch=7.5)
Driver version 516.01
CUDA_HOME C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7
Pillow 9.0.1
torchvision 0.12.0 @C:\Users\Uni Bonn\anaconda3\envs\Felipe\lib\site-packages\torchvision
torchvision arch flags C:\Users\Uni Bonn\anaconda3\envs\Felipe\lib\site-packages\torchvision\_C.pyd; cannot find cuobjdump
fvcore 0.1.5.post20220512
iopath 0.1.9
cv2 4.5.5
---------------------- ---------------------------------------------------------------------------------------------------
PyTorch built with:
- C++ Version: 199711
- MSVC 192829337
- Intel(R) Math Kernel Library Version 2020.0.2 Product Build 20200624 for Intel(R) 64 architecture applications
- Intel(R) MKL-DNN v2.5.2 (Git Hash a9302535553c73243c632ad3c4c80beec3d19a1e)
- OpenMP 2019
- LAPACK is enabled (usually provided by MKL)
- CPU capability usage: AVX512
- CUDA Runtime 11.3
- NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;arch=compute_86,code=sm_86;-gencode;arch=compute_37,code=compute_37
- CuDNN 8.2
- Magma 2.5.4
- Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=11.3, CUDNN_VERSION=8.2.0, CXX_COMPILER=C:/cb/pytorch_1000000000000/work/tmp_bin/sccache-cl.exe, CXX_FLAGS=/DWIN32 /D_WINDOWS /GR /EHsc /w /bigobj -DUSE_PTHREADPOOL -openmp:experimental -IC:/cb/pytorch_1000000000000/work/mkl/include -DNDEBUG -DUSE_KINETO -DLIBKINETO_NOCUPTI -DUSE_FBGEMM -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE -DEDGE_PROFILER_USE_KINETO, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, TORCH_VERSION=1.11.0, USE_CUDA=ON, USE_CUDNN=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=OFF, USE_MPI=OFF, USE_NCCL=OFF, USE_NNPACK=OFF, USE_OPENMP=ON, USE_ROCM=OFF,
Thanks for the help
Contributor guide
Assessment
This issue has not been assessed yet.