矩阵和tensor相互转换

cvmat到tensor

tips:这里主要要注意的就是在opencv和pytorch中存储顺序的差异

 cv::cvtColor(frame, frame, CV_BGR2RGB);
 //normalization
 frame.convertTo(frame, CV_32FC3, 1.0f / 255.0f);
 //opencv format H*W*C
 auto input_tensor = torch::from_blob(frame.data, {1, frame_h, frame_w, kCHANNELS});
 //pytorch format N*C*H*W
 input_tensor = input_tensor.permute({0, 3, 1, 2});

tensor 到cvmat

tips:1.squeeze只用于batchsize为1的场景2.permute 是将存储格式从pytorch形式转成opencv格式3.因为在处理前对cvmat中的值做了归一化,所以现在要*255恢复,同时对于不在0-255范围内的数据,需要做限制4.因为cvmat的数据格式是8UC3,所以torch tensor要提前转换成kU8

//send tensor to cpu
input_tensor = input_tensor.to(at::kCUDA);
//inference
torch::Tensor out_tensor = module->forward({input_tensor}).toTensor();
//sequeeze trans tensor shape from 1*C*H*W to C*H*W
//permute C*H*W to H*W*C
out_tensor = out_tensor.squeeze().detach().permute({1, 2, 0});
//see tip3,tip4
out_tensor = out_tensor.mul(255).clamp(0, 255).to(torch::kU8);
out_tensor = out_tensor.to(torch::kCPU);
cv::Mat resultImg(frame_h, frame_w, CV_8UC1);
//copy the data from out_tensor to resultImg
std::memcpy((void *) resultImg.data, out_tensor.data_ptr(), sizeof(torch::kU8) * out_tensor.numel());

如有错误,感谢指正!


2020/01/14更新

1.tensor 到 cv::Mat 修改一处错误

memcpy 复制过程,resultImg对应的数据结构应该为CV_8UC1。

2.对于非uint类型的数据类型转换,例如float、double,需做以下两点修改:

1)resultImg需修改成其他opencv的数据结构

2)std::memcpy 行中sizeof()中内容,需要修改成c++中内建的数据类型,如果使用torch::kF32或者其他浮点型,会出现数据复制缺失的情况。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐