简体   繁体   中英

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="resnet18",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["local"]["dataset_train"].num_classes();
    if model_name == "resnet18":
        retinanet = model.resnet18(num_classes=num_classes, pretrained=True)
    elif model_name == "resnet34":
        retinanet = model.resnet34(num_classes=num_classes, pretrained=True)
    elif model_name == "resnet50":
        # 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 == "resnet101":
        retinanet = model.resnet101(num_classes=num_classes, pretrained=True)
    elif model_name == "resnet152":
        retinanet = model.resnet152(num_classes=num_classes, pretrained=True)

    if self.system_dict["params"]["use_gpu"]:
        self.system_dict["params"]["gpu_devices"] = gpu_devices
        if len(self.system_dict["params"]["gpu_devices"])==1:
            os.environ["CUDA_VISIBLE_DEVICES"] = str(self.system_dict["params"]["gpu_devices"][0])
        else:
            os.environ["CUDA_VISIBLE_DEVICES"] = ','.join([str(id) for id in self.system_dict["params"]["gpu_devices"]])
        self.system_dict["local"]["device"] = '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["local"]["device"])
        retinanet = torch.nn.DataParallel(retinanet).to(self.system_dict["local"]["device"])

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

    self.system_dict["local"]["model"] = 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="resnet50", gpu_devices=[0, 1, 2, 3])

error:

AttributeError                            Traceback (most recent call last)
<ipython-input-22-1a0c8d446904> in <module>()
      3 if PRE_TRAINED:
      4   #Initialising Model
----> 5   gtf.Model(model_name="resnet50", 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
--> 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["local"]["device"])

/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)
-> 1455         state_dict = state_dict.copy()
   1456         if metadata is not None:
   1457             # mypy isn't aware that "_metadata" 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("'{}' object has no attribute '{}'".format(
-> 1178             type(self).__name__, name))
   1179 
   1180     def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM