seg.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  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. from . import cv
  15. from .cv.models.utils.visualize import visualize_segmentation
  16. import paddlex.utils.logging as logging
  17. visualize = visualize_segmentation
  18. class UNet(cv.models.UNet):
  19. def __init__(self,
  20. num_classes=2,
  21. upsample_mode='bilinear',
  22. use_bce_loss=False,
  23. use_dice_loss=False,
  24. class_weight=None,
  25. ignore_index=None,
  26. input_channel=None):
  27. if num_classes > 2 and (use_bce_loss or use_dice_loss):
  28. raise ValueError(
  29. "dice loss and bce loss is only applicable to binary classification"
  30. )
  31. elif num_classes == 2:
  32. if use_bce_loss and use_dice_loss:
  33. use_mixed_loss = [('CrossEntropyLoss', 1), ('DiceLoss', 1)]
  34. elif use_bce_loss:
  35. use_mixed_loss = [('CrossEntropyLoss', 1)]
  36. elif use_dice_loss:
  37. use_mixed_loss = [('DiceLoss', 1)]
  38. else:
  39. use_mixed_loss = False
  40. else:
  41. use_mixed_loss = False
  42. if class_weight is not None:
  43. logging.warning(
  44. "`class_weight` is not supported in PaddleX 2.0 currently and is forcibly set to None."
  45. )
  46. if ignore_index is not None:
  47. logging.warning(
  48. "`ignore_index` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 255."
  49. )
  50. if input_channel is not None:
  51. logging.warning(
  52. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  53. )
  54. if upsample_mode == 'bilinear':
  55. use_deconv = False
  56. else:
  57. use_deconv = True
  58. super(UNet, self).__init__(
  59. num_classes=num_classes,
  60. use_mixed_loss=use_mixed_loss,
  61. use_deconv=use_deconv)
  62. class DeepLabV3P(cv.models.DeepLabV3P):
  63. def __init__(self,
  64. num_classes=2,
  65. backbone='ResNet50_vd',
  66. output_stride=8,
  67. aspp_with_sep_conv=None,
  68. decoder_use_sep_conv=None,
  69. encoder_with_aspp=None,
  70. enable_decoder=None,
  71. use_bce_loss=False,
  72. use_dice_loss=False,
  73. class_weight=None,
  74. ignore_index=None,
  75. pooling_crop_size=None,
  76. input_channel=None):
  77. if num_classes > 2 and (use_bce_loss or use_dice_loss):
  78. raise ValueError(
  79. "dice loss and bce loss is only applicable to binary classification"
  80. )
  81. elif num_classes == 2:
  82. if use_bce_loss and use_dice_loss:
  83. use_mixed_loss = [('CrossEntropyLoss', 1), ('DiceLoss', 1)]
  84. elif use_bce_loss:
  85. use_mixed_loss = [('CrossEntropyLoss', 1)]
  86. elif use_dice_loss:
  87. use_mixed_loss = [('DiceLoss', 1)]
  88. else:
  89. use_mixed_loss = False
  90. else:
  91. use_mixed_loss = False
  92. if aspp_with_sep_conv is not None:
  93. logging.warning(
  94. "`aspp_with_sep_conv` is deprecated in PaddleX 2.0 and will not take effect. "
  95. "Defaults to True")
  96. if decoder_use_sep_conv is not None:
  97. logging.warning(
  98. "`decoder_use_sep_conv` is deprecated in PaddleX 2.0 and will not take effect. "
  99. "Defaults to True")
  100. if encoder_with_aspp is not None:
  101. logging.warning(
  102. "`encoder_with_aspp` is deprecated in PaddleX 2.0 and will not take effect. "
  103. "Defaults to True")
  104. if enable_decoder is not None:
  105. logging.warning(
  106. "`enable_decoder` is deprecated in PaddleX 2.0 and will not take effect. "
  107. "Defaults to True")
  108. if class_weight is not None:
  109. logging.warning(
  110. "`class_weight` is not supported in PaddleX 2.0 currently and is forcibly set to None."
  111. )
  112. if ignore_index is not None:
  113. logging.warning(
  114. "`ignore_index` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 255."
  115. )
  116. if pooling_crop_size is not None:
  117. logging.warning(
  118. "Backbone 'MobileNetV3_large_x1_0_ssld' is currently not supported in PaddleX 2.0. "
  119. "`pooling_crop_size` will not take effect. Defaults to None")
  120. if input_channel is not None:
  121. logging.warning(
  122. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  123. )
  124. super(DeepLabV3P, self).__init__(
  125. num_classes=num_classes,
  126. backbone=backbone,
  127. use_mixed_loss=use_mixed_loss,
  128. output_stride=output_stride)
  129. class HRNet(cv.models.HRNet):
  130. def __init__(self,
  131. num_classes=2,
  132. width=18,
  133. use_bce_loss=False,
  134. use_dice_loss=False,
  135. class_weight=None,
  136. ignore_index=None,
  137. input_channel=None):
  138. if num_classes > 2 and (use_bce_loss or use_dice_loss):
  139. raise ValueError(
  140. "dice loss and bce loss is only applicable to binary classification"
  141. )
  142. elif num_classes == 2:
  143. if use_bce_loss and use_dice_loss:
  144. use_mixed_loss = [('CrossEntropyLoss', 1), ('DiceLoss', 1)]
  145. elif use_bce_loss:
  146. use_mixed_loss = [('CrossEntropyLoss', 1)]
  147. elif use_dice_loss:
  148. use_mixed_loss = [('DiceLoss', 1)]
  149. else:
  150. use_mixed_loss = False
  151. else:
  152. use_mixed_loss = False
  153. if class_weight is not None:
  154. logging.warning(
  155. "`class_weight` is not supported in PaddleX 2.0 currently and is forcibly set to None."
  156. )
  157. if ignore_index is not None:
  158. logging.warning(
  159. "`ignore_index` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 255."
  160. )
  161. if input_channel is not None:
  162. logging.warning(
  163. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  164. )
  165. super(HRNet, self).__init__(
  166. num_classes=num_classes,
  167. width=width,
  168. use_mixed_loss=use_mixed_loss)
  169. class FastSCNN(cv.models.FastSCNN):
  170. def __init__(self,
  171. num_classes=2,
  172. use_bce_loss=False,
  173. use_dice_loss=False,
  174. class_weight=None,
  175. ignore_index=255,
  176. multi_loss_weight=None,
  177. input_channel=3):
  178. if num_classes > 2 and (use_bce_loss or use_dice_loss):
  179. raise ValueError(
  180. "dice loss and bce loss is only applicable to binary classification"
  181. )
  182. elif num_classes == 2:
  183. if use_bce_loss and use_dice_loss:
  184. use_mixed_loss = [('CrossEntropyLoss', 1), ('DiceLoss', 1)]
  185. elif use_bce_loss:
  186. use_mixed_loss = [('CrossEntropyLoss', 1)]
  187. elif use_dice_loss:
  188. use_mixed_loss = [('DiceLoss', 1)]
  189. else:
  190. use_mixed_loss = False
  191. else:
  192. use_mixed_loss = False
  193. if class_weight is not None:
  194. logging.warning(
  195. "`class_weight` is not supported in PaddleX 2.0 currently and is forcibly set to None."
  196. )
  197. if ignore_index is not None:
  198. logging.warning(
  199. "`ignore_index` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 255."
  200. )
  201. if multi_loss_weight is not None:
  202. logging.warning(
  203. "`multi_loss_weight` is deprecated in PaddleX 2.0 and will not take effect. "
  204. "Defaults to [1.0, 0.4]")
  205. if input_channel is not None:
  206. logging.warning(
  207. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  208. )
  209. super(FastSCNN, self).__init__(
  210. num_classes=num_classes, use_mixed_loss=use_mixed_loss)