test_vision_models.py 4.6 KB
Newer Older
L
LielinJiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 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
import numpy as np

17 18 19
import paddle
from paddle.static import InputSpec
import paddle.vision.models as models
L
LielinJiang 已提交
20 21 22 23 24 25 26


class TestVisonModels(unittest.TestCase):
    def models_infer(self, arch, pretrained=False, batch_norm=False):

        x = np.array(np.random.random((2, 3, 224, 224)), dtype=np.float32)
        if batch_norm:
27
            net = models.__dict__[arch](pretrained=pretrained, batch_norm=True)
L
LielinJiang 已提交
28
        else:
29
            net = models.__dict__[arch](pretrained=pretrained)
L
LielinJiang 已提交
30

31 32
        input = InputSpec([None, 3, 224, 224], 'float32', 'image')
        model = paddle.Model(net, input)
33
        model.prepare()
L
LielinJiang 已提交
34

35
        model.predict_batch(x)
L
LielinJiang 已提交
36 37

    def test_mobilenetv2_pretrained(self):
L
LielinJiang 已提交
38
        self.models_infer('mobilenet_v2', pretrained=False)
L
LielinJiang 已提交
39 40 41 42

    def test_mobilenetv1(self):
        self.models_infer('mobilenet_v1')

N
Nyakku Shigure 已提交
43 44 45 46 47 48
    def test_mobilenetv3_small(self):
        self.models_infer('mobilenet_v3_small')

    def test_mobilenetv3_large(self):
        self.models_infer('mobilenet_v3_large')

L
LielinJiang 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    def test_vgg11(self):
        self.models_infer('vgg11')

    def test_vgg13(self):
        self.models_infer('vgg13')

    def test_vgg16(self):
        self.models_infer('vgg16')

    def test_vgg16_bn(self):
        self.models_infer('vgg16', batch_norm=True)

    def test_vgg19(self):
        self.models_infer('vgg19')

    def test_resnet18(self):
        self.models_infer('resnet18')

    def test_resnet34(self):
        self.models_infer('resnet34')

    def test_resnet50(self):
        self.models_infer('resnet50')

    def test_resnet101(self):
        self.models_infer('resnet101')

    def test_resnet152(self):
        self.models_infer('resnet152')

79 80 81 82 83 84
    def test_wide_resnet50_2(self):
        self.models_infer('wide_resnet50_2')

    def test_wide_resnet101_2(self):
        self.models_infer('wide_resnet101_2')

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    def test_densenet121(self):
        self.models_infer('densenet121')

    def test_densenet161(self):
        self.models_infer('densenet161')

    def test_densenet169(self):
        self.models_infer('densenet169')

    def test_densenet201(self):
        self.models_infer('densenet201')

    def test_densenet264(self):
        self.models_infer('densenet264')

100 101 102 103 104 105
    def test_squeezenet1_0(self):
        self.models_infer('squeezenet1_0')

    def test_squeezenet1_1(self):
        self.models_infer('squeezenet1_1')

106 107 108
    def test_alexnet(self):
        self.models_infer('alexnet')

N
Nyakku Shigure 已提交
109 110 111
    def test_shufflenetv2_swish(self):
        self.models_infer('shufflenet_v2_swish')

N
Nyakku Shigure 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    def test_resnext50_32x4d(self):
        self.models_infer('resnext50_32x4d')

    def test_resnext50_64x4d(self):
        self.models_infer('resnext50_64x4d')

    def test_resnext101_32x4d(self):
        self.models_infer('resnext101_32x4d')

    def test_resnext101_64x4d(self):
        self.models_infer('resnext101_64x4d')

    def test_resnext152_32x4d(self):
        self.models_infer('resnext152_32x4d')

    def test_resnext152_64x4d(self):
        self.models_infer('resnext152_64x4d')

130 131 132
    def test_inception_v3(self):
        self.models_infer('inception_v3')

N
Nyakku Shigure 已提交
133 134 135
    def test_googlenet(self):
        self.models_infer('googlenet')

N
Nyakku Shigure 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    def test_shufflenetv2_x0_25(self):
        self.models_infer('shufflenet_v2_x0_25')

    def test_shufflenetv2_x0_33(self):
        self.models_infer('shufflenet_v2_x0_33')

    def test_shufflenetv2_x0_5(self):
        self.models_infer('shufflenet_v2_x0_5')

    def test_shufflenetv2_x1_0(self):
        self.models_infer('shufflenet_v2_x1_0')

    def test_shufflenetv2_x1_5(self):
        self.models_infer('shufflenet_v2_x1_5')

    def test_shufflenetv2_x2_0(self):
        self.models_infer('shufflenet_v2_x2_0')

154 155 156
    def test_vgg16_num_classes(self):
        vgg16 = models.__dict__['vgg16'](pretrained=False, num_classes=10)

L
LielinJiang 已提交
157
    def test_lenet(self):
158 159
        input = InputSpec([None, 1, 28, 28], 'float32', 'x')
        lenet = paddle.Model(models.__dict__['LeNet'](), input)
160
        lenet.prepare()
L
LielinJiang 已提交
161 162

        x = np.array(np.random.random((2, 1, 28, 28)), dtype=np.float32)
163
        lenet.predict_batch(x)
L
LielinJiang 已提交
164 165 166 167


if __name__ == '__main__':
    unittest.main()