test_architectures.py 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#   Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import unittest

K
Kaipeng Deng 已提交
21
import paddle
22 23 24 25 26 27
import os
import sys
# add python path of PadleDetection to sys.path
parent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 4)))
if parent_path not in sys.path:
    sys.path.append(parent_path)
28

29
try:
M
Manuel Garcia 已提交
30
    from ppdet.utils.check import enable_static_mode, logger
31 32 33 34 35 36 37 38 39 40 41 42 43 44
    from ppdet.modeling.tests.decorator_helper import prog_scope
    from ppdet.core.workspace import load_config, merge_config, create
except ImportError as e:
    if sys.argv[0].find('static') >= 0:
        logger.error("Importing ppdet failed when running static model "
                     "with error: {}\n"
                     "please try:\n"
                     "\t1. run static model under PaddleDetection/static "
                     "directory\n"
                     "\t2. run 'pip uninstall ppdet' to uninstall ppdet "
                     "dynamic version firstly.".format(e))
        sys.exit(-1)
    else:
        raise e
45 46 47 48 49 50 51 52 53 54 55 56 57 58


class TestFasterRCNN(unittest.TestCase):
    def setUp(self):
        self.set_config()
        self.cfg = load_config(self.cfg_file)
        self.detector_type = self.cfg['architecture']

    def set_config(self):
        self.cfg_file = 'configs/faster_rcnn_r50_1x.yml'

    @prog_scope()
    def test_train(self):
        model = create(self.detector_type)
59 60 61
        inputs_def = self.cfg['TrainReader']['inputs_def']
        inputs_def['image_shape'] = [3, None, None]
        feed_vars, _ = model.build_inputs(**inputs_def)
62 63 64 65
        train_fetches = model.train(feed_vars)

    @prog_scope()
    def test_test(self):
66 67
        inputs_def = self.cfg['EvalReader']['inputs_def']
        inputs_def['image_shape'] = [3, None, None]
68
        model = create(self.detector_type)
69
        feed_vars, _ = model.build_inputs(**inputs_def)
70 71 72 73 74 75 76 77
        test_fetches = model.eval(feed_vars)


class TestMaskRCNN(TestFasterRCNN):
    def set_config(self):
        self.cfg_file = 'configs/mask_rcnn_r50_1x.yml'


W
whs 已提交
78 79 80
@unittest.skip(
    reason="It should be fixed to adapt https://github.com/PaddlePaddle/Paddle/pull/23797"
)
81 82 83 84 85
class TestCascadeRCNN(TestFasterRCNN):
    def set_config(self):
        self.cfg_file = 'configs/cascade_rcnn_r50_fpn_1x.yml'


K
Kaipeng Deng 已提交
86
@unittest.skipIf(
87
    paddle.version.full_version < "1.8.4",
K
Kaipeng Deng 已提交
88 89
    "Paddle 2.0 should be used for YOLOv3 takes scale_x_y as inputs, "
    "disable this unittest for Paddle major version < 2")
90 91 92 93 94 95 96 97 98 99 100 101
class TestYolov3(TestFasterRCNN):
    def set_config(self):
        self.cfg_file = 'configs/yolov3_darknet.yml'


class TestRetinaNet(TestFasterRCNN):
    def set_config(self):
        self.cfg_file = 'configs/retinanet_r50_fpn_1x.yml'


class TestSSD(TestFasterRCNN):
    def set_config(self):
G
Guanghua Yu 已提交
102
        self.cfg_file = 'configs/ssd/ssd_mobilenet_v1_voc.yml'
103 104 105


if __name__ == '__main__':
106
    enable_static_mode()
107
    unittest.main()