How do I copy the parameters of a model learned in c ++?

21fa417e3fb06e56040c: <p>I want to use it in c ++.<br>
I want to learn new network by reusing previously learned data, but I do not know what to do.</p>

yf225: <p>Do you have the network defined in C++ frontend?</p>

21fa417e3fb06e56040c: <p>Yes. I Have a network</p>

yf225: <p>I saw your other post in <a href="https://discuss.pytorch.org/t/how-to-load-the-prebuilt-resnet-models-weight-or-any-other-prebuilt-models/44982" class="inline-onebox">How to load the prebuilt Resnet models weight ?(or any other prebuilt models)</a>, did you resolve this problem?</p>

21fa417e3fb06e56040c: <p>Yes. this problem Resolve</p>

CF-chen-feng-CF: <p>Can you tell me how you solved this problem?<br>
I built the resnet50 network in C++, and I have the pre-training weight of resnet50 under Python. How can I load this weight into resnet50 in C++ and continue training?</p>

yf225: <p>This is one way to share pre-trained weights from Python to C++:</p>
<ol>
<li>Convert Python model to TorchScript, and save the model to file using <code>model.save(filename)</code>.</li>
<li>Create a C++ frontend model with the exact same model structure.</li>
<li>Load the parameters and buffers from TorchScript model to C++ frontend model using <code>torch::load(cpp_module, file_path_to_torchscript_model)</code>
</li>
</ol>
<p>This is a minimal example:<br>
JIT model:</p>
<pre><code class="lang-python">import torch
class Model(torch.jit.ScriptModule):
    def __init__(self):
        super(Model, self).__init__()
        self.linear = torch.nn.Linear(2, 2)

model = Model()
print(model.linear.weight)  # For comparison
model.save('model1.pt')
</code></pre>
<p>C++ frontend model:</p>
<pre><code class="lang-cpp">struct Net : torch::nn::Module {
  Net() {
    linear = register_module("linear", torch::nn::Linear(2, 2));  // Exactly match the Python model
  }
  torch::nn::Linear linear{nullptr};
};
</code></pre>
<p>Load parameters of JIT model into C++ frontend model:</p>
<pre><code class="lang-cpp">int main(int argc, const char* argv[]) {
    auto module = std::make_shared&lt;Net&gt;();
    std::cout &lt;&lt; module-&gt;linear-&gt;named_parameters()["weight"] &lt;&lt; "\n";  // before loading pre-trained params
    torch::load(module, argv[1]);
    std::cout &lt;&lt; module-&gt;linear-&gt;named_parameters()["weight"] &lt;&lt; "\n";  // after loading pre-trained params

    return 0;
}
</code></pre>
<p>For converting your existing Python model to TorchScript (i.e. step 1), you can also use tracing (<a href="https://pytorch.org/tutorials/advanced/cpp_export.html#converting-to-torch-script-via-tracing" rel="nofollow noopener">https://pytorch.org/tutorials/advanced/cpp_export.html#converting-to-torch-script-via-tracing</a>). However you still need to manually define the same model structure in C++ API before loading the parameters in C++.</p>

21fa417e3fb06e56040c: <p>This example is very simple, but it works well. If you want more information. Check this Repository.<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="16" height="16">
      <a href="https://github.com/ShahriarSS/vision/tree/update-c++-models" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars0.githubusercontent.com/u/24262836?s=400&amp;amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/ShahriarSS/vision/tree/update-c++-models" target="_blank" rel="nofollow noopener">ShahriarSS/vision</a></h3>

<p>Datasets, Transforms and Models specific to Computer Vision - ShahriarSS/vision</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>There is also a way to not use JIT.<br>
If it does not work, talk to me. I’ll tell you another way.</p>
<p><img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=9" title=":blush:" class="emoji" alt=":blush:"></p>

CF-chen-feng-CF: <p>I hope you can share another method with me, thank you.<img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=9" title=":blush:" class="emoji" alt=":blush:"></p>

CF-chen-feng-CF: <p>I hope you can share another method with me, thank you.<img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=9" title=":blush:" class="emoji" alt=":blush:"></p>

21fa417e3fb06e56040c: <p>you See here.<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/Kolkir/mlcpp/blob/master/mask_rcnn_pytorch/stateloader.h" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/Kolkir/mlcpp/blob/master/mask_rcnn_pytorch/stateloader.h" target="_blank" rel="nofollow noopener">Kolkir/mlcpp/blob/master/mask_rcnn_pytorch/stateloader.h</a></h4>
<pre><code class="lang-h">#ifndef STATELOADER_H
#define STATELOADER_H

#include &lt;torch/torch.h&gt;
#include &lt;vector&gt;

/* Correspondig Python export code
 * raw_state_dict = {}
 * for k, v in model.state_dict().items():
 *     if isinstance(v, torch.Tensor):
 *         raw_state_dict[k] = (list(v.size()), v.numpy().tolist())
 *         break
 *     else:
 *         print("State parameter type error : {}".format(k))
 *         exit(-1)
 *
 * with open('mask_rcnn_coco.json', 'w') as outfile:
 *     json.dump(raw_state_dict, outfile)
 */

</code></pre>

  This file has been truncated. <a href="https://github.com/Kolkir/mlcpp/blob/master/mask_rcnn_pytorch/stateloader.h" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>The method is as</p>
<ol>
<li>Python Model State JSON File Dump</li>
<li>Load libtorch JSON to parameter</li>
</ol>
<p>JSON file Dump Like that</p>
<pre><code class="lang-python">import torch
import torchvision
import torch.nn as nn
import torchvision.models as models
import json

resnet18 = torchvision.models.resnet18(True)

raw_state_dict = {}
for k, v in resnet18.state_dict().items():
	if isinstance(v, torch.Tensor):
		raw_state_dict[k] = (list(v.size()), v.numpy().tolist())
	else:
		print("State parameter type error : {}".format(k))
		exit(-1)


with open('resnet18.json', 'w') as outfile:
	json.dump(raw_state_dict, outfile,indent= "\t")
</code></pre>
<p>Load  JSON model c++</p>
<pre><code class="lang-auto">//you defined model
Network model;

LoadStateDictJson(model, "JSON File Path");

torch::save(model, "File path");
</code></pre>
<p>Caution you doing for  Define Network.<br>
All networks should be in Impl so that torch :: save works normally.</p>
<p>use to TORCH_MODULE</p>
<p>In a similar way to</p>
<pre><code class="lang-auto">#ifndef ALEXNET_H
#define ALEXNET_H

#include &lt;torch/torch.h&gt;

namespace vision {
namespace models {
// AlexNet model architecture from the
// "One weird trick..." &lt;https://arxiv.org/abs/1404.5997&gt; paper.
struct AlexNetImpl : torch::nn::Module {
  torch::nn::Sequential features{nullptr}, classifier{nullptr};

  AlexNetImpl(int64_t num_classes = 1000);

  torch::Tensor forward(torch::Tensor x);
};

TORCH_MODULE(AlexNet);

} // namespace models
} // namespace vision

#endif // ALEXNET_H
</code></pre>

Houssam_Eddine_Ben_S: <p>hello i got the same problem when i did torch.jit.script and load it to model in c++ it load without problem but forward pass is incorrect , dont know what to do next ,</p>