classifier.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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 math
  16. import os.path as osp
  17. from collections import OrderedDict
  18. import numpy as np
  19. import paddle
  20. from paddle import to_tensor
  21. import paddle.nn.functional as F
  22. from paddle.static import InputSpec
  23. from paddlex.utils import logging, TrainingStats, DisablePrint
  24. from paddlex.cv.models.base import BaseModel
  25. from paddlex.cv.transforms import arrange_transforms
  26. from paddlex.cv.transforms.operators import Resize
  27. with DisablePrint():
  28. from PaddleClas.ppcls.modeling import architectures
  29. from PaddleClas.ppcls.modeling.loss import CELoss
  30. __all__ = [
  31. "ResNet18", "ResNet34", "ResNet50", "ResNet101", "ResNet152",
  32. "ResNet18_vd", "ResNet34_vd", "ResNet50_vd", "ResNet50_vd_ssld",
  33. "ResNet101_vd", "ResNet101_vd_ssld", "ResNet152_vd", "ResNet200_vd",
  34. "AlexNet", "DarkNet53", "MobileNetV1", "MobileNetV2", "MobileNetV3_small",
  35. "MobileNetV3_small_ssld", "MobileNetV3_large", "MobileNetV3_large_ssld",
  36. "DenseNet121", "DenseNet161", "DenseNet169", "DenseNet201", "DenseNet264",
  37. "HRNet_W18_C", "HRNet_W30_C", "HRNet_W32_C", "HRNet_W40_C", "HRNet_W44_C",
  38. "HRNet_W48_C", "HRNet_W64_C", "Xception41", "Xception65", "Xception71",
  39. "ShuffleNetV2", "ShuffleNetV2_swish"
  40. ]
  41. class BaseClassifier(BaseModel):
  42. """Parent class of all classification models.
  43. Args:
  44. model_name (str, optional): Name of classification model. Defaults to 'ResNet50'.
  45. num_classes (int, optional): The number of target classes. Defaults to 1000.
  46. """
  47. def __init__(self, model_name='ResNet50', num_classes=1000, **params):
  48. self.init_params = locals()
  49. self.init_params.update(params)
  50. if 'lr_mult_list' in self.init_params:
  51. del self.init_params['lr_mult_list']
  52. super(BaseClassifier, self).__init__('classifier')
  53. if not hasattr(architectures, model_name):
  54. raise Exception("ERROR: There's no model named {}.".format(
  55. model_name))
  56. self.model_name = model_name
  57. self.labels = None
  58. self.num_classes = num_classes
  59. for k, v in params.items():
  60. setattr(self, k, v)
  61. self.net = self.build_net(**params)
  62. def build_net(self, **params):
  63. with paddle.utils.unique_name.guard():
  64. net = architectures.__dict__[self.model_name](
  65. class_dim=self.num_classes, **params)
  66. return net
  67. def _fix_transforms_shape(self, image_shape):
  68. if hasattr(self, 'test_transforms'):
  69. if self.test_transforms is not None:
  70. self.test_transforms.transforms.append(
  71. Resize(target_size=image_shape))
  72. def _get_test_inputs(self, image_shape):
  73. if image_shape is not None:
  74. if len(image_shape) == 2:
  75. image_shape = [None, 3] + image_shape
  76. self._fix_transforms_shape(image_shape[-2:])
  77. else:
  78. image_shape = [None, 3, -1, -1]
  79. input_spec = [
  80. InputSpec(
  81. shape=image_shape, name='image', dtype='float32')
  82. ]
  83. return input_spec
  84. def run(self, net, inputs, mode):
  85. net_out = net(inputs[0])
  86. softmax_out = F.softmax(net_out)
  87. if mode == 'test':
  88. outputs = OrderedDict([('prediction', softmax_out)])
  89. elif mode == 'eval':
  90. labels = to_tensor(inputs[1].numpy().astype('int64').reshape(-1,
  91. 1))
  92. acc1 = paddle.metric.accuracy(softmax_out, label=labels)
  93. k = min(5, self.num_classes)
  94. acck = paddle.metric.accuracy(softmax_out, label=labels, k=k)
  95. prediction = softmax_out
  96. # multi cards eval
  97. if paddle.distributed.get_world_size() > 1:
  98. acc1 = paddle.distributed.all_reduce(
  99. acc1, op=paddle.distributed.ReduceOp.
  100. SUM) / paddle.distributed.get_world_size()
  101. acck = paddle.distributed.all_reduce(
  102. acck, op=paddle.distributed.ReduceOp.
  103. SUM) / paddle.distributed.get_world_size()
  104. prediction = []
  105. paddle.distributed.all_gather(prediction, softmax_out)
  106. prediction = paddle.concat(prediction, axis=0)
  107. outputs = OrderedDict([('acc1', acc1), ('acc{}'.format(k), acck),
  108. ('prediction', prediction)])
  109. else:
  110. # mode == 'train'
  111. labels = to_tensor(inputs[1].numpy().astype('int64').reshape(-1,
  112. 1))
  113. loss = CELoss(class_dim=self.num_classes)
  114. loss = loss(net_out, inputs[1])
  115. acc1 = paddle.metric.accuracy(softmax_out, label=labels, k=1)
  116. k = min(5, self.num_classes)
  117. acck = paddle.metric.accuracy(softmax_out, label=labels, k=k)
  118. outputs = OrderedDict([('loss', loss), ('acc1', acc1),
  119. ('acc{}'.format(k), acck)])
  120. return outputs
  121. def default_optimizer(self, parameters, learning_rate, warmup_steps,
  122. warmup_start_lr, lr_decay_epochs, lr_decay_gamma,
  123. num_steps_each_epoch):
  124. boundaries = [b * num_steps_each_epoch for b in lr_decay_epochs]
  125. values = [
  126. learning_rate * (lr_decay_gamma**i)
  127. for i in range(len(lr_decay_epochs) + 1)
  128. ]
  129. scheduler = paddle.optimizer.lr.PiecewiseDecay(boundaries, values)
  130. if warmup_steps > 0:
  131. if warmup_steps > lr_decay_epochs[0] * num_steps_each_epoch:
  132. logging.error(
  133. "In function train(), parameters should satisfy: "
  134. "warmup_steps <= lr_decay_epochs[0]*num_samples_in_train_dataset",
  135. exit=False)
  136. logging.error(
  137. "See this doc for more information: "
  138. "https://github.com/PaddlePaddle/PaddleX/blob/develop/docs/appendix/parameters.md#notice",
  139. exit=False)
  140. logging.error(
  141. "warmup_steps should less than {} or lr_decay_epochs[0] greater than {}, "
  142. "please modify 'lr_decay_epochs' or 'warmup_steps' in train function".
  143. format(lr_decay_epochs[0] * num_steps_each_epoch,
  144. warmup_steps // num_steps_each_epoch))
  145. scheduler = paddle.optimizer.lr.LinearWarmup(
  146. learning_rate=scheduler,
  147. warmup_steps=warmup_steps,
  148. start_lr=warmup_start_lr,
  149. end_lr=learning_rate)
  150. optimizer = paddle.optimizer.Momentum(
  151. scheduler,
  152. momentum=.9,
  153. weight_decay=paddle.regularizer.L2Decay(coeff=1e-04),
  154. parameters=parameters)
  155. return optimizer
  156. def train(self,
  157. num_epochs,
  158. train_dataset,
  159. train_batch_size=64,
  160. eval_dataset=None,
  161. optimizer=None,
  162. save_interval_epochs=1,
  163. log_interval_steps=10,
  164. save_dir='output',
  165. pretrain_weights='IMAGENET',
  166. learning_rate=.025,
  167. warmup_steps=0,
  168. warmup_start_lr=0.0,
  169. lr_decay_epochs=(30, 60, 90),
  170. lr_decay_gamma=0.1,
  171. early_stop=False,
  172. early_stop_patience=5,
  173. use_vdl=True):
  174. """
  175. Train the model.
  176. Args:
  177. num_epochs(int): The number of epochs.
  178. train_dataset(paddlex.dataset): Training dataset.
  179. train_batch_size(int, optional): Total batch size among all cards used in training. Defaults to 64.
  180. eval_dataset(paddlex.dataset, optional):
  181. Evaluation dataset. If None, the model will not be evaluated during training process. Defaults to None.
  182. optimizer(paddle.optimizer.Optimizer or None, optional):
  183. Optimizer used for training. If None, a default optimizer is used. Defaults to None.
  184. save_interval_epochs(int, optional): Epoch interval for saving the model. Defaults to 1.
  185. log_interval_steps(int, optional): Step interval for printing training information. Defaults to 10.
  186. save_dir(str, optional): Directory to save the model. Defaults to 'output'.
  187. pretrain_weights(str or None, optional):
  188. None or name/path of pretrained weights. If None, no pretrained weights will be loaded. Defaults to 'IMAGENET'.
  189. learning_rate(float, optional): Learning rate for training. Defaults to .025.
  190. warmup_steps(int, optional): The number of steps of warm-up training. Defaults to 0.
  191. warmup_start_lr(float, optional): Start learning rate of warm-up training. Defaults to 0..
  192. lr_decay_epochs(List[int] or Tuple[int], optional):
  193. Epoch milestones for learning rate decay. Defaults to (20, 60, 90).
  194. lr_decay_gamma(float, optional): Gamma coefficient of learning rate decay, default .1.
  195. early_stop(bool, optional): Whether to adopt early stop strategy. Defaults to False.
  196. early_stop_patience(int, optional): Early stop patience. Defaults to 5.
  197. use_vdl(bool, optional): Whether to use VisualDL to monitor the training process. Defaults to True.
  198. """
  199. self.labels = train_dataset.labels
  200. # build optimizer if not defined
  201. if optimizer is None:
  202. num_steps_each_epoch = len(train_dataset) // train_batch_size
  203. self.optimizer = self.default_optimizer(
  204. parameters=self.net.parameters(),
  205. learning_rate=learning_rate,
  206. warmup_steps=warmup_steps,
  207. warmup_start_lr=warmup_start_lr,
  208. lr_decay_epochs=lr_decay_epochs,
  209. lr_decay_gamma=lr_decay_gamma,
  210. num_steps_each_epoch=num_steps_each_epoch)
  211. else:
  212. self.optimizer = optimizer
  213. # initiate weights
  214. if pretrain_weights is not None and not osp.exists(pretrain_weights):
  215. if pretrain_weights not in ['IMAGENET']:
  216. logging.warning(
  217. "Path of pretrain_weights('{}') does not exist!".format(
  218. pretrain_weights))
  219. logging.warning(
  220. "Pretrain_weights is forcibly set to 'IMAGENET'. "
  221. "If don't want to use pretrain weights, "
  222. "set pretrain_weights to be None.")
  223. pretrain_weights = 'IMAGENET'
  224. pretrained_dir = osp.join(save_dir, 'pretrain')
  225. self.net_initialize(
  226. pretrain_weights=pretrain_weights, save_dir=pretrained_dir)
  227. # start train loop
  228. self.train_loop(
  229. num_epochs=num_epochs,
  230. train_dataset=train_dataset,
  231. train_batch_size=train_batch_size,
  232. eval_dataset=eval_dataset,
  233. save_interval_epochs=save_interval_epochs,
  234. log_interval_steps=log_interval_steps,
  235. save_dir=save_dir,
  236. early_stop=early_stop,
  237. early_stop_patience=early_stop_patience,
  238. use_vdl=use_vdl)
  239. def quant_aware_train(self,
  240. num_epochs,
  241. train_dataset,
  242. train_batch_size=64,
  243. eval_dataset=None,
  244. optimizer=None,
  245. save_interval_epochs=1,
  246. log_interval_steps=10,
  247. save_dir='output',
  248. learning_rate=.000025,
  249. warmup_steps=0,
  250. warmup_start_lr=0.0,
  251. lr_decay_epochs=(30, 60, 90),
  252. lr_decay_gamma=0.1,
  253. early_stop=False,
  254. early_stop_patience=5,
  255. use_vdl=True,
  256. quant_config=None):
  257. """
  258. Quantization-aware training.
  259. Args:
  260. num_epochs(int): The number of epochs.
  261. train_dataset(paddlex.dataset): Training dataset.
  262. train_batch_size(int, optional): Total batch size among all cards used in training. Defaults to 64.
  263. eval_dataset(paddlex.dataset, optional):
  264. Evaluation dataset. If None, the model will not be evaluated during training process. Defaults to None.
  265. optimizer(paddle.optimizer.Optimizer or None, optional):
  266. Optimizer used for training. If None, a default optimizer is used. Defaults to None.
  267. save_interval_epochs(int, optional): Epoch interval for saving the model. Defaults to 1.
  268. log_interval_steps(int, optional): Step interval for printing training information. Defaults to 10.
  269. save_dir(str, optional): Directory to save the model. Defaults to 'output'.
  270. learning_rate(float, optional): Learning rate for training. Defaults to .025.
  271. warmup_steps(int, optional): The number of steps of warm-up training. Defaults to 0.
  272. warmup_start_lr(float, optional): Start learning rate of warm-up training. Defaults to 0..
  273. lr_decay_epochs(List[int] or Tuple[int], optional):
  274. Epoch milestones for learning rate decay. Defaults to (20, 60, 90).
  275. lr_decay_gamma(float, optional): Gamma coefficient of learning rate decay, default .1.
  276. early_stop(bool, optional): Whether to adopt early stop strategy. Defaults to False.
  277. early_stop_patience(int, optional): Early stop patience. Defaults to 5.
  278. use_vdl(bool, optional): Whether to use VisualDL to monitor the training process. Defaults to True.
  279. quant_config(dict or None, optional): Quantization configuration. If None, a default rule of thumb
  280. configuration will be used. Defaults to None.
  281. """
  282. self._prepare_qat(quant_config)
  283. self.train(
  284. num_epochs=num_epochs,
  285. train_dataset=train_dataset,
  286. train_batch_size=train_batch_size,
  287. eval_dataset=eval_dataset,
  288. optimizer=optimizer,
  289. save_interval_epochs=save_interval_epochs,
  290. log_interval_steps=log_interval_steps,
  291. save_dir=save_dir,
  292. pretrain_weights=None,
  293. learning_rate=learning_rate,
  294. warmup_steps=warmup_steps,
  295. warmup_start_lr=warmup_start_lr,
  296. lr_decay_epochs=lr_decay_epochs,
  297. lr_decay_gamma=lr_decay_gamma,
  298. early_stop=early_stop,
  299. early_stop_patience=early_stop_patience,
  300. use_vdl=use_vdl)
  301. def evaluate(self, eval_dataset, batch_size=1, return_details=False):
  302. """
  303. Evaluate the model.
  304. Args:
  305. eval_dataset(paddlex.dataset): Evaluation dataset.
  306. batch_size(int, optional): Total batch size among all cards used for evaluation. Defaults to 1.
  307. return_details(bool, optional): Whether to return evaluation details. Defaults to False.
  308. Returns:
  309. collections.OrderedDict with key-value pairs: {"acc1": `top 1 accuracy`, "acc5": `top 5 accuracy`}.
  310. """
  311. # 给transform添加arrange操作
  312. arrange_transforms(
  313. model_type=self.model_type,
  314. transforms=eval_dataset.transforms,
  315. mode='eval')
  316. self.net.eval()
  317. nranks = paddle.distributed.get_world_size()
  318. local_rank = paddle.distributed.get_rank()
  319. if nranks > 1:
  320. # Initialize parallel environment if not done.
  321. if not paddle.distributed.parallel.parallel_helper._is_parallel_ctx_initialized(
  322. ):
  323. paddle.distributed.init_parallel_env()
  324. self.eval_data_loader = self.build_data_loader(
  325. eval_dataset, batch_size=batch_size, mode='eval')
  326. eval_metrics = TrainingStats()
  327. eval_details = None
  328. if return_details:
  329. eval_details = list()
  330. logging.info(
  331. "Start to evaluate(total_samples={}, total_steps={})...".format(
  332. eval_dataset.num_samples,
  333. math.ceil(eval_dataset.num_samples * 1.0 / batch_size)))
  334. with paddle.no_grad():
  335. for step, data in enumerate(self.eval_data_loader()):
  336. outputs = self.run(self.net, data, mode='eval')
  337. if return_details:
  338. eval_details.append(outputs['prediction'].tolist())
  339. outputs.pop('prediction')
  340. eval_metrics.update(outputs)
  341. if return_details:
  342. return eval_metrics.get(), eval_details
  343. else:
  344. return eval_metrics.get()
  345. def predict(self, img_file, transforms=None, topk=1):
  346. """
  347. Do inference.
  348. Args:
  349. img_file(List[np.ndarray or str], str or np.ndarray): img_file(list or str or np.array):
  350. Image path or decoded image data in a BGR format, which also could constitute a list,
  351. meaning all images to be predicted as a mini-batch.
  352. transforms(paddlex.transforms.Compose or None, optional):
  353. Transforms for inputs. If None, the transforms for evaluation process will be used. Defaults to None.
  354. topk(int, optional): Keep topk results in prediction. Defaults to 1.
  355. Returns:
  356. If img_file is a string or np.array, the result is a dict with key-value pairs:
  357. {"category_id": `category_id`, "category": `category`, "score": `score`}.
  358. If img_file is a list, the result is a list composed of dicts with the corresponding fields:
  359. category_id(int): the predicted category ID
  360. category(str): category name
  361. score(float): confidence
  362. """
  363. if transforms is None and not hasattr(self, 'test_transforms'):
  364. raise Exception("transforms need to be defined, now is None.")
  365. if transforms is None:
  366. transforms = self.test_transforms
  367. true_topk = min(self.num_classes, topk)
  368. if isinstance(img_file, (str, np.ndarray)):
  369. images = [img_file]
  370. else:
  371. images = img_file
  372. im = self._preprocess(images, transforms, self.model_type)
  373. self.net.eval()
  374. with paddle.no_grad():
  375. outputs = self.run(self.net, im, mode='test')
  376. prediction = outputs['prediction'].numpy()
  377. prediction = self._postprocess(prediction, true_topk, self.labels)
  378. if isinstance(img_file, (str, np.ndarray)):
  379. prediction = prediction[0]
  380. return prediction
  381. def _preprocess(self, images, transforms, model_type):
  382. arrange_transforms(
  383. model_type=model_type, transforms=transforms, mode='test')
  384. batch_im = list()
  385. for im in images:
  386. sample = {'image': im}
  387. batch_im.append(transforms(sample))
  388. batch_im = to_tensor(batch_im)
  389. return batch_im,
  390. def _postprocess(self, results, true_topk, labels):
  391. preds = list()
  392. for i, pred in enumerate(results):
  393. pred_label = np.argsort(pred)[::-1][:true_topk]
  394. preds.append([{
  395. 'category_id': l,
  396. 'category': labels[l],
  397. 'score': results[i][l]
  398. } for l in pred_label])
  399. return preds
  400. class ResNet18(BaseClassifier):
  401. def __init__(self, num_classes=1000):
  402. super(ResNet18, self).__init__(
  403. model_name='ResNet18', num_classes=num_classes)
  404. class ResNet34(BaseClassifier):
  405. def __init__(self, num_classes=1000):
  406. super(ResNet34, self).__init__(
  407. model_name='ResNet34', num_classes=num_classes)
  408. class ResNet50(BaseClassifier):
  409. def __init__(self, num_classes=1000):
  410. super(ResNet50, self).__init__(
  411. model_name='ResNet50', num_classes=num_classes)
  412. class ResNet101(BaseClassifier):
  413. def __init__(self, num_classes=1000):
  414. super(ResNet101, self).__init__(
  415. model_name='ResNet101', num_classes=num_classes)
  416. class ResNet152(BaseClassifier):
  417. def __init__(self, num_classes=1000):
  418. super(ResNet152, self).__init__(
  419. model_name='ResNet152', num_classes=num_classes)
  420. class ResNet18_vd(BaseClassifier):
  421. def __init__(self, num_classes=1000):
  422. super(ResNet18_vd, self).__init__(
  423. model_name='ResNet18_vd', num_classes=num_classes)
  424. class ResNet34_vd(BaseClassifier):
  425. def __init__(self, num_classes=1000):
  426. super(ResNet34_vd, self).__init__(
  427. model_name='ResNet34_vd', num_classes=num_classes)
  428. class ResNet50_vd(BaseClassifier):
  429. def __init__(self, num_classes=1000):
  430. super(ResNet50_vd, self).__init__(
  431. model_name='ResNet50_vd', num_classes=num_classes)
  432. class ResNet50_vd_ssld(BaseClassifier):
  433. def __init__(self, num_classes=1000):
  434. super(ResNet50_vd_ssld, self).__init__(
  435. model_name='ResNet50_vd',
  436. num_classes=num_classes,
  437. lr_mult_list=[.1, .1, .2, .2, .3])
  438. self.model_name = 'ResNet50_vd_ssld'
  439. class ResNet101_vd(BaseClassifier):
  440. def __init__(self, num_classes=1000):
  441. super(ResNet101_vd, self).__init__(
  442. model_name='ResNet101_vd', num_classes=num_classes)
  443. class ResNet101_vd_ssld(BaseClassifier):
  444. def __init__(self, num_classes=1000):
  445. super(ResNet101_vd_ssld, self).__init__(
  446. model_name='ResNet101_vd',
  447. num_classes=num_classes,
  448. lr_mult_list=[.1, .1, .2, .2, .3])
  449. self.model_name = 'ResNet101_vd_ssld'
  450. class ResNet152_vd(BaseClassifier):
  451. def __init__(self, num_classes=1000):
  452. super(ResNet152_vd, self).__init__(
  453. model_name='ResNet152_vd', num_classes=num_classes)
  454. class ResNet200_vd(BaseClassifier):
  455. def __init__(self, num_classes=1000):
  456. super(ResNet200_vd, self).__init__(
  457. model_name='ResNet200_vd', num_classes=num_classes)
  458. class AlexNet(BaseClassifier):
  459. def __init__(self, num_classes=1000):
  460. super(AlexNet, self).__init__(
  461. model_name='AlexNet', num_classes=num_classes)
  462. def _get_test_inputs(self, image_shape):
  463. if image_shape is not None:
  464. if len(image_shape) == 2:
  465. image_shape = [None, 3] + image_shape
  466. else:
  467. image_shape = [None, 3, 224, 224]
  468. logging.warning(
  469. '[Important!!!] When exporting inference model for {},'.format(
  470. self.__class__.__name__) +
  471. ' if fixed_input_shape is not set, it will be forcibly set to [None, 3, 224, 224]'
  472. +
  473. 'Please check image shape after transforms is [3, 224, 224], if not, fixed_input_shape '
  474. + 'should be specified manually.')
  475. self._fix_transforms_shape(image_shape[-2:])
  476. input_spec = [
  477. InputSpec(
  478. shape=image_shape, name='image', dtype='float32')
  479. ]
  480. return input_spec
  481. class DarkNet53(BaseClassifier):
  482. def __init__(self, num_classes=1000):
  483. super(DarkNet53, self).__init__(
  484. model_name='DarkNet53', num_classes=num_classes)
  485. class MobileNetV1(BaseClassifier):
  486. def __init__(self, num_classes=1000, scale=1.0):
  487. supported_scale = [.25, .5, .75, 1.0]
  488. if scale not in supported_scale:
  489. logging.warning("scale={} is not supported by MobileNetV1, "
  490. "scale is forcibly set to 1.0".format(scale))
  491. scale = 1.0
  492. if scale == 1:
  493. model_name = 'MobileNetV1'
  494. else:
  495. model_name = 'MobileNetV1_x' + str(scale).replace('.', '_')
  496. self.scale = scale
  497. super(MobileNetV1, self).__init__(
  498. model_name=model_name, num_classes=num_classes)
  499. class MobileNetV2(BaseClassifier):
  500. def __init__(self, num_classes=1000, scale=1.0):
  501. supported_scale = [.25, .5, .75, 1.0, 1.5, 2.0]
  502. if scale not in supported_scale:
  503. logging.warning("scale={} is not supported by MobileNetV2, "
  504. "scale is forcibly set to 1.0".format(scale))
  505. scale = 1.0
  506. if scale == 1:
  507. model_name = 'MobileNetV2'
  508. else:
  509. model_name = 'MobileNetV2_x' + str(scale).replace('.', '_')
  510. super(MobileNetV2, self).__init__(
  511. model_name=model_name, num_classes=num_classes)
  512. class MobileNetV3_small(BaseClassifier):
  513. def __init__(self, num_classes=1000, scale=1.0):
  514. supported_scale = [.35, .5, .75, 1.0, 1.25]
  515. if scale not in supported_scale:
  516. logging.warning("scale={} is not supported by MobileNetV3_small, "
  517. "scale is forcibly set to 1.0".format(scale))
  518. scale = 1.0
  519. model_name = 'MobileNetV3_small_x' + str(float(scale)).replace('.',
  520. '_')
  521. super(MobileNetV3_small, self).__init__(
  522. model_name=model_name, num_classes=num_classes)
  523. class MobileNetV3_small_ssld(BaseClassifier):
  524. def __init__(self, num_classes=1000, scale=1.0):
  525. supported_scale = [.35, 1.0]
  526. if scale not in supported_scale:
  527. logging.warning(
  528. "scale={} is not supported by MobileNetV3_small_ssld, "
  529. "scale is forcibly set to 1.0".format(scale))
  530. scale = 1.0
  531. model_name = 'MobileNetV3_small_x' + str(float(scale)).replace('.',
  532. '_')
  533. super(MobileNetV3_small_ssld, self).__init__(
  534. model_name=model_name, num_classes=num_classes)
  535. self.model_name = model_name + '_ssld'
  536. class MobileNetV3_large(BaseClassifier):
  537. def __init__(self, num_classes=1000, scale=1.0):
  538. supported_scale = [.35, .5, .75, 1.0, 1.25]
  539. if scale not in supported_scale:
  540. logging.warning("scale={} is not supported by MobileNetV3_large, "
  541. "scale is forcibly set to 1.0".format(scale))
  542. scale = 1.0
  543. model_name = 'MobileNetV3_large_x' + str(float(scale)).replace('.',
  544. '_')
  545. super(MobileNetV3_large, self).__init__(
  546. model_name=model_name, num_classes=num_classes)
  547. class MobileNetV3_large_ssld(BaseClassifier):
  548. def __init__(self, num_classes=1000):
  549. super(MobileNetV3_large_ssld, self).__init__(
  550. model_name='MobileNetV3_large_x1_0', num_classes=num_classes)
  551. self.model_name = 'MobileNetV3_large_x1_0_ssld'
  552. class DenseNet121(BaseClassifier):
  553. def __init__(self, num_classes=1000):
  554. super(DenseNet121, self).__init__(
  555. model_name='DenseNet121', num_classes=num_classes)
  556. class DenseNet161(BaseClassifier):
  557. def __init__(self, num_classes=1000):
  558. super(DenseNet161, self).__init__(
  559. model_name='DenseNet161', num_classes=num_classes)
  560. class DenseNet169(BaseClassifier):
  561. def __init__(self, num_classes=1000):
  562. super(DenseNet169, self).__init__(
  563. model_name='DenseNet169', num_classes=num_classes)
  564. class DenseNet201(BaseClassifier):
  565. def __init__(self, num_classes=1000):
  566. super(DenseNet201, self).__init__(
  567. model_name='DenseNet201', num_classes=num_classes)
  568. class DenseNet264(BaseClassifier):
  569. def __init__(self, num_classes=1000):
  570. super(DenseNet264, self).__init__(
  571. model_name='DenseNet264', num_classes=num_classes)
  572. class HRNet_W18_C(BaseClassifier):
  573. def __init__(self, num_classes=1000):
  574. super(HRNet_W18_C, self).__init__(
  575. model_name='HRNet_W18_C', num_classes=num_classes)
  576. class HRNet_W30_C(BaseClassifier):
  577. def __init__(self, num_classes=1000):
  578. super(HRNet_W30_C, self).__init__(
  579. model_name='HRNet_W30_C', num_classes=num_classes)
  580. class HRNet_W32_C(BaseClassifier):
  581. def __init__(self, num_classes=1000):
  582. super(HRNet_W32_C, self).__init__(
  583. model_name='HRNet_W32_C', num_classes=num_classes)
  584. class HRNet_W40_C(BaseClassifier):
  585. def __init__(self, num_classes=1000):
  586. super(HRNet_W40_C, self).__init__(
  587. model_name='HRNet_W40_C', num_classes=num_classes)
  588. class HRNet_W44_C(BaseClassifier):
  589. def __init__(self, num_classes=1000):
  590. super(HRNet_W44_C, self).__init__(
  591. model_name='HRNet_W44_C', num_classes=num_classes)
  592. class HRNet_W48_C(BaseClassifier):
  593. def __init__(self, num_classes=1000):
  594. super(HRNet_W48_C, self).__init__(
  595. model_name='HRNet_W48_C', num_classes=num_classes)
  596. class HRNet_W64_C(BaseClassifier):
  597. def __init__(self, num_classes=1000):
  598. super(HRNet_W64_C, self).__init__(
  599. model_name='HRNet_W64_C', num_classes=num_classes)
  600. class Xception41(BaseClassifier):
  601. def __init__(self, num_classes=1000):
  602. super(Xception41, self).__init__(
  603. model_name='Xception41', num_classes=num_classes)
  604. class Xception65(BaseClassifier):
  605. def __init__(self, num_classes=1000):
  606. super(Xception65, self).__init__(
  607. model_name='Xception65', num_classes=num_classes)
  608. class Xception71(BaseClassifier):
  609. def __init__(self, num_classes=1000):
  610. super(Xception71, self).__init__(
  611. model_name='Xception71', num_classes=num_classes)
  612. class ShuffleNetV2(BaseClassifier):
  613. def __init__(self, num_classes=1000, scale=1.0):
  614. supported_scale = [.25, .33, .5, 1.0, 1.5, 2.0]
  615. if scale not in supported_scale:
  616. logging.warning("scale={} is not supported by ShuffleNetV2, "
  617. "scale is forcibly set to 1.0".format(scale))
  618. scale = 1.0
  619. model_name = 'ShuffleNetV2_x' + str(float(scale)).replace('.', '_')
  620. super(ShuffleNetV2, self).__init__(
  621. model_name=model_name, num_classes=num_classes)
  622. def _get_test_inputs(self, image_shape):
  623. if image_shape is not None:
  624. if len(image_shape) == 2:
  625. image_shape = [None, 3] + image_shape
  626. else:
  627. image_shape = [None, 3, 224, 224]
  628. logging.warning(
  629. '[Important!!!] When exporting inference model for {},'.format(
  630. self.__class__.__name__) +
  631. ' if fixed_input_shape is not set, it will be forcibly set to [None, 3, 224, 224]'
  632. +
  633. 'Please check image shape after transforms is [3, 224, 224], if not, fixed_input_shape '
  634. + 'should be specified manually.')
  635. self._fix_transforms_shape(image_shape[-2:])
  636. input_spec = [
  637. InputSpec(
  638. shape=image_shape, name='image', dtype='float32')
  639. ]
  640. return input_spec
  641. class ShuffleNetV2_swish(BaseClassifier):
  642. def __init__(self, num_classes=1000):
  643. super(ShuffleNetV2_swish, self).__init__(
  644. model_name='ShuffleNetV2_x1_5', num_classes=num_classes)
  645. def _get_test_inputs(self, image_shape):
  646. if image_shape is not None:
  647. if len(image_shape) == 2:
  648. image_shape = [None, 3] + image_shape
  649. else:
  650. image_shape = [None, 3, 224, 224]
  651. logging.warning(
  652. '[Important!!!] When exporting inference model for {},'.format(
  653. self.__class__.__name__) +
  654. ' if fixed_input_shape is not set, it will be forcibly set to [None, 3, 224, 224]'
  655. +
  656. 'Please check image shape after transforms is [3, 224, 224], if not, fixed_input_shape '
  657. + 'should be specified manually.')
  658. self._fix_transforms_shape(image_shape[-2:])
  659. input_spec = [
  660. InputSpec(
  661. shape=image_shape, name='image', dtype='float32')
  662. ]
  663. return input_spec