pytorch transform数据处理转c++问题

作者:young_s% 时间:2023-08-19 11:24:49 

pytorch transform数据处理转c++

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)

2.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).

3.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;
}

来源:https://blog.csdn.net/weixin_45331269/article/details/123175490

标签:pytorch,transform,c++
0
投稿

猜你喜欢

  • 安装SQL Server 2005时出现计数器错误

    2008-11-28 14:19:00
  • Centos部署django服务nginx+uwsgi的方法

    2022-03-18 07:47:26
  • 用JavaScript判断字符串长度

    2009-10-29 12:15:00
  • 在Python中使用M2Crypto模块实现AES加密的教程

    2022-09-29 17:43:59
  • python新手经常遇到的17个错误分析

    2021-08-09 21:46:00
  • Go语言题解LeetCode35搜索插入位置示例详解

    2023-07-16 17:17:00
  • 在js中的replace方法详解

    2007-08-21 15:47:00
  • 在pytorch 中计算精度、回归率、F1 score等指标的实例

    2022-08-10 06:28:18
  • 使用SqlServer CTE递归查询处理树、图和层次结构

    2024-01-16 07:35:42
  • Python Django给admin添加Action的方法实例详解

    2023-10-30 13:11:35
  • Python实现关键路径和七格图计算详解

    2022-04-25 12:17:46
  • 如何基于python实现不邻接植花

    2023-10-14 16:35:45
  • python学习之基于Python的人脸识别技术学习

    2021-04-23 22:09:25
  • python输出第n个默尼森数的实现示例

    2022-08-12 21:07:25
  • 对python调用RPC接口的实例详解

    2022-12-30 00:09:20
  • python标准库random模块处理随机数

    2023-11-23 16:22:49
  • 纯CSS下拉菜单代码

    2008-09-10 12:35:00
  • python之关于数组和列表的区别及说明

    2021-07-29 17:35:31
  • JS重现80后儿时经典拼板(模板)游戏

    2011-09-11 18:36:46
  • Django中使用 Closure Table 储存无限分级数据

    2021-05-25 03:05:07
  • asp之家 网络编程 m.aspxhome.com