model在IOS上加载出错提示“parse magic failed”
lmq5294249 opened this issue · 0 comments
Hi,
The code is
int SkySegmentation::load(const char* paramString, const char* binString, unsigned int _num_threads, int _input_width, int _input_height)
{
net.clear();
net.opt.use_vulkan_compute = false;
net.load_param(paramString);
net.load_model(binString);
input_width = _input_width;
input_height = _input_height;
num_threads = _num_threads;
return 0;
}
void SkySegmentation::transform(const cv::Mat &mat_rs, ncnn::Mat &in)
{
// BGR NHWC -> RGB NCHW
in = ncnn::Mat::from_pixels(mat_rs.data, ncnn::Mat::PIXEL_BGR2RGB, input_width, input_height);
in.substract_mean_normalize(mean_vals, norm_vals);
}
void SkySegmentation::detect(cv::Mat &rgb, cv::Mat &mask,)
{
if (rgb.empty()) return;
int img_height = static_cast(rgb.rows);
int img_width = static_cast(rgb.cols);
ncnn::Mat input;
this->transform(rgb, input);
ncnn::Extractor ex = net.create_extractor();
ex.set_light_mode(true);
ex.set_num_threads(4);
ex.input("input.1", input);
ncnn::Mat out;
ex.extract("1959", out);
//cv::Mat opencv_mask(out.h, out.w, CV_32FC1);
//memcpy((uchar*)opencv_mask.data, out.data, out.w * out.h * sizeof(float));
//cv::resize(opencv_mask,opencv_mask,cv::Size(w,h),cv::INTER_LINEAR);
float *pha_data = (float*)out.data;
cv::Mat cv_mask = cv::Mat::zeros(out.h, out.w, CV_8UC1);
cv::Mat cv_pha = cv::Mat(out.h, out.w, CV_32FC1, pha_data);
cv_pha.convertTo(cv_mask, CV_8UC1, 255.0, 0);
cv_mask.copyTo(mask);
}
当我运行上面的代码时提示 parse magic failed
加载model代码
NSString *parmaPath = [[NSBundle mainBundle] pathForResource:@"skysegsmall_sim-opt-fp16" ofType:@"param"];
NSString *binPath = [[NSBundle mainBundle] pathForResource:@"skysegsmall_sim-opt-fp16" ofType:@"bin"];
skySegmentation = new SkySegmentation;
skySegmentation->load([parmaPath UTF8String], [binPath UTF8String]);
我检查了代码发现在 net.load_param(binString); 时有问题。是不是.bin文件有问题?
我测试其他的model在demo上加载运行正常。