網(wǎng)站首頁 編程語言 正文
pytorch transform數(shù)據(jù)處理轉(zhuǎn)c++
python推理代碼轉(zhuǎn)c++ sdk過程遇到pytorch數(shù)據(jù)處理的轉(zhuǎn)換
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;
}
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_45331269/article/details/123175490
相關推薦
- 2022-08-28 Go讀寫鎖操作方法示例詳解_Golang
- 2022-09-03 .NET使用System.Timers.Timer類實現(xiàn)程序定時執(zhí)行_實用技巧
- 2023-02-02 Python實現(xiàn)自動生成請假條_python
- 2022-07-28 numpy?array找出符合條件的數(shù)并賦值的示例代碼_python
- 2024-07-18 Spring ioc容器與Bean的生命周期。
- 2022-04-17 Specified key was too long; max key length is 767
- 2022-11-09 Python數(shù)據(jù)庫sqlite3圖文實例詳解_python
- 2023-07-04 Linux直接創(chuàng)建SSH無密碼連接
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支