params_v2.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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('PPYOLOTiny'):
  36. batch_size = (per_gpu_memory - 579) // 1025 * num_gpu
  37. batch_size = min(batch_size, num_gpu * 16)
  38. elif model_type.startswith('PPYOLO'):
  39. batch_size = (per_gpu_memory - 1691) // 1025 * num_gpu
  40. batch_size = min(batch_size, num_gpu * 4)
  41. elif model_type.startswith('FasterRCNN'):
  42. batch_size = (per_gpu_memory - 1755) // 915 * num_gpu
  43. batch_size = min(batch_size, num_gpu * 2)
  44. elif model_type.startswith('MaskRCNN'):
  45. batch_size = (per_gpu_memory - 2702) // 1188 * num_gpu
  46. batch_size = min(batch_size, num_gpu * 2)
  47. elif model_type.startswith('DeepLab'):
  48. batch_size = (per_gpu_memory - 1469) // 1605 * num_gpu
  49. batch_size = min(batch_size, num_gpu * 4)
  50. elif model_type.startswith('BiSeNetV2'):
  51. batch_size = (per_gpu_memory - 591) // 1605 * num_gpu
  52. batch_size = min(batch_size, num_gpu * 4)
  53. elif model_type.startswith('UNet'):
  54. batch_size = (per_gpu_memory - 1275) // 1256 * num_gpu
  55. batch_size = min(batch_size, num_gpu * 4)
  56. elif model_type.startswith('HRNet_W18'):
  57. batch_size = (per_gpu_memory - 800) // 682 * num_gpu
  58. batch_size = min(batch_size, num_gpu * 4)
  59. elif model_type.startswith('FastSCNN'):
  60. batch_size = (per_gpu_memory - 636) // 144 * num_gpu
  61. batch_size = min(batch_size, num_gpu * 4)
  62. if batch_size > num_train_samples // 2:
  63. batch_size = num_train_samples // 2
  64. if batch_size < 1:
  65. batch_size = 1
  66. brightness_range = 0.5
  67. contrast_range = 0.5
  68. saturation_range = 0.5
  69. saturation = False
  70. hue = False
  71. if model_type.startswith('DenseNet') or model_type.startswith('ResNet') \
  72. or model_type.startswith('Xception') or model_type.startswith('DarkNet') \
  73. or model_type.startswith('ShuffleNet') or model_type.startswith('MobileNet'):
  74. if model_type.startswith('MobileNet'):
  75. lr = (batch_size / 500.0) * 0.1
  76. else:
  77. lr = (batch_size / 256.0) * 0.1
  78. shape = [224, 224]
  79. save_interval_epochs = 5
  80. num_epochs = 10
  81. lr_decay_epochs = [4, 6, 8]
  82. image_mean = [0.485, 0.456, 0.406]
  83. image_std = [0.229, 0.224, 0.225]
  84. brightness_range = 0.9
  85. contrast_range = 0.9
  86. saturation_range = 0.9
  87. brightness = True
  88. contrast = True
  89. elif model_type.startswith('YOLOv3'):
  90. shape = [608, 608]
  91. save_interval_epochs = 30
  92. num_epochs = 270
  93. lr_decay_epochs = [216, 243]
  94. lr = .005 / 12 * batch_size / 8
  95. image_mean = [0.485, 0.456, 0.406]
  96. image_std = [0.229, 0.224, 0.225]
  97. brightness = True
  98. contrast = True
  99. saturation = True
  100. hue = True
  101. num_steps_each_epoch = num_train_samples // batch_size
  102. min_warmup_step = max(3 * num_steps_each_epoch, 50 * num_classes)
  103. if num_gpu == 0:
  104. num_gpu = 1
  105. warmup_step = min(min_warmup_step, int(400 * num_classes / num_gpu))
  106. elif model_type.startswith('PPYOLOTiny'):
  107. shape = [320, 320]
  108. save_interval_epochs = 30
  109. num_epochs = 550
  110. lr_decay_epochs = [130, 540]
  111. lr = .005 * batch_size / 16
  112. image_mean = [0.485, 0.456, 0.406]
  113. image_std = [0.229, 0.224, 0.225]
  114. brightness = True
  115. contrast = True
  116. saturation = True
  117. hue = True
  118. num_steps_each_epoch = num_train_samples // batch_size
  119. min_warmup_step = max(3 * num_steps_each_epoch, 50 * num_classes)
  120. if num_gpu == 0:
  121. num_gpu = 1
  122. warmup_step = min(min_warmup_step, int(400 * num_classes / num_gpu))
  123. elif model_type.startswith('PPYOLOv2'):
  124. shape = [608, 608]
  125. save_interval_epochs = 30
  126. num_epochs = 170
  127. lr_decay_epochs = [105, 135, 150]
  128. lr = .005 / 12 * batch_size / 8
  129. image_mean = [0.485, 0.456, 0.406]
  130. image_std = [0.229, 0.224, 0.225]
  131. brightness = True
  132. contrast = True
  133. saturation = True
  134. hue = True
  135. num_steps_each_epoch = num_train_samples // batch_size
  136. min_warmup_step = max(3 * num_steps_each_epoch, 50 * num_classes)
  137. if num_gpu == 0:
  138. num_gpu = 1
  139. warmup_step = min(min_warmup_step, int(400 * num_classes / num_gpu))
  140. elif model_type.startswith('PPYOLO'):
  141. shape = [608, 608]
  142. save_interval_epochs = 30
  143. num_epochs = 200
  144. lr_decay_epochs = [85, 135]
  145. lr = .005 / 12 * batch_size / 8
  146. image_mean = [0.485, 0.456, 0.406]
  147. image_std = [0.229, 0.224, 0.225]
  148. brightness = True
  149. contrast = True
  150. saturation = True
  151. hue = True
  152. num_steps_each_epoch = num_train_samples // batch_size
  153. min_warmup_step = max(3 * num_steps_each_epoch, 50 * num_classes)
  154. if num_gpu == 0:
  155. num_gpu = 1
  156. warmup_step = min(min_warmup_step, int(400 * num_classes / num_gpu))
  157. elif model_type.startswith('FasterRCNN') or model_type.startswith(
  158. 'MaskRCNN'):
  159. shape = [800, 1333]
  160. save_interval_epochs = 1
  161. num_epochs = 12
  162. lr_decay_epochs = [8, 11]
  163. image_mean = [0.485, 0.456, 0.406]
  164. image_std = [0.229, 0.224, 0.225]
  165. brightness = False
  166. contrast = False
  167. lr = 0.02 * batch_size / 16
  168. num_steps_each_epoch = num_train_samples // batch_size
  169. min_warmup_step = max(num_steps_each_epoch, 50)
  170. if num_gpu == 0:
  171. num_gpu = 1
  172. warmup_step = min(min_warmup_step, int(4000 / num_gpu))
  173. elif model_type.startswith('DeepLab') or model_type.startswith('UNet') \
  174. or model_type.startswith('HRNet_W18') or model_type.startswith('FastSCNN') or model_type.startswith('BiSeNetV2'):
  175. shape = [512, 512]
  176. save_interval_epochs = 10
  177. num_epochs = 100
  178. lr_decay_epochs = [10, 20]
  179. image_mean = [0.5, 0.5, 0.5]
  180. image_std = [0.5, 0.5, 0.5]
  181. brightness = False
  182. contrast = False
  183. lr = 0.01 * batch_size / 2
  184. loss_type = [False, False]
  185. if 'loss_type' in params:
  186. params['loss_type'] = loss_type
  187. params['batch_size'] = batch_size
  188. params['learning_rate'] = lr
  189. params["image_shape"] = shape
  190. params['save_interval_epochs'] = save_interval_epochs
  191. params['num_epochs'] = num_epochs
  192. params['lr_decay_epochs'] = lr_decay_epochs
  193. params['resume_checkpoint'] = None
  194. params["sensitivities_path"] = None
  195. params["image_mean"] = image_mean
  196. params["image_std"] = image_std
  197. params["horizontal_flip_prob"] = 0.5
  198. params['brightness'] = brightness
  199. params["brightness_range"] = brightness_range
  200. params["brightness_prob"] = 0.5
  201. params['contrast'] = contrast
  202. params['contrast_range'] = contrast_range
  203. params['contrast_prob'] = 0.5
  204. params['saturation'] = saturation
  205. params['saturation_range'] = saturation_range
  206. params['saturation_prob'] = 0.5
  207. params['hue'] = hue
  208. params['hue_range'] = 18
  209. params['hue_prob'] = 0.5
  210. params['horizontal_flip'] = True
  211. if model_type in [
  212. 'YOLOv3', 'PPYOLO', 'PPYOLOTiny', 'PPYOLOv2', 'FasterRCNN',
  213. 'MaskRCNN'
  214. ]:
  215. num_epochs = params['num_epochs']
  216. lr_decay_epochs = params['lr_decay_epochs']
  217. if warmup_step >= lr_decay_epochs[0] * num_steps_each_epoch:
  218. for i in range(len(lr_decay_epochs)):
  219. lr_decay_epochs[i] += warmup_step // num_steps_each_epoch
  220. num_epochs += warmup_step // num_steps_each_epoch
  221. params['num_epochs'] = num_epochs
  222. params['lr_decay_epochs'] = lr_decay_epochs
  223. params['warmup_steps'] = warmup_step
  224. return params
  225. def get_classification_params(params):
  226. params["pretrain_weights"] = 'IMAGENET'
  227. params["lr_policy"] = "Piecewise"
  228. params['vertical_flip_prob'] = 0.5
  229. params['vertical_flip'] = True
  230. params['rotate'] = True
  231. params['rotate_prob'] = 0.5
  232. params['rotate_range'] = 30
  233. return params
  234. def get_detection_params(params):
  235. params['with_fpn'] = True
  236. params["pretrain_weights"] = 'IMAGENET'
  237. if params['model'].startswith('YOLOv3') or params['model'].startswith(
  238. 'PPYOLO'):
  239. if params['model'].startswith('YOLOv3'):
  240. params['backbone'] = 'DarkNet53'
  241. elif params['model'].startswith('PPYOLO'):
  242. params['backbone'] = 'ResNet50_vd_ssld'
  243. params['use_mixup'] = True
  244. params['mixup_alpha'] = 1.5
  245. params['mixup_beta'] = 1.5
  246. params['expand_prob'] = 0.5
  247. params['expand_image'] = True
  248. params['crop_image'] = True
  249. params['random_shape'] = True
  250. params['random_shape_sizes'] = [
  251. 320, 352, 384, 416, 448, 480, 512, 544, 576, 608
  252. ]
  253. elif params['model'].startswith('FasterRCNN') or params[
  254. 'model'].startswith('MaskRCNN'):
  255. params['backbone'] = 'ResNet50'
  256. return params
  257. def get_segmentation_params(params):
  258. if params['model'].startswith('DeepLab'):
  259. params['backbone'] = 'Xception65'
  260. params["pretrain_weights"] = 'IMAGENET'
  261. elif params['model'].startswith('UNet'):
  262. params["pretrain_weights"] = 'COCO'
  263. elif params['model'].startswith('HRNet_W18') or params['model'].startswith(
  264. 'FastSCNN'):
  265. params["pretrain_weights"] = 'CITYSCAPES'
  266. params['loss_type'] = [False, False]
  267. params['lr_policy'] = 'Polynomial'
  268. params['optimizer'] = 'SGD'
  269. params['blur'] = False
  270. params['blur_prob'] = 0.1
  271. params['rotate'] = False
  272. params['scale_aspect'] = False
  273. params['min_ratio'] = 0.5
  274. params['aspect_ratio'] = 0.33
  275. params['vertical_flip'] = False
  276. params['vertical_flip_prob'] = 0.5
  277. return params
  278. def get_params(data, project_type, num_train_samples, num_classes, num_gpu,
  279. per_gpu_memory, gpu_list):
  280. if project_type == "classification":
  281. if 'model_type' in data:
  282. model_type = data['model_type']
  283. else:
  284. model_type = "MobileNetV2"
  285. params = get_base_params(model_type, per_gpu_memory, num_train_samples,
  286. num_gpu, gpu_list, num_classes)
  287. return get_classification_params(params)
  288. if project_type == "detection":
  289. if 'model_type' in data:
  290. model_type = data['model_type']
  291. else:
  292. model_type = "YOLOv3"
  293. params = get_base_params(model_type, per_gpu_memory, num_train_samples,
  294. num_gpu, gpu_list, num_classes)
  295. return get_detection_params(params)
  296. if project_type == "instance_segmentation":
  297. if 'model_type' in data:
  298. model_type = data['model_type']
  299. else:
  300. model_type = "MaskRCNN"
  301. params = get_base_params(model_type, per_gpu_memory, num_train_samples,
  302. num_gpu, gpu_list, num_classes)
  303. return get_detection_params(params)
  304. if project_type == 'segmentation' or project_type == "remote_segmentation":
  305. if 'model_type' in data:
  306. model_type = data['model_type']
  307. else:
  308. model_type = "DeepLabv3+"
  309. params = get_base_params(model_type, per_gpu_memory, num_train_samples,
  310. num_gpu, gpu_list, num_classes)
  311. return get_segmentation_params(params)