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

   2023-02-09 学习力0
核心提示:目录pytorch transform数据处理转c++1.python代码2.transforms.Resize(256)3.transforms.ToTensor()总结pytorch transform数据处理转c++python推理代码转c++ sdk过程遇到pytorch数据处理的转换1.python代码import torchfrom PIL import Imagefrom torchvision

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举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • Python源码加密与Pytorch模型加密分别介绍
    目录前言一、python源代码的保护二、pytorch模型.pth的加密前言深度学习领域,常常用python写代码,而且是建立在一些开源框架之上,如pytorch。在实际的项目部署中,也有用conda环境和python代码去部署服务器,在这个时候,又分为两种情况。部署方式可分为两
  • 迁移pytorch工程至matlab
    好久没写博客了,险些以为自己找不到密码了。最近抽空参与了个小项目,很惭愧,只做了三件小事1. 基于PyTorch训练了一系列单图像超分辨神经网络 基于PyTorch训练了一系列单图像超分辨神经网络,超分辨系数从2-10。该部分的实现参考了pytorch官方repo中的SR例
    02-08
点击排行