我才接触到的深度学习的一系列不那么相关的概念weight decay ,crop等等概念

来源:互联网 发布:阿里云 新网 编辑:程序博客网 时间:2024/06/05 21:54

一、weight decay(权值衰减)的使用既不是为了提高你所说的收敛精确度也不是为了提高收敛速度,其最终目的是防止过拟合。在损失函数中,weight decay是放在正则项(regularization)前面的一个系数,正则项一般指示模型的复杂度,所以weight decay的作用是调节模型复杂度对损失函数的影响,若weight decay很大,则复杂的模型损失函数的值也就大。

二、题主的意思应该是batch normalization吧。batch normalization的是指在神经网络中激活函数的前面,将wx+b按照特征进行normalization,这样做的好处有三点:
1、提高梯度在网络中的流动。Normalization能够使特征全部缩放到[0,1],这样在反向传播时候的梯度都是在1左右,避免了梯度消失现象。
2、提升学习速率。归一化后的数据能够快速的达到收敛。
3、减少模型训练对初始化的依赖。

PyTorch 数据处理模块:(学习crop等概念)

PyTorch 的数据处理模块是 torchvision.transform,只对 PIL.Image 或维度为 (H, W, C) 的图片数据进行数据预处理。由于 OpenCV 读入图片的数据维度是 (H, W, C),所以不能直接使用 torchvision.transform 处理 OpenCV 的图片数据。


1. class torchvision.transforms.Compose(transforms)

输入一个 transform 列表,将多个 transform 组合使用。

  1. transforms.Compose([transforms.CenterCrop(10),
  2. transfroms.ToTensor()])

2. class torchvision.transforms.Scale(size, interpolation=2)

将输入的 PIL.Image 重新改变大小成给定的 size,其中 size 是最小边长。举一个例子,如果原图的 height > width,那么改变大小后的图片大小是 (size*height\width, size)

  1. form torchvision import transforms
  2. from PIL import Image
  3. crop = transforms.Scale(12)
  4. img = Image.open('test.jpg')
  5. print type(img)
  6. print img.size
  7. croped_img = crop(img)
  8. print type(crop_img)
  9. print type(crop_img.size)

输出结果为:

  1. <class 'PIL.PngImagePlugin.PngImageFile'>
  2. (10, 10, 3)
  3. <class 'PIL.Image.Image'>
  4. (12, 12, 3)

3. class torchvision.transforms.CenterCrop(size)

将给定的 PIL.Image 进行中心切割,得到给定的 sizesize 可以是 tuple(target_height, target_width)size 也可以是一个 Integer,在这种情况下,切出来的图片形状是正方形。


4. class torchvision.transforms.RandomCrop(size, padding=0)

切割中心点的位置随机选取。size 可以是 tuple 也可以是 Integer


5. class torchvision.transforms.RandomHorizontalFlip

随机水平翻转给定的 PIL.Image,概率为 0.5。即:一半的概率翻转,一半的概率不翻转。


6. class torchvision.transforms.RandomSizedCrop(size, interpolation=2)

先将给定的 PIL.Image 随机切,然后再 resize 成给定的 size 大小


7. class torchvision.transforms.Pad(padding, fill=0)

将给定的 PIL.Image 的所有边给定的 pad value 填充。padding:要填充多少像素,fill: 用什么值填充

  1. from torchvision import transforms
  2. form PIL import Image
  3. padding_img = transforms.Pad(padding=10, fill=0)
  4. img = Image.open('test.jpg')
  5. print type(img)
  6. print img.size
  7. padded_img = padding(img)
  8. print type(padded_img)
  9. print type(padded_img.size)

输出结果为:

  1. <class 'PIL.PngImagePlugin.PngImageFile'>
  2. (10, 10, 3)
  3. <class 'PIL.Image.Image'>
  4. (30, 30, 3) # 由于上下左右都要填充 10 个像素

8. class torchvision.transforms.Normalize(mean, std)

给定均值:mean = (R, G, B),方差:std = (R, G, B),将会把 Tensor 归一化。即:Normalized_image = (image - mean) / std

9. class torchvision.transforms.ToTensor

把一个取值范围是 [0, 255] 的 PIL.Image 或者 shape 为 (H, W, C) 的 numpy.ndarray,转换为形状为 (C, H, W),取值范围是 [0, 1] 的 torch.FloatTensor

  1. data = np.random.randint(0, 255, size=300)
  2. img = data.reshape(10, 10, 3)
  3. print img.shape
  4. img_tensor = transforms.ToTensor()(img) # 转换成 tensor
  5. print img_tensor

输出结果为:

  1. (10, 10, 3)
  2. (3, 10, 10)

10. class torchvision.transforms.ToPILImage

将 shape 为 (C, H, W) 的 Tensor 或者 shape 为 (H, W, C) 的 numpy.ndarray 转换成 PIL.Image,值不变。

11. class torchvision.transforms.Lambda(lambda)

通用变换,使用 lambda 作为转换器