Pytorch weight(.pth) port to Libtorch weight(.pt)

DongkyuLee: <p>Hello guys.</p>
<p>I used to use C++ when I code something.</p>
<p>So, I prefer to make Libtorch(frontend version, not torch::jit::script) to implement deep learning.<br>
(I make Libtorch frontend model refer to original python(Pytorch) code, and make all Libtorch frontend model’s submodule names are same with Pytorch model’s submodule.)</p>
<p>I have trained model without pre-trained, if I use my own dataset.</p>
<p>But, now I would like to use pre-trained weight files, such as what was trained with KITTI dataset.</p>
<p>Most of pre-trained is as “.pth”(Pytorch), but I want to port it to “.pt” that can be loaded with Libtorch frontent model.<br>
(e.g., torch::load(model, “pre-trained.pt”)<img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/6/a621c4ff635d66db7ac7969f29f976d9850add31.png" data-download-href="https://discuss.pytorch.org/uploads/default/a621c4ff635d66db7ac7969f29f976d9850add31" title="Screenshot from 2023-04-05 17-25-37"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/6/a621c4ff635d66db7ac7969f29f976d9850add31_2_690x109.png" alt="Screenshot from 2023-04-05 17-25-37" data-base62-sha1="nHFBSxfml0eqLiv77AZd9v2k8Qp" width="690" height="109" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/6/a621c4ff635d66db7ac7969f29f976d9850add31_2_690x109.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/6/a621c4ff635d66db7ac7969f29f976d9850add31_2_1035x163.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/6/a621c4ff635d66db7ac7969f29f976d9850add31_2_1380x218.png 2x" data-dominant-color="DFDFDF"><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 from 2023-04-05 17-25-37</span><span class="informations">1847×293 46.1 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>
<p>In figure, left is Pytorch version weight, right is Libtorch frontend version weight, configurations are different, but they are same at layer counts and submodule name.</p>
<p>Please let me know how to convert .pth to .pt(Please note It isn’t torch::jit:script module based).</p>
<p>Thanks.</p>

dfalbel: <p>How are you saving the model in PyTorch. Are you saving the entire module or just the <code>state_dict()</code>?<br>
I think you might want to do torch.save(model.state_dict())` so you don’t get that code directory.</p>

DongkyuLee: <p>I am using as torch.save(model.state_dict()) at Python, but they don’t have constant.pkl in pth file.</p>
<p>It outputs “terminate called after throwing an instance of ‘c10::Error’  what():  PytorchStreamReader failed locating file constants.pkl: file not found”, when I load .pth using Libtorch frontend(torch::load()).</p>
<p>Thanks.</p>

Gerald_Lodron: <p>did you solved that issue? i ran in the exactly the same issue</p>