Load_model a model saved with DataParallelin pytorch

username: marziehoghbaie
cooked: <p>Hi,</p>
<p>I like to use a pre-trained model in pytorch. Although I changed the device type to ‘cpu’, I still get the following error:<br>
“RuntimeError: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cpu”<br>
The model is originally saved  with DataParallel.<br>
I change the device of both data and model to ‘cpu’.</p>

username: marziehoghbaie
cooked: <p>The test code is from:</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/JDAI-CV/FaceX-Zoo/blob/main/face_sdk/api_usage/face_pipline.py" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/JDAI-CV/FaceX-Zoo/blob/main/face_sdk/api_usage/face_pipline.py" target="_blank" rel="noopener nofollow ugc">JDAI-CV/FaceX-Zoo/blob/main/face_sdk/api_usage/face_pipline.py</a></h4>
<pre><code class="lang-py">"""
@author: JiXuan Xu, Jun Wang
@date: 20201024
@contact: jun21wangustc@gmail.com 
"""
import sys
sys.path.append('.')
import logging
mpl_logger = logging.getLogger('matplotlib')
mpl_logger.setLevel(logging.WARNING)
import logging.config
logging.config.fileConfig("config/logging.conf")
logger = logging.getLogger('api')

import yaml
import cv2
import numpy as np
from core.model_loader.face_detection.FaceDetModelLoader import FaceDetModelLoader
from core.model_handler.face_detection.FaceDetModelHandler import FaceDetModelHandler
from core.model_loader.face_alignment.FaceAlignModelLoader import FaceAlignModelLoader
</code></pre>

  This file has been truncated. <a href="https://github.com/JDAI-CV/FaceX-Zoo/blob/main/face_sdk/api_usage/face_pipline.py" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


username: ptrblck
cooked: <p>You could try to save the <code>state_dict</code> of the underlying model without the <code>nn.DataParallel</code> wrapper as described <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html#saving-torch-nn-dataparallel-models">here</a>. Additionally, you could also push it to the CPU before saving it. This would allow you to load the model on any system later.</p>

username: marziehoghbaie
cooked: <p>Thanks, I could not use this solution, because the model is previously saved by someone else and I just want to test it. However, I found the solution by changing the model to the following:<br>
self.model = self.model.module.cpu()<br>
More info can be found in the following issue:</p><aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/JDAI-CV/FaceX-Zoo/issues/40" target="_blank" rel="noopener nofollow ugc">github.com/JDAI-CV/FaceX-Zoo</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/JDAI-CV/FaceX-Zoo/issues/40" target="_blank" rel="noopener nofollow ugc">Test face_pipline.py on CPU</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2021-02-13" data-time="12:03:19" data-timezone="UTC">12:03PM - 13 Feb 21 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2021-02-14" data-time="06:44:09" data-timezone="UTC">06:44AM - 14 Feb 21 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/marziehoghbaie" target="_blank" rel="noopener nofollow ugc">
          <img alt="marziehoghbaie" src="https://avatars.githubusercontent.com/u/24191889?v=4" class="onebox-avatar-inline" width="20" height="20">
          marziehoghbaie
        </a>
      </div>
    </div>
  </div>
</div>

<div class="github-row">
  <p class="github-content">Hi,
I like to test face_pipline.py on cpu, but I keep taking the following error:
RuntimeError: module must have its parameters and buffers...</p>
</div>

<div class="labels">
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>