• pytorch入门1


    参考来自PyTorch从入门到放弃(目录) - 二十三岁的有德 - 博客园 (cnblogs.com)

    torch.cat(inputs, dimension=0) → Tensor

    链接(dim=0是列链接,1是航链接)

    torch.chunk(tensor, chunks, dim=0)

    切割,cat的反向操作

    torch.gather(input, dim, index, out=None) → Tensor

    沿着不同维度取出tensor的值:

     torch.index_select(input, dim, index, out=None) → Tensor

    切片,针对不同维度

     torch.nonzero(input, out=None) → LongTensor

    返回不是0的下标

    利用torch解决线性回归问题:

    1. import numpy as np
    2. import torch
    3. import torch.nn as nn
    4. x=np.arange(1,12,dtype=np.float32).reshape(-1,1)
    5. y=2*x+3
    6. class LinearRegressionModel(nn.Module):
    7. def __init__(self,input_dim,output_dim):
    8. super().__init__()
    9. self.linear=nn.Linear(input_dim,output_dim)
    10. def forward(self,inp):
    11. out=self.linear(inp)
    12. return out
    13. regression_model=LinearRegressionModel(1,1)
    14. device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    15. regression_model.to(device)
    16. epochs=20
    17. learning_rate=0.01
    18. optimizer=torch.optim.SGD(regression_model.parameters(),learning_rate)
    19. criterion=nn.MSELoss()
    20. for epoch in range(epochs):
    21. inputs=torch.from_numpy(x).to(device)
    22. labels=torch.from_numpy(y).to(device)
    23. optimizer.zero_grad()
    24. outputs=regression_model(inputs)
    25. loss=criterion(outputs,labels)
    26. loss.backward()
    27. optimizer.step()
    28. if epoch%10==0:
    29. print("epoch:",epoch,"loss:",loss.item())
    30. predict=regression_model(torch.from_numpy(x).requires_grad()).data.numpy()
    31. torch.save(regression_model.state_dict(),"model.pk1")
    32. result=regression_model.load_state_dict(torch.load("model.pk1"))

    用nn手写网络lenet网络

    1. import torch as t
    2. import torch.nn as nn
    3. import torch.nn.functional as F
    4. from torch.autograd import Variable as V
    5. from matplotlib import pyplot as plt
    6. from IPython import display
    7. class Net(nn.Module):
    8. def __init__(self):
    9. super(Net,self).__init__()
    10. self.conv1=nn.Conv2d(1,6,5)
    11. self.conv2=nn.Conv2d(6,16,5)
    12. self.fc1=nn.Linear(16*5*5,120)
    13. self.fc2=nn.Linear(120,84)
    14. self.fc3=nn.Linear(84,10)
    15. def forward(self,x):
    16. x=self.conv1(x)
    17. x=F.relu(x)
    18. x=F.max_pool2d(x,(2,2))
    19. x=self.conv2(x)
    20. x=F.relu(x)
    21. x=F.max_pool2d(x,2)
    22. x=x.view(x.size()[0],-1)
    23. x=F.relu(self.fc1(x))
    24. x=F.relu(self.fc2(x))
    25. x=self.fc3(x)
    26. return x
    27. net=Net()
    28. print(net)

     

    练习:

    1. input=V(t.randn(1,1,32,32))
    2. out=net(input)
    3. output=net(input)
    4. target=V(t.arange(0,10)).view(1,10).float()
    5. criterion=nn.MSELoss()
    6. loss=criterion(output,target)
    7. optimizer=optim.SGD(net.parameters(),lr=0.01)
    8. optimizer.zero_grad()
    9. output=net(input)
    10. loss=criterion(output,target)
    11. loss.backward()
    12. optimizer.step()

    nn.Module

    全连接层

    1. import torch as t
    2. import torch.nn as nn
    3. import torch.nn.functional as F
    4. from torch.autograd import Variable as V
    5. from matplotlib import pyplot as plt
    6. from IPython import display
    7. import torch.optim as optim
    8. class Linear(nn.Module):
    9. def __init__(self,in_features,out_features):
    10. super(Linear,self).__init__()
    11. self.w=nn.Parameter(t.randn(in_features,out_features))
    12. self.b=nn.Parameter(t.randn(out_features))
    13. def forward(self,x):
    14. x=x.mm(self.w)
    15. return x+self.b.expand_as(x)
    16. layer=Linear(4,3)
    17. input=V(t.randn(2,4))
    18. output=layer(input)
    19. print(output)

    构建多层全连接网络:

    1. import torch as t
    2. import torch.nn as nn
    3. import torch.nn.functional as F
    4. from torch.autograd import Variable as V
    5. from matplotlib import pyplot as plt
    6. from IPython import display
    7. import torch.optim as optim
    8. class Linear(nn.Module):
    9. def __init__(self, in_features, out_features): # 输入的数据维度,输出的数据维度
    10. super(Linear,
    11. self).__init__() # 等价于 nn.Module.__init__(self),继承父类的init构造函数
    12. self.w = nn.Parameter(t.randn(in_features, out_features))
    13. self.b = nn.Parameter(t.randn(out_features))
    14. def forward(self, x):
    15. x = x.mm(self.w)
    16. return x + self.b.expand_as(x)
    17. class Perceptron(nn.Module):
    18. def __init__(self,in_features,hidden_features,out_features):
    19. super(Perceptron,self).__init__()
    20. self.layer1=Linear(in_features,hidden_features)
    21. self.layer2=Linear(hidden_features,out_features)
    22. def forward(self,x):
    23. x=self.layer1(x)
    24. x=t.sigimoid(x)
    25. x=self.layer2(x)
    26. return x
    27. perceptron=Perceptron(3,4,1)
    28. for name,param in perceptron.named_parameters():
    29. print(name,param.size())

     常用的神经网络层:

    1.显示图片(温客行yyds)

    1. import torch as t
    2. import torch.nn as nn
    3. import torch.nn.functional as F
    4. from torch.autograd import Variable as V
    5. from matplotlib import pyplot as plt
    6. from IPython import display
    7. import torch.optim as optim
    8. from PIL import Image
    9. from torchvision.transforms import ToTensor,ToPILImage
    10. to_tensor=ToTensor()
    11. to_pil=ToPILImage()
    12. nick=Image.open('G:/3.bmp')
    13. nick.show()

    加锐化卷积

    1. import torch as t
    2. import torch.nn as nn
    3. import torch.nn.functional as F
    4. from torch.autograd import Variable as V
    5. from matplotlib import pyplot as plt
    6. from IPython import display
    7. import torch.optim as optim
    8. from PIL import Image
    9. from torchvision.transforms import ToTensor,ToPILImage
    10. to_tensor=ToTensor()
    11. to_pil=ToPILImage()
    12. yeye=Image.open('G:/3.bmp')
    13. inp=to_tensor(yeye).unsqueeze(0)
    14. kernel=t.ones(3,3,3)/-9
    15. kernel[:,1,1]=1
    16. conv=nn.Conv2d(
    17. in_channels=3 , #彩色图
    18. out_channels=1,
    19. kernel_size=3,
    20. stride=1,
    21. bias=False
    22. )
    23. conv.weight.data=kernel.view(1,3,3,3)
    24. #输出通道数和卷积核数一样,1代表卷积核数,3代表输入数,后两个为卷积核尺寸
    25. out=conv(V(inp))
    26. y=to_pil(out.data.squeeze(0))
    27. y.show()

     加普通卷积

    1. import torch as t
    2. import torch.nn as nn
    3. import torch.nn.functional as F
    4. from torch.autograd import Variable as V
    5. from matplotlib import pyplot as plt
    6. from IPython import display
    7. import torch.optim as optim
    8. from PIL import Image
    9. from torchvision.transforms import ToTensor,ToPILImage
    10. to_tensor=ToTensor()
    11. to_pil=ToPILImage()
    12. yeye=Image.open('G:/3.bmp')
    13. inp=to_tensor(yeye).unsqueeze(0)
    14. conv=nn.Conv2d(
    15. in_channels=3,
    16. out_channels=1,
    17. kernel_size=3,
    18. stride=1,
    19. bias=False
    20. )
    21. out=conv(V(inp))
    22. y=to_pil(out.data.squeeze(0))
    23. y.show()

     通过Sequential 构建前馈传播网络

    三种:
     

    1. net1=nn.Sequential()
    2. net1.add_module('conv',nn.Conv2d(3,3,3))
    3. net1.add_module('batchnorm',nn.BatchNorm2d(3))
    4. net1.add_module('activation_layer',nn.ReLU())
    5. net2=nn.Sequential(nn.Conv2d(3,3,3),nn.BatchNorm2d(3),nn.ReLU())
    6. from collections import OrderedDict
    7. net3=nn.Sequential(
    8. OrderedDict([('conv1',nn.Conv2d(3,3,3)),
    9. ('bn1',nn.BatchNorm2d(3)),
    10. ('relu',nn.ReLU())])
    11. )

     通过 ModuleList 构建前馈传播网络

    1. modellist=nn.ModuleList([nn.Linear(3,4),nn.ReLU(),nn.Linear(4,2)])
    2. inp=V(t.rand(1,3))
    3. for model in modellist:
    4. inp=model(inp)
    5. class MyModule(nn.Module):
    6. def __init__(self):
    7. super(MyModule,self).__init__()
    8. self.list=[nn.Linear(3,4),nn.ReLU()]
    9. self.module_list=nn.ModuleList([nn.Conv2d(3,3,3),nn.ReLU()])
    10. def forward(self):
    11. pass
    12. model=MyModule()
    13. print(model)

    循环神经网络层

    1. t.manual_seed(1000)
    2. inp=V(t.randn(2,3,4))#长度为2,batchsize为3,每个元素占4维
    3. lstm=nn.LSTM(4,3,1)#4维,3个隐藏元,1层
    4. h0=V(t.randn(1,3,3))
    5. c0=V(t.randn(1,3,3))
    6. out,hn=lstm(inp,(h0,c0))
    7. print(out)

     词向量在自然语言中应用十分广泛,torch 同样也提供了 Embedding 层

    1. embedding=nn.Embedding(4,5)
    2. embedding.weight.data=t.arange(0,20).view(4,5)
    3. with t.no_grad():
    4. inp=V(t.arange(3,0,-1)).long()
    5. output=embedding(inp)
    6. print(output)

    损失函数

    1. score=V(t.randn(3,2))
    2. label=V(t.Tensor([1,0,1])).long()
    3. criterion=nn.CrossEntropyLoss()
    4. loss=criterion(score,label)
    5. print(loss)

    优化器:

    1. class Net(nn.Module):
    2. def __init__(self):
    3. super(Net,self).__init__()
    4. self.features=nn.Sequential(nn.Conv2d(3,6,2),
    5. nn.ReLU(),
    6. nn.MaxPool2d(2,2),
    7. nn.Conv2d(6,16,5),
    8. nn.ReLU(),
    9. nn.MaxPool2d(2,2))
    10. self.classifier=nn.Sequential(nn.Linear(16*5*5,120),
    11. nn.ReLU(),
    12. nn.Linear(120,84),
    13. nn.ReLU(),
    14. nn.Linear(84,10))
    15. def forward(self,x):
    16. x=self.features(x)
    17. x=x.view(-1,16*5*5)
    18. x=self.classifier(x)
    19. return x
    20. net=Net()
    21. optimizer=optim.SGD(params=net.parameters(),lr=0.01)
    22. optimizer.zero_grad()
    23. inp=V(t.randn(1,3,32,32))
    24. output=net(inp)
    25. output.backward(output)
    26. optimizer.step()
    1. #针对不同的网络设置不同学习率
    2. ptimizer=optim.SGD(
    3. [
    4. {
    5. 'params':net.features.parameters()
    6. },
    7. {
    8. 'params':net.classifier.parameters(),
    9. 'lr':1e-2
    10. },
    11. ],
    12. lr=1e-5
    13. )

     nn.functional 和 nn.Module 的区别

    1. inp=V(t.randn(2,3))
    2. model=nn.Linear(3,4)
    3. output1=model(inp)
    4. output2=nn.functional.linear(inp,model.weight,model.bias)
    1. class MyLinear(nn.Module):
    2. def __init__(self):
    3. super(MyLinear,self).__init__()
    4. self.weight=nn.Parameter(t.randn(3,4))
    5. self.bias=nn.Parameter(t.zeros(3))
    6. def forward(self):
    7. return F.linear(input,self.weight,self.bias)

    搭建ResNet网络(torch实现)

    1. class ResidualBlock(nn.Module):
    2. def __init__(self,inchannel,outchannel,stride=1,shortcut=None):
    3. super(ResidualBlock,self).__init__()
    4. self.left=nn.Sequential(
    5. nn.Conv2d(inchannel,outchannel,3,stride,1,bias=False),
    6. nn.BatchNorm2d(outchannel),
    7. nn.ReLU(inplace=True),
    8. nn.Conv2d(outchannel,outchannel,3,1,1,bias=False),
    9. nn.BatchNorm(outchannel)
    10. )
    11. self.right=shortcut
    12. def forward(self,x):
    13. out=self.left(x)
    14. residual=x if self.right is None else self.right(x)
    15. out=out+residual
    16. out=F.relu(out)
    17. return out
    18. class ResNet(nn.Module):
    19. def __init__(self,num_classes=1000):
    20. super(ResNet,self).__init__()
    21. self.pre=nn.Sequential(
    22. nn.conv2d(3,64,7,2,3,bias=False),
    23. nn.BatchNorm2d(64),
    24. nn.ReLU(inplace=True),
    25. nn.MaxPool2d(3,2,1),
    26. )
    27. self.layer1=self._make_layer(64,128,3)
    28. self.layer2=self._make_layer(128,256,4,stride=2)
    29. self.layer3=self._make_layer(256,512,6,stride=2)
    30. self.layer4=self._make_layer(512,512,3,stride=2)
    31. self.fc=nn.Linear(512,num_classes)
    32. def _make_layer(self,inchannel,outchannel,block_num,stride=1):
    33. shortcut=nn.Sequential(
    34. nn.Conv2d(inchannel,outchannel,1,stride,bias=False),
    35. nn.BatchNorm2d(outchannel)
    36. )
    37. layers=[]
    38. layers.append(ResidualBlock(inchannel,outchannel,stride,shortcut))
    39. for i in range(1,block_num):
    40. layers.append(ResidualBlock(outchannel,outchannel))
    41. return nn.Sequential(*layers)
    42. def forward(self,x):
    43. x=self.pre(x)
    44. x=self.layer1(x)
    45. x=self.layer2(x)
    46. x=self.layer3(x)
    47. x=self.layer4(x)
    48. x=F.avg_pool2d(x,7)
    49. x=x.view(x.size(0),-1)
    50. res_net=ResNet()
    51. inp=t.autograd.Variable(t.randn(1,3,224,224))
    52. output=res_net(inp)

    torchvision 中的 resnet34网络调用

    1. from torchvision import models
    2. res_net=models.resnet34()
    3. inp=t.autograd.Variable(t.randn(1,3,224,224))
    4. output=res_net(inp)
    5. output.size()

    深度网络模型持久化

    tensor对象保存

    1. import torch as t
    2. a=t.Tensor(3,4)
    3. if t.cuda.is_available():
    4. a=a.cuda(1)
    5. t.save(a,'a.pth')
    6. b=t.load('a.pth')
    7. c=t.load('a.pth',map_location=lambda storage,loc:storage)
    8. d=t.load('a.pth',map_location={'cuda:1','cuda:0'})

    Module 对象的保存和加载

    1. import torch as t
    2. t.set_default_tensor_type('torch.FloatTensor')
    3. from torchvision.models import AlexNet
    4. model=AlexNet()
    5. model.state_dict().keys()
    6. t.save(model.state_dict(),'alexnet.pth')
    7. model.load_state_dict(t.load('alexnet.pth'))

    Optimizer 对象的保存和加载

    1. optimizer=t.optim.Adam(model.parameters(),lr=0.1)
    2. t.save(optimizer.state_dict(),'optimizer.pth')
    3. optimizer.load_state_dict(t.load('optimizer.pth'))

    深度学习程序架构设计

    模型定义
    数据处理和加载
    训练模型(Train & Validate)
    训练过程可视化
    测试(Test/Inference)

    1. checkpoints/
    2. data/
    3. __init__.py
    4. dataset.py
    5. get_data.sh
    6. models/
    7. __init__.py
    8. AlexNet.py
    9. BasicModule.py
    10. ResNet34.py
    11. utils/
    12. __init__.py
    13. visualize.py
    14. config.py
    15. main.py
    16. requirement.txt
    17. README.md

    比较复杂的可视化工具tensorboard和visdom

    使用的时候可以使用colab或者科学上网方法,不然链接会断

     数据集可以百度云盘获得

    编程实战_猫和狗二分类_深度学习项目架构

    1.数据加载

    1. class DogCat(data.Dataset):
    2. def __init__(self,root,transforms=None,train=True,test=False):
    3. self.test=test
    4. imgs=[os.path.join(root,img)
    5. for img in os.listdir(root)]
    6. if self.test:
    7. imgs=sorted(
    8. imgs,
    9. key=lambda x:int(x.split('.')[-2].split('/')[-1])
    10. )
    11. else:
    12. imgs=sorted(imgs,
    13. key=lambda x:int(x.split('.')[-2]))
    14. imgs_num=len(imgs)
    15. if self.test:
    16. self.imgs=imgs
    17. elif train:
    18. self.imgs=imgs[:int(0.7*imgs_num)]
    19. else:
    20. self.imgs=imgs[int(0.7*imgs_num):]
    21. if transforms is None:
    22. normalize=T.Normalize(mean=[0.485,0.456,0.406],
    23. std=[0.229,0.224,0.225])
    24. if self.test or not train:#测试集和验证集
    25. self.transforms=T.Compose([
    26. T.Scale(224),
    27. T.CenterCrop(224),
    28. T.ToTensor(),
    29. normalize
    30. ])
    31. else:#训练集
    32. self.transforms=T.Compose([
    33. T.Scale(256),
    34. T.RandomResizedCrop(224),
    35. T.RandomHorizontalFlip(),
    36. T.ToTensor(),
    37. normalize
    38. ])
    39. def __getitm__(self,index):
    40. #返回一张图片
    41. img_path=self.imgs[index]
    42. if self.text:
    43. label=self.imgs[index].split('.')[-2]
    44. label=int(label.split('/')[-1])
    45. else :
    46. label=1 if 'dog' in img_path.split('/')[-1] else 0
    47. data=Image.open(img_path)
    48. data=self.transforms(data)
    49. return data,label
    50. def __len__(self):
    51. return len(self.imgs)

    2.定义模型

    1. import time
    2. import torch as t
    3. class BasicModule(t.nn.Module):#封装save和load
    4. def __init__(self):
    5. super(BasicModule,self).__init__()
    6. self.model_name=str(type(self))
    7. def load(self,path):
    8. self.load_state_dict(t.load(path))
    9. def save(self,name=None):
    10. #模型名字+时间
    11. if name is None:
    12. prefix='checkpoints/'+self.model_name+'.'
    13. name=time.strftime(prefix+'%m%d_%H:%M:%S.pth')
    14. t.save(self.state_dict(),name)
    15. return name

    封装save和load

    配置文件

    1. class DefaultConfig(object):
    2. env='default'
    3. model='AlexNet'
    4. train_data_root=' ./data/train/'
    5. test_data_root='./data/test/'
    6. load_model_path='checkpoints/model.pth'
    7. batch_size=128
    8. use_gpu=False
    9. num_workers=4
    10. print_freq=20
    11. result_file='result.csv'
    12. max_epoch=10
    13. lr=0.1
    14. lr_decay=0.95
    15. weight_decay=1e-4

    main.py的代码组织结构

    在我们这个项目的 main.py 中主要包括以下四个函数,其中三个需要命令行执行,main.py 的代码组织结构如下所示:

    train:

    1. def train(**kwargs):
    2. opt.parse(kwargs)
    3. vis=Visualizer(opt.env)
    4. #模型
    5. model=opt.model()
    6. if opt.load_model_path:
    7. model.load(opt.laod_model_path)
    8. if opt,use_gpu:model.cuda()
    9. #数据
    10. train_data=DogCat(opt.train_data_root,train=True)
    11. val_data=DogCat(opt.train_data_root,train=False)
    12. train_dataloader=Dataloader(train_data,
    13. opt.batch_size,
    14. shuffle=True,
    15. num_workers=opt.num_workers)
    16. val_dataloader = Dataloader(train_data,
    17. opt.batch_size,
    18. shuffle=False,
    19. num_workers=opt.num_workers)
    20. #函数和优化器
    21. criterion=torch.nn.CrossEntropyLoss()
    22. lr=opt.lr
    23. optimizer=t.optim.Adam(model.parameters(),
    24. lr=lr,
    25. weight_decay=opt.weight.decay)
    26. #指标
    27. loss_meter = meter.AverageValueMeter() # 平均损失
    28. confusion_matrix = meter.ConfusionMeter(2) # 混淆矩阵
    29. previous_loss = 1e100
    30. #训练
    31. for epoch in range(opt.max_epoch):
    32. loss_meter.reset()
    33. confusion_matrix.reset()
    34. for ii,(data,label) in enumerate(train_dataloader):
    35. inp=Variable(data)
    36. target=Variable(label)
    37. if opt.use_gpu:
    38. inp=inp.cuda()
    39. target=target.cuda()
    40. optimizer.zero_grad()
    41. score=model(inp)
    42. loss=criterion(score,target)
    43. loss.backward()
    44. optimizer.step()
    45. loss_meter.add(loss.data[0])
    46. confusion_matrix.add(score.data, target.data)
    47. if ii % opt.print_freq == opt.print_freq - 1:
    48. vis.plot('loss', loss_meter.value()[0])
    49. # 如果需要的话,进入 debug 模式
    50. if os.path.exists(opt.debug_file):
    51. ipdb.set_trace()
    52. model.save()
    53. if loss_meter.value()[0] > previous_loss:
    54. lr = lr * opt.lr_decay
    55. for param_group in optimizer.param_groups:
    56. param_group['lr'] = lr
    57. previous_loss = loss_meter.value()[0]

    val

    1. def val(model,dataloader):
    2. model.eval()
    3. confusion_matrix = meter.ConfusionMeter(2)
    4. for ii, data in enumerate(dataloader):
    5. inp, label = data
    6. val_inp = Variable(inp, volatile=True)
    7. val_label = Variable(label.long(), volatile=True)
    8. if opt.use_gpu:
    9. val_inp = val_inp.cuda()
    10. val_label = val_label.cuda()
    11. score = model(val_inp)
    12. confusion_matrix.add(score.data.squeeze(), label.long())
    13. # 把模型恢复为训练模式
    14. model.train()
    15. cm_value = confusion_matrix.value()
    16. accuracy = 100. * (cm_value[0][0] + cm_value[1][1]) / (cm_value.sum())
    17. return confusion_matrix, accuracy

    测试

    1. def test(**kwargs):
    2. opt.parse(kwargs)
    3. model=getattr(models,opt.model)().eval()
    4. if opt.load_model_path:
    5. model.load(opt.load_model_path)
    6. if opt.use_gpu: model.cuda()
    7. train_data=DogCat(opt.test_data_root,test=True)
    8. test_dataloader=DataLoader(train_data,
    9. batch_sampler=opt.batch_size,
    10. shuffle=False,
    11. num_workers=opt.num_workers
    12. )
    13. result=[]
    14. for ii, (data, path) in enumerate(test_dataloader):
    15. inp = Variable(data, volatile=True)
    16. if opt.use_gpu: inp = inp.cuda()
    17. score = model(inp)
    18. probability = probability = functional.softmax(score, dim=1)[:, 0].detach().tolist()
    19. batch_results = [(path_, probability_) for path_, probability_ in zip(path, probability)]
    20. results += batch_results
    21. write_csv(results, opt.result_file)
    22. return results

  • 相关阅读:
    【uniapp】跨端开发问题记录
    一些框架使用总结
    Unity中Shader光照模型Phong的实现
    前后端交互之网络服务的基本概念
    华为OD机考算法题:字符串解密
    微服务3 Eureka注册中心
    iOS - Runloop在实际开发中的应用
    ORB-SLAM3复现过程中遇到的问题及解决办法
    windows下-mysql环境配置,以及使用navicat可视化数据库,便捷撰写sql语句。
    Linux操作系统的基础指令 III
  • 原文地址:https://blog.csdn.net/kling_bling/article/details/125942303