最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - ValueError: Expected x_min for bbox to be in the range [0.0, 1.0] - Stack Overflow

programmeradmin2浏览0评论

I am using albumentations for my object detection data augmentation because it deals with bbox at the same time. My format is [xmin, ymin, xmax, ymax] with pascal_voc format.

        A.Compose(
            [
                A.HorizontalFlip(p=0.5),
                A.VerticalFlip(p=0.5),
                A.OneOf(
                    [
                        A.ShiftScaleRotate(
                            shift_limit=0.2,
                            scale_limit=(-0.1, 0.6),
                            rotate_limit=0,
                            p=0.5,
                        ),
                        A.RandomSizedCrop(
                            min_max_height=[200, 352], size=(352, 2048), p=0.5
                        ),
                    ],
                    p=0.5,
                ),
                A.AdditiveNoise(
                    noise_type="uniform", noise_params={"ranges":[(-0.2, 0.2)]}, p=0.5
                ),
                ToTensorV2(),
            ],
            bbox_params=A.BboxParams(
                format="pascal_voc", label_fields=["class_labels"]
            ),
     )

Then I create my data as:

    def __getitem__(self, index):
        data = get_data(index)
        torch_target = _to_torch_target(data)
        torch_img = _get_torch_image(data)
        if self.transform:
            sample = self.transform(
                image=torch_img.numpy(),
                bboxes=torch_target["boxes"],
                class_labels=torch_target["labels"],
            )
            torch_img = sample["image"]
            torch_target["boxes"] = torch.Tensor(sample["bboxes"])
            torch_target["labels"] = torch.Tensor(sample["class_labels"])
        
        torch_target["boxes"] = torch.as_tensor(torch_target["boxes"], dtype=torch.float32)
        torch_target["labels"] = torch.as_tensor(torch_target["labels"], dtype=torch.int64)
        return torch_img, torch_target

But I am getting an error:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/opt/conda/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/.../main.py", line 116, in <module>
    train(results_path, dataset_path, weights_path, **conf)
  File "/home/.../main.py", line 77, in train
    trainer.fit(model, train_dataloader, val_dataloader)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py", line 543, in fit
    call._call_and_handle_interrupt(
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/trainer/call.py", line 44, in _call_and_handle_interrupt
    return trainer_fn(*args, **kwargs)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py", line 579, in _fit_impl
    self._run(model, ckpt_path=ckpt_path)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py", line 986, in _run
    results = self._run_stage()
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py", line 1032, in _run_stage
    self.fit_loop.run()
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/loops/fit_loop.py", line 205, in run
    self.advance()
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/loops/fit_loop.py", line 363, in advance
    self.epoch_loop.run(self._data_fetcher)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/loops/training_epoch_loop.py", line 138, in run
    self.advance(data_fetcher)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/loops/training_epoch_loop.py", line 204, in advance
    batch, _, __ = next(data_fetcher)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/loops/fetchers.py", line 133, in __next__
    batch = super().__next__()
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/loops/fetchers.py", line 60, in __next__
    batch = next(self.iterator)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/utilities/combined_loader.py", line 341, in __next__
    out = next(self._iterator)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/utilities/combined_loader.py", line 78, in __next__
    out[i] = next(self.iterators[i])
  File "/opt/conda/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 628, in __next__
    data = self._next_data()
  File "/opt/conda/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 671, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "/opt/conda/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 58, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/opt/conda/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 58, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/jovyan/trv/MVP_SPACE/mvp_space/src/train_pipeline/dataset.py", line 184, in __getitem__
    sample = self.transform(
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/composition.py", line 493, in __call__
    self.preprocess(data)
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/composition.py", line 533, in preprocess
    self._preprocess_processors(data)
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/composition.py", line 560, in _preprocess_processors
    processor.preprocess(data)
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/utils.py", line 155, in preprocess
    data[data_name] = self.check_and_convert(data[data_name], shape, direction="to")
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/bbox_utils.py", line 306, in check_and_convert
    self.check(converted_data, shape)
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/bbox_utils.py", line 314, in check
    check_bboxes(data)
  File "/opt/conda/lib/python3.10/site-packages/albumentations/augmentations/utils.py", line 189, in wrapper
    return func(*args, **kwargs)
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/bbox_utils.py", line 549, in check_bboxes
    raise ValueError(
ValueError: Expected x_min for bbox [ 4.372159  20.666666   4.9176135 26.666666   1.       ] to be in the range [0.0, 1.0], got 4.372159004211426.
Epoch 0:   0%|          | 0/569 [00:00<?, ?it/s]       

However, pascal_voc is NOT supposed to be between [0, 1]... What is going on here?


Something more weird are the numbers, they are all integers, as you can see by this assert I added at the start. I don't know when they are transformed to float.

        for bbox in torch_target["boxes"]:
            assert np.all([int(x) == x for x in bbox])
        if self.transform:
            sample = self.transform(
                image=torch_img.numpy(),
                bboxes=torch_target["boxes"],
                class_labels=torch_target["labels"],
            )

I am using albumentations for my object detection data augmentation because it deals with bbox at the same time. My format is [xmin, ymin, xmax, ymax] with pascal_voc format.

        A.Compose(
            [
                A.HorizontalFlip(p=0.5),
                A.VerticalFlip(p=0.5),
                A.OneOf(
                    [
                        A.ShiftScaleRotate(
                            shift_limit=0.2,
                            scale_limit=(-0.1, 0.6),
                            rotate_limit=0,
                            p=0.5,
                        ),
                        A.RandomSizedCrop(
                            min_max_height=[200, 352], size=(352, 2048), p=0.5
                        ),
                    ],
                    p=0.5,
                ),
                A.AdditiveNoise(
                    noise_type="uniform", noise_params={"ranges":[(-0.2, 0.2)]}, p=0.5
                ),
                ToTensorV2(),
            ],
            bbox_params=A.BboxParams(
                format="pascal_voc", label_fields=["class_labels"]
            ),
     )

Then I create my data as:

    def __getitem__(self, index):
        data = get_data(index)
        torch_target = _to_torch_target(data)
        torch_img = _get_torch_image(data)
        if self.transform:
            sample = self.transform(
                image=torch_img.numpy(),
                bboxes=torch_target["boxes"],
                class_labels=torch_target["labels"],
            )
            torch_img = sample["image"]
            torch_target["boxes"] = torch.Tensor(sample["bboxes"])
            torch_target["labels"] = torch.Tensor(sample["class_labels"])
        
        torch_target["boxes"] = torch.as_tensor(torch_target["boxes"], dtype=torch.float32)
        torch_target["labels"] = torch.as_tensor(torch_target["labels"], dtype=torch.int64)
        return torch_img, torch_target

But I am getting an error:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/opt/conda/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/.../main.py", line 116, in <module>
    train(results_path, dataset_path, weights_path, **conf)
  File "/home/.../main.py", line 77, in train
    trainer.fit(model, train_dataloader, val_dataloader)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py", line 543, in fit
    call._call_and_handle_interrupt(
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/trainer/call.py", line 44, in _call_and_handle_interrupt
    return trainer_fn(*args, **kwargs)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py", line 579, in _fit_impl
    self._run(model, ckpt_path=ckpt_path)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py", line 986, in _run
    results = self._run_stage()
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/trainer/trainer.py", line 1032, in _run_stage
    self.fit_loop.run()
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/loops/fit_loop.py", line 205, in run
    self.advance()
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/loops/fit_loop.py", line 363, in advance
    self.epoch_loop.run(self._data_fetcher)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/loops/training_epoch_loop.py", line 138, in run
    self.advance(data_fetcher)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/loops/training_epoch_loop.py", line 204, in advance
    batch, _, __ = next(data_fetcher)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/loops/fetchers.py", line 133, in __next__
    batch = super().__next__()
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/loops/fetchers.py", line 60, in __next__
    batch = next(self.iterator)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/utilities/combined_loader.py", line 341, in __next__
    out = next(self._iterator)
  File "/opt/conda/lib/python3.10/site-packages/lightning/pytorch/utilities/combined_loader.py", line 78, in __next__
    out[i] = next(self.iterators[i])
  File "/opt/conda/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 628, in __next__
    data = self._next_data()
  File "/opt/conda/lib/python3.10/site-packages/torch/utils/data/dataloader.py", line 671, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "/opt/conda/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 58, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/opt/conda/lib/python3.10/site-packages/torch/utils/data/_utils/fetch.py", line 58, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/jovyan/trv/MVP_SPACE/mvp_space/src/train_pipeline/dataset.py", line 184, in __getitem__
    sample = self.transform(
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/composition.py", line 493, in __call__
    self.preprocess(data)
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/composition.py", line 533, in preprocess
    self._preprocess_processors(data)
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/composition.py", line 560, in _preprocess_processors
    processor.preprocess(data)
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/utils.py", line 155, in preprocess
    data[data_name] = self.check_and_convert(data[data_name], shape, direction="to")
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/bbox_utils.py", line 306, in check_and_convert
    self.check(converted_data, shape)
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/bbox_utils.py", line 314, in check
    check_bboxes(data)
  File "/opt/conda/lib/python3.10/site-packages/albumentations/augmentations/utils.py", line 189, in wrapper
    return func(*args, **kwargs)
  File "/opt/conda/lib/python3.10/site-packages/albumentations/core/bbox_utils.py", line 549, in check_bboxes
    raise ValueError(
ValueError: Expected x_min for bbox [ 4.372159  20.666666   4.9176135 26.666666   1.       ] to be in the range [0.0, 1.0], got 4.372159004211426.
Epoch 0:   0%|          | 0/569 [00:00<?, ?it/s]       

However, pascal_voc is NOT supposed to be between [0, 1]... What is going on here?


Something more weird are the numbers, they are all integers, as you can see by this assert I added at the start. I don't know when they are transformed to float.

        for bbox in torch_target["boxes"]:
            assert np.all([int(x) == x for x in bbox])
        if self.transform:
            sample = self.transform(
                image=torch_img.numpy(),
                bboxes=torch_target["boxes"],
                class_labels=torch_target["labels"],
            )
Share Improve this question edited Mar 6 at 14:23 J Agustin Barrachina asked Mar 6 at 13:55 J Agustin BarrachinaJ Agustin Barrachina 4,1203 gold badges42 silver badges68 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

So, it was the fact that I was already using a torch format numpy image. That has channels first. The solution was using

image=torch_img.permute(1, 2, 0).numpy()

发布评论

评论列表(0)

  1. 暂无评论