? ? ?今天发一下前几天学习的内容吧,前几天学习了dataloader的使用、还学习了如何简单的搭建一个神经网络的模型、了解了图像处理中的卷积操作、学习了二位卷积处理的实例应用、学习了池化层的使用,下面是学习中遇到的代码
? ? dataloader:
import torchvision
from torch.utils.data import DataLoader
from tensorboardX import SummaryWriter#导入一些库
test_data = torchvision.datasets.CIFAR10("dataset",train=False,
transform=torchvision.transforms.ToTensor())
#本次使用CIFAR10数据集,采用训练集,transform就是转化为张量形式
test_loader = DataLoader(dataset=test_data,batch_size=64,
shuffle=True,num_workers=0,drop_last=False)
#batch_size是指每一次送进去的图片数量
#shuffle是指第一次与第二次调用是否要进行打乱
#num_workers是指导入图片的进程数,取默认值即可
#drop_last是指如果不足64张图片时,我们还要不要这个图象
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()#关闭writer
? ? nn_module:
import torch
from torch import nn#导入一些包
class B(nn.Module):
def __init__(self):
super().__init__()#通过super函数进行继承
def forward(self,input):
output = input+1
return output#编写forward函数用于简单的处理
b=B()#创建类
x=torch.tensor(1.0)
output = b(x)
print(output)
? ? nn_conv:
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))
#因为输入要求是四维,所以我们通过reshape改造成
#(batch_size,channel,H,W)
print(input.shape)
print(kernel.shape)
output = F.conv2d(input,kernel,stride=1,padding=1)
print(output)
? ? nn_conv2d:
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from tensorboardX import SummaryWriter
#导入了很多很多库
dataset = torchvision.datasets.CIFAR10("dataset",train=False,
transform=torchvision.transforms.ToTensor())
#这里也是导入了需要的库
dataloader = DataLoader(dataset,batch_size=64)
#这里用了前面的dataloader
class B(nn.Module):
def __init__(self):
super(Baijiajun,self).__init__()
self.conv1 = Conv2d(in_channels=3,out_channels=6,kernel_size=3,stride=1,padding=0)
#这里定义了conv1函数,输入通道为3,输出通道为6,卷积核大小为3,不使用padding
def forward(self,x):
x = self.conv1(x)
return x
b = B()
writer = SummaryWriter("logs")
step=0
for data in dataloader:
imgs,targets = data
output = b(imgs)
print(imgs.shape)
print(output.shape)
writer.add_images("input",imgs,step)
output=torch.reshape(output,(-1,3,30,30))
writer.add_images("output",output,step)
writer.close()
#同样是通过函数进行了tensorboard的显示
? ? nn_maxpool:
import torch
import torchvision.datasets
from torch import nn
from torch.nn import MaxPool2d
from tensorboardX import SummaryWriter
from torch.utils.data import DataLoader
#导入很多很多库
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)
#这里通过dtype函数将其中的元素都转化成torch.floa32的
input = torch.reshape(input,(-1,1,5,5))
#这里的-1是要求reshape函数自己决定
print(input.shape)
dataset = torchvision.datasets.CIFAR10(root="dataset",train=True,download=False,
transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset,batch_size=64)
class B(nn.Module):
def __init__(self):
super(B,self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3,ceil_mode=True)
def forward(self,input):
output = self.maxpool1(input)
return output
b = B()
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
imgs,targets=data
output = b(imgs)
writer.add_images("input",imgs,step)
writer.add_images("output",output,step)
step=step+1
writer.close()
|