pytorch transform數(shù)據(jù)處理轉(zhuǎn)c++問題
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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Windows平臺Python連接sqlite3數(shù)據(jù)庫的方法分析
這篇文章主要介紹了Windows平臺Python連接sqlite3數(shù)據(jù)庫的方法,結(jié)合實例形式分析了Windows平臺安裝SQLite數(shù)據(jù)庫及創(chuàng)建、連接數(shù)據(jù)庫的實現(xiàn)方法與相關(guān)注意事項,需要的朋友可以參考下2017-07-07
Python pyautogui模塊實現(xiàn)鼠標(biāo)鍵盤自動化方法詳解
這篇文章主要介紹了Python pyautogui 模塊實現(xiàn)鼠標(biāo)鍵盤自動化方法詳解,需要的朋友可以參考下2020-02-02
django admin search_fields placeholder 管理后臺添加搜索框提示文字
這篇文章主要介紹了django admin search_fields placeholder 管理后臺添加搜索框提示文字,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
python 和c++實現(xiàn)旋轉(zhuǎn)矩陣到歐拉角的變換方式
今天小編就為大家分享一篇python 和c++實現(xiàn)旋轉(zhuǎn)矩陣到歐拉角的變換方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python學(xué)習(xí)筆記嵌套循環(huán)詳解
這篇文章主要介紹了Python學(xué)習(xí)筆記嵌套循環(huán)詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07
Python基礎(chǔ)之列表常見操作經(jīng)典實例詳解
這篇文章主要介紹了Python基礎(chǔ)之列表常見操作,結(jié)合實例形式詳細(xì)分析了Python列表創(chuàng)建方式、內(nèi)置函數(shù)與相關(guān)使用技巧,需要的朋友可以參考下2020-02-02

