"vscode:/vscode.git/clone" did not exist on "72bfb0baf06f7af061764d92747465aaa14bcad6"
smoke_test.py 3.29 KB
Newer Older
1
2
"""Run smoke tests"""

3
import sys
4
from pathlib import Path
5

6
import torch
soumith's avatar
soumith committed
7
import torchvision
Philip Meier's avatar
Philip Meier committed
8
from torchvision.io import decode_jpeg, read_file, read_image
9
from torchvision.models import resnet50, ResNet50_Weights
10

11
12
13
14
15
SCRIPT_DIR = Path(__file__).parent


def smoke_test_torchvision() -> None:
    print(
16
        "Is torchvision usable?",
17
18
19
        all(x is not None for x in [torch.ops.image.decode_png, torch.ops.torchvision.roi_align]),
    )

20

21
22
def smoke_test_torchvision_read_decode() -> None:
    img_jpg = read_image(str(SCRIPT_DIR / "assets" / "encode_jpeg" / "grace_hopper_517x606.jpg"))
Philip Meier's avatar
Philip Meier committed
23
    if img_jpg.shape != (3, 606, 517):
24
25
        raise RuntimeError(f"Unexpected shape of img_jpg: {img_jpg.shape}")
    img_png = read_image(str(SCRIPT_DIR / "assets" / "interlaced_png" / "wizard_low.png"))
Philip Meier's avatar
Philip Meier committed
26
    if img_png.shape != (4, 471, 354):
27
28
        raise RuntimeError(f"Unexpected shape of img_png: {img_png.shape}")

Philip Meier's avatar
Philip Meier committed
29

Philip Meier's avatar
Philip Meier committed
30
31
32
33
34
35
36
def smoke_test_torchvision_decode_jpeg_cuda():
    img_jpg_data = read_file(str(SCRIPT_DIR / "assets" / "encode_jpeg" / "grace_hopper_517x606.jpg"))
    img_jpg = decode_jpeg(img_jpg_data, device="cuda")
    if img_jpg.shape != (3, 606, 517):
        raise RuntimeError(f"Unexpected shape of img_jpg: {img_jpg.shape}")


37
def smoke_test_compile() -> None:
38
39
40
41
42
43
44
    try:
        model = resnet50().cuda()
        model = torch.compile(model)
        x = torch.randn(1, 3, 224, 224, device="cuda")
        out = model(x)
        print(f"torch.compile model output: {out.shape}")
    except RuntimeError:
45
        if sys.platform == "win32":
46
            print("Successfully caught torch.compile RuntimeError on win")
47
48
        elif sys.version_info >= (3, 11, 0):
            print("Successfully caught torch.compile RuntimeError on Python 3.11")
49
50
        else:
            raise
51

Philip Meier's avatar
Philip Meier committed
52

53
54
def smoke_test_torchvision_resnet50_classify(device: str = "cpu") -> None:
    img = read_image(str(SCRIPT_DIR / ".." / "gallery" / "assets" / "dog2.jpg")).to(device)
55
56
57

    # Step 1: Initialize model with the best available weights
    weights = ResNet50_Weights.DEFAULT
58
    model = resnet50(weights=weights).to(device)
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    model.eval()

    # Step 2: Initialize the inference transforms
    preprocess = weights.transforms()

    # Step 3: Apply inference preprocessing transforms
    batch = preprocess(img).unsqueeze(0)

    # Step 4: Use the model and print the predicted category
    prediction = model(batch).squeeze(0).softmax(0)
    class_id = prediction.argmax().item()
    score = prediction[class_id].item()
    category_name = weights.meta["categories"][class_id]
    expected_category = "German shepherd"
73
    print(f"{category_name} ({device}): {100 * score:.1f}%")
74
    if category_name != expected_category:
75
76
        raise RuntimeError(f"Failed ResNet50 classify {category_name} Expected: {expected_category}")

77
78
79

def main() -> None:
    print(f"torchvision: {torchvision.__version__}")
80
    print(f"torch.cuda.is_available: {torch.cuda.is_available()}")
81
82
83
    smoke_test_torchvision()
    smoke_test_torchvision_read_decode()
    smoke_test_torchvision_resnet50_classify()
84
    if torch.cuda.is_available():
Philip Meier's avatar
Philip Meier committed
85
        smoke_test_torchvision_decode_jpeg_cuda()
86
        smoke_test_torchvision_resnet50_classify("cuda")
87
88
        smoke_test_compile()

89
90
    if torch.backends.mps.is_available():
        smoke_test_torchvision_resnet50_classify("mps")
91

92

93
94
if __name__ == "__main__":
    main()