首页 > 其他分享 >图像处理(1):PyTorch垃圾分类 数据预处理

图像处理(1):PyTorch垃圾分类 数据预处理

时间:2023-03-17 21:08:33浏览次数:44  
标签:... tensor 裁剪 PyTorch 图像处理 transforms 归一化 input 预处理


基于深度学习框架PyTorch transforms 方法进行数据的预处理

产品和技术负责人,专注于NLP、图像、推荐系统

整个过程主要包括:缩放、裁剪、归一化、标准化几个基本步骤。

图像归一化是计算机视觉、模式识别等领域广泛使用的一种技术。所谓图像归一化, 就是通过一系列变换, 将待处理的原始图像转换成相应的唯一标准形式(该标准形式图像对平移、旋转、缩放等仿射变换具有不变特性)

基于矩的图像归一化过程包括 4 个步骤 即坐标中心化、x-shearing 归一化、缩放归一化和旋转归一化。

Pytorch:transforms方法

  1. 裁剪——Crop

中心裁剪:transforms.CenterCrop
随机裁剪:transforms.RandomCrop
随机长宽比裁剪:transforms.RandomResizedCrop
上下左右中心裁剪:transforms.FiveCrop
上下左右中心裁剪后翻转,transforms.TenCrop

  1. 翻转和旋转——Flip and Rotation

依概率p水平翻转:transforms.RandomHorizontalFlip(p=0.5)
依概率p垂直翻转:transforms.RandomVerticalFlip(p=0.5)
随机旋转:transforms.RandomRotation

  1. 图像变换

resize:transforms.Resize
标准化:transforms.Normalize
转为tensor,并归一化至[0-1]:transforms.ToTensor
填充:transforms.Pad
修改亮度、对比度和饱和度:transforms.ColorJitter
转灰度图:transforms.Grayscale
线性变换:transforms.LinearTransformation()
仿射变换:transforms.RandomAffine
依概率p转为灰度图:transforms.RandomGrayscale
将数据转换为PILImage:transforms.ToPILImage
transforms.Lambda:Apply a user-defined lambda as a transform.

  1. 对transforms操作,使数据增强更灵活

transforms.RandomChoice(transforms), 从给定的一系列transforms中选一个进行操作
transforms.RandomApply(transforms, p=0.5),给一个transform加上概率,依概率进行操作
transforms.RandomOrder,将transforms中的操作随机打乱

导入库

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

加载图片数据

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)

图像处理(1):PyTorch垃圾分类 数据预处理_Pytorch

图片数据预处理

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

数据预处理后的效果

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)

图像处理(1):PyTorch垃圾分类 数据预处理_归一化_02


标签:...,tensor,裁剪,PyTorch,图像处理,transforms,归一化,input,预处理
From: https://blog.51cto.com/u_14361901/6128368

相关文章

  • 全志R128芯片 如何在FreeRTOS下对代码源文件进行快速预处理?
    1.主题FreeRTOS_R128_如何对代码源文件进行快速预处理2.问题背景硬件:R128软件:FreeRTOS客户在日常的开发过程中,会碰到源文件中有许多的宏或许多条件编译的代码,有时候需......
  • Pytorch中LayerNorm的使用
      LayerNorm和BatchNorm相比,与一次传入网络的size大小无关,这一点与GroupNorm相似。      经过一番搜索以后,发现可能确实不适用于卷积神经网络中。  更直接的......
  • PyTorch学习记录(三):onnx模型部署
    原则:PythonforTrainingC++forInferencePyTorch模型导出:PyTorch使用.pth文件来对神经网络的权重进行保存,.pth文件中的模型权重则是按照字典格式进行保存的,但是.pth......
  • opencv图像处理基础操作之边缘检测
    概述边缘检测是利用图像中边缘像素值会发生剧烈变化来进行检测的,主要用于分割图像、提取特征等多个方面。opencv中常用的算法有Canny边缘检测算法、Sobel算子、Laplacian算......
  • Matlab常用图像处理命令108例(三)
    文章和代码以及样例图片等相关资源,已经归档至【Github仓库:​​digital-image-processing-matlab​​】或者公众号【AIShareLab】回复数字图像处理也可获取。26.edge功能:识......
  • 【Pytorch】RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED 问题解决
    问题起因:在运行论文代码时出现了RuntimeError:cuDNNerror:CUDNN_STATUS_NOT_INITIALIZED报错。 解决方法:如果不是cuda、cudnn配置的问题,那有可能是电脑的线......
  • Pytorch初步应用
    2.1使用Pytorch构建一个神经网络学习目标掌握用Pytorch构建神经网络的基本流程.掌握用Pytorch构建神经网络的实现过程.关于torch.nn:使用Pytorch来构建神经网络,主要的工......
  • pytorch ssd 代码疑惑, flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_
    https://github.com/amdegroot/ssd.pytorch/blob/5b0b77faa955c1917b0c710d770739ba8fbff9b7/layers/functions/detection.py#L58defforward(self,loc_data,conf_d......
  • 图像处理踩坑笔记
    训练模型时候一定要知道,模型使用的是什么格式的输入,是BGR还是RGB。数据增强过程中,要看到其中是否经过了BGR和RGB的变换。测试的时候,一定要和训练时候的格式保持......
  • 图像处理之高斯滤波4
    ​​​​1高斯滤波的理论简析高斯滤波是一种线性平滑滤波,可以消除高斯噪声,广泛应用于图像处理的减噪过程。通俗地讲,高斯滤波就是对整幅图像进行加权平均的过程,每一个像素点......