It seems that cv::Mat --> tensor works fine, but tensor --> cv::Mat gives strange results
cv::Mat toMat(torch::Tensor& tensor, cv::Size size, int matType) //tensor must be C * H * W
{
   auto det = tensor.detach();
   det *= 255;
   det = det.to(torch::kByte).cpu().contiguous();

   std::vector<cv::Mat> vm;
   for (int i = 0; i < det.size(0); i++)
   {
      auto yo = det[i];
      uchar* data = (uint8_t*)(yo.data_ptr());
      //注意这里的data的作用域
      cv::Mat mr(300, 200, CV_8UC1, data); //return only first channel
      return mr;
   }
}

torch::Tensor toTensor(cv::Mat& img) // img must be CV_32F
{
   auto img_t = torch::zeros({ img.rows, img.cols, img.channels() });
   //stride=width*channels
	memcpy(img_t.data_ptr(), img.data, img_t.numel() * sizeof(float));
	img_t = img_t.permute({ 2, 0, 1 });
   return img_t.clone();
}

It seems it has to do something with stride/step, because the rows are misaligned