prochet commited on
Commit ·
7229448
1
Parent(s): 4f3e3c6
Add pytest test suite for modeling and image processor
Browse files- test_modeling_schp.py +46 -0
test_modeling_schp.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from transformers import AutoImageProcessor, AutoModelForSemanticSegmentation
|
| 7 |
+
|
| 8 |
+
MODEL_PATH = str(Path(__file__).parent)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@pytest.fixture(scope="module")
|
| 12 |
+
def model():
|
| 13 |
+
return AutoModelForSemanticSegmentation.from_pretrained(
|
| 14 |
+
MODEL_PATH, trust_remote_code=True
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@pytest.fixture(scope="module")
|
| 19 |
+
def processor():
|
| 20 |
+
return AutoImageProcessor.from_pretrained(MODEL_PATH, trust_remote_code=True)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_forward_shape(model):
|
| 24 |
+
pixel_values = torch.randn(1, 3, 512, 512)
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
outputs = model(pixel_values=pixel_values)
|
| 27 |
+
assert outputs.logits.shape == (1, 18, 512, 512)
|
| 28 |
+
assert outputs.parsing_logits.shape == (1, 18, 512, 512)
|
| 29 |
+
assert outputs.edge_logits.shape == (1, 2, 512, 512)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def test_seg_map(model):
|
| 33 |
+
pixel_values = torch.randn(1, 3, 512, 512)
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
outputs = model(pixel_values=pixel_values)
|
| 36 |
+
seg_map = outputs.logits.argmax(dim=1).squeeze()
|
| 37 |
+
assert seg_map.shape == (512, 512)
|
| 38 |
+
assert seg_map.min() >= 0
|
| 39 |
+
assert seg_map.max() < 18
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def test_processor_output_shape(processor):
|
| 43 |
+
image = Image.new("RGB", (640, 480))
|
| 44 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 45 |
+
assert "pixel_values" in inputs
|
| 46 |
+
assert inputs["pixel_values"].shape == (1, 3, 512, 512)
|