注:参考B站‘小土堆’视频教程
视频链接:【PyTorch深度学习快速入门教程(绝对通俗易懂!)【小土堆】
tensorboard的作用:可视化神经网络的训练过程
'''
1、tensorboard的使用
2、图像变换,transform的使用
'''
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
writer = SummaryWriter("logs")
# 可以观察训练过程中不同阶段的显示情况
image_path = "dataset/train/bees_image/16838648_415acd9e3f.jpg"
# 图片类型转换
img_PIL = Image.open(image_path)
img_array = np.array(img_PIL)
writer.add_image("test",img_array,2,dataformats='HWC')
# y=x
for i in range(100):
writer.add_scalar("y=2x",2*i,i)
writer.close()
# tensorboard --logdir=logs --port=6007
在teiminal中输入“tensorboard --logdir=logs --port=6007”(也可以使用默认端口6006)打开tensorboard界面
展示效果:
通过Image.open()打开的图片是PIL类型的,转换为Tensor类型:
ToTensor()能够把灰度范围从0-255变换到0-1之间
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")
img = Image.open("dataset/train/ants_image/0013035.jpg")
# ToTensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("Tensor_img",img_tensor)
writer.close()
用均值和标准差归一化张量图像
input[channel] = (input[channel]-mean[channel])/std[channel]
即:(input-0.5)/0.5 = 2*input -1
举例:2 x 0.3137 - 1 = -0.3725
# Normalize归一化
# input[channel] = (input[channel]-mean[channel])/std[channel]
# (input-0.5)/0.5 = 2*input -1
# input[0,1] result[-1,1]
print(img_tensor[0][0][0]) # tensor(0.3137)
# trans_norm = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
# trans_norm = transforms.Normalize([1,3,5],[3,2,1])
trans_norm = transforms.Normalize([6,3,2],[9,3,5])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0]) # tensor(-0.3725)
writer.add_image("Normalize",img_norm,2)
更改图片尺寸,把给定的图片resize到given size;输入图像应该是PIL image类型
# 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_reszie tensor
img_resize = trans_totensor(img_resize)
writer.add_image("Resize",img_resize,0)
# print(img_resize)
原来图片尺寸为(768, 512),压缩后变为(512,512)
Compose的主要作用是串联多个图片变换,其中的参数必须是一个列表,数据需要是Transforms类型
# 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()
CIFAR10数据集
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[0])
# print(test_set.classes)
#
# img, target = test_set[0]
# print(img)
# print(target)
# print(test_set.classes[target])
# img.show()
print(test_set[0])
writer = SummaryWriter("p10")
for i in range(10):
img, target = test_set[i]
writer.add_image("test_set", img, i)
writer.close()
PyTorch 提供了两个数据集管理相关的类torch.utils.data.DataLoader/Dataset。其中,Dataset用于存储样本及其对应的标签,而DataLoader则提供了一个高效的迭代器,可以实现对Dataset的数据内容进行高效检索。
dataset
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=True)
# 测试数据集中第一张图片及target
img, target = test_data[0]
print(img.shape)
print(target)
writer = SummaryWriter("dataloader")
for epoch in range(2):
step = 0
for data in test_loader:
imgs, targets = data
# print(imgs.shape)
# print(targets)
writer.add_images("Epoch: {}".format(epoch), imgs, step)
step = step + 1
writer.close()
drop_last=True
drop_last=False
import torch
from torch import nn
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)
import torch
import torch.nn.functional as F
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]])
kernel = torch.tensor([[1, 2, 1],
[0, 1, 0],
[2, 1, 0]])
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))
print(input.shape)
print(kernel.shape)
output = F.conv2d(input, kernel, stride=1)
print(output)
output2 = F.conv2d(input, kernel, stride=2)
print(output2)
output3 = F.conv2d(input, kernel, stride=1, padding=1)
print(output3)
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
dataset = torchvision.datasets.CIFAR10("dataset", train=False, transform=torchvision.transforms.ToTensor(),
download=True)
dataloader = DataLoader(dataset, batch_size=64)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, 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
tudui = Tudui()
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
imgs, targets = data
output = tudui(imgs)
# print(imgs.shape)
# print(output.shape)
# 输入的大小:torch.Size([64, 3, 32, 32])
writer.add_images("input", imgs, step)
# 经过卷积后输出的大小:torch.Size([64, 6, 30, 30]) -> [xxx, 3, 30, 30]
# 刚开始xxx未知时,先用-1
output = torch.reshape(output, (-1, 3, 30, 30))
writer.add_images("output", output, step)
step = step + 1
Pytorch官方文档
import torch
from torch import nn
from torch.nn import MaxPool2d
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)
input = torch.reshape(input,(-1,1,5,5))
print(input.shape)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)
def forward(self, input):
output = self.maxpool1(input)
return output
tudui = Tudui()
output = tudui(input)
print(output)
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("dataset", 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.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=False)
def forward(self, input):
output = self.maxpool1(input)
return output
tudui = Tudui()
writer = SummaryWriter("logs_maxpool")
step = 0
for data in dataloader:
imgs, targets = data
writer.add_images("input", imgs, step)
output = tudui(imgs)
writer.add_images("output", output, step)
step = step + 1
writer.close()