export_center.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (c) 2020 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 pickle
  20. __dir__ = os.path.dirname(os.path.abspath(__file__))
  21. sys.path.append(__dir__)
  22. sys.path.append(os.path.abspath(os.path.join(__dir__, '..')))
  23. from ppocr.data import build_dataloader
  24. from ppocr.modeling.architectures import build_model
  25. from ppocr.postprocess import build_post_process
  26. from ppocr.utils.save_load import load_model
  27. from ppocr.utils.utility import print_dict
  28. import tools.program as program
  29. def main():
  30. global_config = config['Global']
  31. # build dataloader
  32. config['Eval']['dataset']['name'] = config['Train']['dataset']['name']
  33. config['Eval']['dataset']['data_dir'] = config['Train']['dataset'][
  34. 'data_dir']
  35. config['Eval']['dataset']['label_file_list'] = config['Train']['dataset'][
  36. 'label_file_list']
  37. eval_dataloader = build_dataloader(config, 'Eval', device, logger)
  38. # build post process
  39. post_process_class = build_post_process(config['PostProcess'],
  40. global_config)
  41. # build model
  42. # for rec algorithm
  43. if hasattr(post_process_class, 'character'):
  44. char_num = len(getattr(post_process_class, 'character'))
  45. config['Architecture']["Head"]['out_channels'] = char_num
  46. #set return_features = True
  47. config['Architecture']["Head"]["return_feats"] = True
  48. model = build_model(config['Architecture'])
  49. best_model_dict = load_model(config, model)
  50. if len(best_model_dict):
  51. logger.info('metric in ckpt ***************')
  52. for k, v in best_model_dict.items():
  53. logger.info('{}:{}'.format(k, v))
  54. # get features from train data
  55. char_center = program.get_center(model, eval_dataloader, post_process_class)
  56. #serialize to disk
  57. with open("train_center.pkl", 'wb') as f:
  58. pickle.dump(char_center, f)
  59. return
  60. if __name__ == '__main__':
  61. config, device, logger, vdl_writer = program.preprocess()
  62. main()