params_v2.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. def get_base_params(model_type, per_gpu_memory, num_train_samples, num_gpu,
  15. gpu_list, num_classes):
  16. params = dict()
  17. params["model"] = model_type
  18. params["cpu_num"] = 1
  19. if num_gpu == 0:
  20. batch_size = 4
  21. params['cuda_visible_devices'] = ''
  22. else:
  23. params['cuda_visible_devices'] = str(gpu_list).strip("[]")
  24. if model_type.startswith('MobileNet'):
  25. batch_size = (per_gpu_memory - 513) // 57 * num_gpu
  26. batch_size = min(batch_size, num_gpu * 125)
  27. elif model_type.startswith('DenseNet') or model_type.startswith('ResNet') \
  28. or model_type.startswith('Xception') or model_type.startswith('DarkNet') \
  29. or model_type.startswith('ShuffleNet'):
  30. batch_size = (per_gpu_memory - 739) // 211 * num_gpu
  31. batch_size = min(batch_size, num_gpu * 16)
  32. elif model_type.startswith('YOLOv3'):
  33. batch_size = (per_gpu_memory - 1555) // 943 * num_gpu
  34. batch_size = min(batch_size, num_gpu * 8)
  35. elif model_type.startswith('PPYOLO'):
  36. batch_size = (per_gpu_memory - 1691) // 1025 * num_gpu
  37. batch_size = min(batch_size, num_gpu * 8)
  38. elif model_type.startswith('FasterRCNN'):
  39. batch_size = (per_gpu_memory - 1755) // 915 * num_gpu
  40. batch_size = min(batch_size, num_gpu * 2)
  41. elif model_type.startswith('MaskRCNN'):
  42. batch_size = (per_gpu_memory - 2702) // 1188 * num_gpu
  43. batch_size = min(batch_size, num_gpu * 2)
  44. elif model_type.startswith('DeepLab'):
  45. batch_size = (per_gpu_memory - 1469) // 1605 * num_gpu
  46. batch_size = min(batch_size, num_gpu * 4)
  47. elif model_type.startswith('UNet'):
  48. batch_size = (per_gpu_memory - 1275) // 1256 * num_gpu
  49. batch_size = min(batch_size, num_gpu * 4)
  50. elif model_type.startswith('HRNet_W18'):
  51. batch_size = (per_gpu_memory - 800) // 682 * num_gpu
  52. batch_size = min(batch_size, num_gpu * 4)
  53. elif model_type.startswith('FastSCNN'):
  54. batch_size = (per_gpu_memory - 636) // 144 * num_gpu
  55. batch_size = min(batch_size, num_gpu * 4)
  56. if batch_size > num_train_samples // 2:
  57. batch_size = num_train_samples // 2
  58. if batch_size < 1:
  59. batch_size = 1
  60. brightness_range = 0.5
  61. contrast_range = 0.5
  62. saturation_range = 0.5
  63. saturation = False
  64. hue = False
  65. if model_type.startswith('DenseNet') or model_type.startswith('ResNet') \
  66. or model_type.startswith('Xception') or model_type.startswith('DarkNet') \
  67. or model_type.startswith('ShuffleNet') or model_type.startswith('MobileNet'):
  68. if model_type.startswith('MobileNet'):
  69. lr = (batch_size / 500.0) * 0.1
  70. else:
  71. lr = (batch_size / 256.0) * 0.1
  72. shape = [224, 224]
  73. save_interval_epochs = 5
  74. num_epochs = 120
  75. lr_decay_epochs = [30, 60, 90]
  76. image_mean = [0.485, 0.456, 0.406]
  77. image_std = [0.229, 0.224, 0.225]
  78. brightness_range = 0.9
  79. contrast_range = 0.9
  80. saturation_range = 0.9
  81. brightness = True
  82. contrast = True
  83. elif model_type.startswith('YOLOv3') or model_type.startswith('PPYOLO'):
  84. shape = [608, 608]
  85. save_interval_epochs = 30
  86. num_epochs = 270
  87. lr_decay_epochs = [210, 240]
  88. lr = 0.001 * batch_size / 64
  89. image_mean = [0.485, 0.456, 0.406]
  90. image_std = [0.229, 0.224, 0.225]
  91. brightness = True
  92. contrast = True
  93. saturation = True
  94. hue = True
  95. num_steps_each_epoch = num_train_samples // batch_size
  96. min_warmup_step = max(3 * num_steps_each_epoch, 50 * num_classes)
  97. if num_gpu == 0:
  98. num_gpu = 1
  99. warmup_step = min(min_warmup_step, int(400 * num_classes / num_gpu))
  100. elif model_type.startswith('FasterRCNN') or model_type.startswith(
  101. 'MaskRCNN'):
  102. shape = [800, 1333]
  103. save_interval_epochs = 1
  104. num_epochs = 12
  105. lr_decay_epochs = [8, 11]
  106. image_mean = [0.485, 0.456, 0.406]
  107. image_std = [0.229, 0.224, 0.225]
  108. brightness = False
  109. contrast = False
  110. lr = 0.02 * batch_size / 16
  111. num_steps_each_epoch = num_train_samples // batch_size
  112. min_warmup_step = max(num_steps_each_epoch, 50)
  113. if num_gpu == 0:
  114. num_gpu = 1
  115. warmup_step = min(min_warmup_step, int(4000 / num_gpu))
  116. elif model_type.startswith('DeepLab') or model_type.startswith('UNet') \
  117. or model_type.startswith('HRNet_W18') or model_type.startswith('FastSCNN'):
  118. shape = [512, 512]
  119. save_interval_epochs = 10
  120. num_epochs = 100
  121. lr_decay_epochs = [10, 20]
  122. image_mean = [0.5, 0.5, 0.5]
  123. image_std = [0.5, 0.5, 0.5]
  124. brightness = False
  125. contrast = False
  126. lr = 0.01 * batch_size / 2
  127. params['batch_size'] = batch_size
  128. params['learning_rate'] = lr
  129. params["image_shape"] = shape
  130. params['save_interval_epochs'] = save_interval_epochs
  131. params['num_epochs'] = num_epochs
  132. params['lr_decay_epochs'] = lr_decay_epochs
  133. params['resume_checkpoint'] = None
  134. params["sensitivities_path"] = None
  135. params["image_mean"] = image_mean
  136. params["image_std"] = image_std
  137. params["horizontal_flip_prob"] = 0.5
  138. params['brightness'] = brightness
  139. params["brightness_range"] = brightness_range
  140. params["brightness_prob"] = 0.5
  141. params['contrast'] = contrast
  142. params['contrast_range'] = contrast_range
  143. params['contrast_prob'] = 0.5
  144. params['saturation'] = saturation
  145. params['saturation_range'] = saturation_range
  146. params['saturation_prob'] = 0.5
  147. params['hue'] = hue
  148. params['hue_range'] = 18
  149. params['hue_prob'] = 0.5
  150. params['horizontal_flip'] = True
  151. if model_type in ['YOLOv3', 'PPYOLO', 'FasterRCNN', 'MaskRCNN']:
  152. num_epochs = params['num_epochs']
  153. lr_decay_epochs = params['lr_decay_epochs']
  154. if warmup_step >= lr_decay_epochs[0] * num_steps_each_epoch:
  155. for i in range(len(lr_decay_epochs)):
  156. lr_decay_epochs[i] += warmup_step // num_steps_each_epoch
  157. num_epochs += warmup_step // num_steps_each_epoch
  158. params['num_epochs'] = num_epochs
  159. params['lr_decay_epochs'] = lr_decay_epochs
  160. params['warmup_steps'] = warmup_step
  161. return params
  162. def get_classification_params(params):
  163. params["pretrain_weights"] = 'IMAGENET'
  164. params["lr_policy"] = "Piecewise"
  165. params['vertical_flip_prob'] = 0.5
  166. params['vertical_flip'] = True
  167. params['rotate'] = True
  168. params['rotate_prob'] = 0.5
  169. params['rotate_range'] = 30
  170. return params
  171. def get_detection_params(params):
  172. params['with_fpn'] = True
  173. params["pretrain_weights"] = 'IMAGENET'
  174. if params['model'].startswith('YOLOv3') or params['model'].startswith(
  175. 'PPYOLO'):
  176. if params['model'].startswith('YOLOv3'):
  177. params['backbone'] = 'DarkNet53'
  178. elif params['model'].startswith('PPYOLO'):
  179. params['backbone'] = 'ResNet50_vd_ssld'
  180. params['use_mixup'] = True
  181. params['mixup_alpha'] = 1.5
  182. params['mixup_beta'] = 1.5
  183. params['expand_prob'] = 0.5
  184. params['expand_image'] = True
  185. params['crop_image'] = True
  186. params['random_shape'] = True
  187. params['random_shape_sizes'] = [
  188. 320, 352, 384, 416, 448, 480, 512, 544, 576, 608
  189. ]
  190. elif params['model'].startswith('FasterRCNN') or params[
  191. 'model'].startswith('MaskRCNN'):
  192. params['backbone'] = 'ResNet50'
  193. return params
  194. def get_segmentation_params(params):
  195. if params['model'].startswith('DeepLab'):
  196. params['backbone'] = 'Xception65'
  197. params["pretrain_weights"] = 'IMAGENET'
  198. elif params['model'].startswith('UNet'):
  199. params["pretrain_weights"] = 'COCO'
  200. elif params['model'].startswith('HRNet_W18') or params['model'].startswith(
  201. 'FastSCNN'):
  202. params["pretrain_weights"] = 'CITYSCAPES'
  203. params['loss_type'] = [False, False]
  204. params['lr_policy'] = 'Polynomial'
  205. params['optimizer'] = 'SGD'
  206. params['blur'] = False
  207. params['blur_prob'] = 0.1
  208. params['rotate'] = False
  209. params['max_rotation'] = 15
  210. params['scale_aspect'] = False
  211. params['min_ratio'] = 0.5
  212. params['aspect_ratio'] = 0.33
  213. params['vertical_flip'] = False
  214. params['vertical_flip_prob'] = 0.5
  215. return params
  216. def get_params(data, project_type, num_train_samples, num_classes, num_gpu,
  217. per_gpu_memory, gpu_list):
  218. if project_type == "classification":
  219. if 'model_type' in data:
  220. model_type = data['model_type']
  221. else:
  222. model_type = "MobileNetV2"
  223. params = get_base_params(model_type, per_gpu_memory, num_train_samples,
  224. num_gpu, gpu_list, num_classes)
  225. return get_classification_params(params)
  226. if project_type == "detection":
  227. if 'model_type' in data:
  228. model_type = data['model_type']
  229. else:
  230. model_type = "YOLOv3"
  231. params = get_base_params(model_type, per_gpu_memory, num_train_samples,
  232. num_gpu, gpu_list, num_classes)
  233. return get_detection_params(params)
  234. if project_type == "instance_segmentation":
  235. if 'model_type' in data:
  236. model_type = data['model_type']
  237. else:
  238. model_type = "MaskRCNN"
  239. params = get_base_params(model_type, per_gpu_memory, num_train_samples,
  240. num_gpu, gpu_list, num_classes)
  241. return get_detection_params(params)
  242. if project_type == 'segmentation' or project_type == "remote_segmentation":
  243. if 'model_type' in data:
  244. model_type = data['model_type']
  245. else:
  246. model_type = "DeepLabv3+"
  247. params = get_base_params(model_type, per_gpu_memory, num_train_samples,
  248. num_gpu, gpu_list, num_classes)
  249. return get_segmentation_params(params)