detector.py 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  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 __future__ import absolute_import
  15. import collections
  16. import copy
  17. import os
  18. import os.path as osp
  19. import six
  20. import numpy as np
  21. import paddle
  22. from paddle.static import InputSpec
  23. import ppdet
  24. from ppdet.modeling.proposal_generator.target_layer import BBoxAssigner, MaskAssigner
  25. import paddlex
  26. import paddlex.utils.logging as logging
  27. from paddlex.cv.transforms.operators import _NormalizeBox, _PadBox, _BboxXYXY2XYWH, Resize, Padding
  28. from paddlex.cv.transforms.batch_operators import BatchCompose, BatchRandomResize, BatchRandomResizeByShort, _BatchPadding, _Gt2YoloTarget
  29. from paddlex.cv.transforms import arrange_transforms
  30. from .base import BaseModel
  31. from .utils.det_metrics import VOCMetric, COCOMetric
  32. from .utils.ema import ExponentialMovingAverage
  33. from paddlex.utils.checkpoint import det_pretrain_weights_dict
  34. __all__ = [
  35. "YOLOv3", "FasterRCNN", "PPYOLO", "PPYOLOTiny", "PPYOLOv2", "MaskRCNN"
  36. ]
  37. class BaseDetector(BaseModel):
  38. def __init__(self, model_name, num_classes=80, **params):
  39. self.init_params.update(locals())
  40. super(BaseDetector, self).__init__('detector')
  41. if not hasattr(ppdet.modeling, model_name):
  42. raise Exception("ERROR: There's no model named {}.".format(
  43. model_name))
  44. self.model_name = model_name
  45. self.num_classes = num_classes
  46. self.labels = None
  47. self.net = self.build_net(**params)
  48. def build_net(self, **params):
  49. with paddle.utils.unique_name.guard():
  50. net = ppdet.modeling.__dict__[self.model_name](**params)
  51. return net
  52. def _fix_transforms_shape(self, image_shape):
  53. raise NotImplementedError("_fix_transforms_shape: not implemented!")
  54. def _get_test_inputs(self, image_shape):
  55. if image_shape is not None:
  56. if len(image_shape) == 2:
  57. image_shape = [None, 3] + image_shape
  58. if image_shape[-2] % 32 > 0 or image_shape[-1] % 32 > 0:
  59. raise Exception(
  60. "Height and width in fixed_input_shape must be a multiple of 32, but recieved is {}.".
  61. format(image_shape[-2:]))
  62. self._fix_transforms_shape(image_shape[-2:])
  63. else:
  64. image_shape = [None, 3, -1, -1]
  65. input_spec = [{
  66. "image": InputSpec(
  67. shape=image_shape, name='image', dtype='float32'),
  68. "im_shape": InputSpec(
  69. shape=[image_shape[0], 2], name='im_shape', dtype='float32'),
  70. "scale_factor": InputSpec(
  71. shape=[image_shape[0], 2],
  72. name='scale_factor',
  73. dtype='float32')
  74. }]
  75. return input_spec
  76. def _get_backbone(self, backbone_name, **params):
  77. backbone = getattr(ppdet.modeling, backbone_name)(**params)
  78. return backbone
  79. def run(self, net, inputs, mode):
  80. net_out = net(inputs)
  81. if mode in ['train', 'eval']:
  82. outputs = net_out
  83. else:
  84. for key in ['im_shape', 'scale_factor']:
  85. net_out[key] = inputs[key]
  86. outputs = dict()
  87. for key in net_out:
  88. outputs[key] = net_out[key].numpy()
  89. return outputs
  90. def default_optimizer(self, parameters, learning_rate, warmup_steps,
  91. warmup_start_lr, lr_decay_epochs, lr_decay_gamma,
  92. num_steps_each_epoch):
  93. boundaries = [b * num_steps_each_epoch for b in lr_decay_epochs]
  94. values = [(lr_decay_gamma**i) * learning_rate
  95. for i in range(len(lr_decay_epochs) + 1)]
  96. scheduler = paddle.optimizer.lr.PiecewiseDecay(
  97. boundaries=boundaries, values=values)
  98. if warmup_steps > 0:
  99. if warmup_steps > lr_decay_epochs[0] * num_steps_each_epoch:
  100. logging.error(
  101. "In function train(), parameters should satisfy: "
  102. "warmup_steps <= lr_decay_epochs[0]*num_samples_in_train_dataset",
  103. exit=False)
  104. logging.error(
  105. "See this doc for more information: "
  106. "https://github.com/PaddlePaddle/PaddleX/blob/develop/docs/appendix/parameters.md#notice",
  107. exit=False)
  108. scheduler = paddle.optimizer.lr.LinearWarmup(
  109. learning_rate=scheduler,
  110. warmup_steps=warmup_steps,
  111. start_lr=warmup_start_lr,
  112. end_lr=learning_rate)
  113. optimizer = paddle.optimizer.Momentum(
  114. scheduler,
  115. momentum=.9,
  116. weight_decay=paddle.regularizer.L2Decay(coeff=1e-04),
  117. parameters=parameters)
  118. return optimizer
  119. def train(self,
  120. num_epochs,
  121. train_dataset,
  122. train_batch_size=64,
  123. eval_dataset=None,
  124. optimizer=None,
  125. save_interval_epochs=1,
  126. log_interval_steps=10,
  127. save_dir='output',
  128. pretrain_weights='IMAGENET',
  129. learning_rate=.001,
  130. warmup_steps=0,
  131. warmup_start_lr=0.0,
  132. lr_decay_epochs=(216, 243),
  133. lr_decay_gamma=0.1,
  134. metric=None,
  135. use_ema=False,
  136. early_stop=False,
  137. early_stop_patience=5,
  138. use_vdl=True):
  139. """
  140. Train the model.
  141. Args:
  142. num_epochs(int): The number of epochs.
  143. train_dataset(paddlex.dataset): Training dataset.
  144. train_batch_size(int, optional): Total batch size among all cards used in training. Defaults to 64.
  145. eval_dataset(paddlex.dataset, optional):
  146. Evaluation dataset. If None, the model will not be evaluated during training process. Defaults to None.
  147. optimizer(paddle.optimizer.Optimizer or None, optional):
  148. Optimizer used for training. If None, a default optimizer is used. Defaults to None.
  149. save_interval_epochs(int, optional): Epoch interval for saving the model. Defaults to 1.
  150. log_interval_steps(int, optional): Step interval for printing training information. Defaults to 10.
  151. save_dir(str, optional): Directory to save the model. Defaults to 'output'.
  152. pretrain_weights(str or None, optional):
  153. None or name/path of pretrained weights. If None, no pretrained weights will be loaded. Defaults to 'IMAGENET'.
  154. learning_rate(float, optional): Learning rate for training. Defaults to .001.
  155. warmup_steps(int, optional): The number of steps of warm-up training. Defaults to 0.
  156. warmup_start_lr(float, optional): Start learning rate of warm-up training. Defaults to 0..
  157. lr_decay_epochs(list or tuple, optional): Epoch milestones for learning rate decay. Defaults to (216, 243).
  158. lr_decay_gamma(float, optional): Gamma coefficient of learning rate decay. Defaults to .1.
  159. metric({'VOC', 'COCO', None}, optional):
  160. Evaluation metric. If None, determine the metric according to the dataset format. Defaults to None.
  161. use_ema(bool, optional): Whether to use exponential moving average strategy. Defaults to False.
  162. early_stop(bool, optional): Whether to adopt early stop strategy. Defaults to False.
  163. early_stop_patience(int, optional): Early stop patience. Defaults to 5.
  164. use_vdl(bool, optional): Whether to use VisualDL to monitor the training process. Defaults to True.
  165. """
  166. if train_dataset.__class__.__name__ == 'VOCDetection':
  167. train_dataset.data_fields = {
  168. 'im_id', 'image_shape', 'image', 'gt_bbox', 'gt_class',
  169. 'difficult'
  170. }
  171. elif train_dataset.__class__.__name__ == 'CocoDetection':
  172. if self.__class__.__name__ == 'MaskRCNN':
  173. train_dataset.data_fields = {
  174. 'im_id', 'image_shape', 'image', 'gt_bbox', 'gt_class',
  175. 'gt_poly', 'is_crowd'
  176. }
  177. else:
  178. train_dataset.data_fields = {
  179. 'im_id', 'image_shape', 'image', 'gt_bbox', 'gt_class',
  180. 'is_crowd'
  181. }
  182. if metric is None:
  183. if eval_dataset.__class__.__name__ == 'VOCDetection':
  184. self.metric = 'voc'
  185. elif eval_dataset.__class__.__name__ == 'CocoDetection':
  186. self.metric = 'coco'
  187. else:
  188. assert metric.lower() in ['coco', 'voc'], \
  189. "Evaluation metric {} is not supported, please choose form 'COCO' and 'VOC'"
  190. self.metric = metric.lower()
  191. self.labels = train_dataset.labels
  192. self.num_max_boxes = train_dataset.num_max_boxes
  193. train_dataset.batch_transforms = self._compose_batch_transform(
  194. train_dataset.transforms, mode='train')
  195. # build optimizer if not defined
  196. if optimizer is None:
  197. num_steps_each_epoch = len(train_dataset) // train_batch_size
  198. self.optimizer = self.default_optimizer(
  199. parameters=self.net.parameters(),
  200. learning_rate=learning_rate,
  201. warmup_steps=warmup_steps,
  202. warmup_start_lr=warmup_start_lr,
  203. lr_decay_epochs=lr_decay_epochs,
  204. lr_decay_gamma=lr_decay_gamma,
  205. num_steps_each_epoch=num_steps_each_epoch)
  206. else:
  207. self.optimizer = optimizer
  208. # initiate weights
  209. if pretrain_weights is not None and not osp.exists(pretrain_weights):
  210. if pretrain_weights not in det_pretrain_weights_dict['_'.join(
  211. [self.model_name, self.backbone_name])]:
  212. logging.warning(
  213. "Path of pretrain_weights('{}') does not exist!".format(
  214. pretrain_weights))
  215. pretrain_weights = det_pretrain_weights_dict['_'.join(
  216. [self.model_name, self.backbone_name])][0]
  217. logging.warning("Pretrain_weights is forcibly set to '{}'. "
  218. "If you don't want to use pretrain weights, "
  219. "set pretrain_weights to be None.".format(
  220. pretrain_weights))
  221. elif pretrain_weights is not None and osp.exists(pretrain_weights):
  222. if osp.splitext(pretrain_weights)[-1] != '.pdparams':
  223. logging.error(
  224. "Invalid pretrain weights. Please specify a '.pdparams' file.",
  225. exit=True)
  226. pretrained_dir = osp.join(save_dir, 'pretrain')
  227. self.net_initialize(
  228. pretrain_weights=pretrain_weights, save_dir=pretrained_dir)
  229. if use_ema:
  230. ema = ExponentialMovingAverage(
  231. decay=.9998, model=self.net, use_thres_step=True)
  232. else:
  233. ema = None
  234. # start train loop
  235. self.train_loop(
  236. num_epochs=num_epochs,
  237. train_dataset=train_dataset,
  238. train_batch_size=train_batch_size,
  239. eval_dataset=eval_dataset,
  240. save_interval_epochs=save_interval_epochs,
  241. log_interval_steps=log_interval_steps,
  242. save_dir=save_dir,
  243. ema=ema,
  244. early_stop=early_stop,
  245. early_stop_patience=early_stop_patience,
  246. use_vdl=use_vdl)
  247. def quant_aware_train(self,
  248. num_epochs,
  249. train_dataset,
  250. train_batch_size=64,
  251. eval_dataset=None,
  252. optimizer=None,
  253. save_interval_epochs=1,
  254. log_interval_steps=10,
  255. save_dir='output',
  256. learning_rate=.00001,
  257. warmup_steps=0,
  258. warmup_start_lr=0.0,
  259. lr_decay_epochs=(216, 243),
  260. lr_decay_gamma=0.1,
  261. metric=None,
  262. use_ema=False,
  263. early_stop=False,
  264. early_stop_patience=5,
  265. use_vdl=True,
  266. quant_config=None):
  267. """
  268. Quantization-aware training.
  269. Args:
  270. num_epochs(int): The number of epochs.
  271. train_dataset(paddlex.dataset): Training dataset.
  272. train_batch_size(int, optional): Total batch size among all cards used in training. Defaults to 64.
  273. eval_dataset(paddlex.dataset, optional):
  274. Evaluation dataset. If None, the model will not be evaluated during training process. Defaults to None.
  275. optimizer(paddle.optimizer.Optimizer or None, optional):
  276. Optimizer used for training. If None, a default optimizer is used. Defaults to None.
  277. save_interval_epochs(int, optional): Epoch interval for saving the model. Defaults to 1.
  278. log_interval_steps(int, optional): Step interval for printing training information. Defaults to 10.
  279. save_dir(str, optional): Directory to save the model. Defaults to 'output'.
  280. learning_rate(float, optional): Learning rate for training. Defaults to .001.
  281. warmup_steps(int, optional): The number of steps of warm-up training. Defaults to 0.
  282. warmup_start_lr(float, optional): Start learning rate of warm-up training. Defaults to 0..
  283. lr_decay_epochs(list or tuple, optional): Epoch milestones for learning rate decay. Defaults to (216, 243).
  284. lr_decay_gamma(float, optional): Gamma coefficient of learning rate decay. Defaults to .1.
  285. metric({'VOC', 'COCO', None}, optional):
  286. Evaluation metric. If None, determine the metric according to the dataset format. Defaults to None.
  287. use_ema(bool, optional): Whether to use exponential moving average strategy. Defaults to False.
  288. early_stop(bool, optional): Whether to adopt early stop strategy. Defaults to False.
  289. early_stop_patience(int, optional): Early stop patience. Defaults to 5.
  290. use_vdl(bool, optional): Whether to use VisualDL to monitor the training process. Defaults to True.
  291. quant_config(dict or None, optional): Quantization configuration. If None, a default rule of thumb
  292. configuration will be used. Defaults to None.
  293. """
  294. self._prepare_qat(quant_config)
  295. self.train(
  296. num_epochs=num_epochs,
  297. train_dataset=train_dataset,
  298. train_batch_size=train_batch_size,
  299. eval_dataset=eval_dataset,
  300. optimizer=optimizer,
  301. save_interval_epochs=save_interval_epochs,
  302. log_interval_steps=log_interval_steps,
  303. save_dir=save_dir,
  304. pretrain_weights=None,
  305. learning_rate=learning_rate,
  306. warmup_steps=warmup_steps,
  307. warmup_start_lr=warmup_start_lr,
  308. lr_decay_epochs=lr_decay_epochs,
  309. lr_decay_gamma=lr_decay_gamma,
  310. metric=metric,
  311. use_ema=use_ema,
  312. early_stop=early_stop,
  313. early_stop_patience=early_stop_patience,
  314. use_vdl=use_vdl)
  315. def evaluate(self,
  316. eval_dataset,
  317. batch_size=1,
  318. metric=None,
  319. return_details=False):
  320. """
  321. Evaluate the model.
  322. Args:
  323. eval_dataset(paddlex.dataset): Evaluation dataset.
  324. batch_size(int, optional): Total batch size among all cards used for evaluation. Defaults to 1.
  325. metric({'VOC', 'COCO', None}, optional):
  326. Evaluation metric. If None, determine the metric according to the dataset format. Defaults to None.
  327. return_details(bool, optional): Whether to return evaluation details. Defaults to False.
  328. Returns:
  329. collections.OrderedDict with key-value pairs: {"mAP(0.50, 11point)":`mean average precision`}.
  330. """
  331. if metric is None:
  332. if not hasattr(self, 'metric'):
  333. if eval_dataset.__class__.__name__ == 'VOCDetection':
  334. self.metric = 'voc'
  335. elif eval_dataset.__class__.__name__ == 'CocoDetection':
  336. self.metric = 'coco'
  337. else:
  338. assert metric.lower() in ['coco', 'voc'], \
  339. "Evaluation metric {} is not supported, please choose form 'COCO' and 'VOC'"
  340. self.metric = metric.lower()
  341. if self.metric == 'voc':
  342. eval_dataset.data_fields = {
  343. 'im_id', 'image_shape', 'image', 'gt_bbox', 'gt_class',
  344. 'difficult'
  345. }
  346. elif self.metric == 'coco':
  347. if self.__class__.__name__ == 'MaskRCNN':
  348. eval_dataset.data_fields = {
  349. 'im_id', 'image_shape', 'image', 'gt_bbox', 'gt_class',
  350. 'gt_poly', 'is_crowd'
  351. }
  352. else:
  353. eval_dataset.data_fields = {
  354. 'im_id', 'image_shape', 'image', 'gt_bbox', 'gt_class',
  355. 'is_crowd'
  356. }
  357. eval_dataset.batch_transforms = self._compose_batch_transform(
  358. eval_dataset.transforms, mode='eval')
  359. arrange_transforms(
  360. model_type=self.model_type,
  361. transforms=eval_dataset.transforms,
  362. mode='eval')
  363. self.net.eval()
  364. nranks = paddle.distributed.get_world_size()
  365. local_rank = paddle.distributed.get_rank()
  366. if nranks > 1:
  367. # Initialize parallel environment if not done.
  368. if not paddle.distributed.parallel.parallel_helper._is_parallel_ctx_initialized(
  369. ):
  370. paddle.distributed.init_parallel_env()
  371. if batch_size > 1:
  372. logging.warning(
  373. "Detector only supports single card evaluation with batch_size=1 "
  374. "during evaluation, so batch_size is forcibly set to 1.")
  375. batch_size = 1
  376. if nranks < 2 or local_rank == 0:
  377. self.eval_data_loader = self.build_data_loader(
  378. eval_dataset, batch_size=batch_size, mode='eval')
  379. is_bbox_normalized = False
  380. if eval_dataset.batch_transforms is not None:
  381. is_bbox_normalized = any(
  382. isinstance(t, _NormalizeBox)
  383. for t in eval_dataset.batch_transforms.batch_transforms)
  384. if self.metric == 'voc':
  385. eval_metric = VOCMetric(
  386. labels=eval_dataset.labels,
  387. coco_gt=copy.deepcopy(eval_dataset.coco_gt),
  388. is_bbox_normalized=is_bbox_normalized,
  389. classwise=False)
  390. else:
  391. eval_metric = COCOMetric(
  392. coco_gt=copy.deepcopy(eval_dataset.coco_gt),
  393. classwise=False)
  394. scores = collections.OrderedDict()
  395. logging.info(
  396. "Start to evaluate(total_samples={}, total_steps={})...".
  397. format(eval_dataset.num_samples, eval_dataset.num_samples))
  398. with paddle.no_grad():
  399. for step, data in enumerate(self.eval_data_loader):
  400. outputs = self.run(self.net, data, 'eval')
  401. eval_metric.update(data, outputs)
  402. eval_metric.accumulate()
  403. self.eval_details = eval_metric.details
  404. scores.update(eval_metric.get())
  405. eval_metric.reset()
  406. if return_details:
  407. return scores, self.eval_details
  408. return scores
  409. def predict(self, img_file, transforms=None):
  410. """
  411. Do inference.
  412. Args:
  413. img_file(List[np.ndarray or str], str or np.ndarray): img_file(list or str or np.array):
  414. Image path or decoded image data in a BGR format, which also could constitute a list,
  415. meaning all images to be predicted as a mini-batch.
  416. transforms(paddlex.transforms.Compose or None, optional):
  417. Transforms for inputs. If None, the transforms for evaluation process will be used. Defaults to None.
  418. Returns:
  419. If img_file is a string or np.array, the result is a list of dict with key-value pairs:
  420. {"category_id": `category_id`, "category": `category`, "bbox": `[x, y, w, h]`, "score": `score`}.
  421. If img_file is a list, the result is a list composed of dicts with the corresponding fields:
  422. category_id(int): the predicted category ID. 0 represents the first category in the dataset, and so on.
  423. category(str): category name
  424. bbox(list): bounding box in [x, y, w, h] format
  425. score(str): confidence
  426. mask(dict): Only for instance segmentation task. Mask of the object in RLE format
  427. """
  428. if transforms is None and not hasattr(self, 'test_transforms'):
  429. raise Exception("transforms need to be defined, now is None.")
  430. if transforms is None:
  431. transforms = self.test_transforms
  432. if isinstance(img_file, (str, np.ndarray)):
  433. images = [img_file]
  434. else:
  435. images = img_file
  436. batch_samples = self._preprocess(images, transforms)
  437. self.net.eval()
  438. outputs = self.run(self.net, batch_samples, 'test')
  439. prediction = self._postprocess(outputs)
  440. if isinstance(img_file, (str, np.ndarray)):
  441. prediction = prediction[0]
  442. return prediction
  443. def _preprocess(self, images, transforms):
  444. arrange_transforms(
  445. model_type=self.model_type, transforms=transforms, mode='test')
  446. batch_samples = list()
  447. for im in images:
  448. sample = {'image': im}
  449. batch_samples.append(transforms(sample))
  450. batch_transforms = self._compose_batch_transform(transforms, 'test')
  451. batch_samples = batch_transforms(batch_samples)
  452. for k, v in batch_samples.items():
  453. batch_samples[k] = paddle.to_tensor(v)
  454. return batch_samples
  455. def _postprocess(self, batch_pred):
  456. infer_result = {}
  457. if 'bbox' in batch_pred:
  458. bboxes = batch_pred['bbox']
  459. bbox_nums = batch_pred['bbox_num']
  460. det_res = []
  461. k = 0
  462. for i in range(len(bbox_nums)):
  463. det_nums = bbox_nums[i]
  464. for j in range(det_nums):
  465. dt = bboxes[k]
  466. k = k + 1
  467. num_id, score, xmin, ymin, xmax, ymax = dt.tolist()
  468. if int(num_id) < 0:
  469. continue
  470. category = self.labels[int(num_id)]
  471. w = xmax - xmin
  472. h = ymax - ymin
  473. bbox = [xmin, ymin, w, h]
  474. dt_res = {
  475. 'category_id': int(num_id),
  476. 'category': category,
  477. 'bbox': bbox,
  478. 'score': score
  479. }
  480. det_res.append(dt_res)
  481. infer_result['bbox'] = det_res
  482. if 'mask' in batch_pred:
  483. masks = batch_pred['mask']
  484. bboxes = batch_pred['bbox']
  485. mask_nums = batch_pred['bbox_num']
  486. seg_res = []
  487. k = 0
  488. for i in range(len(mask_nums)):
  489. det_nums = mask_nums[i]
  490. for j in range(det_nums):
  491. mask = masks[k].astype(np.uint8)
  492. score = float(bboxes[k][1])
  493. label = int(bboxes[k][0])
  494. k = k + 1
  495. if label == -1:
  496. continue
  497. category = self.labels[int(label)]
  498. import pycocotools.mask as mask_util
  499. rle = mask_util.encode(
  500. np.array(
  501. mask[:, :, None], order="F", dtype="uint8"))[0]
  502. if six.PY3:
  503. if 'counts' in rle:
  504. rle['counts'] = rle['counts'].decode("utf8")
  505. sg_res = {
  506. 'category_id': int(label),
  507. 'category': category,
  508. 'mask': rle,
  509. 'score': score
  510. }
  511. seg_res.append(sg_res)
  512. infer_result['mask'] = seg_res
  513. bbox_num = batch_pred['bbox_num']
  514. results = []
  515. start = 0
  516. for num in bbox_num:
  517. end = start + num
  518. curr_res = infer_result['bbox'][start:end]
  519. if 'mask' in infer_result:
  520. mask_res = infer_result['mask'][start:end]
  521. for box, mask in zip(curr_res, mask_res):
  522. box.update(mask)
  523. results.append(curr_res)
  524. start = end
  525. return results
  526. class YOLOv3(BaseDetector):
  527. def __init__(self,
  528. num_classes=80,
  529. backbone='MobileNetV1',
  530. anchors=[[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],
  531. [59, 119], [116, 90], [156, 198], [373, 326]],
  532. anchor_masks=[[6, 7, 8], [3, 4, 5], [0, 1, 2]],
  533. ignore_threshold=0.7,
  534. nms_score_threshold=0.01,
  535. nms_topk=1000,
  536. nms_keep_topk=100,
  537. nms_iou_threshold=0.45,
  538. label_smooth=False):
  539. self.init_params = locals()
  540. if backbone not in [
  541. 'MobileNetV1', 'MobileNetV1_ssld', 'MobileNetV3',
  542. 'MobileNetV3_ssld', 'DarkNet53', 'ResNet50_vd_dcn', 'ResNet34'
  543. ]:
  544. raise ValueError(
  545. "backbone: {} is not supported. Please choose one of "
  546. "('MobileNetV1', 'MobileNetV1_ssld', 'MobileNetV3', 'MobileNetV3_ssld', 'DarkNet53', 'ResNet50_vd_dcn', 'ResNet34')".
  547. format(backbone))
  548. if paddlex.env_info['place'] == 'gpu' and paddlex.env_info[
  549. 'num'] > 1 and not os.environ.get('PADDLEX_EXPORT_STAGE'):
  550. norm_type = 'sync_bn'
  551. else:
  552. norm_type = 'bn'
  553. self.backbone_name = backbone
  554. if 'MobileNetV1' in backbone:
  555. norm_type = 'bn'
  556. backbone = self._get_backbone('MobileNet', norm_type=norm_type)
  557. elif 'MobileNetV3' in backbone:
  558. backbone = self._get_backbone(
  559. 'MobileNetV3', norm_type=norm_type, feature_maps=[7, 13, 16])
  560. elif backbone == 'ResNet50_vd_dcn':
  561. backbone = self._get_backbone(
  562. 'ResNet',
  563. norm_type=norm_type,
  564. variant='d',
  565. return_idx=[1, 2, 3],
  566. dcn_v2_stages=[3],
  567. freeze_at=-1,
  568. freeze_norm=False)
  569. elif backbone == 'ResNet34':
  570. backbone = self._get_backbone(
  571. 'ResNet',
  572. depth=34,
  573. norm_type=norm_type,
  574. return_idx=[1, 2, 3],
  575. freeze_at=-1,
  576. freeze_norm=False,
  577. norm_decay=0.)
  578. else:
  579. backbone = self._get_backbone('DarkNet', norm_type=norm_type)
  580. neck = ppdet.modeling.YOLOv3FPN(
  581. norm_type=norm_type,
  582. in_channels=[i.channels for i in backbone.out_shape])
  583. loss = ppdet.modeling.YOLOv3Loss(
  584. num_classes=num_classes,
  585. ignore_thresh=ignore_threshold,
  586. label_smooth=label_smooth)
  587. yolo_head = ppdet.modeling.YOLOv3Head(
  588. in_channels=[i.channels for i in neck.out_shape],
  589. anchors=anchors,
  590. anchor_masks=anchor_masks,
  591. num_classes=num_classes,
  592. loss=loss)
  593. post_process = ppdet.modeling.BBoxPostProcess(
  594. decode=ppdet.modeling.YOLOBox(num_classes=num_classes),
  595. nms=ppdet.modeling.MultiClassNMS(
  596. score_threshold=nms_score_threshold,
  597. nms_top_k=nms_topk,
  598. keep_top_k=nms_keep_topk,
  599. nms_threshold=nms_iou_threshold))
  600. params = {
  601. 'backbone': backbone,
  602. 'neck': neck,
  603. 'yolo_head': yolo_head,
  604. 'post_process': post_process
  605. }
  606. super(YOLOv3, self).__init__(
  607. model_name='YOLOv3', num_classes=num_classes, **params)
  608. self.anchors = anchors
  609. self.anchor_masks = anchor_masks
  610. def _compose_batch_transform(self, transforms, mode='train'):
  611. if mode == 'train':
  612. default_batch_transforms = [
  613. _BatchPadding(pad_to_stride=-1), _NormalizeBox(),
  614. _PadBox(getattr(self, 'num_max_boxes', 50)), _BboxXYXY2XYWH(),
  615. _Gt2YoloTarget(
  616. anchor_masks=self.anchor_masks,
  617. anchors=self.anchors,
  618. downsample_ratios=getattr(self, 'downsample_ratios',
  619. [32, 16, 8]),
  620. num_classes=self.num_classes)
  621. ]
  622. else:
  623. default_batch_transforms = [_BatchPadding(pad_to_stride=-1)]
  624. if mode == 'eval' and self.metric == 'voc':
  625. collate_batch = False
  626. else:
  627. collate_batch = True
  628. custom_batch_transforms = []
  629. for i, op in enumerate(transforms.transforms):
  630. if isinstance(op, (BatchRandomResize, BatchRandomResizeByShort)):
  631. if mode != 'train':
  632. raise Exception(
  633. "{} cannot be present in the {} transforms. ".format(
  634. op.__class__.__name__, mode) +
  635. "Please check the {} transforms.".format(mode))
  636. custom_batch_transforms.insert(0, copy.deepcopy(op))
  637. batch_transforms = BatchCompose(
  638. custom_batch_transforms + default_batch_transforms,
  639. collate_batch=collate_batch)
  640. return batch_transforms
  641. def _fix_transforms_shape(self, image_shape):
  642. if hasattr(self, 'test_transforms'):
  643. if self.test_transforms is not None:
  644. has_resize_op = False
  645. resize_op_idx = -1
  646. normalize_op_idx = len(self.test_transforms.transforms)
  647. for idx, op in enumerate(self.test_transforms.transforms):
  648. name = op.__class__.__name__
  649. if name == 'Resize':
  650. has_resize_op = True
  651. resize_op_idx = idx
  652. if name == 'Normalize':
  653. normalize_op_idx = idx
  654. if not has_resize_op:
  655. self.test_transforms.transforms.insert(
  656. normalize_op_idx,
  657. Resize(
  658. target_size=image_shape, interp='CUBIC'))
  659. else:
  660. self.test_transforms.transforms[
  661. resize_op_idx].target_size = image_shape
  662. class FasterRCNN(BaseDetector):
  663. def __init__(self,
  664. num_classes=80,
  665. backbone='ResNet50',
  666. with_fpn=True,
  667. with_dcn=False,
  668. aspect_ratios=[0.5, 1.0, 2.0],
  669. anchor_sizes=[[32], [64], [128], [256], [512]],
  670. keep_top_k=100,
  671. nms_threshold=0.5,
  672. score_threshold=0.05,
  673. fpn_num_channels=256,
  674. rpn_batch_size_per_im=256,
  675. rpn_fg_fraction=0.5,
  676. test_pre_nms_top_n=None,
  677. test_post_nms_top_n=1000):
  678. self.init_params = locals()
  679. if backbone not in [
  680. 'ResNet50', 'ResNet50_vd', 'ResNet50_vd_ssld', 'ResNet34',
  681. 'ResNet34_vd', 'ResNet101', 'ResNet101_vd', 'HRNet_W18'
  682. ]:
  683. raise ValueError(
  684. "backbone: {} is not supported. Please choose one of "
  685. "('ResNet50', 'ResNet50_vd', 'ResNet50_vd_ssld', 'ResNet34', 'ResNet34_vd', "
  686. "'ResNet101', 'ResNet101_vd', 'HRNet_W18')".format(backbone))
  687. self.backbone_name = backbone
  688. dcn_v2_stages = [1, 2, 3] if with_dcn else [-1]
  689. if backbone == 'HRNet_W18':
  690. if not with_fpn:
  691. logging.warning(
  692. "Backbone {} should be used along with fpn enabled, 'with_fpn' is forcibly set to True".
  693. format(backbone))
  694. with_fpn = True
  695. if with_dcn:
  696. logging.warning(
  697. "Backbone {} should be used along with dcn disabled, 'with_dcn' is forcibly set to False".
  698. format(backbone))
  699. backbone = self._get_backbone(
  700. 'HRNet', width=18, freeze_at=0, return_idx=[0, 1, 2, 3])
  701. elif backbone == 'ResNet50_vd_ssld':
  702. if not with_fpn:
  703. logging.warning(
  704. "Backbone {} should be used along with fpn enabled, 'with_fpn' is forcibly set to True".
  705. format(backbone))
  706. with_fpn = True
  707. backbone = self._get_backbone(
  708. 'ResNet',
  709. variant='d',
  710. norm_type='bn',
  711. freeze_at=0,
  712. return_idx=[0, 1, 2, 3],
  713. num_stages=4,
  714. lr_mult_list=[0.05, 0.05, 0.1, 0.15],
  715. dcn_v2_stages=dcn_v2_stages)
  716. elif 'ResNet50' in backbone:
  717. if with_fpn:
  718. backbone = self._get_backbone(
  719. 'ResNet',
  720. variant='d' if '_vd' in backbone else 'b',
  721. norm_type='bn',
  722. freeze_at=0,
  723. return_idx=[0, 1, 2, 3],
  724. num_stages=4,
  725. dcn_v2_stages=dcn_v2_stages)
  726. else:
  727. if with_dcn:
  728. logging.warning(
  729. "Backbone {} without fpn should be used along with dcn disabled, 'with_dcn' is forcibly set to False".
  730. format(backbone))
  731. backbone = self._get_backbone(
  732. 'ResNet',
  733. variant='d' if '_vd' in backbone else 'b',
  734. norm_type='bn',
  735. freeze_at=0,
  736. return_idx=[2],
  737. num_stages=3)
  738. elif 'ResNet34' in backbone:
  739. if not with_fpn:
  740. logging.warning(
  741. "Backbone {} should be used along with fpn enabled, 'with_fpn' is forcibly set to True".
  742. format(backbone))
  743. with_fpn = True
  744. backbone = self._get_backbone(
  745. 'ResNet',
  746. depth=34,
  747. variant='d' if 'vd' in backbone else 'b',
  748. norm_type='bn',
  749. freeze_at=0,
  750. return_idx=[0, 1, 2, 3],
  751. num_stages=4,
  752. dcn_v2_stages=dcn_v2_stages)
  753. else:
  754. if not with_fpn:
  755. logging.warning(
  756. "Backbone {} should be used along with fpn enabled, 'with_fpn' is forcibly set to True".
  757. format(backbone))
  758. with_fpn = True
  759. backbone = self._get_backbone(
  760. 'ResNet',
  761. depth=101,
  762. variant='d' if 'vd' in backbone else 'b',
  763. norm_type='bn',
  764. freeze_at=0,
  765. return_idx=[0, 1, 2, 3],
  766. num_stages=4,
  767. dcn_v2_stages=dcn_v2_stages)
  768. rpn_in_channel = backbone.out_shape[0].channels
  769. if with_fpn:
  770. self.backbone_name = self.backbone_name + '_fpn'
  771. if 'HRNet' in self.backbone_name:
  772. neck = ppdet.modeling.HRFPN(
  773. in_channels=[i.channels for i in backbone.out_shape],
  774. out_channel=fpn_num_channels,
  775. spatial_scales=[
  776. 1.0 / i.stride for i in backbone.out_shape
  777. ],
  778. share_conv=False)
  779. else:
  780. neck = ppdet.modeling.FPN(
  781. in_channels=[i.channels for i in backbone.out_shape],
  782. out_channel=fpn_num_channels,
  783. spatial_scales=[
  784. 1.0 / i.stride for i in backbone.out_shape
  785. ])
  786. rpn_in_channel = neck.out_shape[0].channels
  787. anchor_generator_cfg = {
  788. 'aspect_ratios': aspect_ratios,
  789. 'anchor_sizes': anchor_sizes,
  790. 'strides': [4, 8, 16, 32, 64]
  791. }
  792. train_proposal_cfg = {
  793. 'min_size': 0.0,
  794. 'nms_thresh': .7,
  795. 'pre_nms_top_n': 2000,
  796. 'post_nms_top_n': 1000,
  797. 'topk_after_collect': True
  798. }
  799. test_proposal_cfg = {
  800. 'min_size': 0.0,
  801. 'nms_thresh': .7,
  802. 'pre_nms_top_n': 1000
  803. if test_pre_nms_top_n is None else test_pre_nms_top_n,
  804. 'post_nms_top_n': test_post_nms_top_n
  805. }
  806. head = ppdet.modeling.TwoFCHead(
  807. in_channel=neck.out_shape[0].channels, out_channel=1024)
  808. roi_extractor_cfg = {
  809. 'resolution': 7,
  810. 'spatial_scale': [1. / i.stride for i in neck.out_shape],
  811. 'sampling_ratio': 0,
  812. 'aligned': True
  813. }
  814. with_pool = False
  815. else:
  816. neck = None
  817. anchor_generator_cfg = {
  818. 'aspect_ratios': aspect_ratios,
  819. 'anchor_sizes': anchor_sizes,
  820. 'strides': [16]
  821. }
  822. train_proposal_cfg = {
  823. 'min_size': 0.0,
  824. 'nms_thresh': .7,
  825. 'pre_nms_top_n': 12000,
  826. 'post_nms_top_n': 2000,
  827. 'topk_after_collect': False
  828. }
  829. test_proposal_cfg = {
  830. 'min_size': 0.0,
  831. 'nms_thresh': .7,
  832. 'pre_nms_top_n': 6000
  833. if test_pre_nms_top_n is None else test_pre_nms_top_n,
  834. 'post_nms_top_n': test_post_nms_top_n
  835. }
  836. head = ppdet.modeling.Res5Head()
  837. roi_extractor_cfg = {
  838. 'resolution': 14,
  839. 'spatial_scale': [1. / i.stride for i in backbone.out_shape],
  840. 'sampling_ratio': 0,
  841. 'aligned': True
  842. }
  843. with_pool = True
  844. rpn_target_assign_cfg = {
  845. 'batch_size_per_im': rpn_batch_size_per_im,
  846. 'fg_fraction': rpn_fg_fraction,
  847. 'negative_overlap': .3,
  848. 'positive_overlap': .7,
  849. 'use_random': True
  850. }
  851. rpn_head = ppdet.modeling.RPNHead(
  852. anchor_generator=anchor_generator_cfg,
  853. rpn_target_assign=rpn_target_assign_cfg,
  854. train_proposal=train_proposal_cfg,
  855. test_proposal=test_proposal_cfg,
  856. in_channel=rpn_in_channel)
  857. bbox_assigner = BBoxAssigner(num_classes=num_classes)
  858. bbox_head = ppdet.modeling.BBoxHead(
  859. head=head,
  860. in_channel=head.out_shape[0].channels,
  861. roi_extractor=roi_extractor_cfg,
  862. with_pool=with_pool,
  863. bbox_assigner=bbox_assigner,
  864. num_classes=num_classes)
  865. bbox_post_process = ppdet.modeling.BBoxPostProcess(
  866. num_classes=num_classes,
  867. decode=ppdet.modeling.RCNNBox(num_classes=num_classes),
  868. nms=ppdet.modeling.MultiClassNMS(
  869. score_threshold=score_threshold,
  870. keep_top_k=keep_top_k,
  871. nms_threshold=nms_threshold))
  872. params = {
  873. 'backbone': backbone,
  874. 'neck': neck,
  875. 'rpn_head': rpn_head,
  876. 'bbox_head': bbox_head,
  877. 'bbox_post_process': bbox_post_process
  878. }
  879. self.with_fpn = with_fpn
  880. super(FasterRCNN, self).__init__(
  881. model_name='FasterRCNN', num_classes=num_classes, **params)
  882. def _compose_batch_transform(self, transforms, mode='train'):
  883. if mode == 'train':
  884. default_batch_transforms = [
  885. _BatchPadding(pad_to_stride=32 if self.with_fpn else -1)
  886. ]
  887. collate_batch = False
  888. else:
  889. default_batch_transforms = [
  890. _BatchPadding(pad_to_stride=32 if self.with_fpn else -1)
  891. ]
  892. collate_batch = True
  893. custom_batch_transforms = []
  894. for i, op in enumerate(transforms.transforms):
  895. if isinstance(op, (BatchRandomResize, BatchRandomResizeByShort)):
  896. if mode != 'train':
  897. raise Exception(
  898. "{} cannot be present in the {} transforms. ".format(
  899. op.__class__.__name__, mode) +
  900. "Please check the {} transforms.".format(mode))
  901. custom_batch_transforms.insert(0, copy.deepcopy(op))
  902. batch_transforms = BatchCompose(
  903. custom_batch_transforms + default_batch_transforms,
  904. collate_batch=collate_batch)
  905. return batch_transforms
  906. def _fix_transforms_shape(self, image_shape):
  907. if hasattr(self, 'test_transforms'):
  908. if self.test_transforms is not None:
  909. has_resize_op = False
  910. resize_op_idx = -1
  911. normalize_op_idx = len(self.test_transforms.transforms)
  912. for idx, op in enumerate(self.test_transforms.transforms):
  913. name = op.__class__.__name__
  914. if name == 'ResizeByShort':
  915. has_resize_op = True
  916. resize_op_idx = idx
  917. if name == 'Normalize':
  918. normalize_op_idx = idx
  919. if not has_resize_op:
  920. self.test_transforms.transforms.insert(
  921. normalize_op_idx,
  922. Resize(
  923. target_size=image_shape,
  924. keep_ratio=True,
  925. interp='CUBIC'))
  926. else:
  927. self.test_transforms.transforms[resize_op_idx] = Resize(
  928. target_size=image_shape,
  929. keep_ratio=True,
  930. interp='CUBIC')
  931. self.test_transforms.transforms.append(
  932. Padding(im_padding_value=[0., 0., 0.]))
  933. class PPYOLO(YOLOv3):
  934. def __init__(self,
  935. num_classes=80,
  936. backbone='ResNet50_vd_dcn',
  937. anchors=None,
  938. anchor_masks=None,
  939. use_coord_conv=True,
  940. use_iou_aware=True,
  941. use_spp=True,
  942. use_drop_block=True,
  943. scale_x_y=1.05,
  944. ignore_threshold=0.7,
  945. label_smooth=False,
  946. use_iou_loss=True,
  947. use_matrix_nms=True,
  948. nms_score_threshold=0.01,
  949. nms_topk=-1,
  950. nms_keep_topk=100,
  951. nms_iou_threshold=0.45):
  952. self.init_params = locals()
  953. if backbone not in [
  954. 'ResNet50_vd_dcn', 'ResNet18_vd', 'MobileNetV3_large',
  955. 'MobileNetV3_small'
  956. ]:
  957. raise ValueError(
  958. "backbone: {} is not supported. Please choose one of "
  959. "('ResNet50_vd_dcn', 'ResNet18_vd', 'MobileNetV3_large', 'MobileNetV3_small')".
  960. format(backbone))
  961. self.backbone_name = backbone
  962. if paddlex.env_info['place'] == 'gpu' and paddlex.env_info[
  963. 'num'] > 1 and not os.environ.get('PADDLEX_EXPORT_STAGE'):
  964. norm_type = 'sync_bn'
  965. else:
  966. norm_type = 'bn'
  967. if anchors is None and anchor_masks is None:
  968. if 'MobileNetV3' in backbone:
  969. anchors = [[11, 18], [34, 47], [51, 126], [115, 71],
  970. [120, 195], [254, 235]]
  971. anchor_masks = [[3, 4, 5], [0, 1, 2]]
  972. elif backbone == 'ResNet50_vd_dcn':
  973. anchors = [[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],
  974. [59, 119], [116, 90], [156, 198], [373, 326]]
  975. anchor_masks = [[6, 7, 8], [3, 4, 5], [0, 1, 2]]
  976. else:
  977. anchors = [[10, 14], [23, 27], [37, 58], [81, 82], [135, 169],
  978. [344, 319]]
  979. anchor_masks = [[3, 4, 5], [0, 1, 2]]
  980. elif anchors is None or anchor_masks is None:
  981. raise ValueError("Please define both anchors and anchor_masks.")
  982. if backbone == 'ResNet50_vd_dcn':
  983. backbone = self._get_backbone(
  984. 'ResNet',
  985. variant='d',
  986. norm_type=norm_type,
  987. return_idx=[1, 2, 3],
  988. dcn_v2_stages=[3],
  989. freeze_at=-1,
  990. freeze_norm=False,
  991. norm_decay=0.)
  992. downsample_ratios = [32, 16, 8]
  993. elif backbone == 'ResNet18_vd':
  994. backbone = self._get_backbone(
  995. 'ResNet',
  996. depth=18,
  997. variant='d',
  998. norm_type=norm_type,
  999. return_idx=[2, 3],
  1000. freeze_at=-1,
  1001. freeze_norm=False,
  1002. norm_decay=0.)
  1003. downsample_ratios = [32, 16, 8]
  1004. elif backbone == 'MobileNetV3_large':
  1005. backbone = self._get_backbone(
  1006. 'MobileNetV3',
  1007. model_name='large',
  1008. norm_type=norm_type,
  1009. scale=1,
  1010. with_extra_blocks=False,
  1011. extra_block_filters=[],
  1012. feature_maps=[13, 16])
  1013. downsample_ratios = [32, 16]
  1014. elif backbone == 'MobileNetV3_small':
  1015. backbone = self._get_backbone(
  1016. 'MobileNetV3',
  1017. model_name='small',
  1018. norm_type=norm_type,
  1019. scale=1,
  1020. with_extra_blocks=False,
  1021. extra_block_filters=[],
  1022. feature_maps=[9, 12])
  1023. downsample_ratios = [32, 16]
  1024. neck = ppdet.modeling.PPYOLOFPN(
  1025. norm_type=norm_type,
  1026. in_channels=[i.channels for i in backbone.out_shape],
  1027. coord_conv=use_coord_conv,
  1028. drop_block=use_drop_block,
  1029. spp=use_spp,
  1030. conv_block_num=0 if ('MobileNetV3' in self.backbone_name or
  1031. self.backbone_name == 'ResNet18_vd') else 2)
  1032. loss = ppdet.modeling.YOLOv3Loss(
  1033. num_classes=num_classes,
  1034. ignore_thresh=ignore_threshold,
  1035. downsample=downsample_ratios,
  1036. label_smooth=label_smooth,
  1037. scale_x_y=scale_x_y,
  1038. iou_loss=ppdet.modeling.IouLoss(
  1039. loss_weight=2.5, loss_square=True) if use_iou_loss else None,
  1040. iou_aware_loss=ppdet.modeling.IouAwareLoss(loss_weight=1.0)
  1041. if use_iou_aware else None)
  1042. yolo_head = ppdet.modeling.YOLOv3Head(
  1043. in_channels=[i.channels for i in neck.out_shape],
  1044. anchors=anchors,
  1045. anchor_masks=anchor_masks,
  1046. num_classes=num_classes,
  1047. loss=loss,
  1048. iou_aware=use_iou_aware)
  1049. if use_matrix_nms:
  1050. nms = ppdet.modeling.MatrixNMS(
  1051. keep_top_k=nms_keep_topk,
  1052. score_threshold=nms_score_threshold,
  1053. post_threshold=.05
  1054. if 'MobileNetV3' in self.backbone_name else .01,
  1055. nms_top_k=nms_topk,
  1056. background_label=-1)
  1057. else:
  1058. nms = ppdet.modeling.MultiClassNMS(
  1059. score_threshold=nms_score_threshold,
  1060. nms_top_k=nms_topk,
  1061. keep_top_k=nms_keep_topk,
  1062. nms_threshold=nms_iou_threshold)
  1063. post_process = ppdet.modeling.BBoxPostProcess(
  1064. decode=ppdet.modeling.YOLOBox(
  1065. num_classes=num_classes,
  1066. conf_thresh=.005
  1067. if 'MobileNetV3' in self.backbone_name else .01,
  1068. scale_x_y=scale_x_y),
  1069. nms=nms)
  1070. params = {
  1071. 'backbone': backbone,
  1072. 'neck': neck,
  1073. 'yolo_head': yolo_head,
  1074. 'post_process': post_process
  1075. }
  1076. super(YOLOv3, self).__init__(
  1077. model_name='YOLOv3', num_classes=num_classes, **params)
  1078. self.anchors = anchors
  1079. self.anchor_masks = anchor_masks
  1080. self.downsample_ratios = downsample_ratios
  1081. self.model_name = 'PPYOLO'
  1082. class PPYOLOTiny(YOLOv3):
  1083. def __init__(self,
  1084. num_classes=80,
  1085. backbone='MobileNetV3',
  1086. anchors=[[10, 15], [24, 36], [72, 42], [35, 87], [102, 96],
  1087. [60, 170], [220, 125], [128, 222], [264, 266]],
  1088. anchor_masks=[[6, 7, 8], [3, 4, 5], [0, 1, 2]],
  1089. use_iou_aware=False,
  1090. use_spp=True,
  1091. use_drop_block=True,
  1092. scale_x_y=1.05,
  1093. ignore_threshold=0.5,
  1094. label_smooth=False,
  1095. use_iou_loss=True,
  1096. use_matrix_nms=False,
  1097. nms_score_threshold=0.005,
  1098. nms_topk=1000,
  1099. nms_keep_topk=100,
  1100. nms_iou_threshold=0.45):
  1101. self.init_params = locals()
  1102. if backbone != 'MobileNetV3':
  1103. logging.warning(
  1104. "PPYOLOTiny only supports MobileNetV3 as backbone. "
  1105. "Backbone is forcibly set to MobileNetV3.")
  1106. self.backbone_name = 'MobileNetV3'
  1107. if paddlex.env_info['place'] == 'gpu' and paddlex.env_info[
  1108. 'num'] > 1 and not os.environ.get('PADDLEX_EXPORT_STAGE'):
  1109. norm_type = 'sync_bn'
  1110. else:
  1111. norm_type = 'bn'
  1112. backbone = self._get_backbone(
  1113. 'MobileNetV3',
  1114. model_name='large',
  1115. norm_type=norm_type,
  1116. scale=.5,
  1117. with_extra_blocks=False,
  1118. extra_block_filters=[],
  1119. feature_maps=[7, 13, 16])
  1120. downsample_ratios = [32, 16, 8]
  1121. neck = ppdet.modeling.PPYOLOTinyFPN(
  1122. detection_block_channels=[160, 128, 96],
  1123. in_channels=[i.channels for i in backbone.out_shape],
  1124. spp=use_spp,
  1125. drop_block=use_drop_block)
  1126. loss = ppdet.modeling.YOLOv3Loss(
  1127. num_classes=num_classes,
  1128. ignore_thresh=ignore_threshold,
  1129. downsample=downsample_ratios,
  1130. label_smooth=label_smooth,
  1131. scale_x_y=scale_x_y,
  1132. iou_loss=ppdet.modeling.IouLoss(
  1133. loss_weight=2.5, loss_square=True) if use_iou_loss else None,
  1134. iou_aware_loss=ppdet.modeling.IouAwareLoss(loss_weight=1.0)
  1135. if use_iou_aware else None)
  1136. yolo_head = ppdet.modeling.YOLOv3Head(
  1137. in_channels=[i.channels for i in neck.out_shape],
  1138. anchors=anchors,
  1139. anchor_masks=anchor_masks,
  1140. num_classes=num_classes,
  1141. loss=loss,
  1142. iou_aware=use_iou_aware)
  1143. if use_matrix_nms:
  1144. nms = ppdet.modeling.MatrixNMS(
  1145. keep_top_k=nms_keep_topk,
  1146. score_threshold=nms_score_threshold,
  1147. post_threshold=.05,
  1148. nms_top_k=nms_topk,
  1149. background_label=-1)
  1150. else:
  1151. nms = ppdet.modeling.MultiClassNMS(
  1152. score_threshold=nms_score_threshold,
  1153. nms_top_k=nms_topk,
  1154. keep_top_k=nms_keep_topk,
  1155. nms_threshold=nms_iou_threshold)
  1156. post_process = ppdet.modeling.BBoxPostProcess(
  1157. decode=ppdet.modeling.YOLOBox(
  1158. num_classes=num_classes,
  1159. conf_thresh=.005,
  1160. downsample_ratio=32,
  1161. clip_bbox=True,
  1162. scale_x_y=scale_x_y),
  1163. nms=nms)
  1164. params = {
  1165. 'backbone': backbone,
  1166. 'neck': neck,
  1167. 'yolo_head': yolo_head,
  1168. 'post_process': post_process
  1169. }
  1170. super(YOLOv3, self).__init__(
  1171. model_name='YOLOv3', num_classes=num_classes, **params)
  1172. self.anchors = anchors
  1173. self.anchor_masks = anchor_masks
  1174. self.downsample_ratios = downsample_ratios
  1175. self.model_name = 'PPYOLOTiny'
  1176. class PPYOLOv2(YOLOv3):
  1177. def __init__(self,
  1178. num_classes=80,
  1179. backbone='ResNet50_vd_dcn',
  1180. anchors=[[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],
  1181. [59, 119], [116, 90], [156, 198], [373, 326]],
  1182. anchor_masks=[[6, 7, 8], [3, 4, 5], [0, 1, 2]],
  1183. use_iou_aware=True,
  1184. use_spp=True,
  1185. use_drop_block=True,
  1186. scale_x_y=1.05,
  1187. ignore_threshold=0.7,
  1188. label_smooth=False,
  1189. use_iou_loss=True,
  1190. use_matrix_nms=True,
  1191. nms_score_threshold=0.01,
  1192. nms_topk=-1,
  1193. nms_keep_topk=100,
  1194. nms_iou_threshold=0.45):
  1195. self.init_params = locals()
  1196. if backbone not in ['ResNet50_vd_dcn', 'ResNet101_vd_dcn']:
  1197. raise ValueError(
  1198. "backbone: {} is not supported. Please choose one of "
  1199. "('ResNet50_vd_dcn', 'ResNet18_vd')".format(backbone))
  1200. self.backbone_name = backbone
  1201. if paddlex.env_info['place'] == 'gpu' and paddlex.env_info[
  1202. 'num'] > 1 and not os.environ.get('PADDLEX_EXPORT_STAGE'):
  1203. norm_type = 'sync_bn'
  1204. else:
  1205. norm_type = 'bn'
  1206. if backbone == 'ResNet50_vd_dcn':
  1207. backbone = self._get_backbone(
  1208. 'ResNet',
  1209. variant='d',
  1210. norm_type=norm_type,
  1211. return_idx=[1, 2, 3],
  1212. dcn_v2_stages=[3],
  1213. freeze_at=-1,
  1214. freeze_norm=False,
  1215. norm_decay=0.)
  1216. downsample_ratios = [32, 16, 8]
  1217. elif backbone == 'ResNet101_vd_dcn':
  1218. backbone = self._get_backbone(
  1219. 'ResNet',
  1220. depth=101,
  1221. variant='d',
  1222. norm_type=norm_type,
  1223. return_idx=[1, 2, 3],
  1224. dcn_v2_stages=[3],
  1225. freeze_at=-1,
  1226. freeze_norm=False,
  1227. norm_decay=0.)
  1228. downsample_ratios = [32, 16, 8]
  1229. neck = ppdet.modeling.PPYOLOPAN(
  1230. norm_type=norm_type,
  1231. in_channels=[i.channels for i in backbone.out_shape],
  1232. drop_block=use_drop_block,
  1233. block_size=3,
  1234. keep_prob=.9,
  1235. spp=use_spp)
  1236. loss = ppdet.modeling.YOLOv3Loss(
  1237. num_classes=num_classes,
  1238. ignore_thresh=ignore_threshold,
  1239. downsample=downsample_ratios,
  1240. label_smooth=label_smooth,
  1241. scale_x_y=scale_x_y,
  1242. iou_loss=ppdet.modeling.IouLoss(
  1243. loss_weight=2.5, loss_square=True) if use_iou_loss else None,
  1244. iou_aware_loss=ppdet.modeling.IouAwareLoss(loss_weight=1.0)
  1245. if use_iou_aware else None)
  1246. yolo_head = ppdet.modeling.YOLOv3Head(
  1247. in_channels=[i.channels for i in neck.out_shape],
  1248. anchors=anchors,
  1249. anchor_masks=anchor_masks,
  1250. num_classes=num_classes,
  1251. loss=loss,
  1252. iou_aware=use_iou_aware,
  1253. iou_aware_factor=.5)
  1254. if use_matrix_nms:
  1255. nms = ppdet.modeling.MatrixNMS(
  1256. keep_top_k=nms_keep_topk,
  1257. score_threshold=nms_score_threshold,
  1258. post_threshold=.01,
  1259. nms_top_k=nms_topk,
  1260. background_label=-1)
  1261. else:
  1262. nms = ppdet.modeling.MultiClassNMS(
  1263. score_threshold=nms_score_threshold,
  1264. nms_top_k=nms_topk,
  1265. keep_top_k=nms_keep_topk,
  1266. nms_threshold=nms_iou_threshold)
  1267. post_process = ppdet.modeling.BBoxPostProcess(
  1268. decode=ppdet.modeling.YOLOBox(
  1269. num_classes=num_classes,
  1270. conf_thresh=.01,
  1271. downsample_ratio=32,
  1272. clip_bbox=True,
  1273. scale_x_y=scale_x_y),
  1274. nms=nms)
  1275. params = {
  1276. 'backbone': backbone,
  1277. 'neck': neck,
  1278. 'yolo_head': yolo_head,
  1279. 'post_process': post_process
  1280. }
  1281. super(YOLOv3, self).__init__(
  1282. model_name='YOLOv3', num_classes=num_classes, **params)
  1283. self.anchors = anchors
  1284. self.anchor_masks = anchor_masks
  1285. self.downsample_ratios = downsample_ratios
  1286. self.model_name = 'PPYOLOv2'
  1287. def _get_test_inputs(self, image_shape):
  1288. if image_shape is not None:
  1289. if len(image_shape) == 2:
  1290. image_shape = [None, 3] + image_shape
  1291. if image_shape[-2] % 32 > 0 or image_shape[-1] % 32 > 0:
  1292. raise Exception(
  1293. "Height and width in fixed_input_shape must be a multiple of 32, but recieved is {}.".
  1294. format(image_shape[-2:]))
  1295. self._fix_transforms_shape(image_shape[-2:])
  1296. else:
  1297. logging.warning(
  1298. '[Important!!!] When exporting inference model for {},'.format(
  1299. self.__class__.__name__) +
  1300. ' if fixed_input_shape is not set, it will be forcibly set to [None, 3, 608, 608]. '
  1301. +
  1302. 'Please check image shape after transforms is [3, 608, 608], if not, fixed_input_shape '
  1303. + 'should be specified manually.')
  1304. image_shape = [None, 3, 608, 608]
  1305. input_spec = [{
  1306. "image": InputSpec(
  1307. shape=image_shape, name='image', dtype='float32'),
  1308. "im_shape": InputSpec(
  1309. shape=[image_shape[0], 2], name='im_shape', dtype='float32'),
  1310. "scale_factor": InputSpec(
  1311. shape=[image_shape[0], 2],
  1312. name='scale_factor',
  1313. dtype='float32')
  1314. }]
  1315. return input_spec
  1316. class MaskRCNN(BaseDetector):
  1317. def __init__(self,
  1318. num_classes=80,
  1319. backbone='ResNet50_vd',
  1320. with_fpn=True,
  1321. with_dcn=False,
  1322. aspect_ratios=[0.5, 1.0, 2.0],
  1323. anchor_sizes=[[32], [64], [128], [256], [512]],
  1324. keep_top_k=100,
  1325. nms_threshold=0.5,
  1326. score_threshold=0.05,
  1327. fpn_num_channels=256,
  1328. rpn_batch_size_per_im=256,
  1329. rpn_fg_fraction=0.5,
  1330. test_pre_nms_top_n=None,
  1331. test_post_nms_top_n=1000):
  1332. self.init_params = locals()
  1333. if backbone not in [
  1334. 'ResNet50', 'ResNet50_vd', 'ResNet50_vd_ssld', 'ResNet101',
  1335. 'ResNet101_vd'
  1336. ]:
  1337. raise ValueError(
  1338. "backbone: {} is not supported. Please choose one of "
  1339. "('ResNet50', 'ResNet50_vd', 'ResNet50_vd_ssld', 'ResNet101', 'ResNet101_vd')".
  1340. format(backbone))
  1341. self.backbone_name = backbone + '_fpn' if with_fpn else backbone
  1342. dcn_v2_stages = [1, 2, 3] if with_dcn else [-1]
  1343. if backbone == 'ResNet50':
  1344. if with_fpn:
  1345. backbone = self._get_backbone(
  1346. 'ResNet',
  1347. norm_type='bn',
  1348. freeze_at=0,
  1349. return_idx=[0, 1, 2, 3],
  1350. num_stages=4,
  1351. dcn_v2_stages=dcn_v2_stages)
  1352. else:
  1353. if with_dcn:
  1354. logging.warning(
  1355. "Backbone {} should be used along with dcn disabled, 'with_dcn' is forcibly set to False".
  1356. format(backbone))
  1357. backbone = self._get_backbone(
  1358. 'ResNet',
  1359. norm_type='bn',
  1360. freeze_at=0,
  1361. return_idx=[2],
  1362. num_stages=3)
  1363. elif 'ResNet50_vd' in backbone:
  1364. if not with_fpn:
  1365. logging.warning(
  1366. "Backbone {} should be used along with fpn enabled, 'with_fpn' is forcibly set to True".
  1367. format(backbone))
  1368. with_fpn = True
  1369. backbone = self._get_backbone(
  1370. 'ResNet',
  1371. variant='d',
  1372. norm_type='bn',
  1373. freeze_at=0,
  1374. return_idx=[0, 1, 2, 3],
  1375. num_stages=4,
  1376. lr_mult_list=[0.05, 0.05, 0.1, 0.15]
  1377. if '_ssld' in backbone else [1.0, 1.0, 1.0, 1.0],
  1378. dcn_v2_stages=dcn_v2_stages)
  1379. else:
  1380. if not with_fpn:
  1381. logging.warning(
  1382. "Backbone {} should be used along with fpn enabled, 'with_fpn' is forcibly set to True".
  1383. format(backbone))
  1384. with_fpn = True
  1385. backbone = self._get_backbone(
  1386. 'ResNet',
  1387. variant='d' if '_vd' in backbone else 'b',
  1388. depth=101,
  1389. norm_type='bn',
  1390. freeze_at=0,
  1391. return_idx=[0, 1, 2, 3],
  1392. num_stages=4,
  1393. dcn_v2_stages=dcn_v2_stages)
  1394. rpn_in_channel = backbone.out_shape[0].channels
  1395. if with_fpn:
  1396. neck = ppdet.modeling.FPN(
  1397. in_channels=[i.channels for i in backbone.out_shape],
  1398. out_channel=fpn_num_channels,
  1399. spatial_scales=[1.0 / i.stride for i in backbone.out_shape])
  1400. rpn_in_channel = neck.out_shape[0].channels
  1401. anchor_generator_cfg = {
  1402. 'aspect_ratios': aspect_ratios,
  1403. 'anchor_sizes': anchor_sizes,
  1404. 'strides': [4, 8, 16, 32, 64]
  1405. }
  1406. train_proposal_cfg = {
  1407. 'min_size': 0.0,
  1408. 'nms_thresh': .7,
  1409. 'pre_nms_top_n': 2000,
  1410. 'post_nms_top_n': 1000,
  1411. 'topk_after_collect': True
  1412. }
  1413. test_proposal_cfg = {
  1414. 'min_size': 0.0,
  1415. 'nms_thresh': .7,
  1416. 'pre_nms_top_n': 1000
  1417. if test_pre_nms_top_n is None else test_pre_nms_top_n,
  1418. 'post_nms_top_n': test_post_nms_top_n
  1419. }
  1420. bb_head = ppdet.modeling.TwoFCHead(
  1421. in_channel=neck.out_shape[0].channels, out_channel=1024)
  1422. bb_roi_extractor_cfg = {
  1423. 'resolution': 7,
  1424. 'spatial_scale': [1. / i.stride for i in neck.out_shape],
  1425. 'sampling_ratio': 0,
  1426. 'aligned': True
  1427. }
  1428. with_pool = False
  1429. m_head = ppdet.modeling.MaskFeat(
  1430. in_channel=neck.out_shape[0].channels,
  1431. out_channel=256,
  1432. num_convs=4)
  1433. m_roi_extractor_cfg = {
  1434. 'resolution': 14,
  1435. 'spatial_scale': [1. / i.stride for i in neck.out_shape],
  1436. 'sampling_ratio': 0,
  1437. 'aligned': True
  1438. }
  1439. mask_assigner = MaskAssigner(
  1440. num_classes=num_classes, mask_resolution=28)
  1441. share_bbox_feat = False
  1442. else:
  1443. neck = None
  1444. anchor_generator_cfg = {
  1445. 'aspect_ratios': aspect_ratios,
  1446. 'anchor_sizes': anchor_sizes,
  1447. 'strides': [16]
  1448. }
  1449. train_proposal_cfg = {
  1450. 'min_size': 0.0,
  1451. 'nms_thresh': .7,
  1452. 'pre_nms_top_n': 12000,
  1453. 'post_nms_top_n': 2000,
  1454. 'topk_after_collect': False
  1455. }
  1456. test_proposal_cfg = {
  1457. 'min_size': 0.0,
  1458. 'nms_thresh': .7,
  1459. 'pre_nms_top_n': 6000
  1460. if test_pre_nms_top_n is None else test_pre_nms_top_n,
  1461. 'post_nms_top_n': test_post_nms_top_n
  1462. }
  1463. bb_head = ppdet.modeling.Res5Head()
  1464. bb_roi_extractor_cfg = {
  1465. 'resolution': 14,
  1466. 'spatial_scale': [1. / i.stride for i in backbone.out_shape],
  1467. 'sampling_ratio': 0,
  1468. 'aligned': True
  1469. }
  1470. with_pool = True
  1471. m_head = ppdet.modeling.MaskFeat(
  1472. in_channel=bb_head.out_shape[0].channels,
  1473. out_channel=256,
  1474. num_convs=0)
  1475. m_roi_extractor_cfg = {
  1476. 'resolution': 14,
  1477. 'spatial_scale': [1. / i.stride for i in backbone.out_shape],
  1478. 'sampling_ratio': 0,
  1479. 'aligned': True
  1480. }
  1481. mask_assigner = MaskAssigner(
  1482. num_classes=num_classes, mask_resolution=14)
  1483. share_bbox_feat = True
  1484. rpn_target_assign_cfg = {
  1485. 'batch_size_per_im': rpn_batch_size_per_im,
  1486. 'fg_fraction': rpn_fg_fraction,
  1487. 'negative_overlap': .3,
  1488. 'positive_overlap': .7,
  1489. 'use_random': True
  1490. }
  1491. rpn_head = ppdet.modeling.RPNHead(
  1492. anchor_generator=anchor_generator_cfg,
  1493. rpn_target_assign=rpn_target_assign_cfg,
  1494. train_proposal=train_proposal_cfg,
  1495. test_proposal=test_proposal_cfg,
  1496. in_channel=rpn_in_channel)
  1497. bbox_assigner = BBoxAssigner(num_classes=num_classes)
  1498. bbox_head = ppdet.modeling.BBoxHead(
  1499. head=bb_head,
  1500. in_channel=bb_head.out_shape[0].channels,
  1501. roi_extractor=bb_roi_extractor_cfg,
  1502. with_pool=with_pool,
  1503. bbox_assigner=bbox_assigner,
  1504. num_classes=num_classes)
  1505. mask_head = ppdet.modeling.MaskHead(
  1506. head=m_head,
  1507. roi_extractor=m_roi_extractor_cfg,
  1508. mask_assigner=mask_assigner,
  1509. share_bbox_feat=share_bbox_feat,
  1510. num_classes=num_classes)
  1511. bbox_post_process = ppdet.modeling.BBoxPostProcess(
  1512. num_classes=num_classes,
  1513. decode=ppdet.modeling.RCNNBox(num_classes=num_classes),
  1514. nms=ppdet.modeling.MultiClassNMS(
  1515. score_threshold=score_threshold,
  1516. keep_top_k=keep_top_k,
  1517. nms_threshold=nms_threshold))
  1518. mask_post_process = ppdet.modeling.MaskPostProcess(binary_thresh=.5)
  1519. params = {
  1520. 'backbone': backbone,
  1521. 'neck': neck,
  1522. 'rpn_head': rpn_head,
  1523. 'bbox_head': bbox_head,
  1524. 'mask_head': mask_head,
  1525. 'bbox_post_process': bbox_post_process,
  1526. 'mask_post_process': mask_post_process
  1527. }
  1528. self.with_fpn = with_fpn
  1529. super(MaskRCNN, self).__init__(
  1530. model_name='MaskRCNN', num_classes=num_classes, **params)
  1531. def _compose_batch_transform(self, transforms, mode='train'):
  1532. if mode == 'train':
  1533. default_batch_transforms = [
  1534. _BatchPadding(pad_to_stride=32 if self.with_fpn else -1)
  1535. ]
  1536. collate_batch = False
  1537. else:
  1538. default_batch_transforms = [
  1539. _BatchPadding(pad_to_stride=32 if self.with_fpn else -1)
  1540. ]
  1541. collate_batch = True
  1542. custom_batch_transforms = []
  1543. for i, op in enumerate(transforms.transforms):
  1544. if isinstance(op, (BatchRandomResize, BatchRandomResizeByShort)):
  1545. if mode != 'train':
  1546. raise Exception(
  1547. "{} cannot be present in the {} transforms. ".format(
  1548. op.__class__.__name__, mode) +
  1549. "Please check the {} transforms.".format(mode))
  1550. custom_batch_transforms.insert(0, copy.deepcopy(op))
  1551. batch_transforms = BatchCompose(
  1552. custom_batch_transforms + default_batch_transforms,
  1553. collate_batch=collate_batch)
  1554. return batch_transforms
  1555. def _fix_transforms_shape(self, image_shape):
  1556. if hasattr(self, 'test_transforms'):
  1557. if self.test_transforms is not None:
  1558. has_resize_op = False
  1559. resize_op_idx = -1
  1560. normalize_op_idx = len(self.test_transforms.transforms)
  1561. for idx, op in enumerate(self.test_transforms.transforms):
  1562. name = op.__class__.__name__
  1563. if name == 'ResizeByShort':
  1564. has_resize_op = True
  1565. resize_op_idx = idx
  1566. if name == 'Normalize':
  1567. normalize_op_idx = idx
  1568. if not has_resize_op:
  1569. self.test_transforms.transforms.insert(
  1570. normalize_op_idx,
  1571. Resize(
  1572. target_size=image_shape,
  1573. keep_ratio=True,
  1574. interp='CUBIC'))
  1575. else:
  1576. self.test_transforms.transforms[resize_op_idx] = Resize(
  1577. target_size=image_shape,
  1578. keep_ratio=True,
  1579. interp='CUBIC')
  1580. self.test_transforms.transforms.append(
  1581. Padding(im_padding_value=[0., 0., 0.]))