MCPcopy Create free account
hub / github.com/AllentDan/LibtorchTutorials / loadPretrained

Method loadPretrained

lesson7-Detection/src/Detector.cpp:53–91  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

51
52
53void Detector::loadPretrained(std::string pretrained_pth) {
54 auto net_pretrained = YoloBody_tiny(3, 80);
55 torch::load(net_pretrained, pretrained_pth);
56 if (this->name_list.size() == 80)
57 {
58 detector = net_pretrained;
59 }
60
61 torch::OrderedDict<std::string, at::Tensor> pretrained_dict = net_pretrained->named_parameters();
62 torch::OrderedDict<std::string, at::Tensor> model_dict = detector->named_parameters();
63
64
65 for (auto n = pretrained_dict.begin(); n != pretrained_dict.end(); n++)
66 {
67 if (strstr((*n).key().c_str(), "yolo_head")) {
68 continue;
69 }
70 model_dict[(*n).key()] = (*n).value();
71 }
72
73 torch::autograd::GradMode::set_enabled(false); // make parameters copying possible
74 auto new_params = model_dict; // implement this
75 auto params = detector->named_parameters(true /*recurse*/);
76 auto buffers = detector->named_buffers(true /*recurse*/);
77 for (auto& val : new_params) {
78 auto name = val.key();
79 auto* t = params.find(name);
80 if (t != nullptr) {
81 t->copy_(val.value());
82 }
83 else {
84 t = buffers.find(name);
85 if (t != nullptr) {
86 t->copy_(val.value());
87 }
88 }
89 }
90 torch::autograd::GradMode::set_enabled(true);
91}
92
93inline bool does_exist(const std::string& name) {
94 struct stat buffer;

Callers

nothing calls this directly

Calls 7

beginMethod · 0.80
endMethod · 0.80
c_strMethod · 0.80
sizeMethod · 0.45
keyMethod · 0.45
valueMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected