Repository: pytorch
Issue: MaskRCNN model loaded fail with torch::jit::load(model_path) (C++ API)
Labels: oncall: jit

### 🐛 Describe the bug

Environment:

* Pytorch: 1.11.0 (for training) -> platform: ubuntu 20.04
* torchvision: 0.12.0
* libtorch-cpu: 1.13.1 (for predicting) -> platform: windows 10

Describe: 
I'm trying to get a scripted model with torch.jit.script (python) and load it using the C++ API, however I get a Debug Error when executing torch::jit::load
![1](https://user-images.githubusercontent.com/48231519/215404604-9a975d64-a4c7-47e5-9620-386501b97f99.jpg)
![2](https://user-images.githubusercontent.com/48231519/215404613-1c25bb95-2677-4dff-a29b-039ae0a8a022.jpg)

Here is model code:
```
def get_model_instance_segmentation(num_classes, load_pretrain_weights=True):
    # get maskrcnn model form torchvision.models
    # load an instance segmentation model pre-trained on COCO
    model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=load_pretrain_weights)

    # get number of input features for the classifier
    in_features = model.roi_heads.box_predictor.cls_score.in_features
    # replace the pre-trained head with a new one
    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)

    # now get the number of input features for the mask classifier
    in_features_mask = model.roi_heads.mask_predictor.conv5_mask.in_channels
    hidden_layer = 256
    # and replace the mask predictor with a new one
    model.roi_heads.mask_predictor = MaskRCNNPredictor(in_features_mask,
                                                       hidden_layer,
                                                       num_classes)

    return model
```
After trained MaskRCNN model, I convert it to torch script with code:
```
def generate_torch_script(pytorch_model, device, img_file, save_path):
    # input of the model (from pil image to tensor, do not normalize image)
    original_img = Image.open(img_file).convert('RGB')      # load image
    data_transform = transforms.Compose([transforms.ToTensor()])
    img = data_transform(original_img)
    img = torch.unsqueeze(img, dim=0).to(device)        # expand batch dimension to device

    # export the model
    pytorch_model.eval()
    if device.type == 'cpu':
        pytorch_model = pytorch_model.cpu()
    traced_script_module = torch.jit.script(pytorch_model, img)
    traced_script_module.save(save_path)
```

Additionally, the export script model can work correctly with python api:
```
def main():

    # load image
    assert os.path.exists(IMG_PATH), f"{IMG_PATH} does not exits."
    original_img = Image.open(IMG_PATH).convert('RGB')

    # from pil image to tensor, do not normalize image
    data_transform = transforms.Compose([transforms.ToTensor()])
    img = data_transform(original_img)

    # check onnx model
    model_script = torch.jit.load(model_save_path)
    model_script.eval()
    with torch.no_grad():
        predictions = model_script([img.to(device)])[1][0]

        predict_boxes = predictions["boxes"].to("cpu").numpy()
        predict_classes = predictions["labels"].to("cpu").numpy()
        predict_scores = predictions["scores"].to("cpu").numpy()
        predict_mask = predictions["masks"].to("cpu").numpy()
```


### Versions

Here is the minimal code using the C++ API:
```
#include <torch/script.h>   // One-stop header.

#include <iostream>
#include <memory>

std::string model_path = "C:/Users/GilbertPan/Desktop/libtorch_deploy/traced_model.pt";

int main() {

  torch::jit::script::Module module;
  try {
    // Deserialize the ScriptModule from a file using torch::jit::load().
    module = torch::jit::load(model_path);   // fail
  }
  catch (const c10::Error& e) {
    std::cerr << "error loading the model\n";
    return -1;
  }

  std::cout << "ok\n";
  return 0;
}
```

cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel