由pth转化为pt:
import torch
print(torch.cuda.is_available())
from nets.segformer import SegFormer
ckptfile=“/home/nslidar/Downloads/segformer-pytorch-master/logs/best_epoch_weights.pth”
savedfile=“/home/nslidar/Documents/C++_weight/human_seg.pt111”
net = SegFormer(num_classes=3, phi=“b0”, pretrained=False)
device = torch.device(‘cuda’)
net.load_state_dict(torch.load(ckptfile, map_location=device))
net=net.eval()
x = torch.rand(1,3,512,512)
ts = torch.jit.trace(net, x)
ts.save(savedfile)
C++调用语义分割模型pt,为什么效果反差这么大:
#include <torch/torch.h>
#include <torch/script.h>
#include <iostream>
#include <vector>
#include <string>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include<ctime>
std::string infer(std::string, torch::jit::script::Module);
clock_t start,end;
void Visualization(cv::Mat prediction_map, std::string LUT_file) {
cv::cvtColor(prediction_map.clone(), prediction_map, cv::COLOR_GRAY2BGR);
cv::Mat label_colours = cv::

本文介绍了如何将PyTorch的SegFormer模型转换为pt文件,并在C++环境中进行调用。通过示例代码展示了模型加载和状态字典的迁移过程,同时也提出了在C++调用后模型效果反差较大的问题。
最低0.47元/天 解锁文章
759





