深度学习入门
安装anaconda后,使用conda指令创建pytorch环境:
1 | conda create -n pytorch python=3.8.18 |
在pytorch命令行环境下安装pytorch:
1 | (pytorch) donn@Macc ~ % pip3 install torch torchvision torchaudio |
测试是否安装完成:
1 | 1.进入python环境 |
tensorboard使用:
–方便地用于记录训练过程中各个阶段的输入:
1 | tensorboard --logdir=logs --port=6007 |
安装opencv:
1 | pip install opencv-python |
获取numpy类型的图像:
1 | image_path = "dataset/train/ants_image/0013035.jpg" |
transforms结构及用法:
就是一个封装了对图片进行操作的工具库
transforms.py工具箱:
- totensor
- resize
- ……
tensor数据类型:
tensor数据类型含有神经网络中需要的各种参数
如何读取Numpy型图像数据:
1 | import cv2 |
读取PIL类型图像数据实例:
1 | from torch.utils.data import Dataset |
常见的transforms:
Normalize:
归一化操作
1 | # output[channel] = (input[channel] - mean[channel]) / std[channel] |
Resize:
缩放,输入为PIL数据类型
1 | # Resize |
Compose:
等比缩放(锁定长宽比)
1 | # Compose |
总结用法:
- 使用时先看源码,关注方法输入和输出的数据类型
- 不知道返回值数据类型时,print试试或者debug看看参数
- 多看官方文档
- 关注方法需要的参数
DataSet:
torchvision中的数据集使用:
1 | import torchvision |
Dataloader:
数据读取器:对数据集进行读取,可以设置每次读几个、每次读取时是否洗牌、余下的数据还要不要……
Parameters
- dataset (Dataset) – dataset from which to load the data.
- batch_size (int, optional) – how many samples per batch to load (default:
1
). - shuffle (bool, optional) – set to
True
to have the data reshuffled (洗牌) at every epoch (default:False
). - sampler (Sampler or Iterable, optional) – defines the strategy to draw samples from the dataset. Can be any
Iterable
with__len__
implemented. If specified,shuffle
must not be specified. - batch_sampler (Sampler or Iterable, optional) – like
sampler
, but returns a batch of indices at a time. Mutually exclusive withbatch_size
,shuffle
,sampler
, anddrop_last
. - num_workers (int, optional) – how many subprocesses to use for data loading.
0
means that the data will be loaded in the main process. (default:0
) - collate_fn (Callable, optional) – merges a list of samples to form a mini-batch of Tensor(s). Used when using batched loading from a map-style dataset.
- pin_memory (bool, optional) – If
True
, the data loader will copy Tensors into device/CUDA pinned memory before returning them. If your data elements are a custom type, or yourcollate_fn
returns a batch that is a custom type, see the example below. - drop_last (bool, optional) – set to
True
to drop the last incomplete batch, if the dataset size is not divisible by the batch size. IfFalse
and the size of dataset is not divisible by the batch size, then the last batch will be smaller. (default:False
) - timeout (numeric, optional) – if positive, the timeout value for collecting a batch from workers. Should always be non-negative. (default:
0
) - worker_init_fn (Callable, optional) – If not
None
, this will be called on each worker subprocess with the worker id (an int in[0, num_workers - 1]
) as input, after seeding and before data loading. (default:None
) - multiprocessing_context (str or multiprocessing.context.BaseContext, optional) – If
None
, the default multiprocessing context of your operating system will be used. (default:None
) - generator (torch.Generator, optional) – If not
None
, this RNG will be used by RandomSampler to generate random indexes and multiprocessing to generatebase_seed
for workers. (default:None
) - prefetch_factor (int, optional, keyword-only arg) – Number of batches loaded in advance by each worker.
2
means there will be a total of 2 * num_workers batches prefetched across all workers. (default value depends on the set value for num_workers. If value of num_workers=0 default isNone
. Otherwise, if value ofnum_workers > 0
default is2
). - persistent_workers (bool, optional) – If
True
, the data loader will not shut down the worker processes after a dataset has been consumed once. This allows to maintain the workers Dataset instances alive. (default:False
) - pin_memory_device (str, optional) – the device to
pin_memory
to ifpin_memory
isTrue
.
1 | import torch |
神经网络的基本骨架:
卷积层:
CONV2D:
CLASS: torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode=’zeros’, device=None, dtype=None)
in_channels (int) – Number of channels in the input image
- 输入图像的通道数
out_channels (int) – Number of channels produced by the convolution
- 输出图像的通道数 =
kernel_size (int or tuple) – Size of the convolving kernel
- 卷积核的size
stride (int or tuple, optional) – Stride of the convolution. Default: 1
- 卷积的步长
padding (int, tuple or str, optional) – Padding added to all four sides of the input. Default: 0
- 对输入图像周围需要填充数据的层数
池化层:
MAXPOOL2D:
- CLASS: torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
- kernel_size (Union[*int,* Tuple[*int,* int]]) – the size of the window to take a max over
- 池化窗口大小
- stride (Union[*int,* Tuple[*int,* int]]) – the stride of the window. Default value is
kernel_size
- 步长
- padding (Union[*int,* Tuple[*int,* int]]) – Implicit negative infinity padding to be added on both sides
- dilation (Union[*int,* Tuple[*int,* int]]) – a parameter that controls the stride of elements in the window
- return_indices (bool) – if
True
, will return the max indices along with the outputs. Useful fortorch.nn.MaxUnpool2d
later - ceil_mode (bool) – when True, will use ceil instead of floor to compute the output shape
- 用于决定在池化图像的边缘时,如果不够(池化窗口大小大于余下的区域时),是否还需要得出池化的数
- kernel_size (Union[*int,* Tuple[*int,* int]]) – the size of the window to take a max over
非线性激活:
给神经网络引入非线形特质
使用各种非线形的函数就用下面的模板代码即可
1 | # 例1 |
1 | # 例2 |
线形层:
给神经网络引入线形特质
损失函数与反向传播:
计算实际输出和目标之间的差距
为我们更新输出提供一定的依据(反向传播)
优化器:
1 | import torchvision.datasets |
现有网络模型的使用及修改
1 | import torchvision |
网络模型的保存与读取:
保存:
1 | import torch |
读取:
1 | import torch |
完整的模型训练、验证套路:
1 | import torchvision.datasets |
1 | # 搭建神经网络 |
利用GPU训练:
- 对网络模型、数据、损失函数使用 “ .cuda() ” 方法来使用GPU
1 | import torch |
- 对 网络模型、数据、损失函数 使用 .to(“设备名称”) 即可