Detectron2: `get_event_storage() has to be called inside a 'with EventStorage(...)' context!` when training

Created on 15 Oct 2019  路  2Comments  路  Source: facebookresearch/detectron2

Hi!

When I try to do a training of a MaskRCNN model in the "classic" pytorch way, I get the following error:
get_event_storage() has to be called inside a 'with EventStorage(...)' context!

But no problem when I'm in inference mode (model.eval() before the dataloader loop).

Is this a bug or just a misuse on my part?

Detailed error:

Traceback (most recent call last):
  File "t.py", line 42, in <module>
    outputs = model(data)
  File "/home/j/miniconda3/envs/alp/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/j/detectron2/detectron2/modeling/meta_arch/rcnn.py", line 82, in forward
    proposals, proposal_losses = self.proposal_generator(images, features, gt_instances)
  File "/home/j/miniconda3/envs/alp/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/j/detectron2/detectron2/modeling/proposal_generator/rpn.py", line 161, in forward
    losses = {k: v * self.loss_weight for k, v in outputs.losses().items()}
  File "/home/j/detectron2/detectron2/modeling/proposal_generator/rpn_outputs.py", line 336, in losses
    storage = get_event_storage()
  File "/home/j/detectron2/detectron2/utils/events.py", line 18, in get_event_storage
    ), "get_event_storage() has to be called inside a 'with EventStorage(...)' context!"
AssertionError: get_event_storage() has to be called inside a 'with EventStorage(...)' context!

To Reproduce

import torch
from torch.utils.data import DataLoader

from detectron2.structures import Instances,  Boxes,  PolygonMasks
from detectron2.config import get_cfg
from detectron2.modeling import build_model


class TestDataset(torch.utils.data.Dataset):
    def __len__(self):
        return 20

    def __getitem__(self, idx):
        img = torch.rand([3, 400, 400])

        masks = PolygonMasks(
            [[[10, 10, 20, 20, 30, 30, 10, 40]],
            [[100, 100, 200, 200, 300, 300, 100, 400]]],
        )
        boxes = masks.get_bounding_boxes()
        classes = torch.as_tensor([1, 2], dtype=torch.int64)

        instances = Instances(
            img.shape[1:],
            gt_boxes=boxes,
            gt_masks=masks,
            gt_classes=classes
        )

        return {"image": img, "instances": instances}

def collate_fn(batch):
    return batch

ds = TestDataset()
dl = DataLoader(ds, batch_size=2, num_workers=0, collate_fn=collate_fn)

cfg = get_cfg()
cfg.merge_from_file("mask_rcnn_X_101_32x8d_FPN_3x.yaml")
model = build_model(cfg) 

for data in dl:
    outputs = model(data)

Environment

output of python -m detectron2.utils.collect_env.

Most helpful comment

When in training, the model needs to be called under:

from detectron2.utils.events import EventStorage
with EventStorage() as storage:
  outputs = model(inputs)

the storage is used to collect metrics during training. We'll update the docs shortly.

All 2 comments

When in training, the model needs to be called under:

from detectron2.utils.events import EventStorage
with EventStorage() as storage:
  outputs = model(inputs)

the storage is used to collect metrics during training. We'll update the docs shortly.

Thanks !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DeepLakhani99 picture DeepLakhani99  路  4Comments

danielgordon10 picture danielgordon10  路  3Comments

RomRoc picture RomRoc  路  4Comments

jinfagang picture jinfagang  路  3Comments

Ormagardskvaedi picture Ormagardskvaedi  路  4Comments