classifier.py 30 KB

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