pytorch transform数据处理转c++

14 篇文章 0 订阅
8 篇文章 0 订阅

python推理代码转c++ sdk过程遇到pytorch数据处理的转换

  1. python代码
import torch
from PIL import Image
from torchvision import transforms

data_transform = transforms.Compose(
     [transforms.Resize(256),
      transforms.CenterCrop(224),
      transforms.ToTensor(),
      transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

 img = Image.open(img_path)
 img = data_transform(img)
    
  1. transforms.Resize(256)

Parameters
size (sequence or int) –
Desired output size. If size is a sequence like (h, w), output size will be matched to this. If size is an int, smaller edge of the image will be matched to this number. i.e, if height > width, then image will be rescaled to (size * height / width, size).

  1. transforms.ToTensor()

Convert a PIL Image or numpy.ndarray to tensor. This transform does not support torchscript.
Converts a PIL Image or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0] if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1) or if the numpy.ndarray has dtype = np.uint8


cv::Mat ClsSixPrivate::processImage(cv::Mat &img) {
	int inW = img.cols;
	int inH = img.rows;
	cv::Mat croped_image;
	if (inW > inH)
	{
		int newWidth = 256 * inW / inH;
		cv::resize(img, img, cv::Size(newWidth, 256), 0, 0, cv::INTER_LINEAR);
		croped_image = img(cv::Rect((newWidth - 224) / 2, 16, 224, 224)).clone();
	}
	else {
		int newHeight= 256 * inH / inW;
		cv::resize(img, img, cv::Size(256, newHeight), 0, 0, cv::INTER_LINEAR);
		croped_image = img(cv::Rect(16, (newHeight - 224) / 2, 224, 224)).clone();
	}
	
	std::vector<float> mean_value{ 0.485, 0.456,0.406 };
	std::vector<float> std_value{ 0.229, 0.224, 0.225 }; 
	cv::Mat dst;
	std::vector<cv::Mat> rgbChannels(3);
	cv::split(croped_image, rgbChannels);

	for (auto i = 0; i < rgbChannels.size(); i++)
	{
		rgbChannels[i].convertTo(rgbChannels[i], CV_32FC1, 1.0 / (std_value[i] * 255.0), (0.0 - mean_value[i]) / std_value[i]);
	}

	cv::merge(rgbChannels, dst);
	return dst;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值