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有助于將多個轉換綁定在一起,因此我們可以使用多個轉換。

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)

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)

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

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

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

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,一經查實,立即刪除!