you See here.

  
      github.com
  
  
    Kolkir/mlcpp/blob/master/mask_rcnn_pytorch/stateloader.h 37
#ifndef STATELOADER_H
#define STATELOADER_H

#include <torch/torch.h>
#include <vector>

/* 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)
 */



  This file has been truncated. show original

  
  
    
    
  
  


The method is as

Python Model State JSON File Dump
Load libtorch JSON to parameter

JSON file Dump Like that
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")

Load  JSON model c++
//you defined model
Network model;

LoadStateDictJson(model, "JSON File Path");

torch::save(model, "File path");

Caution you doing for  Define Network.
All networks should be in Impl so that torch :: save works normally.
use to TORCH_MODULE
In a similar way to
#ifndef ALEXNET_H
#define ALEXNET_H

#include <torch/torch.h>

namespace vision {
namespace models {
// AlexNet model architecture from the
// "One weird trick..." <https://arxiv.org/abs/1404.5997> 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