test_cpp_models.py 5.88 KB
Newer Older
Shahriar's avatar
Shahriar committed
1
import os
2
import sys
3
import unittest
Shahriar's avatar
Shahriar committed
4

5
import torch
Shahriar's avatar
Shahriar committed
6
import torchvision.transforms.functional as F
7
8
from PIL import Image
from torchvision import models, transforms
Shahriar's avatar
Shahriar committed
9

10
11
12
13
14
try:
    from torchvision import _C_tests
except ImportError:
    _C_tests = None

Shahriar's avatar
Shahriar committed
15
16
17
18
19
20
21
22
23

def process_model(model, tensor, func, name):
    model.eval()
    traced_script_module = torch.jit.trace(model, tensor)
    traced_script_module.save("model.pt")

    py_output = model.forward(tensor)
    cpp_output = func("model.pt", tensor)

24
    assert torch.allclose(py_output, cpp_output), "Output mismatch of " + name + " models"
Shahriar's avatar
Shahriar committed
25
26
27


def read_image1():
28
29
30
    image_path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "assets", "encode_jpeg", "grace_hopper_517x606.jpg"
    )
Shahriar's avatar
Shahriar committed
31
32
33
34
35
36
37
    image = Image.open(image_path)
    image = image.resize((224, 224))
    x = F.to_tensor(image)
    return x.view(1, 3, 224, 224)


def read_image2():
38
39
40
    image_path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "assets", "encode_jpeg", "grace_hopper_517x606.jpg"
    )
Shahriar's avatar
Shahriar committed
41
42
43
44
45
46
47
    image = Image.open(image_path)
    image = image.resize((299, 299))
    x = F.to_tensor(image)
    x = x.view(1, 3, 299, 299)
    return torch.cat([x, x], 0)


48
49
@unittest.skipIf(
    sys.platform == "darwin" or True,
50
    "C++ models are broken on OS X at the moment, and there's a BC breakage on main; "
51
52
    "see https://github.com/pytorch/vision/issues/1191",
)
Shahriar's avatar
Shahriar committed
53
54
55
56
57
class Tester(unittest.TestCase):
    pretrained = False
    image = read_image1()

    def test_alexnet(self):
58
        process_model(models.alexnet(self.pretrained), self.image, _C_tests.forward_alexnet, "Alexnet")
Shahriar's avatar
Shahriar committed
59
60

    def test_vgg11(self):
61
        process_model(models.vgg11(self.pretrained), self.image, _C_tests.forward_vgg11, "VGG11")
Shahriar's avatar
Shahriar committed
62
63

    def test_vgg13(self):
64
        process_model(models.vgg13(self.pretrained), self.image, _C_tests.forward_vgg13, "VGG13")
Shahriar's avatar
Shahriar committed
65
66

    def test_vgg16(self):
67
        process_model(models.vgg16(self.pretrained), self.image, _C_tests.forward_vgg16, "VGG16")
Shahriar's avatar
Shahriar committed
68
69

    def test_vgg19(self):
70
        process_model(models.vgg19(self.pretrained), self.image, _C_tests.forward_vgg19, "VGG19")
Shahriar's avatar
Shahriar committed
71
72

    def test_vgg11_bn(self):
73
        process_model(models.vgg11_bn(self.pretrained), self.image, _C_tests.forward_vgg11bn, "VGG11BN")
Shahriar's avatar
Shahriar committed
74
75

    def test_vgg13_bn(self):
76
        process_model(models.vgg13_bn(self.pretrained), self.image, _C_tests.forward_vgg13bn, "VGG13BN")
Shahriar's avatar
Shahriar committed
77
78

    def test_vgg16_bn(self):
79
        process_model(models.vgg16_bn(self.pretrained), self.image, _C_tests.forward_vgg16bn, "VGG16BN")
Shahriar's avatar
Shahriar committed
80
81

    def test_vgg19_bn(self):
82
        process_model(models.vgg19_bn(self.pretrained), self.image, _C_tests.forward_vgg19bn, "VGG19BN")
Shahriar's avatar
Shahriar committed
83
84

    def test_resnet18(self):
85
        process_model(models.resnet18(self.pretrained), self.image, _C_tests.forward_resnet18, "Resnet18")
Shahriar's avatar
Shahriar committed
86
87

    def test_resnet34(self):
88
        process_model(models.resnet34(self.pretrained), self.image, _C_tests.forward_resnet34, "Resnet34")
Shahriar's avatar
Shahriar committed
89
90

    def test_resnet50(self):
91
        process_model(models.resnet50(self.pretrained), self.image, _C_tests.forward_resnet50, "Resnet50")
Shahriar's avatar
Shahriar committed
92
93

    def test_resnet101(self):
94
        process_model(models.resnet101(self.pretrained), self.image, _C_tests.forward_resnet101, "Resnet101")
Shahriar's avatar
Shahriar committed
95
96

    def test_resnet152(self):
97
        process_model(models.resnet152(self.pretrained), self.image, _C_tests.forward_resnet152, "Resnet152")
Shahriar's avatar
Shahriar committed
98
99

    def test_resnext50_32x4d(self):
100
        process_model(models.resnext50_32x4d(), self.image, _C_tests.forward_resnext50_32x4d, "ResNext50_32x4d")
Shahriar's avatar
Shahriar committed
101
102

    def test_resnext101_32x8d(self):
103
        process_model(models.resnext101_32x8d(), self.image, _C_tests.forward_resnext101_32x8d, "ResNext101_32x8d")
Shahriar's avatar
Shahriar committed
104

105
    def test_wide_resnet50_2(self):
106
        process_model(models.wide_resnet50_2(), self.image, _C_tests.forward_wide_resnet50_2, "WideResNet50_2")
107
108

    def test_wide_resnet101_2(self):
109
        process_model(models.wide_resnet101_2(), self.image, _C_tests.forward_wide_resnet101_2, "WideResNet101_2")
110

Shahriar's avatar
Shahriar committed
111
    def test_squeezenet1_0(self):
112
113
114
        process_model(
            models.squeezenet1_0(self.pretrained), self.image, _C_tests.forward_squeezenet1_0, "Squeezenet1.0"
        )
Shahriar's avatar
Shahriar committed
115
116

    def test_squeezenet1_1(self):
117
118
119
        process_model(
            models.squeezenet1_1(self.pretrained), self.image, _C_tests.forward_squeezenet1_1, "Squeezenet1.1"
        )
Shahriar's avatar
Shahriar committed
120
121

    def test_densenet121(self):
122
        process_model(models.densenet121(self.pretrained), self.image, _C_tests.forward_densenet121, "Densenet121")
Shahriar's avatar
Shahriar committed
123
124

    def test_densenet169(self):
125
        process_model(models.densenet169(self.pretrained), self.image, _C_tests.forward_densenet169, "Densenet169")
Shahriar's avatar
Shahriar committed
126
127

    def test_densenet201(self):
128
        process_model(models.densenet201(self.pretrained), self.image, _C_tests.forward_densenet201, "Densenet201")
Shahriar's avatar
Shahriar committed
129
130

    def test_densenet161(self):
131
        process_model(models.densenet161(self.pretrained), self.image, _C_tests.forward_densenet161, "Densenet161")
Shahriar's avatar
Shahriar committed
132
133

    def test_mobilenet_v2(self):
134
        process_model(models.mobilenet_v2(self.pretrained), self.image, _C_tests.forward_mobilenetv2, "MobileNet")
Shahriar's avatar
Shahriar committed
135
136

    def test_googlenet(self):
137
        process_model(models.googlenet(self.pretrained), self.image, _C_tests.forward_googlenet, "GoogLeNet")
Shahriar's avatar
Shahriar committed
138

Shahriar's avatar
Shahriar committed
139
    def test_mnasnet0_5(self):
140
        process_model(models.mnasnet0_5(self.pretrained), self.image, _C_tests.forward_mnasnet0_5, "MNASNet0_5")
Shahriar's avatar
Shahriar committed
141
142

    def test_mnasnet0_75(self):
143
        process_model(models.mnasnet0_75(self.pretrained), self.image, _C_tests.forward_mnasnet0_75, "MNASNet0_75")
Shahriar's avatar
Shahriar committed
144
145

    def test_mnasnet1_0(self):
146
        process_model(models.mnasnet1_0(self.pretrained), self.image, _C_tests.forward_mnasnet1_0, "MNASNet1_0")
Shahriar's avatar
Shahriar committed
147
148

    def test_mnasnet1_3(self):
149
        process_model(models.mnasnet1_3(self.pretrained), self.image, _C_tests.forward_mnasnet1_3, "MNASNet1_3")
Shahriar's avatar
Shahriar committed
150

Shahriar's avatar
Shahriar committed
151
152
    def test_inception_v3(self):
        self.image = read_image2()
153
        process_model(models.inception_v3(self.pretrained), self.image, _C_tests.forward_inceptionv3, "Inceptionv3")
Shahriar's avatar
Shahriar committed
154
155


156
if __name__ == "__main__":
Shahriar's avatar
Shahriar committed
157
    unittest.main()