当前位置:网站首页>Transformations of pytorch torch torch vision
Transformations of pytorch torch torch vision
2022-06-25 05:27:00 【HheeFish】
pytorch torchvision transform: Common data preprocessing methods
Partial reference :
PyTorch Learning notes ( 3、 ... and ):transforms Twenty two ways to
pytorch torchvision transform
1. Yes PIL.Image To transform
1.1. Combine
transform:class torchvision.transforms.Compose(transforms)
Will be multiple transform
Use in combination .transforms
: from transform
The list of components . Example :
transforms.Compose([
transforms.CenterCrop(10),
transforms.ToTensor(),
])
1.2. Reshape size
class torchvision.transforms.Scale(size, interpolation=2)
Will input PIL.Image
Resize to a given size size
,size
Is the side length of the smallest side . for instance , If the original height>width
, So the size of the picture after changing the size is (size*height/width, size)
.
Use cases :
from torchvision import transforms
from PIL import Image
crop = transforms.Scale(12)
img = Image.open('test.jpg')
print(type(img))
print(img.size)
croped_img=crop(img)
print(type(croped_img))
print(croped_img.size)
<class 'PIL.PngImagePlugin.PngImageFile'>
(10, 10)
<class 'PIL.Image.Image'>
(12, 12)
1.3. Center cut
class torchvision.transforms.CenterCrop(size)
Will be given PIL.Image
Make a center cut , Get the given size
,size
It can be tuple
,(target_height, target_width)
.size
It can also be a Integer
, under these circumstances , The shape of the cut image is square .
1.4. Random center cut
class torchvision.transforms.RandomCrop(size, padding=0)
The position of the cutting center point is randomly selected .size
It can be tuple
It can also be Integer
.
1.5. Random horizontal flip
class torchvision.transforms.RandomHorizontalFlip(p=0.5)
Randomly flip a given... Horizontally PIL.Image
, The probability of 0.5. namely : Half the chance to flip , Half the probability doesn't flip .
1.6. Cut at random + Reshape size
class torchvision.transforms.RandomSizedCrop(size, interpolation=2)
First, the given PIL.Image
Random cut , And then again resize
Into a given size
size .
1.7. fill
class torchvision.transforms.Pad(padding, fill=0)
Will be given PIL.Image
All the edges of a given pad value
fill .
padding
: How many pixels to fillfill
: What values to fill in
Example :
from torchvision import transforms
from PIL import Image
padding_img = transforms.Pad(padding=10, fill=0)
img = Image.open('test.jpg')
print(type(img))
print(img.size)
padded_img=padding(img)
print(type(padded_img))
print(padded_img.size)
<class 'PIL.PngImagePlugin.PngImageFile'>
(10, 10)
<class 'PIL.Image.Image'>
(30, 30) # Because it has to be filled up, down, left and right 10 Pixel , So after filling size yes (30,30)
1.8. Random vertical flip
class torchvision.transforms.RandomVerticalFlip(p=0.5)
Randomly flip the given... Vertically PIL.Image
, The probability of 0.5. namely : Half the chance to flip , Half the probability doesn't flip .
1.9. Random rotation
class torchvision.transforms.Rotation(degrees, resample=False, expand=False, center=None)
In accordance with the degrees Randomly rotate a certain angle
degress
:(sequence or float or int) , If it is a single number , Such as 30, It means in (-30,+30) Random rotation between
if sequence, Such as (30,60), It means in 30-60 Rotate randomly between degreesresample
: Resampling method selection , Optional PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC, The default is nearest neighborcenter
: You can select center rotation or upper left rotation
2. Yes Tensor To transform
2.1. Standardization
class torchvision.transforms.Normalize(mean, std)
Given the mean :(R,G,B)
variance :(R,G,B)
, It will put Tensor
Regularization . namely :Normalized_image=(image-mean)/std
.
3.Conversion Transforms
3.1. Convert to tensor
class torchvision.transforms.ToTensor
Let's take a value range [0,255]
Of PIL.Image
perhaps shape
by (H,W,C)
Of numpy.ndarray
, Convert to shape [C,H,W]
, The value range is [0,1.0]
Of torch.FloadTensor
data = np.random.randint(0, 255, size=300)
img = data.reshape(10,10,3)
print(img.shape)
img_tensor = transforms.ToTensor()(img) # convert to tensor
print(img_tensor)
3.2. Convert to PIL image
class torchvision.transforms.ToPILImage
take shape
by (C,H,W)
Of Tensor
or shape
by (H,W,C)
Of numpy.ndarray
convert to PIL.Image
, The value remains the same .
4. Universal transformation
4.1.class torchvision.transforms.Lambda(lambd)
Use lambd As a converter .
边栏推荐
- Understand JS high-order function and write a high-order function
- Large number operation (capable of square root, power, permutation and combination, logarithm and trigonometric value)
- 1.6.3 use tcpdump to observe DNS communication process
- "APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare
- How to open the DWG file of the computer
- Go Concurrency
- Create an environment for new projects
- Could not find “store“ in the context of “Connect(homePage)
- Deeply understand the characteristics of standard flow and off standard elements
- 滲透測試-提權專題
猜你喜欢
Activereportsjs V3.0 comes on stage
Use js to simply implement the apply, call and bind methods
I got to know data types and function variables for the first time. I learned data types and function variables together today and yesterday, so I saved them in the first issue to record.
Eyeshot Ultimate 2022 Crack By Xacker
SSRF-lab
CTFHUB SSRF
Design of IM login server and message server
The construction and usage of wampserver framework
IronOCR 2022.1 Crack
February 20ctf record
随机推荐
Read the general components of antd source code
Duplicate symbols for architecture i386 clang
Eyeshot Ultimate 2022 Crack By Xacker
In depth understanding of line height and vertical align
Japanese fifty tone diagram
TeeChart Pro ActiveX 2022.1
PHP uses JWT
[Huawei machine test] hj16 shopping list
Database overview
Summary of SQL injection (I)
Deep analysis of epoll reactor code
A review of small sample learning
Word of the Day
Ctfhub eggs
2022.1.25
Click to jump out and drag the pop-up window
Implementation of websocket long connection by workman under laravel
Ranorex Studio 10.1 Crack
2021-10-24
Prototypical Networks for Few-shot Learning