目录
1. torchvision中数据集的使用
下面主要讲解torchvision.dataset以及dataset如何和transforms联合使用。这部分是对PyTorch入门1中内容的一个总结。
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)
writer = SummaryWriter("P10")
for i in range(10):
img, target = test_set[i]
writer.add_image("test_set", img, i)
writer.close()
2. DataLoader的使用
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)
writer = SummaryWriter("dataloader")
step = 0
for data in test_loader:
imgs, targets = data
print(imgs.shape)
print(targets)
writer.add_images("test_data", imgs, step)
step = step + 1
writer.close()
drop_last=False的时候,如果数据量不能被batch_size整除,会保留最后不够batch_size数量的数据:
3. 神经网络的基本骨架——nn.Module的使用
下面是一个简单的使用nn.Module的例子:
import torch
from torch import nn
class Simple_Example(nn.Module):
def __init__(self):
super().__init__()
def forward(self, input):
output = input + 1
return output
simple_example = Simple_Example()
x = torch.tensor(1.0)
output = simple_example(x)
print(output)
4. 神经网络
——卷积层
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
testset = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(testset, batch_size=64)
class NN_conv2d(nn.Module):
def __init__(self):
super(NN_conv2d, self).__init__()
self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)
def forward(self, x):
x = self.conv1(x)
return x
nn_conv2d = NN_conv2d()
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
imgs, targets = data
output = nn_conv2d(imgs)
# print(imgs.shape)
# torch.Size([64, 3, 32, 32])
writer.add_images("input", imgs, step)
# print(output.shape)
# torch.Size([64, 6, 30, 30])
# output的channel是6层,无法显示,所以把它的channel reshape成3层的
output = torch.reshape(output, (-1, 3, 30, 30))
writer.add_images("output", output, step)
step = step + 1
writer.close()
——最大池化的使用
最大池化的目的:保留输入的特征,同时减少数据量。
import torch
from torch import nn
from torch.nn import MaxPool2d
# MaxPool2d不能处理Long类型的数据,这里输入的数不带小数点则默认为整数类型,所以要设置dtype为float
input = torch.tensor([[1, 2, 0, 3, 1],
[0, 1, 2, 3, 1],
[1, 2, 1, 0, 0],
[5, 2, 3, 1, 1],
[2, 1, 0, 1, 1]], dtype=torch.float32)
# -1的地方会自动填充合适的数字
input = torch.reshape(input, (-1, 1, 5, 5))
# print(input.shape)
# 输出结果:torch.Size([1, 1, 5, 5])
class nnmax(nn.Module):
def __init__(self):
super(nnmax, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)
def forward(self, input):
output = self.maxpool1(input)
return output
nn_max = nnmax()
output = nn_max(input)
print(output)
# print(output)的输出如下:
# tensor([[[[2., 3.],
# [5., 1.]]]])
# 若把ceil_mode设为False,则print(output)的输出如下:
# tensor([[[[2.]]]])
在图像上做最大池化,可视化效果如下:
import torchvision.datasets
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
testset = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(testset, batch_size=64)
class nnmax(nn.Module):
def __init__(self):
super(nnmax, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)
def forward(self, input):
output = self.maxpool1(input)
return output
nn_max = nnmax()
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
imgs, targets = data
output = nn_max(imgs)
writer.add_images("input", imgs, step)
writer.add_images("output", output, step)
step = step + 1
writer.close()
——线性层介绍
import torch
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader
testset = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(testset, batch_size=64, drop_last=True)
class linear(nn.Module):
def __init__(self):
super(linear, self).__init__()
self.linear1 = Linear(196608, 10)
def forward(self, input):
output = self.linear1(input)
return output
li = linear()
step = 0
for data in dataloader:
imgs, tagerts = data
print(imgs.shape)
output = torch.reshape(imgs, (1, 1, 1, -1))
# output = torch.flatten(imgs)
output = li(output)
print(output.shape)