Performance trick¶
For best performance use the Mosaic.d_matrix_cuda
method to compute the distance matrix. It requires installing phomo
with pip install 'phomo[cuda]'
. If you do not have a GPU or it is incompatible with cuda
, consider using this trick.
The trick consist in computing the distance matrix on a smaller master/pool set but then building the mosaic image with higher resolution images.
from phomo import Master, Pool, Mosaic
from PIL import Image
from pathlib import Path
/home/lcoyle/.cache/pypoetry/virtualenvs/phomo-pX3Qwu7w-py3.12/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm
Pools¶
We create a tile pool with small tiles (20x20) and one with big tiles (200x200).
small_pool = Pool.from_dir("faces/", crop_ratio=1, tile_size=(20, 20))
large_pool = Pool.from_dir("faces/", crop_ratio=1, tile_size=(200, 200))
Loading tiles: 100%|█████████████████████████████████████████████████████████████████| 9780/9780 [00:04<00:00, 2174.39it/s] Loading tiles: 100%|█████████████████████████████████████████████████████████████████| 9780/9780 [00:03<00:00, 2932.25it/s]
Master images¶
Same with the master image, one small (400x400) and one big (4000x4000).
master_file = list(Path("faces").glob('*'))[321]
small_master = Master.from_file(master_file, img_size=(400, 400))
large_master = Master.from_file(master_file, img_size=(4000, 4000))
small_master.img
Mosaics¶
Now the mosaics.
small_mosaic = Mosaic(small_master, small_pool)
large_mosaic = Mosaic(large_master, large_pool)
We compute the distance matrix on the small mosaic.
d_matrix = small_mosaic.d_matrix()
Building distance matrix: 100%|██████████████████████████████████████████████████████████| 400/400 [00:12<00:00, 32.25it/s]
And give it to the large mosaic to build the mosaic. This way we get the performance of the small images and the resolution of the big mosaic. The downside is that by downsampling we do lose some accuracy when computing the distance matrix.
large_mosaic_img = large_mosaic.build(d_matrix=d_matrix)
Building mosaic: 100%|█████████████████████████████████████████████████████████████████| 400/400 [00:00<00:00, 8016.56it/s]
large_mosaic_img