tesorboard
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")
img = Image.open("test.jpg")
img_array = np.array(img)
print(type(img_array))
print(img_array.shape)
writer.add_image("test",img_array,2,dataformats="HWC")
writer.add_image()
for i in range(100):
writer.add_scalar("y=3x",3*i,i)
writer.close()
transform
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
img = Image.open("test.jpg")
trans = transforms.ToTensor()
img_tensor = trans(img)
writer = SummaryWriter("logs2")
writer.add_image("tensor_img",img_tensor)
print(img_tensor[0][0][0])
trans_norm = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize",img_norm)
trans_resize2 = transforms.Resize(512)
trans_compose = transforms.Compose([trans_resize2,trans])
img_resize2 = trans_compose(img)
writer.add_image("Resize",img_resize2,1)
trans_random = transforms.RandomCrop(512)
trans_compose_2 = transforms.Compose([trans_random,trans])
for i in range(10):
img_crop = trans_compose_2(img)
writer.add_image("RandomCrop",img_crop,i)
writer.close()
? 这里只是举了几个例子,总结在调用这些函数时,我们要关注输入和输出,看官方文档,关注方法需要的参数,不知道返回值的时候可以利用print(type())或者debug
torch.nn.functional
import torch.nn.functional as F
import torch
input = torch.tensor([[1,2,3,4],
[2,4,6,8],
[3,5,7,9],
[5,4,6,3]])
kerner = torch.tensor([[1,0,1],
[2,0,2],
[1,1,1]])
input = torch.reshape(input,(1,1,4,4))
kerner = torch.reshape(kerner,(1,1,3,3))
output = F.conv2d(input,kerner,stride=1,padding=1)
print(output)
convolution
from torch import nn
import torchvision
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10("./data",train=False,transform=torchvision.transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset,batch_size=64)
class Conv(nn.Module):
def __init__(self):
super(Conv,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
myConv = Conv()
import torch.utils.tensorboard
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
imgs, targets = data
output = myConv(imgs)
output = torch.reshape(output,(-1,3,30,30))
writer.add_images("input",imgs,step)
writer.add_images("output",output,step)
step = step+1
maxpooling
from torch import nn
from torch.nn import MaxPool2d
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10("./data",train=False,transform=torchvision.transforms.ToTensor(),download=True)
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
writer = SummaryWriter("logs_maxpool")
myMaxpool = maxpool()
step = 0
for data in dataloader:
imgs, targets = data
writer.add_images("input_maxpool",imgs,step)
output = myMaxpool(imgs)
writer.add_images("output_maxpool",output,step)
step = step+1
非线性激活
import torchvision
from torch.nn import Sigmoid
from torch.utils.data import DataLoader
from torch import nn
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10("./data",train=False,transform=torchvision.transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset,batch_size=64)
class sigmoid(nn.Module):
def __init__(self):
super(sigmod,self).__init__()
sigmod1 = Sigmoid()
def forward(self,input):
output = sigmoid(input)
return output
mySigmoid = Sigmoid()
writer = SummaryWriter("logs_sigmoid")
step = 0
for data in dataloader:
imgs, targets = data
writer.add_images("input_sigmoid",imgs,global_step=step)
output = mySigmoid(imgs)
writer.add_images("output_sigmoid",output,step)
step = step+1
线性层
class Liner(nn.Module):
def __init__(self):
super(Liner, self).__init__()
self.linear1 = Linear(196608,10)
def forward(self,input):
output = self.linear1(input)
return output
myLinear = Liner()
for data in dataloader:
imgs, targets = data
print(imgs.shape)
output = torch.flatten(imgs)
print(output.shape)
output = myLinear(output)
print(output.shape)
Sequential
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Linear, Sequential
import torch
from torch.nn.modules.flatten import Flatten
from torch.utils.tensorboard import SummaryWriter
class Tudui(nn.Module):
def __init__(self):
super(Tudui,self).__init__()
self.model1 = Sequential(
Conv2d(3,32,5,padding=2),
MaxPool2d(2),
Conv2d(32, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self,x):
x = self.model1(x)
return x
tudui = Tudui()
input = torch.ones((64,3,32,32))
output = tudui(input)
writer = SummaryWriter("logs_seq")
writer.add_graph(tudui,input)
writer.close()
|