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!
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)
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 !
Most helpful comment
When in training, the model needs to be called under:
the storage is used to collect metrics during training. We'll update the docs shortly.