Pytorch-Python基础学习(1)

本文介绍了如何使用Ctrl+P和函数帮助理解PyTorch工具包结构,包括dir()和help()的用法,以及如何使用Dataset和Dataloader处理数据。此外,还详细讲解了Tensorboard、各种Transforms(如ToTensor,Normalize,Resize,Compose和RandomCrop)在神经网络开发中的应用,以及CIFAR10数据集和DataLoader的使用实例。
摘要由CSDN通过智能技术生成

一些常用工具

Ctrl+p 查看函数input需求

ctrl+点击查看函数,工具包文档

不知道返回值,就print或者print(input())

简介

pytorch工具包

package有很多分隔区

两个道具

  • dir()

  • help()

dir(pytorch)
输出:1、2、3、4
dir(pytorch.3)
输出:a,b,c

再使用help(pytorch.3.a)

输出:将此扳手放在特定的地方,然后拧动

总结:
dir()函数,能让我们知道工具箱
以及工具箱中的分隔区有什么东西。
help()函数,能让我们知道每个工具
是如何使用的,工具的使用方法。

pycharm控制台是局部的,分解每一步

Jupyter是以任意块运行

两种数据:

  • Dataset

提供一种方式去获取数据及其label

在这里插入图片描述

  • Dataloader

为后面网络提供不同的数据形式

Dataset

先看一段编程

定义新类,继承Dataset

from torch.utils.data import Dataset
from PIL import Image
import os
class MyData(Dataset):

    def __init__(self,root_dir,label_dir):
        self.root_dir = root_dir
        self.label_dir = label_dir
        self.path = os.path.join(self.root_dir,self.label_dir)
        self.img_path = os.listdir(self.path)

    def __getitem__(self,idx):
        img_name = self.img_path[idx]
        img_item_path = os.path.join(self.root_dir,self.label_dir,img_name)
        img = Image.open(img_item_path)
        label = self.label_dir
        return img,label

    def __len__(self):
        return len(self.img_path)

root_dir = "image"
dogs_label_dir = "dogs"
cats_label_dir = "cats"
dogs_dataset = MyData(root_dir,dogs_label_dir)
cats_dataset= MyData(root_dir,cats_label_dir)

train_dataset = cats_dataset + dogs_dataset

构造函数传入数据“图片位置root”和“标签label”,然后geiitem获取(待完善),len输出标签长度

Tensorboard

查看模型演变的数据,过程。

from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter("logs")

# writer.add_image()
# y=x
for i in range(100):
    writer.add_scalar("y=x",i,i)

writer.close()
  • SummaryWriter 写入文件名
  • 后续循环导入图像

问题:tensorboard出现大量文件并进行报错

解决:更新最高版本tensorboard ,使用pip install tensorboard -U

add_image

add_image("名称",图片,1)

from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image

writer = SummaryWriter("logs")
image_path="hymenoptera_data/train/ants/0013035.jpg"
img_PIL = Image.open(image_path)
img_array=np.array(img_PIL)
print(type(img_array))
print(img_array.shape)

writer.add_image("test",img_array,1,dataformats='HWC')

for i in range(100):
    writer.add_scalar("y=2x",3*i,i)

writer.close()

从PIL到numpy,需要在add——image()中指定shape中每一i个数字/维i表示的含义

注意:不改变tag的情况下,改变GLobal_step可以在同一标题下查看图片。

Transfroms工具箱

工具:

  • totensor
  • resize

将图片格式转化成我们想要的结果

PIL transfroms

可迭代对象call

举例:

from torchvision import transforms
from PIL import Image

tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)

在这段代码中,tensor_trans变成函数,将工具实例化,方便后面使用

再看下面一段代码:

class Person:
    def __call__(self, name):
        print("__call__" + "Hello" + name)

    def hello(self, name):
        print("hello" + name)


person = Person()
person("zhangsan")
person.hello("lisi")

可以很清晰理解。

常见的Transfroms

  • PIL -> Image.open
  • tensor -> ToTensor()
  • narrays -> cv.imread()

ToTensor

格式转化,使得PIL格式转为tensor模式以供后续函数使用

Normalize

使用均值和标准差创建一个归一化对象

作用:提升训练性能,节省时间

格式:

input[channel]=(input[channel]-mean[channel])/std[channel]

比如:(input-0.5)/0.5=2*input-1

input的像素值为[0,1]

最后result[-1,1]

Resize

进行等比的缩放

改变时,先变为resize形式,再变回tensor形式即可输出

Compose

只改变边的关系,不进行等比缩放

RandomCrop

两个参数(纵,横),如果只填一个即为正方形

实际案例:

from PIL import Image
from torchvision import transforms

from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter("logs")
img = Image.open("hymenoptera_data/train/bees/95238259_98470c5b10.jpg")
print(img)
# ToTensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor", img_tensor)

# Normalize
print(img_tensor[0][0][0])
trans_norm = transforms.Normalize([6, 3, 2], [9, 3, 5])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize", img_norm, 2)

# Resize
print(img.size)
trans_resize = transforms.Resize((512, 512))
# img PIL ->resize ->img_resize PIL
img_resize = trans_resize(img)
# img_resize PIL -> totensor ->img_resize tensor
img_resize = trans_totensor(img_resize)
writer.add_image("Resize", img_resize, 0)
print(img_resize)

# Compose - resize - 2
trans_resize_2 = transforms.Resize(512)
# PIL -> PIL -> tensor
trans_compose = transforms.Compose([trans_resize_2, trans_totensor])
img_resize_2 = trans_compose(img)
writer.add_image("Resize", img_resize_2, 1)

# RandomCrop
trans_random = transforms.RandomCrop(512)
trans_compose_2 = transforms.Compose([trans_random,trans_totensor])
for i in range(10):
    img_crop = trans_compose_2(img)
    writer.add_image("RandomCrop",img_crop,i)

writer.close()

数据集

dataset

  • dataset_transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor()])基本为固定格式,导入数据

  • datasets.后续输入需要的数据库

具体编程:

import torchvision
from torch.utils.tensorboard import SummaryWriter

dataset_transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor()])
train_set = torchvision.datasets.CIFAR10(root="./dataset", train=True, transform=dataset_transform, download=True)
test_set = torchvision.datasets.CIFAR10(root=".dataset", train=False, transform=dataset_transform, download=True)

print(test_set[1])
print(test_set.classes)

# img,target = test_set[0]
# print(img)
# print(target)
# print(test_set.classes[target])
# img.show()

writer = SummaryWriter("p10")
for i in range(10):
    img, target = test_set[i]
    writer.add_image('test_set', img)

dataloader

batch_size提取4个不同的图片

  • batch_size一组照片数量
  • drop_last最后一组不够组数时舍去
  • shuffle第一次,第二次录入顺序是否打乱

具体编程:

import torchvision

from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

test_data = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor())

test_loader = DataLoader(dataset=test_data, batch_size=64, shuffle=True, num_workers=0, drop_last=False)

# 测试数据集第一张图片
img, target = test_data[0]
print(img.shape)
print(target)

writer = SummaryWriter('dataloader')
step = 0
for data in test_loader:
    imgs,targets = data
    writer.add_images("test_data",imgs,step)
    step = step + 1

writer.close()

神经网络的搭建

torch.nn(Neural network)

input -> forward ->output

torch.nn.funcitional可以了解卷积原理

  • 卷积核(kernel)
  • stride 决定卷积核行走步数
  • 卷积后的输出
  • reshape 重新生成4维格式
  • padding 填充

神经网络

卷积层

  • in_channels 输入通道(待完善)
  • out_channels 输出通道 (待完善)
from torch import nn
import torch


class Tudui(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, input):
        output = input + 1
        return output


tudui = Tudui()
x = torch.tensor(1.0)
output = tudui(x)
print(output)

最大池化

  • kernel_size (Union[int, Tuple[int, int]**]) – the size of the window to take a max over
  • stride (Union[int, Tuple[int, int]**]) – the stride of the window. Default value is kernel_size
  • padding (Union[int, Tuple[int, int]**]) – Implicit negative infinity padding to be added on both sides
  • dilation (Union[int, Tuple[int, int]**]) – a parameter that controls the stride of elements in the window
  • return_indices (bool) – if True, will return the max indices along with the outputs. Useful for torch.nn.MaxUnpool2d later
  • ceil_mode (bool) – when True, will use ceil instead of floor to compute the output shape

作用:

  1. 模糊化图片,减少数据量,加快速度
  2. 防止过拟合
  3. dtype=使得矩阵系数变为浮点数

具体实现:

import torch
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("../data", train=False, download=True, transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64)


class MaxPool(nn.Module):
    def __init__(self):
        super(MaxPool, self).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)

    def forward(self, input):
        output = self.maxpool1(input)
        return output


maxpool = MaxPool()

step = 0
writer = SummaryWriter("./logs_maxpool")
for data in dataloader:
    imgs, targets = data
    writer.add_images("input", imgs, step)
    output = maxpool(imgs)
    writer.add_images("output", output, step)
    step = step + 1

writer.close()

非线性激活

查询pytorch官网,通过一系列变换

具体实现:

import torch
import torchvision.datasets
from torch import nn
from torch.nn import ReLU, Sigmoid
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DataLoader

input = torch.tensor([[1, -0.5], [-1, 3]])

output = torch.reshape(input, (-1, 1, 2, 2))

dataset = torchvision.datasets.CIFAR10("../data", train=False, download=True,
                                       transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64)


class Tudui(nn.Module):
    def __init__(self):
        super(Tudui,self).__init__()
        self.relu1 = ReLU()
        self.sigmoid1 = Sigmoid()

    def forward(self, input):
        output = self.sigmoid1(input)
        return output


tudui = Tudui()

writer = SummaryWriter("./logs_relu")
step = 0
for data in dataloader:
    imgs, targets = data
    writer.add_images("input", imgs, global_step=step)
    output = tudui(imgs)
    writer.add_images("output", output, step)
    step += 1

writer.close()

最终使得图片透明化

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值