From a4b669d58097ee5bdbbc302fbe6122a74d718166 Mon Sep 17 00:00:00 2001 From: atalman Date: Tue, 6 Dec 2022 08:17:02 -0800 Subject: [PATCH 1/5] Add more advanced smoke test --- test/smoke_test.py | 52 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/test/smoke_test.py b/test/smoke_test.py index e4334c65938..a0eba43ecdc 100644 --- a/test/smoke_test.py +++ b/test/smoke_test.py @@ -1,12 +1,52 @@ """Run smoke tests""" import os - import torchvision from torchvision.io import read_image +from torchvision.models import resnet50, ResNet50_Weights + + +def smoke_test_torchvision() -> None: + print("Is torchvision useable?", all(x is not None for x in [torch.ops.image.decode_png, torch.ops.torchvision.roi_align])) + +def smoke_test_torchvision_read_decode() -> None: + img_jpg = read_image(str(SCRIPT_DIR / "assets" / "encode_jpeg" / "grace_hopper_517x606.jpg")) + if img_jpg.ndim != 3 or img_jpg.numel() < 100: + raise RuntimeError(f"Unexpected shape of img_jpg: {img_jpg.shape}") + img_png = read_image(str(SCRIPT_DIR / "assets" / "interlaced_png" / "wizard_low.png")) + if img_png.ndim != 3 or img_png.numel() < 100: + raise RuntimeError(f"Unexpected shape of img_png: {img_png.shape}") + +def smoke_test_torchvision_resnet50_classify() -> None: + img = read_image(str(SCRIPT_DIR / ".." / "gallery" / "assets" / "dog2.jpg")) + + # Step 1: Initialize model with the best available weights + weights = ResNet50_Weights.DEFAULT + model = resnet50(weights=weights) + 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" + print(f"{category_name}: {100 * score:.1f}%") + if(category_name != expected_category): + raise RuntimeError(f"Failed ResNet50 classify {category_name} Expected: {expected_category}") + + +def main() -> None: + print(f"torchvision: {torchvision.__version__}") + smoke_test_torchvision() + smoke_test_torchvision_read_decode() + smoke_test_torchvision_resnet50_classify() -image_path = os.path.join( - os.path.dirname(os.path.abspath(__file__)), "assets", "encode_jpeg", "grace_hopper_517x606.jpg" -) -print("torchvision version is ", torchvision.__version__) -img = read_image(image_path) +if __name__ == "__main__": + main() From 4f268f0016be2bafd1b4ad3aa4860fdb7ac3cdef Mon Sep 17 00:00:00 2001 From: atalman Date: Tue, 6 Dec 2022 09:13:33 -0800 Subject: [PATCH 2/5] add torch import --- test/smoke_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/smoke_test.py b/test/smoke_test.py index a0eba43ecdc..4f5b1b059bd 100644 --- a/test/smoke_test.py +++ b/test/smoke_test.py @@ -2,6 +2,7 @@ import os import torchvision +import torch from torchvision.io import read_image from torchvision.models import resnet50, ResNet50_Weights From da05b834fc2df159939fcd7918f66c7fcd0fa055 Mon Sep 17 00:00:00 2001 From: atalman Date: Tue, 6 Dec 2022 09:20:41 -0800 Subject: [PATCH 3/5] remove dependency on torch --- test/smoke_test.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/smoke_test.py b/test/smoke_test.py index 4f5b1b059bd..6b9e8036251 100644 --- a/test/smoke_test.py +++ b/test/smoke_test.py @@ -2,14 +2,10 @@ import os import torchvision -import torch from torchvision.io import read_image from torchvision.models import resnet50, ResNet50_Weights -def smoke_test_torchvision() -> None: - print("Is torchvision useable?", all(x is not None for x in [torch.ops.image.decode_png, torch.ops.torchvision.roi_align])) - def smoke_test_torchvision_read_decode() -> None: img_jpg = read_image(str(SCRIPT_DIR / "assets" / "encode_jpeg" / "grace_hopper_517x606.jpg")) if img_jpg.ndim != 3 or img_jpg.numel() < 100: @@ -45,7 +41,6 @@ def smoke_test_torchvision_resnet50_classify() -> None: def main() -> None: print(f"torchvision: {torchvision.__version__}") - smoke_test_torchvision() smoke_test_torchvision_read_decode() smoke_test_torchvision_resnet50_classify() From 75886d3a2b9b0ecc75ab98ff6032b80425b00bed Mon Sep 17 00:00:00 2001 From: atalman Date: Tue, 6 Dec 2022 09:32:48 -0800 Subject: [PATCH 4/5] Add missing vars --- test/smoke_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/smoke_test.py b/test/smoke_test.py index 6b9e8036251..c4adb51669d 100644 --- a/test/smoke_test.py +++ b/test/smoke_test.py @@ -2,9 +2,11 @@ import os import torchvision +from pathlib import Path from torchvision.io import read_image from torchvision.models import resnet50, ResNet50_Weights +SCRIPT_DIR = Path(__file__).parent def smoke_test_torchvision_read_decode() -> None: img_jpg = read_image(str(SCRIPT_DIR / "assets" / "encode_jpeg" / "grace_hopper_517x606.jpg")) From 061b679ea2b23ff428e9045e51428fe2ec51b545 Mon Sep 17 00:00:00 2001 From: atalman Date: Tue, 6 Dec 2022 14:56:08 -0800 Subject: [PATCH 5/5] More code and ufmt --- test/smoke_test.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/test/smoke_test.py b/test/smoke_test.py index c4adb51669d..f80aba1d19f 100644 --- a/test/smoke_test.py +++ b/test/smoke_test.py @@ -1,20 +1,31 @@ """Run smoke tests""" import os -import torchvision from pathlib import Path + +import torch +import torchvision from torchvision.io import read_image from torchvision.models import resnet50, ResNet50_Weights SCRIPT_DIR = Path(__file__).parent + +def smoke_test_torchvision() -> None: + print( + "Is torchvision useable?", + all(x is not None for x in [torch.ops.image.decode_png, torch.ops.torchvision.roi_align]), + ) + + def smoke_test_torchvision_read_decode() -> None: img_jpg = read_image(str(SCRIPT_DIR / "assets" / "encode_jpeg" / "grace_hopper_517x606.jpg")) if img_jpg.ndim != 3 or img_jpg.numel() < 100: - raise RuntimeError(f"Unexpected shape of img_jpg: {img_jpg.shape}") - img_png = read_image(str(SCRIPT_DIR / "assets" / "interlaced_png" / "wizard_low.png")) + raise RuntimeError(f"Unexpected shape of img_jpg: {img_jpg.shape}") + img_png = read_image(str(SCRIPT_DIR / "assets" / "interlaced_png" / "wizard_low.png")) if img_png.ndim != 3 or img_png.numel() < 100: - raise RuntimeError(f"Unexpected shape of img_png: {img_png.shape}") + raise RuntimeError(f"Unexpected shape of img_png: {img_png.shape}") + def smoke_test_torchvision_resnet50_classify() -> None: img = read_image(str(SCRIPT_DIR / ".." / "gallery" / "assets" / "dog2.jpg")) @@ -37,14 +48,16 @@ def smoke_test_torchvision_resnet50_classify() -> None: category_name = weights.meta["categories"][class_id] expected_category = "German shepherd" print(f"{category_name}: {100 * score:.1f}%") - if(category_name != expected_category): + if category_name != expected_category: raise RuntimeError(f"Failed ResNet50 classify {category_name} Expected: {expected_category}") def main() -> None: print(f"torchvision: {torchvision.__version__}") + smoke_test_torchvision() smoke_test_torchvision_read_decode() smoke_test_torchvision_resnet50_classify() + if __name__ == "__main__": main()