AttributeError: 'DataParallel' object has no attribute 'copy'
I am trying to resume training monkAI pytorch retinanet. I have loaded with .pt file instead of actual model. The changes are made in Monk_Object_Detection/5_pytorch_retinanet/lib/train_detector.py, check for '# change' in the places where its modified.
def Model(self, model_name=&quot;resnet18&quot;,gpu_devices=[0]):
    '''
    User function: Set Model parameters

        Available Models
            resnet18
            resnet34
            resnet50
            resnet101
            resnet152

    Args:
        model_name (str): Select model from available models
        gpu_devices (list): List of GPU Device IDs to be used in training

    Returns:
        None
    '''

    num_classes = self.system_dict[&quot;local&quot;][&quot;dataset_train&quot;].num_classes();
    if model_name == &quot;resnet18&quot;:
        retinanet = model.resnet18(num_classes=num_classes, pretrained=True)
    elif model_name == &quot;resnet34&quot;:
        retinanet = model.resnet34(num_classes=num_classes, pretrained=True)
    elif model_name == &quot;resnet50&quot;:
        # retinanet = model.resnet50(num_classes=num_classes, pretrained=True)
        # change
        retinanet = torch.load('/content/drive/MyDrive/Object_detection_retinanet/trained_retinanet_40.pt')
    elif model_name == &quot;resnet101&quot;:
        retinanet = model.resnet101(num_classes=num_classes, pretrained=True)
    elif model_name == &quot;resnet152&quot;:
        retinanet = model.resnet152(num_classes=num_classes, pretrained=True)

    if self.system_dict[&quot;params&quot;][&quot;use_gpu&quot;]:
        self.system_dict[&quot;params&quot;][&quot;gpu_devices&quot;] = gpu_devices
        if len(self.system_dict[&quot;params&quot;][&quot;gpu_devices&quot;])==1:
            os.environ[&quot;CUDA_VISIBLE_DEVICES&quot;] = str(self.system_dict[&quot;params&quot;][&quot;gpu_devices&quot;][0])
        else:
            os.environ[&quot;CUDA_VISIBLE_DEVICES&quot;] = ','.join([str(id) for id in self.system_dict[&quot;params&quot;][&quot;gpu_devices&quot;]])
        self.system_dict[&quot;local&quot;][&quot;device&quot;] = 'cuda' if torch.cuda.is_available() else 'cpu'

        # change - added 3 lines below
        if isinstance(retinanet,torch.nn.DataParallel):
            retinanet = retinanet.module
        retinanet.load_state_dict(torch.load('/content/drive/MyDrive/Object_detection_retinanet/trained_retinanet_40.pt'))
        
        retinanet = retinanet.to(self.system_dict[&quot;local&quot;][&quot;device&quot;])
        retinanet = torch.nn.DataParallel(retinanet).to(self.system_dict[&quot;local&quot;][&quot;device&quot;])

    
    retinanet.training = True
    retinanet.train()
    retinanet.module.freeze_bn()

    self.system_dict[&quot;local&quot;][&quot;model&quot;] = retinanet;

I am getting attribute error, when I call the Model() from main function as shown below:
from train_detector import Detector
gtf = Detector() 

#Loading the dataset
root_dir = './'
coco_dir = 'coco_dir'
img_dir = 'images'
set_dir ='train'
gtf.Train_Dataset(root_dir, coco_dir, img_dir, set_dir, batch_size=8, use_gpu=True)

gtf.Model(model_name=&quot;resnet50&quot;, gpu_devices=[0, 1, 2, 3])

error:
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-22-1a0c8d446904&gt; in &lt;module&gt;()
      3 if PRE_TRAINED:
      4   #Initialising Model
----&gt; 5   gtf.Model(model_name=&quot;resnet50&quot;, gpu_devices=[0, 1, 2, 3])
      6   #Setting up hyperparameters
      7   gtf.Set_Hyperparams(lr=0.001, val_interval=1, print_interval=20)

2 frames
/content/Monk_Object_Detection/5_pytorch_retinanet/lib/train_detector.py in Model(self, model_name, gpu_devices)
    245             if isinstance(retinanet,torch.nn.DataParallel):
    246                 retinanet = retinanet.module
--&gt; 247             retinanet.load_state_dict(torch.load('/content/drive/MyDrive/Object_detection_retinanet/trained_retinanet_40.pt'))
    248 
    249             retinanet = retinanet.to(self.system_dict[&quot;local&quot;][&quot;device&quot;])

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict)
   1453         # copy state_dict so _load_from_state_dict can modify it
   1454         metadata = getattr(state_dict, '_metadata', None)
-&gt; 1455         state_dict = state_dict.copy()
   1456         if metadata is not None:
   1457             # mypy isn't aware that &quot;_metadata&quot; exists in state_dict

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in __getattr__(self, name)
   1176                 return modules[name]
   1177         raise AttributeError(&quot;'{}' object has no attribute '{}'&quot;.format(
-&gt; 1178             type(self).__name__, name))
   1179 
   1180     def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -&gt; None:

AttributeError: 'DataParallel' object has no attribute 'copy'

kindly, help me with the solution!

I found this by simply googling your problem:
retinanet.load_state_dict(torch.load('filename').module.state_dict())
The link to the discussion is here.
