SplatAnything / tests /test_sky.py
ritianyu's picture
Load compressed Gaussian previews first; support single images and clearable scenes
cca3524 verified
Raw History Blame Contribute Delete
4.78 kB
"""Sky weight caching, CPU preprocessing, and scene defaults."""
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch, Mock
import numpy as np
from PIL import Image
from fastapi.testclient import TestClient
from demo.runtime import Pipeline, Options
from demo.ui import create_app
from demo.examples import CATEGORIES
class SkyTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
def images(self, root):
root.mkdir(parents=True)
for i in range(2):
Image.new('RGB', (28, 28), (50, 90, 140)).save(root / f'{i:03d}.png')
return root
def test_missing_sky_weights_download_once_and_cpu_masks_do_not_leak_when_disabled(self):
images = self.images(self.root / 'input')
model = self.root / 'cached/skyseg.onnx'
model.parent.mkdir()
model.write_bytes(b'onnx fixture')
def mask(rgb):
result = np.zeros(rgb.shape[:2], dtype=bool)
result[:len(result)//2] = True
return result
segmenter = Mock()
segmenter.sky_mask.side_effect = mask
with patch('demo.runtime.ROOT', self.root), \
patch.dict('os.environ', {'SPLATANYTHING_SKY_REPO': 'owner/sky', 'SPLATANYTHING_SKY_REVISION': 'fixed'}), \
patch('huggingface_hub.hf_hub_download', return_value=str(model)) as download, \
patch('src.utils.sky_segmentation.SkySegmenter', return_value=segmenter) as construct:
pipeline = Pipeline(device='cpu')
enabled, paths = pipeline.prepare_inputs(images, None, Options(width=28, height=28, sky=True))
self.assertEqual(enabled['img'].device.type, 'cpu')
self.assertEqual(len(paths), 2)
self.assertEqual(int((enabled['depthmap'] < 0).sum()), 2 * 14 * 28)
disabled, _ = pipeline.prepare_inputs(images, None, Options(width=28, height=28, sky=False))
self.assertNotIn('depthmap', disabled)
pipeline.prepare_inputs(images, None, Options(width=28, height=28, sky=True))
download.assert_called_once_with('owner/sky', 'skyseg.onnx', revision='fixed')
construct.assert_called_once_with(str(model), device='cpu')
self.assertEqual(segmenter.sky_mask.call_count, 4)
self.assertIsNone(pipeline.model, 'CPU preparation must not load/reserve the geometry GPU models')
def test_missing_explicit_model_path_does_not_silently_download_another_model(self):
images = self.images(self.root / 'input')
pipeline = Pipeline(device='cpu', sky_path=str(self.root / 'missing.onnx'))
with patch('huggingface_hub.hf_hub_download') as download:
with self.assertRaisesRegex(ValueError, 'configured'):
pipeline.prepare_inputs(images, None, Options(width=28, height=28, sky=True))
download.assert_not_called()
def test_scene_selection_sets_sky_for_street_and_rig_and_clears_it_for_other_categories(self):
for folder, _, _ in CATEGORIES:
self.images(self.root / 'examples' / folder / '1/images')
app = create_app(Pipeline(device='cpu'), self.root / 'results', self.root / 'examples')
sky = next(c['id'] for c in app.state.demo.config['components']
if c.get('props', {}).get('elem_id') == 'remove-sky')
controls = [c['props'] for c in app.state.demo.config['components']
if c.get('props', {}).get('label') in ('Width', 'Height')]
self.assertEqual(len(controls), 2)
for control in controls:
self.assertEqual([choice[1] for choice in control['choices']], [504, 756, 896, 1022])
self.assertTrue(control['allow_custom_value'])
self.assertEqual(control['value'], 896)
with TestClient(app) as client:
for folder in ['street', 'object', 'rig', 'indoor', 'aerial', 'pano']:
fn = next(d for d in app.state.demo.config['dependencies'] if d.get('api_name') == f'example_{folder}')
self.assertEqual(fn['outputs'][-1], sky)
r = client.post('/gradio_api/run/predict', json=dict(
fn_index=fn['id'], data=[], session_hash='sky-scene',
event_data=dict(index=0, value=None, selected=True)))
self.assertEqual(r.status_code, 200, r.text)
self.assertEqual(r.json()['data'][-1], folder in ('street', 'rig'))
expected = 504 if folder in ('street', 'rig', 'aerial') else 756
self.assertEqual(r.json()['data'][6:8], [expected, expected])
if __name__ == '__main__':
unittest.main()