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

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


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:
28
            net = models.__dict__[arch](pretrained=pretrained, batch_norm=True)
L
LielinJiang 已提交
29
        else:
30
            net = models.__dict__[arch](pretrained=pretrained)
L
LielinJiang 已提交
31

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

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

    def test_mobilenetv2_pretrained(self):
L
LielinJiang 已提交
39
        self.models_infer('mobilenet_v2', pretrained=False)
L
LielinJiang 已提交
40 41 42 43 44 45 46 47 48 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

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

    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')

74 75 76
    def test_vgg16_num_classes(self):
        vgg16 = models.__dict__['vgg16'](pretrained=False, num_classes=10)

L
LielinJiang 已提交
77
    def test_lenet(self):
78 79
        input = InputSpec([None, 1, 28, 28], 'float32', 'x')
        lenet = paddle.Model(models.__dict__['LeNet'](), input)
80
        lenet.prepare()
L
LielinJiang 已提交
81 82

        x = np.array(np.random.random((2, 1, 28, 28)), dtype=np.float32)
83
        lenet.predict_batch(x)
L
LielinJiang 已提交
84 85 86 87


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