ModuleNotFoundError: No module named 'network'

Oscar_Rangel: <p>I am trying to load a model, but I am getting this error… I am working on windows, I searched the web and this forum but I count not find anything…</p>
<p>Thanks for the help.</p>
<pre><code class="lang-auto">gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
gpu

model = torch.load("./faceforensics_models/faceforensics++_models_subset/full/xception/full_raw.p", map_location=gpu)

----------------------------------------------------------------------------------

ModuleNotFoundError                       Traceback (most recent call last)
&lt;ipython-input-5-a95c0d9b8209&gt; in &lt;module&gt;
----&gt; 1 model = torch.load("./faceforensics_models/faceforensics++_models_subset/full/xception/full_raw.p", map_location=gpu)

c:\users\oscar\appdata\local\programs\python\python36\lib\site-packages\torch\serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
    527             with _open_zipfile_reader(f) as opened_zipfile:
    528                 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
--&gt; 529         return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
    530 
    531 

c:\users\oscar\appdata\local\programs\python\python36\lib\site-packages\torch\serialization.py in _legacy_load(f, map_location, pickle_module, **pickle_load_args)
    700     unpickler = pickle_module.Unpickler(f, **pickle_load_args)
    701     unpickler.persistent_load = persistent_load
--&gt; 702     result = unpickler.load()
    703 
    704     deserialized_storage_keys = pickle_module.load(f, **pickle_load_args)

ModuleNotFoundError: No module named 'network'

</code></pre>

ptrblck: <p>If you store a model directly via <code>torch.save(model, file_path)</code>, you would need to restore the file and folder structure in order to load this model again as explained <a href="https://pytorch.org/docs/stable/notes/serialization.html">here</a>.</p>
<p>Based on the error message it seems that some files with the <code>network</code> definition are missing.</p>

Oscar_Rangel: <aside class="quote no-group" data-username="Oscar_Rangel" data-post="1" data-topic="71721">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/oscar_rangel/48/20363_2.png" class="avatar"> Oscar_Rangel:</div>
<blockquote>
<p>./faceforensics_models/faceforensics++_models_subset/full/xception/full_raw.p"</p>
</blockquote>
</aside>
<p>Hey amigo <a class="mention" href="/u/ptrblck">@ptrblck</a>, thanks for the response!</p>
<p>are you talking about this ?</p>
<p>“./faceforensics_models/faceforensics++_models_subset/full/xception/full_raw.p”</p>
<p>it need to be the same ?</p>
<p>or you talking about this?<br>
the_model = TheModelClass() # declare the class and then try to load it ?</p>
<p>Thanks.</p>

ptrblck: <p>If you are using this approach: <code>model = torch.load(path)</code>, you would need to make sure that all necessary files are in the corresponding folders as they were while storing the model.</p>
<p>The other approach of creating the model first and load the <code>state_dict</code> is more flexible, as you might change the actual file and folder structure and would just have to make sure to create a model with matching parameters.</p>

ayrts: <p><a class="mention" href="/u/oscar_rangel">@Oscar_Rangel</a> I encountered the same problem a few days ago. You have to replicate the model github directory structure to be able to open the file. To avoid that in the future, you can save the model’s state_dict instead. Below is how I solved the error:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/4/948b3dcb5eda1227851ffe6a4f1ff552bc477675.png" data-download-href="https://discuss.pytorch.org/uploads/default/948b3dcb5eda1227851ffe6a4f1ff552bc477675" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/4/948b3dcb5eda1227851ffe6a4f1ff552bc477675.png" alt="image" data-base62-sha1="lc4Zjoyp1yBGbIUPO13xDoLJazb" width="690" height="86" data-dominant-color="292827"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">993×124 7.42 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

Dongdong_Lin: <p>Hi <a class="mention" href="/u/ayrts">@ayrts</a>, I have tried your code to load the model, however it returns the same error as below…</p>
<p>ModuleNotFoundError: No module named ‘network’</p>
<p>Could you help me to fix it? or could you provide me the .pth files? Thank you.</p>

Dongdong_Lin: <p><a class="mention" href="/u/oscar_rangel">@Oscar_Rangel</a> I fix the error. You cannot load the model directly since the state_dict contains a structure like</p>
<pre><code class="lang-python">network
   |--- __init__.py
   |--- models.py
   |--- xception.py
</code></pre>
<p>Therefore, you need to build a folder named network and then copy models.py and xception.py from FF++, then you can import the model file. Remember to add <strong>init</strong>.py then you can import these two files without an absolute path.</p>

anavitor97: <p>hey thank you for this! but did you get also this error? and how did you solve it?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/c/0cfb1d0d8167a375d10b34c4e0ba61f9358d6f6d.png" data-download-href="https://discuss.pytorch.org/uploads/default/0cfb1d0d8167a375d10b34c4e0ba61f9358d6f6d" title="Screenshot 2022-04-09 at 14.33.39"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/c/0cfb1d0d8167a375d10b34c4e0ba61f9358d6f6d_2_689x286.png" alt="Screenshot 2022-04-09 at 14.33.39" data-base62-sha1="1QPJK8X6SzU97plBicD5eZkgg8t" width="689" height="286" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/c/0cfb1d0d8167a375d10b34c4e0ba61f9358d6f6d_2_689x286.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/c/0cfb1d0d8167a375d10b34c4e0ba61f9358d6f6d_2_1033x429.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/c/0cfb1d0d8167a375d10b34c4e0ba61f9358d6f6d.png 2x" data-dominant-color="F4F3F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2022-04-09 at 14.33.39</span><span class="informations">1231×512 114 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

Binh_Le_Minh: <p>Follow the guideline in this repo (<a href="https://github.com/paarthneekhara/AdversarialDeepFakes" class="inline-onebox" rel="noopener nofollow ugc">GitHub - paarthneekhara/AdversarialDeepFakes</a>), you have to download the .zip file and extract it from the directory that already has the <code>network</code> folder</p>