当前位置:网站首页>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 .
边栏推荐
- Attack and defense world web baby Web
- How micro engine uploads remote attachments
- Apache+php uploading large files
- Click to jump out and drag the pop-up window
- Creation and use of MySQL index
- Keyboard key code value
- Database overview
- Small sample learning data set
- Basic knowledge of web pages (URL related)
- On Transform
猜你喜欢

Customize the console plot result style

Use js to simply implement the apply, call and bind methods

SQL lab range explanation
![[relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear](/img/12/d41f8d5abcb61d2632a8b117bf1604.jpg)
[relax's law of life lying on the square] those poisonous chicken soup that seem to be too light and too heavy, but think carefully and fear

Rce code execution & command execution (V)

Read the general components of antd source code

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.

Five simple data types of JS

Voxel based and second network learning

Ranorex Studio 10.1 Crack
随机推荐
A review of small sample learning
How to install the blue lake plug-in to support Photoshop CC 2017
"APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare
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.
H5 canvas drawing circle drawing fillet [detailed explanation]
2021-10-24
Critical dependency: require function is used in a way in which dependencies
Laravel's little knowledge
CSRF (Cross Site Request Forgery) &ssrf (server request forgery) (IV)
2021-04-02
Edge loss 解读
Drag modal box
SQL lab range explanation
XSS (cross site script attack) summary (II)
1.5.3 use tcpdump to observe ARP communication process
Go Context - Cancelation and Propagation
Database low-end SQL query statement fragment
Click to jump out and drag the pop-up window
Notes on non replacement elements in the line (padding, margin, and border)
IronOCR 2022.1 Crack