test_trt_yolo_box_op.py 6.4 KB
Newer Older
Z
zlsh80826 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2020 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.

import unittest
16

Z
zlsh80826 已提交
17 18
import numpy as np
from inference_pass_test import InferencePassTest
19

20
import paddle
21
from paddle import fluid
22
from paddle.fluid.core import AnalysisConfig, PassVersionChecker
Z
zlsh80826 已提交
23 24 25 26 27 28 29


class TRTYoloBoxTest(InferencePassTest):
    def setUp(self):
        self.set_params()
        with fluid.program_guard(self.main_program, self.startup_program):
            image_shape = [self.bs, self.channel, self.height, self.width]
30 31 32 33
            image = paddle.static.data(
                name='image', shape=image_shape, dtype='float32'
            )
            image_size = paddle.static.data(
34 35
                name='image_size', shape=[self.bs, 2], dtype='int32'
            )
Z
zlsh80826 已提交
36 37 38
            boxes, scores = self.append_yolobox(image, image_size)

        self.feeds = {
39 40 41 42
            'image': np.random.random(image_shape).astype('float32'),
            'image_size': np.random.randint(32, 64, size=(self.bs, 2)).astype(
                'int32'
            ),
Z
zlsh80826 已提交
43 44 45
        }
        self.enable_trt = True
        self.trt_parameters = TRTYoloBoxTest.TensorRTParam(
46 47
            1 << 30, self.bs, 1, AnalysisConfig.Precision.Float32, False, False
        )
W
Wilber 已提交
48
        self.fetch_list = [scores, boxes]
Z
zlsh80826 已提交
49 50 51 52 53 54 55 56

    def set_params(self):
        self.bs = 4
        self.channel = 255
        self.height = 64
        self.width = 64
        self.class_num = 80
        self.anchors = [10, 13, 16, 30, 33, 23]
57
        self.conf_thresh = 0.1
Z
zlsh80826 已提交
58 59 60
        self.downsample_ratio = 32

    def append_yolobox(self, image, image_size):
61
        return paddle.vision.ops.yolo_box(
62 63 64 65 66 67 68
            x=image,
            img_size=image_size,
            class_num=self.class_num,
            anchors=self.anchors,
            conf_thresh=self.conf_thresh,
            downsample_ratio=self.downsample_ratio,
        )
Z
zlsh80826 已提交
69 70

    def test_check_output(self):
71
        if paddle.is_compiled_with_cuda():
Z
zlsh80826 已提交
72 73 74
            use_gpu = True
            self.check_output_with_option(use_gpu, flatten=True)
            self.assertTrue(
75 76
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')
            )
Z
zlsh80826 已提交
77 78


W
Wilber 已提交
79 80 81 82 83
class TRTYoloBoxFP16Test(InferencePassTest):
    def setUp(self):
        self.set_params()
        with fluid.program_guard(self.main_program, self.startup_program):
            image_shape = [self.bs, self.channel, self.height, self.width]
84 85 86 87
            image = paddle.static.data(
                name='image', shape=image_shape, dtype='float32'
            )
            image_size = paddle.static.data(
88 89
                name='image_size', shape=[self.bs, 2], dtype='int32'
            )
W
Wilber 已提交
90 91 92 93 94 95 96 97
            boxes, scores = self.append_yolobox(image, image_size)

        self.feeds = {
            'image': np.random.random(image_shape).astype('float32'),
            'image_size': np.array([[416, 416]]).astype('int32'),
        }
        self.enable_trt = True
        self.trt_parameters = TRTYoloBoxFP16Test.TensorRTParam(
98 99
            1 << 30, self.bs, 1, AnalysisConfig.Precision.Half, False, False
        )
W
Wilber 已提交
100 101 102 103 104 105 106 107 108
        self.fetch_list = [scores, boxes]

    def set_params(self):
        self.bs = 1
        self.height = 13
        self.width = 13
        self.class_num = 1
        self.anchors = [106, 148, 92, 300, 197, 334]
        self.channel = 18
109
        self.conf_thresh = 0.05
W
Wilber 已提交
110 111 112
        self.downsample_ratio = 32

    def append_yolobox(self, image, image_size):
113
        return paddle.vision.ops.yolo_box(
114 115 116 117 118 119 120
            x=image,
            img_size=image_size,
            class_num=self.class_num,
            anchors=self.anchors,
            conf_thresh=self.conf_thresh,
            downsample_ratio=self.downsample_ratio,
        )
W
Wilber 已提交
121 122

    def test_check_output(self):
123
        if paddle.is_compiled_with_cuda():
W
Wilber 已提交
124 125 126
            use_gpu = True
            self.check_output_with_option(use_gpu, flatten=True, rtol=1e-1)
            self.assertTrue(
127 128
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')
            )
W
Wilber 已提交
129 130


W
wangxinxin08 已提交
131 132 133 134 135
class TRTYoloBoxIoUAwareTest(InferencePassTest):
    def setUp(self):
        self.set_params()
        with fluid.program_guard(self.main_program, self.startup_program):
            image_shape = [self.bs, self.channel, self.height, self.width]
136 137 138 139
            image = paddle.static.data(
                name='image', shape=image_shape, dtype='float32'
            )
            image_size = paddle.static.data(
140 141
                name='image_size', shape=[self.bs, 2], dtype='int32'
            )
W
wangxinxin08 已提交
142 143 144
            boxes, scores = self.append_yolobox(image, image_size)

        self.feeds = {
145 146 147 148
            'image': np.random.random(image_shape).astype('float32'),
            'image_size': np.random.randint(32, 64, size=(self.bs, 2)).astype(
                'int32'
            ),
W
wangxinxin08 已提交
149 150 151
        }
        self.enable_trt = True
        self.trt_parameters = TRTYoloBoxTest.TensorRTParam(
152 153
            1 << 30, self.bs, 1, AnalysisConfig.Precision.Float32, False, False
        )
W
wangxinxin08 已提交
154 155 156 157 158 159 160 161 162
        self.fetch_list = [scores, boxes]

    def set_params(self):
        self.bs = 4
        self.channel = 258
        self.height = 64
        self.width = 64
        self.class_num = 80
        self.anchors = [10, 13, 16, 30, 33, 23]
163
        self.conf_thresh = 0.1
W
wangxinxin08 已提交
164 165 166 167 168
        self.downsample_ratio = 32
        self.iou_aware = True
        self.iou_aware_factor = 0.5

    def append_yolobox(self, image, image_size):
169
        return paddle.vision.ops.yolo_box(
170 171 172 173 174 175 176 177 178
            x=image,
            img_size=image_size,
            class_num=self.class_num,
            anchors=self.anchors,
            conf_thresh=self.conf_thresh,
            downsample_ratio=self.downsample_ratio,
            iou_aware=self.iou_aware,
            iou_aware_factor=self.iou_aware_factor,
        )
W
wangxinxin08 已提交
179 180

    def test_check_output(self):
181
        if paddle.is_compiled_with_cuda():
W
wangxinxin08 已提交
182 183 184
            use_gpu = True
            self.check_output_with_option(use_gpu, flatten=True)
            self.assertTrue(
185 186
                PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')
            )
W
wangxinxin08 已提交
187 188


Z
zlsh80826 已提交
189 190
if __name__ == "__main__":
    unittest.main()