| 51 | |
| 52 | |
| 53 | void 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 | |
| 93 | inline bool does_exist(const std::string& name) { |
| 94 | struct stat buffer; |