本站消息

站长简介/公众号


站长简介:逗比程序员,理工宅男,前每日优鲜python全栈开发工程师,利用周末时间开发出本站,欢迎关注我的微信公众号:幽默盒子,一个专注于搞笑,分享快乐的公众号

  价值13000svip视频教程,python大神匠心打造,零基础python开发工程师视频教程全套,基础+进阶+项目实战,包含课件和源码

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2020-07(10)

2020-08(50)

reid-strong-baseline 中的tricks

发布于2020-07-31 22:56     阅读(1091)     评论(0)     点赞(25)     收藏(1)



目录

Warmup:

Random Erasing Data Augmentation:


以config/defaults.py为例

  • Warmup:

学习率的变化:上升--平稳--下降

【开始训练以很小的学习率训练,随着训练学习率慢慢上升,再以一定的学习率平稳训练,最后在减小学习率】

  1. # warm up factor
  2. _C.SOLVER.WARMUP_FACTOR = 1.0 / 3
  3. # iterations of warm up
  4. _C.SOLVER.WARMUP_ITERS = 500
  5. # method of warm up, option: 'constant','linear'
  6. _C.SOLVER.WARMUP_METHOD = "linear"
  7. softmax_triplet_with_center.yml中:
  8. WARMUP_FACTOR: 0.01
  9. WARMUP_ITERS: 10
  10. WARMUP_METHOD: 'linear'

WarmupMultiStepLR( )函数

  1. # encoding: utf-8
  2. """
  3. @author: liaoxingyu
  4. @contact: sherlockliao01@gmail.com
  5. """
  6. from bisect import bisect_right
  7. import torch
  8. # FIXME ideally this would be achieved with a CombinedLRScheduler,
  9. # separating MultiStepLR with WarmupLR
  10. # but the current LRScheduler design doesn't allow it
  11. class WarmupMultiStepLR(torch.optim.lr_scheduler._LRScheduler):
  12. def __init__(
  13. self,
  14. optimizer,
  15. milestones,
  16. gamma=0.1,
  17. warmup_factor=1.0 / 3,
  18. warmup_iters=500,
  19. warmup_method="linear",
  20. last_epoch=-1,
  21. ):
  22. if not list(milestones) == sorted(milestones):
  23. raise ValueError(
  24. "Milestones should be a list of" " increasing integers. Got {}",
  25. milestones,
  26. )
  27. if warmup_method not in ("constant", "linear"):
  28. raise ValueError(
  29. "Only 'constant' or 'linear' warmup_method accepted"
  30. "got {}".format(warmup_method)
  31. )
  32. self.milestones = milestones
  33. self.gamma = gamma
  34. self.warmup_factor = warmup_factor #初始学习率
  35. self.warmup_iters = warmup_iters #停止epoch
  36. self.warmup_method = warmup_method
  37. super(WarmupMultiStepLR, self).__init__(optimizer, last_epoch)
  38. def get_lr(self):
  39. warmup_factor = 1
  40. if self.last_epoch < self.warmup_iters: #当还在使用warmup方法epoch范围内
  41. if self.warmup_method == "constant": #constant
  42. warmup_factor = self.warmup_factor #学习率在使用warmup之内保持恒定warmup_factor
  43. elif self.warmup_method == "linear": #linear
  44. alpha = self.last_epoch / self.warmup_iters #相当于计算线性变化的斜率
  45. warmup_factor = self.warmup_factor * (1 - alpha) + alpha #计算新的warmup_factor
  46. return [
  47. base_lr
  48. * warmup_factor
  49. * self.gamma ** bisect_right(self.milestones, self.last_epoch)
  50. for base_lr in self.base_lrs
  51. ]
  • Random Erasing Data Augmentation:

利用随机擦除的方式,将原数据集中的一部分样本随机擦除一个矩形区域。

  1. INPUT:
  2. SIZE_TRAIN: [384, 384]
  3. SIZE_TEST: [384, 384]
  4. PROB: 0.5 # random horizontal flip
  5. RE_PROB: 0.5 # random erasing 随机擦除的概率 一半一半
  6. PADDING: 10
  1. # encoding: utf-8
  2. """
  3. @author: liaoxingyu
  4. @contact: liaoxingyu2@jd.com
  5. """
  6. import math
  7. import random
  8. class RandomErasing(object):
  9. """ Randomly selects a rectangle region in an image and erases its pixels.
  10. 'Random Erasing Data Augmentation' by Zhong et al.
  11. See https://arxiv.org/pdf/1708.04896.pdf
  12. Args:
  13. probability: The probability that the Random Erasing operation will be performed.
  14. sl: Minimum proportion of erased area against input image.
  15. sh: Maximum proportion of erased area against input image.
  16. r1: Minimum aspect ratio of erased area.
  17. mean: Erasing value.
  18. """
  19. def __init__(self, probability=0.5, sl=0.02, sh=0.4, r1=0.3, mean=(0.4914, 0.4822, 0.4465)):
  20. self.probability = probability
  21. self.mean = mean
  22. self.sl = sl
  23. self.sh = sh
  24. self.r1 = r1
  25. def __call__(self, img):
  26. if random.uniform(0, 1) >= self.probability:#随机选择样本
  27. return img
  28. for attempt in range(100):
  29. area = img.size()[1] * img.size()[2]#图像的区域大小
  30. target_area = random.uniform(self.sl, self.sh) * area #擦除面积的比率
  31. aspect_ratio = random.uniform(self.r1, 1 / self.r1) #长宽比的上下界
  32. #求长宽area=wh,ar=h/w,所以:area*ar=h*h;area/ar=w*w
  33. h = int(round(math.sqrt(target_area * aspect_ratio)))
  34. w = int(round(math.sqrt(target_area / aspect_ratio)))
  35. #如果长宽满足要求,就设置为均值
  36. if w < img.size()[2] and h < img.size()[1]:
  37. #找到随机的一个矩形区域的左上角坐标,定矩形位置
  38. x1 = random.randint(0, img.size()[1] - h)
  39. y1 = random.randint(0, img.size()[2] - w)
  40. #根据通道数设置区域像素值
  41. if img.size()[0] == 3:
  42. img[0, x1:x1 + h, y1:y1 + w] = self.mean[0]
  43. img[1, x1:x1 + h, y1:y1 + w] = self.mean[1]
  44. img[2, x1:x1 + h, y1:y1 + w] = self.mean[2]
  45. else:
  46. img[0, x1:x1 + h, y1:y1 + w] = self.mean[0]
  47. return img
  48. return img

 






所属网站分类: 技术文章 > 博客

作者:丸子

链接:https://www.pythonheidong.com/blog/article/467269/eddd097eb70679431730/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

25 0
收藏该文
已收藏

评论内容:(最多支持255个字符)