Spaces:
No application file
No application file
Download visualization/CLIP/tests/test_consistency.py from chencws/stable-text-to-motion-framework: direct link, hf CLI and curl.
- Browser
- Download file 802 Bytes
-
https://huggingface.co/spaces/chencws/stable-text-to-motion-framework/resolve/main/visualization/CLIP/tests/test_consistency.py
- Command line
-
hf download hf://spaces/chencws/stable-text-to-motion-framework/visualization/CLIP/tests/test_consistency.py
-
curl -L -o test_consistency.py https://huggingface.co/spaces/chencws/stable-text-to-motion-framework/resolve/main/visualization/CLIP/tests/test_consistency.py
802 Bytes
| import numpy as np | |
| import pytest | |
| import torch | |
| from PIL import Image | |
| import clip | |
| def test_consistency(model_name): | |
| device = "cpu" | |
| jit_model, transform = clip.load(model_name, device=device) | |
| py_model, _ = clip.load(model_name, device=device, jit=False) | |
| image = transform(Image.open("CLIP.png")).unsqueeze(0).to(device) | |
| text = clip.tokenize(["a diagram", "a dog", "a cat"]).to(device) | |
| with torch.no_grad(): | |
| logits_per_image, _ = jit_model(image, text) | |
| jit_probs = logits_per_image.softmax(dim=-1).cpu().numpy() | |
| logits_per_image, _ = py_model(image, text) | |
| py_probs = logits_per_image.softmax(dim=-1).cpu().numpy() | |
| assert np.allclose(jit_probs, py_probs, atol=0.01, rtol=0.1) | |