├── imgs ├── tree.png └── htop-pi.png ├── app.py ├── README.md └── .gitignore /imgs/tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/straczowski/raspberry-pi-stable-diffusion/HEAD/imgs/tree.png -------------------------------------------------------------------------------- /imgs/htop-pi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/straczowski/raspberry-pi-stable-diffusion/HEAD/imgs/htop-pi.png -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from diffusers import StableDiffusionPipeline 2 | from PIL import Image 3 | 4 | pipe = StableDiffusionPipeline.from_pretrained("../stable-diffusion-v1-5", low_cpu_mem_usage=True) 5 | pipe = pipe.to("cpu") 6 | 7 | prompt = "a black tree with golden leaves painted by Monet, autumn" 8 | image = pipe(prompt, num_inference_steps=31, width=400, height=400).images[0] 9 | 10 | image.save("output.png") 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How to run Stable Diffusion on Raspberry Pi 4 2 | 3 | *Example* *Output* 4 | 5 | ``` 6 | a black tree with golden leaves painted by Monet, autumn 7 | ``` 8 | 9 | ![Black Tree with golden leaves](/imgs/tree.png?raw=true "Black Tree with golden leaves") 10 | 11 | first of all i have to say that this is for sure not the most convinient way to run Stable Diffusion. The Raspberry's 8GB RAM are below the recommended 12GB minimum RAM. And the CPU is so slow that a 400x400 px image takes ~45 minutes to be ready. 12 | 13 | ### so why we are doing this? 14 | 15 | Becaus we can. And because both are great technologies. So consider this repository for educational purposes only and the joy of learning :) 16 | 17 | ## Prerequisites 18 | 19 | running on `Debian GNU/Linux 11 (bullseye)` 20 | 21 | 1. Having cloned the Stable Diffusion model from Huggingface 22 | 23 | i am using currently version 1.5 24 | => https://huggingface.co/runwayml/stable-diffusion-v1-5 25 | 26 | ``` 27 | git lfs install 28 | git clone https://huggingface.co/runwayml/stable-diffusion-v1-5 29 | ``` 30 | 31 | 2. Having Diffusers Library @0.7.x 32 | 33 | https://pypi.org/project/diffusers/ 34 | 35 | ``` 36 | pip install --upgrade diffusers 37 | ``` 38 | 39 | 3. Run Your Raspberry Pi Headless 40 | 41 | As we need any bit of RAM we can get, i am running the Pi without GUI. You can enable this boot mode via `raspi-config` 42 | 43 | 44 | # Execute 45 | 46 | with all that setup you should be able to run that code 47 | 48 | ``` 49 | python app.py 50 | ``` 51 | 52 | The most critical part i encountered while booting the model in the beginning. Mostly the process was killed here. Since the diffuser library supports the `low_cpu_mem_usage` parameter, i got it also running on my Pi 53 | 54 | on peaks i measure ~6.3GB RAM usage 55 | 56 | ![Pi RAM Usage](/imgs/htop-pi.png?raw=true "Pi RAM Usage") 57 | 58 | happy diffusing! 🥳 59 | 60 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | --------------------------------------------------------------------------------