facebookresearch / facebookresearch/detectron2

How to save Detectron model as Vanilla Pytorch model?

Open
#4,589 7 comments 0 reactions 0 assignees View on GitHub
documentation
Dominant language
Python
Stars
34.7k
Forks
7.9k
PR merge metrics
No merged PRs in 30d

Description

I have a `Faster-RCNN` model trained with `Detectron2`. Model weights are saved as `model.pth`.

I have my `config.yml` file and there are a couple of ways to load this model:

```
from detectron2.modeling import build_model
from detectron2.checkpoint import DetectionCheckpointer

cfg = get_cfg()
config_name = "config.yml"
cfg.merge_from_file(config_name)

cfg.MODEL.WEIGHTS = './model.pth'
model = DefaultPredictor(cfg)

OR

model_ = build_model(cfg)
model = DetectionCheckpointer(model_).load("./model.pth")
```

Also, you can get predictions from this model individually as [given in official documentation](https://detectron2.readthedocs.io/en/latest/tutorials/models.html#model-input-format):
```
image = np.array(Image.open('page4.jpg'))[:,:,::-1] # RGB to BGR format
tensor_image = torch.from_numpy(image.copy()).permute(2, 0, 1) # B, channels, W, H

with torch.no_grad():
output = torch_model([{"image":tensor_image}])
```

running the following commands:
```
print(type(model))
print(type(model.model))
print(type(model.model.backbone))
```

Gives you:
```

```

**Problem: I want to use [GradCam for model explainability](https://github.com/jacobgil/pytorch-grad-cam) and it uses `pytorch` models as [given in this tutorial](https://analyticsindiamag.com/explainable-image-classification-using-faster-r-cnn-and-grad-cam/)**

How can I turn `detectron2` model in vanilla `pytorch` model?

I have tried:
```
torch.save(model.model.state_dict(), "torch_weights.pth")
torch.save(model.model, "torch_model.pth")

from torchvision.models.detection import fasterrcnn_resnet50_fpn

dummy = fasterrcnn_resnet50_fpn(pretrained=False, num_classes=1)
# dummy.load_state_dict(torch.load('./model.pth', map_location = 'cpu'))
dummy.load_state_dict(torch.load('./torch_weights.pth', map_location = 'cpu'))
```

but obviously, I'm getting errors due to the different layer names and sizes etc.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.