001-torch normal used function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
## 1. 卷积层

### `torch.nn.Conv2d`
二维卷积层
输入的尺度是`(N, Cin, H, W)`,输出尺度`(N, Cout, Hout, Wout)`

```python
nn.Conv2d(
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
dilation=1,
groups=1,
bias=True
)

Parameters:

  • in_channels (int) - 输入信号的通道数
  • out_channels (int) - 卷积产生的通道数
  • kernel_size (int or tuple) - 卷积核的尺寸
  • stride (int or tuple, optional) - 卷积步长,默认为1
  • padding (int or tuple, optional) - 输入的每一条边补充0的层数,默认为0
  • dilation (int or tuple, optional) - 卷积核元素之间的间距,默认为1
  • groups (int, optional) - 从输入通道到输出通道的阻塞连接数,默认为1
  • bias (bool, optional) - 如果bias=True,添加可学习的偏置到输出中

torch.nn.ConvTranspose2d

对由多个输入平面组成的输入图像应用二维转置卷积操作

1
2
3
4
5
6
7
8
9
10
11
nn.ConvTranspose2d(
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
output_padding=0,
groups=1,
bias=True,
dilation=1
)

Parameters:

  • in_channels (int) - 输入信号的通道数
  • out_channels (int) - 卷积产生的通道数
  • kernel_size (int or tuple) - 卷积核的大小
  • stride (int or tuple, optional) - 卷积步长
  • padding (int or tuple, optional) - 输入的每一条边补充padding = kernel - 1 - padding,即(kernel_size - 1)/2个0的层数
  • output_padding (int or tuple, optional) - 在输出的每一个维度的一边补充0的层数
  • dilation (int or tuple, optional) - 卷积核元素之间的间距
  • groups (int, optional) - 从输入通道到输出通道的阻塞连接数
  • bias (bool, optional) - 如果bias=True,添加偏置

torch.utils.data.DataLoader

1
2
3
4
5
6
7
8
9
10
11
12
13
torch.utils.data.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
)

Parameters:

  • dataset (Dataset): 传入的数据集
  • batch_size (int, optional): 每个batch有多少个样本
  • shuffle (bool, optional): 在每个epoch开始的时候,对数据进行重新排序
  • sampler (Sampler, optional): 自定义从数据集中取样本的策略(与shuffle互斥)
  • batch_sampler (Sampler, optional): 与sampler类似,但一次只返回一个batch的indices(与batch_size/shuffle/sampler互斥)
  • num_workers (int, optional): 处理data loading的进程数(0表示在主进程加载)
  • collate_fn (callable, optional): 将list的sample组成mini-batch的函数
  • pin_memory (bool, optional): 设置为True时会将tensors拷贝到CUDA固定内存中
  • drop_last (bool, optional): 是否丢弃最后一个不完整的batch(默认为False)
  • timeout (numeric, optional): 收集batch的等待时间(>=0)
  • worker_init_fn (callable, optional): 每个worker的初始化函数

Torchvision Transforms

1
2
3
4
transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean, std)
])
  1. torchvision.transforms.Compose()
    主要作用是串联多个图片变换的操作

  2. torchvision.transforms.ToTensor()

    • 将shape为(H, W, C)的numpy.ndarray或PIL Image转为shape为(C, H, W)的tensor
    • 将数值归一化到[0,1](直接除以255)
  3. torchvision.transforms.Normalize()
    对每个通道执行:image = (image - mean) / std
    (ToTensor把0-255变换到0-1,Normalize再把0-1变换到(-1,1))