在PyTorch中轉換數據

In continuation of my previous post ,we will keep on deep diving into basic fundamentals of PyTorch. In this post we will discuss about ways to transform data in PyTorch.

延續我以前的 發布后 我們將繼續深入研究PyTorch的基本原理。 在這篇文章中,我們將討論在PyTorch中轉換數據的方法。

需要數據擴充 (Need for Data Augmentation)

Data augmentation is an approach that aids in increasing the variety of data for training models thus increasing the breadth of available information.The augmented data thus represents a more comprehensive set of possible data points. It improves the performance and ability of the model to generalize and hence addresses overfitting.

數據擴充是一種有助于增加訓練模型數據種類的方法,從而增加了可用信息的廣度。 它提高了模型的性能和泛化能力,從而解決了過擬合問題。

如何進行轉換? (How to perform transformations?)

torchvision module of PyTorch provides transforms to accord common image transformations. These transformations can be chained together using Compose.

PyTorch的torchvision模塊提供轉換以符合常見的圖像轉換。 可以使用Compose將這些轉換鏈接在一起。

  • transforms.Compose- Compose helps to bind multiple transforms together so we can use more than one transformation.

    transforms.Compose -Compose有助于將多個轉換綁定在一起,因此我們可以使用多個轉換。

Image for post

Multiple transformations have been chained using transforms.Compose and then it has been passed as an argument while loading dataset.

已使用transforms.Compose鏈接了多個轉換,然后在加載數據集時將其作為參數傳遞。

  • transforms.ToTensor — Applies a scaling operation of changing range from 0–255 to 0–1. It converts a PIL Image or numpy ndarray to a tensor (C x H x W) in the range of 0–1.

    transforms.ToTensor —應用縮放操作,將范圍從0–255更改為0–1。 它將PIL圖像或numpy ndarray轉換為范圍為0-1的張量(C xH x W)。

  • transforms.Normalize- This operation normalizes a tensor image with provided mean and standard deviation. For an image with 3 channels (RGB), 3 values for mean and 3 values for standard deviation are given as parameters(in form of tuple) corresponding to each channel.

    transforms.Normalize-此操作使用提供的均值和標準差對張量圖像進行歸一化。 對于具有3個通道(RGB)的圖像,將給出3個平均值和3個標準差值作為與每個通道相對應的參數(以元組的形式)。

Example :- transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) where first tuple is for mean and second for standard deviation.

示例: -transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))其中,第一個元組表示平均值,第二個元組表示標準偏差。

  • transforms.Pad — It pads given image on all sides with the given padding value. If a single integer value is provided for padding parameter then that value is used to pad all borders. If tuple of length 2 is provided then this acts as padding on left/right and top/bottom respectively. For constant padding mode default value for fill 0 is used.

    transforms.Pad —它使用給定的填充值在所有面上填充給定的圖像。 如果為填充參數提供單個整數值,則該值將用于填充所有邊界。 如果提供長度為2的元組,則這分別充當左側/右側和頂部/底部的填充。 對于恒定填充模式 ,使用填充 0的默認值。

Example :- torchvision.transforms.Pad(10)

示例: -torchvision.transforms.Pad(10)

Image for post
Using torchvision.transforms.Pad(10)使用torchvision.transforms.Pad(10)

You can notice the output size of the transformed image.

您會注意到轉換后的圖像的輸出大小

Using image from FMNIST dataset, used in my previous post. Image size for this dataset is 28*28 .

使用 我以前的 文章中 使用的 FMNIST數據集中的 圖像 該數據集的圖像大小為28 * 28。

  • transforms.RandomHorizontalFlip — Flipping operation helps in changing the orientation of the image. RandomHorizontalFlip changes the orientation horizontally similarly we can use RandomVerticalFlip for changing vertical orientation. It flips the image randomly with a given probability (p).

    transforms.RandomHorizo??ntalFlip —翻轉操作有助于更改圖像的方向。 類似地,我們可以使用RandomVerticalFlip來改變垂直方向。 它以給定的概率(p)隨機翻轉圖像。

Example :- torchvision.transforms.RandomHorizontalFlip(p=1)

示例: -torchvision.transforms.RandomHorizo??ntalFlip(p = 1)

Image for post
Using torchvision.transforms.RandomHorizontalFlip(p=1)使用torchvision.transforms.RandomHorizo??ntalFlip(p = 1)
  • transforms.CenterCrop- We can do cropping of an image using this transformation. CenterCrop crops the given image at the center as per the size parameter. Crop can be square or rectangle in shape depending on the size parameter dimensions. Similarly, we have RandomCrop that crops the given image at a random location.

    transforms.CenterCrop-我們可以使用此變換來裁剪圖像。 CenterCrop根據大小參數在中心裁剪給定圖像。 作物的形狀可以是正方形或矩形,具體取決于大小參數的尺寸。 同樣,我們有RandomCrop可以在任意位置裁剪給定圖像。

Example :- torchvision.transforms.CenterCrop((18, 18))

示例: -torchvision.transforms.CenterCrop((18,18))

Image for post
Using 使用 torchvision.transforms.CenterCrop((18, 18))torchvision.transforms.CenterCrop((18,18))
  • transforms.Resize —To resize image this transformation can be used. It is also very useful incase of images with large dimensions to reduce it to a particular size (parameter for desired output size) . By resizing, the resolution can be lowered and thus will help in training the network faster.

    transforms.Resize —要調整圖像的大小,可以使用此變換。 在具有大尺寸的圖像的情況下將其減小到特定尺寸 (用于期望的輸出尺寸的參數)也是非常有用的。 通過調整大小,可以降低分辨率,從而有助于更快地訓練網絡。

Example :- torchvision.transforms.Resize((300, 300))

范例: -torchvision.transforms.Resize((300,300))

Image for post
Using torchvision.transforms.Resize((300, 300))使用torchvision.transforms.Resize((300,300))
  • transforms.RandomRotation- To rotate an image by certain degrees (parameter). If degrees is an integer rather than (min, max) then the range is interpreted as (-degrees, +degrees).

    transforms.RandomRotation-將圖像旋轉一定程度 (參數)。 如果度數是整數而不是(min,max),則該范圍將解釋為(-度數+ +度數)。

Example :- torchvision.transforms.RandomRotation(degrees=(180))

范例: -torchvision.transforms.RandomRotation(degrees =(180))

Image for post
Using torchvision.transforms.RandomRotation(degrees=(180))使用torchvision.transforms.RandomRotation(degrees =(180))
  • transforms.ColorJitter- It helps to change the brightness, contrast and saturation of an image.

    transforms.ColorJitter-它有助于 更改圖像的亮度,對比度和飽和度。

Apart from these above mentioned transformations, you can refer to full list here.

除了上述轉換之外,您還可以在此處參考完整列表

最后的想法 (Final Thoughts)

It is not always necessary to use multiple augmentations all at once, it is more of data dependant process.We need to be careful while using these transformations . For example, with crop operation if the crops are too small, we might be at risk of cutting out important parts of the image and making the model train on the wrong thing. For instance, if a dog is playing near a tree and the crop takes out the dog and just leaves part of the tree to be classified as dog that may create issues.

不一定總是一次使用多個擴充,更多的是依賴于數據的過程。在使用這些轉換時,我們需要小心。 例如,如果作物太小,則使用作物操作時,我們可能會面臨切掉圖像重要部分并使模型針對錯誤的事物進行訓練的風險。 例如,如果一只狗在樹附近玩耍,而莊稼將狗拔出,只留下部分樹被分類為狗,這可能會造成問題。

Apart from torchvision.transforms we can explore albumentations library too for deep learning image augmentation.

除了torchvision.transforms我們可以探索albumentations庫過深學習圖像增強。

翻譯自: https://medium.com/analytics-vidhya/transforming-data-in-pytorch-741fab9e008c

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388932.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388932.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388932.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

「網絡流24題」試題庫問題

傳送門&#xff1a;>Here< 題意&#xff1a;有K種類型的共N道試題用來出卷子&#xff0c;要求卷子須有M道試題。已知每道題屬于p種類型&#xff0c;每種類型的試題必須有且僅有k[i]道。現問出這套試卷的一種具體方案 思路分析 昨天打了一天的Dinic&#xff0c;今天又打了…

機器學習實踐六---K-means聚類算法 和 主成分分析(PCA)

在這次練習中將實現K-means 聚類算法并應用它壓縮圖片&#xff0c;第二部分&#xff0c;將使用主成分分析算法去找到一個臉部圖片的低維描述。 K-means Clustering Implementing K-means K-means算法是一種自動將相似的數據樣本聚在一起的方法,K-means背后的直觀是一個迭代過…

航海家軟件公式全破解

水手突破 上趨勢:MA(LOW,20)*1.2,color0080ff,linethick2;次上趨勢:MA(LOW,20)*1.1,COLORYELLOW;次下趨勢:MA(HIGH,20)*0.9,COLORWHITE;下趨勢:MA(HIGH,20)*0.8,COLORGREEN,linethick2;ZD:(C-REF(C,1))/REF(C,1)*100;HDZF:(HHV(H,20)-C)/(HHV(H,20)-LLV(L,20));趨勢強度:IF(C&g…

打包 壓縮 命令tar zip

2019獨角獸企業重金招聘Python工程師標準>>> 打包 壓縮 命令tar zip tar語法 #壓縮 tar -czvf ***.tar.gz tar -cjvf ***.tar.bz2 #解壓縮 tar -xzvf ***.tar.gz tar -xjvf ***.tar.bz2 tar [主選項輔選項] 文件或目錄 主選項是必須要有的&#xff0c;它告訴tar要做…

mysql免安裝5.7.17_mysql免安裝5.7.17數據庫配置

首先要有 mysql-5.7.10-winx64環境: mysql-5.7.10-winx64 win10(64位)配置環境變量&#xff1a;1、把mysql-5.7.10-winx64放到D盤&#xff0c;進入D\mysql-5.7.10-winx64\bin目錄&#xff0c;復制路徑&#xff0c;配置環境變量&#xff0c;在path后面添加D\mysql-5.7.10-winx6…

tidb數據庫_異構數據庫復制到TiDB

tidb數據庫This article is based on a talk given by Tianshuang Qin at TiDB DevCon 2020.本文基于Tianshuang Qin在 TiDB DevCon 2020 上的演講 。 When we convert from a standalone system to a distributed one, one of the challenges is migrating the database. We’…

機器學習實踐七----異常檢測和推薦系統

Anomaly detection 異常檢測是機器學習中比較常見的應用&#xff0c;它主要用于非監督學習問題&#xff0c;從某些角度看&#xff0c; 它又類似于一些監督學習問題。 什么是異常檢測&#xff1f;來看幾個例子&#xff1a; 例1. 假設是飛機引擎制造商&#xff0c; 要對引擎進行…

CODE[VS] 1621 混合牛奶 USACO

題目描述 Description牛奶包裝是一個如此低利潤的生意,所以盡可能低的控制初級產品(牛奶)的價格變的十分重要.請幫助快樂的牛奶制造者(Merry Milk Makers)以可能的最廉價的方式取得他們所需的牛奶.快樂的牛奶制造公司從一些農民那購買牛奶,每個農民賣給牛奶制造公司的價格不一定…

apply和call用法

apply語法 func.apply(name, [array])第一個參數指定函數體內this對象的指向&#xff0e;第二個參數為一個帶下標的集合&#xff0c;可以是數組或類數組,apply方法把這個集合中的元素作為參數傳遞給被調用的函數var func function(a, b, c) {console.log([a, b, c]); // [1,2,…

剛認識女孩說不要浪費時間_不要浪費時間尋找學習數據科學的最佳方法

剛認識女孩說不要浪費時間重點 (Top highlight)Data science train is moving, at a constantly accelerating speed, and increasing its length by adding up new coaches. Businesses want to be on the data science train to keep up with the ever-evolving technology a…

測試工具之badboy

badboy這個工具本身用處不是很大&#xff0c;但有個錄制腳本的功能&#xff0c;還是jmeter腳本&#xff0c;所以針對這一點很多懶人就可以通過這個錄制腳本&#xff0c;而不需要自己去編寫 badboy工具最近還是2016年更新的&#xff0c;后面也沒在更新了&#xff0c;官方下載地址…

hive 集成sentry

2019獨角獸企業重金招聘Python工程師標準>>> 環境 apache-hive-2.3.3-bin apache-sentry-2.1.0-bin 1 2 sentry是目前最新的版本&#xff0c;支持hive的最高版本為2.3.3&#xff0c;hive版本如果高于2.3.3&#xff0c;會出一些版本兼容問題[親測] hive快速安裝 wget…

word模板生成word報表文檔

主要功能為根據word模板生成word報表文檔,注意引用Interop.Word.dll;首先要生成word程序對象Word.Application app new Word.Application();根據模板文件生成新文件框架File.Copy(TemplateFile, FileName);生成documnet對象ord.Document doc new Word.Document(); 打開…

isql 測試mysql連接_[libco] 協程庫學習,測試連接 mysql

歷史原因&#xff0c;一直使用 libev 作為服務底層&#xff1b;異步框架雖然性能比較高&#xff0c;但新人學習和使用門檻非常高&#xff0c;而且串行的邏輯被打散為狀態機&#xff0c;這也會嚴重影響生產效率。用同步方式實現異步功能&#xff0c;既保證了異步性能優勢&#x…

什么是數據倉庫,何時以及為什么要考慮一個

The term “Data Warehouse” is widely used in the data analytics world, however, it’s quite common for people who are new with data analytics to ask the above question.術語“數據倉庫”在數據分析領域中被廣泛使用&#xff0c;但是&#xff0c;對于數據分析新手來…

安裝好MongoDB,但服務中沒有MongoDB服務的解決辦法

以管理員身份打開CMD&#xff0c;添加路徑添加服務即可 winX 然后再選Amongod -dbpath "D:\MongoDB\Server\3.6\data\db" -logpath "D:\MongoDB\Server\3.6\data\log\mongo.log" -install -serviceName "MongoDB"轉載于:https://www.cnblogs.com…

DRF數據驗證+數據存儲

1.驗證數據的自定義類 class BooksDRFt(serializers.ModelSerializer):class Meta:model Bookfields __all__#要驗證的字段author serializers.CharField(requiredFalse)#要驗證的字段name serializers.CharField(min_length2, error_messages{required: 不能為空, min_len…

mysql變量 exec_MySQL slave_exec_mode 參數說明

背景&#xff1a;今天無意當中看到參數slave_exec_mode&#xff0c;從手冊里的說明看出該參數和MySQL復制相關&#xff0c;是可以動態修改的變量&#xff0c;默認是STRICT模式(嚴格模式)&#xff0c;可選值有IDEMPOTENT模式(冪等模式)。設置成IDEMPOTENT模式可以讓從庫避免1032…

C#word

主要功能為根據word模板生成word報表文檔,注意引用Interop.Word.dll;首先要生成word程序對象Word.Application app new Word.Application();根據模板文件生成新文件框架File.Copy(TemplateFile, FileName);生成documnet對象ord.Document doc new Word.Document(); 打開…

機器學習kaggle競賽實戰-泰坦尼克號

數據展示 首先登kaggle 下載泰坦尼克訓練相關數據 import pandas as pd import numpy as np data pd.read_csv(train.csv) print(data.shape) print(data.head) train data[:800] test data[800:] print(train.shape) print(test.shape)選擇特征 selected_features [Pcl…