program.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. from __future__ import division
  16. from __future__ import print_function
  17. import os
  18. import sys
  19. import platform
  20. import yaml
  21. import time
  22. import datetime
  23. import paddle
  24. import paddle.distributed as dist
  25. from tqdm import tqdm
  26. import cv2
  27. import numpy as np
  28. from argparse import ArgumentParser, RawDescriptionHelpFormatter
  29. from ppocr.utils.stats import TrainingStats
  30. from ppocr.utils.save_load import save_model
  31. from ppocr.utils.utility import print_dict, AverageMeter
  32. from ppocr.utils.logging import get_logger
  33. from ppocr.utils.loggers import VDLLogger, WandbLogger, Loggers
  34. from ppocr.utils import profiler
  35. from ppocr.data import build_dataloader
  36. class ArgsParser(ArgumentParser):
  37. def __init__(self):
  38. super(ArgsParser, self).__init__(
  39. formatter_class=RawDescriptionHelpFormatter)
  40. self.add_argument("-c", "--config", help="configuration file to use")
  41. self.add_argument(
  42. "-o", "--opt", nargs='+', help="set configuration options")
  43. self.add_argument(
  44. '-p',
  45. '--profiler_options',
  46. type=str,
  47. default=None,
  48. help='The option of profiler, which should be in format ' \
  49. '\"key1=value1;key2=value2;key3=value3\".'
  50. )
  51. def parse_args(self, argv=None):
  52. args = super(ArgsParser, self).parse_args(argv)
  53. assert args.config is not None, \
  54. "Please specify --config=configure_file_path."
  55. args.opt = self._parse_opt(args.opt)
  56. return args
  57. def _parse_opt(self, opts):
  58. config = {}
  59. if not opts:
  60. return config
  61. for s in opts:
  62. s = s.strip()
  63. k, v = s.split('=')
  64. config[k] = yaml.load(v, Loader=yaml.Loader)
  65. return config
  66. def load_config(file_path):
  67. """
  68. Load config from yml/yaml file.
  69. Args:
  70. file_path (str): Path of the config file to be loaded.
  71. Returns: global config
  72. """
  73. _, ext = os.path.splitext(file_path)
  74. assert ext in ['.yml', '.yaml'], "only support yaml files for now"
  75. config = yaml.load(open(file_path, 'rb'), Loader=yaml.Loader)
  76. return config
  77. def merge_config(config, opts):
  78. """
  79. Merge config into global config.
  80. Args:
  81. config (dict): Config to be merged.
  82. Returns: global config
  83. """
  84. for key, value in opts.items():
  85. if "." not in key:
  86. if isinstance(value, dict) and key in config:
  87. config[key].update(value)
  88. else:
  89. config[key] = value
  90. else:
  91. sub_keys = key.split('.')
  92. assert (
  93. sub_keys[0] in config
  94. ), "the sub_keys can only be one of global_config: {}, but get: " \
  95. "{}, please check your running command".format(
  96. config.keys(), sub_keys[0])
  97. cur = config[sub_keys[0]]
  98. for idx, sub_key in enumerate(sub_keys[1:]):
  99. if idx == len(sub_keys) - 2:
  100. cur[sub_key] = value
  101. else:
  102. cur = cur[sub_key]
  103. return config
  104. def check_device(use_gpu, use_xpu=False, use_npu=False, use_mlu=False):
  105. """
  106. Log error and exit when set use_gpu=true in paddlepaddle
  107. cpu version.
  108. """
  109. err = "Config {} cannot be set as true while your paddle " \
  110. "is not compiled with {} ! \nPlease try: \n" \
  111. "\t1. Install paddlepaddle to run model on {} \n" \
  112. "\t2. Set {} as false in config file to run " \
  113. "model on CPU"
  114. try:
  115. if use_gpu and use_xpu:
  116. print("use_xpu and use_gpu can not both be ture.")
  117. if use_gpu and not paddle.is_compiled_with_cuda():
  118. print(err.format("use_gpu", "cuda", "gpu", "use_gpu"))
  119. sys.exit(1)
  120. if use_xpu and not paddle.device.is_compiled_with_xpu():
  121. print(err.format("use_xpu", "xpu", "xpu", "use_xpu"))
  122. sys.exit(1)
  123. if use_npu and not paddle.device.is_compiled_with_npu():
  124. print(err.format("use_npu", "npu", "npu", "use_npu"))
  125. sys.exit(1)
  126. if use_mlu and not paddle.device.is_compiled_with_mlu():
  127. print(err.format("use_mlu", "mlu", "mlu", "use_mlu"))
  128. sys.exit(1)
  129. except Exception as e:
  130. pass
  131. def to_float32(preds):
  132. if isinstance(preds, dict):
  133. for k in preds:
  134. if isinstance(preds[k], dict) or isinstance(preds[k], list):
  135. preds[k] = to_float32(preds[k])
  136. elif isinstance(preds[k], paddle.Tensor):
  137. preds[k] = preds[k].astype(paddle.float32)
  138. elif isinstance(preds, list):
  139. for k in range(len(preds)):
  140. if isinstance(preds[k], dict):
  141. preds[k] = to_float32(preds[k])
  142. elif isinstance(preds[k], list):
  143. preds[k] = to_float32(preds[k])
  144. elif isinstance(preds[k], paddle.Tensor):
  145. preds[k] = preds[k].astype(paddle.float32)
  146. elif isinstance(preds, paddle.Tensor):
  147. preds = preds.astype(paddle.float32)
  148. return preds
  149. def train(config,
  150. train_dataloader,
  151. valid_dataloader,
  152. device,
  153. model,
  154. loss_class,
  155. optimizer,
  156. lr_scheduler,
  157. post_process_class,
  158. eval_class,
  159. pre_best_model_dict,
  160. logger,
  161. log_writer=None,
  162. scaler=None,
  163. amp_level='O2',
  164. amp_custom_black_list=[]):
  165. cal_metric_during_train = config['Global'].get('cal_metric_during_train',
  166. False)
  167. calc_epoch_interval = config['Global'].get('calc_epoch_interval', 1)
  168. log_smooth_window = config['Global']['log_smooth_window']
  169. epoch_num = config['Global']['epoch_num']
  170. print_batch_step = config['Global']['print_batch_step']
  171. eval_batch_step = config['Global']['eval_batch_step']
  172. profiler_options = config['profiler_options']
  173. global_step = 0
  174. if 'global_step' in pre_best_model_dict:
  175. global_step = pre_best_model_dict['global_step']
  176. start_eval_step = 0
  177. if type(eval_batch_step) == list and len(eval_batch_step) >= 2:
  178. start_eval_step = eval_batch_step[0]
  179. eval_batch_step = eval_batch_step[1]
  180. if len(valid_dataloader) == 0:
  181. logger.info(
  182. 'No Images in eval dataset, evaluation during training ' \
  183. 'will be disabled'
  184. )
  185. start_eval_step = 1e111
  186. logger.info(
  187. "During the training process, after the {}th iteration, " \
  188. "an evaluation is run every {} iterations".
  189. format(start_eval_step, eval_batch_step))
  190. save_epoch_step = config['Global']['save_epoch_step']
  191. save_model_dir = config['Global']['save_model_dir']
  192. if not os.path.exists(save_model_dir):
  193. os.makedirs(save_model_dir)
  194. main_indicator = eval_class.main_indicator
  195. best_model_dict = {main_indicator: 0}
  196. best_model_dict.update(pre_best_model_dict)
  197. train_stats = TrainingStats(log_smooth_window, ['lr'])
  198. model_average = False
  199. model.train()
  200. use_srn = config['Architecture']['algorithm'] == "SRN"
  201. extra_input_models = [
  202. "SRN", "NRTR", "SAR", "SEED", "SVTR", "SPIN", "VisionLAN",
  203. "RobustScanner", "RFL", 'DRRG'
  204. ]
  205. extra_input = False
  206. if config['Architecture']['algorithm'] == 'Distillation':
  207. for key in config['Architecture']["Models"]:
  208. extra_input = extra_input or config['Architecture']['Models'][key][
  209. 'algorithm'] in extra_input_models
  210. else:
  211. extra_input = config['Architecture']['algorithm'] in extra_input_models
  212. try:
  213. model_type = config['Architecture']['model_type']
  214. except:
  215. model_type = None
  216. algorithm = config['Architecture']['algorithm']
  217. start_epoch = best_model_dict[
  218. 'start_epoch'] if 'start_epoch' in best_model_dict else 1
  219. total_samples = 0
  220. train_reader_cost = 0.0
  221. train_batch_cost = 0.0
  222. reader_start = time.time()
  223. eta_meter = AverageMeter()
  224. max_iter = len(train_dataloader) - 1 if platform.system(
  225. ) == "Windows" else len(train_dataloader)
  226. for epoch in range(start_epoch, epoch_num + 1):
  227. if train_dataloader.dataset.need_reset:
  228. train_dataloader = build_dataloader(
  229. config, 'Train', device, logger, seed=epoch)
  230. max_iter = len(train_dataloader) - 1 if platform.system(
  231. ) == "Windows" else len(train_dataloader)
  232. for idx, batch in enumerate(train_dataloader):
  233. profiler.add_profiler_step(profiler_options)
  234. train_reader_cost += time.time() - reader_start
  235. if idx >= max_iter:
  236. break
  237. lr = optimizer.get_lr()
  238. images = batch[0]
  239. if use_srn:
  240. model_average = True
  241. # use amp
  242. if scaler:
  243. with paddle.amp.auto_cast(
  244. level=amp_level,
  245. custom_black_list=amp_custom_black_list):
  246. if model_type == 'table' or extra_input:
  247. preds = model(images, data=batch[1:])
  248. elif model_type in ["kie"]:
  249. preds = model(batch)
  250. elif algorithm in ['CAN']:
  251. preds = model(batch[:3])
  252. else:
  253. preds = model(images)
  254. preds = to_float32(preds)
  255. loss = loss_class(preds, batch)
  256. avg_loss = loss['loss']
  257. scaled_avg_loss = scaler.scale(avg_loss)
  258. scaled_avg_loss.backward()
  259. scaler.minimize(optimizer, scaled_avg_loss)
  260. else:
  261. if model_type == 'table' or extra_input:
  262. preds = model(images, data=batch[1:])
  263. elif model_type in ["kie", 'sr']:
  264. preds = model(batch)
  265. elif algorithm in ['CAN']:
  266. preds = model(batch[:3])
  267. else:
  268. preds = model(images)
  269. loss = loss_class(preds, batch)
  270. avg_loss = loss['loss']
  271. avg_loss.backward()
  272. optimizer.step()
  273. optimizer.clear_grad()
  274. if cal_metric_during_train and epoch % calc_epoch_interval == 0: # only rec and cls need
  275. batch = [item.numpy() for item in batch]
  276. if model_type in ['kie', 'sr']:
  277. eval_class(preds, batch)
  278. elif model_type in ['table']:
  279. post_result = post_process_class(preds, batch)
  280. eval_class(post_result, batch)
  281. elif algorithm in ['CAN']:
  282. model_type = 'can'
  283. eval_class(preds[0], batch[2:], epoch_reset=(idx == 0))
  284. else:
  285. if config['Loss']['name'] in ['MultiLoss', 'MultiLoss_v2'
  286. ]: # for multi head loss
  287. post_result = post_process_class(
  288. preds['ctc'], batch[1]) # for CTC head out
  289. elif config['Loss']['name'] in ['VLLoss']:
  290. post_result = post_process_class(preds, batch[1],
  291. batch[-1])
  292. else:
  293. post_result = post_process_class(preds, batch[1])
  294. eval_class(post_result, batch)
  295. metric = eval_class.get_metric()
  296. train_stats.update(metric)
  297. train_batch_time = time.time() - reader_start
  298. train_batch_cost += train_batch_time
  299. eta_meter.update(train_batch_time)
  300. global_step += 1
  301. total_samples += len(images)
  302. if not isinstance(lr_scheduler, float):
  303. lr_scheduler.step()
  304. # logger and visualdl
  305. stats = {k: v.numpy().mean() for k, v in loss.items()}
  306. stats['lr'] = lr
  307. train_stats.update(stats)
  308. if log_writer is not None and dist.get_rank() == 0:
  309. log_writer.log_metrics(
  310. metrics=train_stats.get(), prefix="TRAIN", step=global_step)
  311. if dist.get_rank() == 0 and (
  312. (global_step > 0 and global_step % print_batch_step == 0) or
  313. (idx >= len(train_dataloader) - 1)):
  314. logs = train_stats.log()
  315. eta_sec = ((epoch_num + 1 - epoch) * \
  316. len(train_dataloader) - idx - 1) * eta_meter.avg
  317. eta_sec_format = str(datetime.timedelta(seconds=int(eta_sec)))
  318. strs = 'epoch: [{}/{}], global_step: {}, {}, avg_reader_cost: ' \
  319. '{:.5f} s, avg_batch_cost: {:.5f} s, avg_samples: {}, ' \
  320. 'ips: {:.5f} samples/s, eta: {}'.format(
  321. epoch, epoch_num, global_step, logs,
  322. train_reader_cost / print_batch_step,
  323. train_batch_cost / print_batch_step,
  324. total_samples / print_batch_step,
  325. total_samples / train_batch_cost, eta_sec_format)
  326. logger.info(strs)
  327. total_samples = 0
  328. train_reader_cost = 0.0
  329. train_batch_cost = 0.0
  330. # eval
  331. if global_step > start_eval_step and \
  332. (global_step - start_eval_step) % eval_batch_step == 0 \
  333. and dist.get_rank() == 0:
  334. if model_average:
  335. Model_Average = paddle.incubate.optimizer.ModelAverage(
  336. 0.15,
  337. parameters=model.parameters(),
  338. min_average_window=10000,
  339. max_average_window=15625)
  340. Model_Average.apply()
  341. cur_metric = eval(
  342. model,
  343. valid_dataloader,
  344. post_process_class,
  345. eval_class,
  346. model_type,
  347. extra_input=extra_input,
  348. scaler=scaler,
  349. amp_level=amp_level,
  350. amp_custom_black_list=amp_custom_black_list)
  351. cur_metric_str = 'cur metric, {}'.format(', '.join(
  352. ['{}: {}'.format(k, v) for k, v in cur_metric.items()]))
  353. logger.info(cur_metric_str)
  354. # logger metric
  355. if log_writer is not None:
  356. log_writer.log_metrics(
  357. metrics=cur_metric, prefix="EVAL", step=global_step)
  358. if cur_metric[main_indicator] >= best_model_dict[
  359. main_indicator]:
  360. best_model_dict.update(cur_metric)
  361. best_model_dict['best_epoch'] = epoch
  362. save_model(
  363. model,
  364. optimizer,
  365. save_model_dir,
  366. logger,
  367. config,
  368. is_best=True,
  369. prefix='best_accuracy',
  370. best_model_dict=best_model_dict,
  371. epoch=epoch,
  372. global_step=global_step)
  373. best_str = 'best metric, {}'.format(', '.join([
  374. '{}: {}'.format(k, v) for k, v in best_model_dict.items()
  375. ]))
  376. logger.info(best_str)
  377. # logger best metric
  378. if log_writer is not None:
  379. log_writer.log_metrics(
  380. metrics={
  381. "best_{}".format(main_indicator):
  382. best_model_dict[main_indicator]
  383. },
  384. prefix="EVAL",
  385. step=global_step)
  386. log_writer.log_model(
  387. is_best=True,
  388. prefix="best_accuracy",
  389. metadata=best_model_dict)
  390. reader_start = time.time()
  391. if dist.get_rank() == 0:
  392. save_model(
  393. model,
  394. optimizer,
  395. save_model_dir,
  396. logger,
  397. config,
  398. is_best=False,
  399. prefix='latest',
  400. best_model_dict=best_model_dict,
  401. epoch=epoch,
  402. global_step=global_step)
  403. if log_writer is not None:
  404. log_writer.log_model(is_best=False, prefix="latest")
  405. if dist.get_rank() == 0 and epoch > 0 and epoch % save_epoch_step == 0:
  406. save_model(
  407. model,
  408. optimizer,
  409. save_model_dir,
  410. logger,
  411. config,
  412. is_best=False,
  413. prefix='iter_epoch_{}'.format(epoch),
  414. best_model_dict=best_model_dict,
  415. epoch=epoch,
  416. global_step=global_step)
  417. if log_writer is not None:
  418. log_writer.log_model(
  419. is_best=False, prefix='iter_epoch_{}'.format(epoch))
  420. best_str = 'best metric, {}'.format(', '.join(
  421. ['{}: {}'.format(k, v) for k, v in best_model_dict.items()]))
  422. logger.info(best_str)
  423. if dist.get_rank() == 0 and log_writer is not None:
  424. log_writer.close()
  425. return
  426. def eval(model,
  427. valid_dataloader,
  428. post_process_class,
  429. eval_class,
  430. model_type=None,
  431. extra_input=False,
  432. scaler=None,
  433. amp_level='O2',
  434. amp_custom_black_list=[]):
  435. model.eval()
  436. with paddle.no_grad():
  437. total_frame = 0.0
  438. total_time = 0.0
  439. pbar = tqdm(
  440. total=len(valid_dataloader),
  441. desc='eval model:',
  442. position=0,
  443. leave=True)
  444. max_iter = len(valid_dataloader) - 1 if platform.system(
  445. ) == "Windows" else len(valid_dataloader)
  446. sum_images = 0
  447. for idx, batch in enumerate(valid_dataloader):
  448. if idx >= max_iter:
  449. break
  450. images = batch[0]
  451. start = time.time()
  452. # use amp
  453. if scaler:
  454. with paddle.amp.auto_cast(
  455. level=amp_level,
  456. custom_black_list=amp_custom_black_list):
  457. if model_type == 'table' or extra_input:
  458. preds = model(images, data=batch[1:])
  459. elif model_type in ["kie"]:
  460. preds = model(batch)
  461. elif model_type in ['can']:
  462. preds = model(batch[:3])
  463. elif model_type in ['sr']:
  464. preds = model(batch)
  465. sr_img = preds["sr_img"]
  466. lr_img = preds["lr_img"]
  467. else:
  468. preds = model(images)
  469. preds = to_float32(preds)
  470. else:
  471. if model_type == 'table' or extra_input:
  472. preds = model(images, data=batch[1:])
  473. elif model_type in ["kie"]:
  474. preds = model(batch)
  475. elif model_type in ['can']:
  476. preds = model(batch[:3])
  477. elif model_type in ['sr']:
  478. preds = model(batch)
  479. sr_img = preds["sr_img"]
  480. lr_img = preds["lr_img"]
  481. else:
  482. preds = model(images)
  483. batch_numpy = []
  484. for item in batch:
  485. if isinstance(item, paddle.Tensor):
  486. batch_numpy.append(item.numpy())
  487. else:
  488. batch_numpy.append(item)
  489. # Obtain usable results from post-processing methods
  490. total_time += time.time() - start
  491. # Evaluate the results of the current batch
  492. if model_type in ['table', 'kie']:
  493. if post_process_class is None:
  494. eval_class(preds, batch_numpy)
  495. else:
  496. post_result = post_process_class(preds, batch_numpy)
  497. eval_class(post_result, batch_numpy)
  498. elif model_type in ['sr']:
  499. eval_class(preds, batch_numpy)
  500. elif model_type in ['can']:
  501. eval_class(preds[0], batch_numpy[2:], epoch_reset=(idx == 0))
  502. else:
  503. post_result = post_process_class(preds, batch_numpy[1])
  504. eval_class(post_result, batch_numpy)
  505. pbar.update(1)
  506. total_frame += len(images)
  507. sum_images += 1
  508. # Get final metric,eg. acc or hmean
  509. metric = eval_class.get_metric()
  510. pbar.close()
  511. model.train()
  512. metric['fps'] = total_frame / total_time
  513. return metric
  514. def update_center(char_center, post_result, preds):
  515. result, label = post_result
  516. feats, logits = preds
  517. logits = paddle.argmax(logits, axis=-1)
  518. feats = feats.numpy()
  519. logits = logits.numpy()
  520. for idx_sample in range(len(label)):
  521. if result[idx_sample][0] == label[idx_sample][0]:
  522. feat = feats[idx_sample]
  523. logit = logits[idx_sample]
  524. for idx_time in range(len(logit)):
  525. index = logit[idx_time]
  526. if index in char_center.keys():
  527. char_center[index][0] = (
  528. char_center[index][0] * char_center[index][1] +
  529. feat[idx_time]) / (char_center[index][1] + 1)
  530. char_center[index][1] += 1
  531. else:
  532. char_center[index] = [feat[idx_time], 1]
  533. return char_center
  534. def get_center(model, eval_dataloader, post_process_class):
  535. pbar = tqdm(total=len(eval_dataloader), desc='get center:')
  536. max_iter = len(eval_dataloader) - 1 if platform.system(
  537. ) == "Windows" else len(eval_dataloader)
  538. char_center = dict()
  539. for idx, batch in enumerate(eval_dataloader):
  540. if idx >= max_iter:
  541. break
  542. images = batch[0]
  543. start = time.time()
  544. preds = model(images)
  545. batch = [item.numpy() for item in batch]
  546. # Obtain usable results from post-processing methods
  547. post_result = post_process_class(preds, batch[1])
  548. #update char_center
  549. char_center = update_center(char_center, post_result, preds)
  550. pbar.update(1)
  551. pbar.close()
  552. for key in char_center.keys():
  553. char_center[key] = char_center[key][0]
  554. return char_center
  555. def preprocess(is_train=False):
  556. FLAGS = ArgsParser().parse_args()
  557. profiler_options = FLAGS.profiler_options
  558. config = load_config(FLAGS.config)
  559. config = merge_config(config, FLAGS.opt)
  560. profile_dic = {"profiler_options": FLAGS.profiler_options}
  561. config = merge_config(config, profile_dic)
  562. if is_train:
  563. # save_config
  564. save_model_dir = config['Global']['save_model_dir']
  565. os.makedirs(save_model_dir, exist_ok=True)
  566. with open(os.path.join(save_model_dir, 'config.yml'), 'w') as f:
  567. yaml.dump(
  568. dict(config), f, default_flow_style=False, sort_keys=False)
  569. log_file = '{}/train.log'.format(save_model_dir)
  570. else:
  571. log_file = None
  572. logger = get_logger(log_file=log_file)
  573. # check if set use_gpu=True in paddlepaddle cpu version
  574. use_gpu = config['Global'].get('use_gpu', False)
  575. use_xpu = config['Global'].get('use_xpu', False)
  576. use_npu = config['Global'].get('use_npu', False)
  577. use_mlu = config['Global'].get('use_mlu', False)
  578. alg = config['Architecture']['algorithm']
  579. assert alg in [
  580. 'EAST', 'DB', 'SAST', 'Rosetta', 'CRNN', 'STARNet', 'RARE', 'SRN',
  581. 'CLS', 'PGNet', 'Distillation', 'NRTR', 'TableAttn', 'SAR', 'PSE',
  582. 'SEED', 'SDMGR', 'LayoutXLM', 'LayoutLM', 'LayoutLMv2', 'PREN', 'FCE',
  583. 'SVTR', 'ViTSTR', 'ABINet', 'DB++', 'TableMaster', 'SPIN', 'VisionLAN',
  584. 'Gestalt', 'SLANet', 'RobustScanner', 'CT', 'RFL', 'DRRG', 'CAN',
  585. 'Telescope'
  586. ]
  587. if use_xpu:
  588. device = 'xpu:{0}'.format(os.getenv('FLAGS_selected_xpus', 0))
  589. elif use_npu:
  590. device = 'npu:{0}'.format(os.getenv('FLAGS_selected_npus', 0))
  591. elif use_mlu:
  592. device = 'mlu:{0}'.format(os.getenv('FLAGS_selected_mlus', 0))
  593. else:
  594. device = 'gpu:{}'.format(dist.ParallelEnv()
  595. .dev_id) if use_gpu else 'cpu'
  596. check_device(use_gpu, use_xpu, use_npu, use_mlu)
  597. device = paddle.set_device(device)
  598. config['Global']['distributed'] = dist.get_world_size() != 1
  599. loggers = []
  600. if 'use_visualdl' in config['Global'] and config['Global']['use_visualdl']:
  601. save_model_dir = config['Global']['save_model_dir']
  602. vdl_writer_path = '{}/vdl/'.format(save_model_dir)
  603. log_writer = VDLLogger(vdl_writer_path)
  604. loggers.append(log_writer)
  605. if ('use_wandb' in config['Global'] and
  606. config['Global']['use_wandb']) or 'wandb' in config:
  607. save_dir = config['Global']['save_model_dir']
  608. wandb_writer_path = "{}/wandb".format(save_dir)
  609. if "wandb" in config:
  610. wandb_params = config['wandb']
  611. else:
  612. wandb_params = dict()
  613. wandb_params.update({'save_dir': save_model_dir})
  614. log_writer = WandbLogger(**wandb_params, config=config)
  615. loggers.append(log_writer)
  616. else:
  617. log_writer = None
  618. print_dict(config, logger)
  619. if loggers:
  620. log_writer = Loggers(loggers)
  621. else:
  622. log_writer = None
  623. logger.info('train with paddle {} and device {}'.format(paddle.__version__,
  624. device))
  625. return config, device, logger, log_writer