Download code/tests/test_preprocessing.py from CollinMa/nextgpt-chapter6-report: direct link, hf CLI and curl.
- Browser
- Download file 2.2 kB
-
https://huggingface.co/spaces/CollinMa/nextgpt-chapter6-report/resolve/main/code/tests/test_preprocessing.py
- Command line
-
hf download hf://spaces/CollinMa/nextgpt-chapter6-report/code/tests/test_preprocessing.py
-
curl -L -o test_preprocessing.py https://huggingface.co/spaces/CollinMa/nextgpt-chapter6-report/resolve/main/code/tests/test_preprocessing.py
2.2 kB
| """Exercise real media decoders before spending GPU time on model loading.""" | |
| import importlib.util | |
| import sys | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| class PreprocessingTests(unittest.TestCase): | |
| def setUpClass(cls): | |
| from settings import CODE | |
| sys.path.insert(0, str(CODE)) | |
| from model.ImageBind import data | |
| cls.data = data | |
| def test_image_is_normalized_to_finite_encoder_input(self): | |
| import torch | |
| from PIL import Image | |
| with tempfile.TemporaryDirectory() as tmp: | |
| image = Path(tmp) / "image.png" | |
| Image.new("RGB", (64, 48), (40, 80, 100)).save(image) | |
| value = self.data.load_and_transform_vision_data([str(image)], "cpu") | |
| self.assertEqual(tuple(value.shape), (1, 3, 224, 224)) | |
| self.assertTrue(torch.isfinite(value).all()) | |
| def test_audio_backend_loads_and_prepares_mels(self): | |
| import numpy as np | |
| import soundfile as sf | |
| import torch | |
| with tempfile.TemporaryDirectory() as tmp: | |
| audio = Path(tmp) / "audio.wav" | |
| sf.write(audio, np.zeros(48000, dtype=np.float32), 16000) | |
| value = self.data.load_and_transform_audio_data([str(audio)], "cpu") | |
| self.assertEqual(tuple(value.shape), (1, 3, 1, 128, 204)) | |
| self.assertTrue(torch.isfinite(value).all()) | |
| def test_ffmpeg_video_is_decoded_by_pytorchvideo(self): | |
| import imageio.v2 as imageio | |
| import numpy as np | |
| import torch | |
| with tempfile.TemporaryDirectory() as tmp: | |
| video = Path(tmp) / "video.mp4" | |
| frames = [np.full((32, 32, 3), i*8, dtype=np.uint8) for i in range(24)] | |
| imageio.mimwrite(str(video), frames, fps=8, codec="libx264") | |
| value = self.data.load_and_transform_video_data([str(video)], "cpu") | |
| self.assertEqual(value.shape[0], 1) | |
| self.assertEqual(tuple(value.shape[-2:]), (224, 224)) | |
| self.assertTrue(torch.isfinite(value).all()) | |
| if __name__ == "__main__": | |
| unittest.main() | |