The image processing (1): PyTorch garbage data preprocessing

Data preprocessing depth PyTorch transforms based learning framework

Author: Shen master's degree welfare Beijing University of Technology, advanced algorithms experts. Responsible products and technologies, focusing on NLP, images, recommendation system

The whole process include: scaling, cropping, normalization, standardization of a few basic steps.

Image normalization is a technique in computer vision, pattern recognition and other fields widely used. The so-called image normalization, is through a series of transformations, converting the original image to be processed into the respective unique standard form (the standard format image having a constant characteristic of translation, rotation, scaling affine transformation)

Based on image normalization process comprises four steps moment i.e. the center of the coordinate, x-shearing normalization, normalization and scaling the rotational normalization.

Pytorch:transforms方法

  1. Crop --Crop

Center cut: transforms.CenterCrop
random cutting: transforms.RandomCrop
random aspect ratio crop: transforms.RandomResizedCrop
up and down the center cut: transforms.FiveCrop
up and down the center flipped after cutting, transforms.TenCrop

  1. Flip and Rotate --Flip and Rotation

Probability p inversion levels: transforms.RandomHorizontalFlip (p = 0.5)
by a vertical flip probability p: transforms.RandomVerticalFlip (p = 0.5)
random rotation: transforms.RandomRotation

  1. Image transformation

resize: transforms.Resize
Standardization: transforms.Normalize
into Tensor, and normalized to [0-1]: transforms.ToTensor
filling: transforms.Pad
modify brightness, contrast, and saturation: transforms.ColorJitter
grayscale: transforms .Grayscale
linear transformation: transforms.LinearTransformation ()
affine transformation: transforms.RandomAffine
converted to grayscale with probability p: transforms.RandomGrayscale
converting the data to PILImage: transforms.ToPILImage
transforms.Lambda: A User-defined the Apply the lambda aS A transform.

  1. Transforms the operation of the data is enhanced and more flexible

transforms.RandomChoice (transforms), to a series of transforms selected from a predetermined operating
transforms.RandomApply (transforms, p = 0.5) , to add a probability transform, operating in probability
transforms.RandomOrder, the operation of random transforms upset

Import library

# 导入torch 包
import torch
import torch.nn as nn
from torchvision import transforms

Load picture data

file_name ='../images/dog.jpg'
from PIL import Image
input_image = Image.open(file_name)
print(input_image)
print(input_image.size) # 尺寸大小:长=1546,宽1213

# 数据处理后,我们看看处理后图片
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示符号
plt.imshow(input_image)

Here Insert Picture Description

Image data preprocessing

preprocess = transforms.Compose([
    # 1. 图像变换:重置图像分辨率,图片缩放256 * 256 
    transforms.Resize(256),
    # 2. 裁剪: 中心裁剪 ,依据给定的size从中心裁剪
    transforms.CenterCrop(224),
    # 3. 将PIL Image或者 ndarray 转换为tensor,并且归一化至[0-1].注意事项:归一化至[0-1]是直接除以255
    transforms.ToTensor(),
    # 4. 对数据按通道进行标准化,即先减均值,再除以标准差
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),#图片归一化
])

# 原始数据预处理
input_tensor = preprocess(input_image)
print('input_tensor.shape = ',input_tensor.shape)
print('input_tensor = ',input_tensor)
input_tensor.shape =  torch.Size([3, 224, 224])
input_tensor =  tensor([[[-1.9295, -1.9295, -1.9124,  ..., -2.0323, -1.9467, -1.9295],
         [-1.9980, -1.8953, -1.9124,  ..., -1.9638, -1.9295, -1.7754],
         [-1.9980, -1.9467, -1.9124,  ..., -2.0494, -1.9638, -1.8953],
         ...,
         [-1.4843, -1.6042, -1.6213,  ..., -0.8678, -1.1075, -1.0733],
         [-1.5357, -1.6042, -1.6213,  ..., -1.0390, -1.6213, -1.4500],
         [-1.5528, -1.4843, -1.2445,  ..., -0.9192, -1.2788, -1.2617]],

        [[-1.8256, -1.8256, -1.8081,  ..., -1.9832, -1.9132, -1.9132],
         [-1.8256, -1.8431, -1.8431,  ..., -1.9657, -1.9307, -1.8782],
         [-1.8256, -1.8431, -1.8606,  ..., -1.9657, -1.9482, -1.9132],
         ...,
         [-0.9853, -0.9678, -0.9853,  ..., -0.4601, -0.6352, -0.6702],
         [-0.9853, -0.9853, -1.0028,  ..., -0.5651, -1.1253, -0.8978],
         [-0.9503, -0.9853, -0.8102,  ..., -0.3901, -0.8102, -0.7402]],

        [[-1.6127, -1.5953, -1.5604,  ..., -1.7173, -1.6999, -1.7173],
         [-1.6650, -1.6302, -1.6302,  ..., -1.7173, -1.7173, -1.6476],
         [-1.6476, -1.6650, -1.6476,  ..., -1.7347, -1.6999, -1.6824],
         ...,
         [-1.3164, -1.5081, -1.5953,  ..., -0.8981, -1.1596, -1.0898],
         [-1.2990, -1.5256, -1.6127,  ..., -0.9504, -1.4907, -1.3861],
         [-1.2816, -1.4210, -1.2293,  ..., -0.7413, -1.1247, -1.2816]]])

Data preprocessing effect after

import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示符号

input_tensor = input_tensor.permute(1,2,0) #Changing from 3x224x224 to 224x224x3
print('input_tensor.matplotlib.shape = ',input_tensor.shape)
#clamp表示夹紧,夹住的意思,torch.clamp(input,min,max,out=None)-> Tensor
input_tensor = torch.clamp(input_tensor,0,1)
print('input_tensor.matplotlib.clamp.shape = ',input_tensor.shape)
plt.imshow(input_tensor)

Here Insert Picture Description
More exciting content, please refer to: https://github.com/shenfuli/ai

Published 267 original articles · won praise 66 · views 430 000 +

Guess you like

Origin blog.csdn.net/shenfuli/article/details/102822918