Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use transforms for Pytorch

Shulou Source: shulou.com Published: 2022-06-02 05:18:09 10月03日 Update

This article mainly introduces Pytorch how to use transforms, the article is very detailed, has a certain reference value, interested friends must read it!

First of all, the tansforms function of this explanation is, generally speaking, similar to the data enhancement in the image preprocessing part of the computer vision process.

The principle of transforms:

Description: the picture (input) gets the result (output) through the tool, this tool is the transforms template tool, (tool=transforms.ToTensor () specific tool), using the tool result=tool (picture)

The call and use of tansforms can be obtained from the following figure:

First create a transforms.Tensor () and switch packets using from torchvision import transforms

Transforms demodulates init function

Init calls the real transforms class, and there are a lot of methods (green pentagram annotations), such as resize,ToTensor,CenterCrop (as you can see from these methods, many of them are data enhancement methods).

Next, go to the code:

Import osfrom torchvision import transformsfrom PIL import Imageroot_path = "D:\ data\\ basic\\ Image" label_path = "aligned" # 1. Get the name of the first picture of aligned img_dir = os.path.join (root_path, label_path) img_list = os.listdir (img_dir) img_path = img_list [0] # 2. Get the path of the first graph of aligned img = os.path.join (root_path, label_path, img_path) # 3. Use the PIL that comes with python to get the picture img = Image.open (img) # 4. Convert PIL to ToTensorto_tensor = transforms.ToTensor () # to create totensor () img = to_tensor (img) # convert PIL of a picture directly to tensorprint (img) # transforms using to_tensor

Code result:

The above is all the contents of the article "how Pytorch uses transforms". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Tools pictures methods content data articles results popular next previous generation next picture pentagram code value interest function function principle name image Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology Microsoft Redmi MySQL