facebookresearch / detr

End-to-End Object Detection with Transformers
Apache License 2.0
13.58k stars 2.45k forks source link

RuntimeError: Error(s) in loading state_dict for DETRModel: #362

Open ishandutta0098 opened 3 years ago

ishandutta0098 commented 3 years ago

I am trying to load detr with resnet50 pretrained model but I am getting the following error.

RuntimeError: Error(s) in loading state_dict for DETRModel:
    Missing key(s) in state_dict: "model.transformer.encoder.layers.0.self_attn.in_proj_weight", "model.transformer.encoder.layers.0.self_attn.in_proj_bias", "model.transformer.encoder.layers.0.self_attn.out_proj.weight", "model.transformer.encoder.layers.0.self_attn.out_proj.bias", "model.transformer.encoder.layers.0.linear1.weight", "model.transformer.encoder.layers.0.linear1.bias" ........

This is the code I am using:

import sys
import torch.nn as nn
from pathlib import Path
sys.path.append('../input/detrmodels/facebookresearch_detr_master')

# copy pretrained weights to the folder PyTorch will search by default
Path('/root/.cache/torch/hub/').mkdir(exist_ok=True, parents=True)
Path('/root/.cache/torch/hub/checkpoints/').mkdir(exist_ok=True, parents=True)

detr_path = '/root/.cache/torch/hub/checkpoints/detr-r50-e632da11.pth'
resnet50_pretrained = '/root/.cache/torch/hub/checkpoints/resnet50-19c8e357.pth'
detr_hub = '/root/.cache/torch/hub/facebookresearch_detr_master'

!cp ../input/detrmodels/detr-r50-e632da11.pth {detr_path}
!cp ../input/detrmodels/resnet50-19c8e357.pth {resnet50_pretrained}
!cp -R ../input/detrmodels/facebookresearch_detr_master {detr_hub}

DIR_INPUT = '../input/shopee-product-matching'
WEIGHTS_FILE = '../input/detrmodels/resnet50-19c8e357.pth'

class DETRModel(nn.Module):
    def __init__(self, num_classes, num_queries, model_name='detr_resnet50'):
        super(DETRModel, self).__init__()
        self.num_classes = num_classes
        self.num_queries = num_queries

        self.model = torch.hub.load('facebookresearch/detr', model_name, pretrained=True)

        self.in_features = self.model.class_embed.in_features

        self.model.class_embed = nn.Linear(in_features=self.in_features,
                                           out_features=self.num_classes)
        self.model.num_queries = self.num_queries

    def forward(self, images):
        return self.model(images)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
num_classes = 11011
num_queries = 100
model = DETRModel(num_classes=N_CLASSES, num_queries=num_queries, model_name='detr_resnet50')
model = model.to(device)
model.load_state_dict(torch.load(WEIGHTS_FILE))
model.eval()

I am using this dataset for the above code.

How to fix this?

parthkvv commented 2 years ago

Hi @ishandutta0098, did you find any solution to this? I am facing the same issue. Any help would be appreciated.