test_mstest.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 unittest
  19. from ppdet.core.workspace import load_config
  20. from ppdet.engine import Trainer
  21. class TestMultiScaleInference(unittest.TestCase):
  22. def setUp(self):
  23. self.set_config()
  24. def set_config(self):
  25. self.mstest_cfg_file = 'configs/faster_rcnn/faster_rcnn_r34_fpn_multiscaletest_1x_coco.yml'
  26. # test evaluation with multi scale test
  27. def test_eval_mstest(self):
  28. cfg = load_config(self.mstest_cfg_file)
  29. trainer = Trainer(cfg, mode='eval')
  30. cfg.weights = 'https://paddledet.bj.bcebos.com/models/faster_rcnn_r34_fpn_1x_coco.pdparams'
  31. trainer.load_weights(cfg.weights)
  32. trainer.evaluate()
  33. # test inference with multi scale test
  34. def test_infer_mstest(self):
  35. cfg = load_config(self.mstest_cfg_file)
  36. trainer = Trainer(cfg, mode='test')
  37. cfg.weights = 'https://paddledet.bj.bcebos.com/models/faster_rcnn_r34_fpn_1x_coco.pdparams'
  38. trainer.load_weights(cfg.weights)
  39. tests_img_root = os.path.join(os.path.dirname(__file__), 'imgs')
  40. # input images to predict
  41. imgs = [
  42. 'coco2017_val2017_000000000139.jpg',
  43. 'coco2017_val2017_000000000724.jpg'
  44. ]
  45. imgs = [os.path.join(tests_img_root, img) for img in imgs]
  46. trainer.predict(
  47. imgs, draw_threshold=0.5, output_dir='output', save_results=False)
  48. if __name__ == '__main__':
  49. unittest.main()