
手把手学习Pytorch
文章平均质量分 57
编程Pytorch(Python)
游客26024
小样,被哥迷住啦?
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
如何利用Pytorch针对自己所设计的数据集进行的简单迁移学习
本博客的内容是讲解新手如何利用Pytorch针对自己所设计的数据集进行简单的迁移学习。以VGG16为Backbone,CIFAR10为数据集,AdamW为梯度下降策略,ReduceLROnPlateau为学习调整机制。注意:显卡是2060,电脑是拯救者;VGG16网络便对此进行了改进(img_size为)!也就是说这个小Demo自己的电脑也可以跑,不用在服务器下运行。文件结构 未进行迁移学习之前的vgg16,我们要用这个vgg16网络预训练CIFAR10数据集,之后再做迁移学习vgg16.py(对i原创 2022-06-04 23:43:43 · 1275 阅读 · 0 评论 -
如何使用Pytorch让网络模型加速训练?(autocast与GradScaler)
题外话,我为什么要写这篇博客,就是因为我穷!没钱!租的服务器一会钱就烧没了,急需要一种trick,来降低内存加速。回到正题,如果我们使用的数据集较大,且网络较深,则会造成训练较慢,此时我们要想加速训练可以使用Pytorch的AMP(autocast与Gradscaler);本文便是依据此写出的博文,对Pytorch的AMP(autocast与Gradscaler进行对比)自动混合精度对模型训练加速。注意Pytorch1.6+,已经内置torch.cuda.amp,因此便不需要加载NVIDIA的ape原创 2022-05-19 17:46:51 · 2991 阅读 · 6 评论 -
Pytorch中几种调整学习率scheduler机制(策略)的用法即其可视化
申明此篇博文是以AlexNet为网络架构(其需要输入的图像大小为227x227x3),CIFAR10为数据集,SGD为梯度下降函数举例。运行此程序时,文件的结构:/content/drive/MyDrive/coder/Simple-CV-Pytorch-master||||----AlexNet----train.py(train_adjust_learning_rate.py,train_MultiStepLR.py等等)||||----tensorboard(保存tensor原创 2022-05-11 01:15:01 · 5460 阅读 · 1 评论 -
1.初识Pytorch之Dataset
Pytorch系列是了解与使用Pytorch编程来实现卷积神经网络。学习如何对卷积神经网络编程;首先,需要了解Pytorch对数据的使用(也是在我们模型流程中对数据的预处理部分),其中有两个包Dataset,DataLoader。Dataset是Pytorch对于单个数据的处理类似于给一堆数据进行编号,(在有标签的图像处理中)对其有序地提取图像与标签,而DataLoader则是一坨一坨的数据进行批次的处理。此实验运用的数据是北邮邓伟洪老师的人脸表情包的数据集,代码根据B站,我是土堆改写。原创 2021-12-15 21:02:46 · 1948 阅读 · 0 评论 -
2.初识Pytorch使用Tensorboard来观察数据
上一章将数据的处理,这一章将数据处理之后呈现的结果即你有可能看到Loss的走向等,这样方便我们调试代码。Tensorboard有两个常用的方法一个是add_scalar()显示曲线一个是add_image()显示图像首先安装Tensorboard在你的编译环境(conda activate XXX)中输入命令pip install tensorboard1.使用add_scalar()输入代码from torch.utils.tensorboard import Summary原创 2021-12-15 23:08:37 · 4241 阅读 · 1 评论 -
3.初识Pytorch使用transforms
首先,这次讲解的tansforms功能,通俗地讲,类似于在计算机视觉流程里的图像预处理部分的数据增强。transforms的原理:说明:图片(输入)通过工具得到结果(输出),这个工具,就是transforms模板工具,(tool=transforms.ToTensor()具体工具),使用工具result=tool(图片)tansforms的调用与使用,由下图可得:先创建一个transforms.Tensor(),使用from torchvision import transforms调包t原创 2021-12-26 18:10:14 · 2540 阅读 · 1 评论 -
4.初识Pytorch之Tensorboard与Transforms搭配使用
这章是结合之前学习的Tensforboard与Transforms的一个练习。直接上代码:from PIL import Imagefrom torch.utils.tensorboard import SummaryWriterfrom torchvision import transformsimport osroot_path = "D:\\data\\basic\\Image"lable_path = "aligned"img_dir = os.path.join(root_pat原创 2021-12-26 21:06:29 · 567 阅读 · 0 评论 -
5.初识Pytorch使用常用的transforms
1.使用的是transforms中常用的Normalize方法,如下公式,其中mean为均值,std的标准差Normalize i.e.,output[channel] = (input[channel]-mean[channel])/std[channel]简单粗暴上代码:import os from torch.utils.tensorboard import SummaryWriterfrom torchvision import transformsfrom PIL import I原创 2021-12-27 14:40:33 · 409 阅读 · 0 评论 -
6.初识Pytorch之torchvision中的数据集使用
首先从下图——Pytorch官网可以看出,在torchvision提供的数据库很多,用红色框出。选择CIFAR-10数据库进行实验,其对应的官方文档如下:其参数有root:CIFAR10 在文件中,放置CIFAR10数据库的位置.train:是否是训练集,是就是True,否则就是测试集False.transform:使用了那种数据增强,要在前面先定义,这里就可以具体使用.target_transform:这个一般使用默认即可(目标的).download:是否下载,是True,否则Fals.原创 2021-12-27 17:01:35 · 900 阅读 · 3 评论 -
7.初识Pytorch使用Dataloader
首先,查看Pytorch官网的帮助文档DataLoader(dataset, batch_size=1, shuffle=False, sampler=None, batch_sampler=None, num_workers=0, collate_fn=None, pin_memory=False, drop_last=False, timeout=0, worker_init_fn=None, *, prefetch_factor=.原创 2021-12-27 23:14:31 · 554 阅读 · 0 评论 -
8.初识Pytorch之nn.Module神经网络基本架构的使用
首先,我们来看一段代码分析,nn.Module的整体用法简单粗暴上代码:import torch from torch import nnclass Example(nn.Module): def __init__(self): super().__init__() def forward(self,input): output = input + 1 return output if __name__ == "__main__": example = Example()原创 2021-12-28 17:25:04 · 1295 阅读 · 0 评论 -
9.初识Pytorch使用卷积层并对其进行可视化
首先,查看Pytorch官方文档 torch.nn.conv2d上一章原创 2021-12-29 16:31:33 · 1800 阅读 · 0 评论 -
10.初识Pytorch使用池化层并对其进行可视化
池化层的编程跟之前的卷积层类似。最大池化层,上代码:import torch.nn as nnimport torchimport torchvisionfrom torch.utils.data import DataLoaderfrom torch.utils.tensorboard import SummaryWriterfrom torchvision import transforms# 创建的单层最大池化层类# Pytorch中模型的三要素 1.nn.Modulecla原创 2021-12-29 22:45:12 · 1224 阅读 · 1 评论 -
11.初识Pytorch激活函数,线性层及其他层(Batch Normalization)等并尽可能对其进行可视化
跟之前的卷积层类似。ReLU激活函数,上代码:import torch.nn as nnimport torchvisionfrom torchvision import transformsfrom torch.utils.tensorboard import SummaryWriterfrom torch.utils.data import DataLoaderclass MyActivation(nn.Module): def __init__(self):原创 2021-12-30 14:57:11 · 1256 阅读 · 1 评论 -
12.初识Pytorch搭建网络 LeNet-5复现(含nn.Sequential用法)
运用上几章的知识点,搭建LeNet-5网络首先,看LeNet-5网络架构,如下图:声明 最后一层的高斯连接操作与全连接层的操作其作用是相似的,在我的代码复现时直接将高斯连接操作复现为全连接层操作。分析:out_size = 1+(in_size+2 * padding_size-kernel_size)/stride (1) 1. 卷积层1 input_1 1*32*32 -> output_1 6*28*28; in_chann原创 2022-01-05 21:08:42 · 1140 阅读 · 0 评论 -
13.初识Pytorch 复现VGG16及卷积神经网络图的可视化(Tensorboard)
搭建VGG16网络其网络架构图如上图(上图图为别人画的VGG16网络架构图,为了加深理解我自己再画一次如下图)原创 2022-01-13 00:34:16 · 1062 阅读 · 2 评论 -
14.初识Pytorch损失函数(Loss_funcations)
查看(view)Pytorch的官方文档(official documents)Loss_funcationsL1_lossL1_loss 数学理论支持(mathematical theory)L1_Loss=|x-y|.其中x是输入,y是我们的Ground Truth,L1_Loss是我们的L1损失函数的结果。并且,这里有两种reduction方式,一种是求平均,另一种是求和。where, x is input, y is target(Ground Truth), and L1_Los.原创 2022-01-20 11:58:25 · 1562 阅读 · 1 评论 -
15.初识Pytorch反向传播(Backward)与优化器(optimizer)SGD
之前我们已经提及如何使用数据,如何搭建网络,如何使用代价函数,现在我们讲反向传播与优化器。We have mentioned how to use the data, how to build the network, how to use the loss function, and now we talk about backpropagation and the optimizer. 为什么要使用反向传播? Why we use backpropagation?这是因为我们从网络架构原创 2022-01-21 01:02:47 · 2267 阅读 · 1 评论 -
16.初识Pytorch现有网络(existing network structures)的使用(use)与修改(modify)
在Pytorch官方文档中,给出了很多现成的网络架构供我们使用。例如 分类的 AlexNet,VGG, ResNet等In the official documentation of Pytorch, many ready-made network architectures are given for us to use. For example,For classification: AlexNet, VGG, ResNet, etc. 语义分割的 FCN,ResNet50等For原创 2022-01-21 18:41:45 · 897 阅读 · 1 评论 -
17.初识Pytorch保存模型(model save)与加载模型(model load)
模型的保存有两种方式:一种是保存模型;另一种是保存模型的参数,将参数以字典的形式保存(官方推荐)。There are two ways to save the model: one is to save the model; the other is to save the parameters of the model, and save the parameters in the form of a dictionary (official recommendation). code:impo原创 2022-01-21 19:26:24 · 2705 阅读 · 1 评论 -
18.初识Pytorch之完整的模型套路-合在一个.py文件中 Complete model routine - in one .py file
1.引入架包Introduce package import torch.optimimport torchvisionfrom torch.utils.tensorboard import SummaryWriterfrom torch import nnfrom torch.utils.data import DataLoader2.准备模型Ready modeluse LeNet-5 as the modelclass LeNet_5(nn.Module): de原创 2022-01-25 23:08:20 · 1809 阅读 · 1 评论 -
19.初识Pytorch之完整的模型套路-整理后的代码 Complete model routine - compiled code
**1**.Ready modelLeNet_5.pyfrom torch import nnclass LeNet_5(nn.Module): def __init__(self): super(LeNet_5, self).__init__() self.model = nn.Sequential( # input:3@32x32 # 6@28x28 nn.Conv2d(in原创 2022-01-28 22:31:23 · 3343 阅读 · 4 评论 -
20.初识Pytorch使用cuda对模型进行训练和测试或使用cuda对模型进行训练再用cpu测试Use cuda to train and test or use cpu test
如果使用cuda进行训练,则需要在以下三个地方进行修改,告诉计算机使用的是cuda,并且有两种方式(待会再讲):If using cuda for training, you need to modify the following three places to tell the computer to use cuda, and there are two ways (more on this later):1.网络结构1.Network structure2.损失函数2.Loss fun原创 2022-01-28 23:42:28 · 3331 阅读 · 0 评论