avatarGonzalo Ruiz de Villa

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2679

Abstract

.</p><p id="7a84">And to install it, I used the following command:</p><div id="8bd6"><pre>bash Mi<span class="hljs-symbol">niforge3</span>-MacOSX-ar<span class="hljs-name">m64.</span>sh</pre></div><p id="19de">I had to accept licensing and terms and when the installation was completed I opened a new terminal and run the following command to verify it worked:</p><div id="52d3"><pre><span class="hljs-attribute">conda</span></pre></div><p id="ab42">This is what it should appear:</p><figure id="09b0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*4x_gaEWRyIG_F1axIIzj_g.png"><figcaption></figcaption></figure><h1 id="8ec5">Step 4: Clone the repo</h1><p id="3ab3">I’ve used <a href="https://github.com/lstein">Lincoln Stein</a> fork of Stable Diffusion.</p><p id="bb36">First, clone the repo:</p><div id="0e5b"><pre>git <span class="hljs-built_in">clone</span> https://github.com/lstein/stable-diffusion.git <span class="hljs-built_in">cd</span> stable-diffusion</pre></div><h1 id="b702">Step 5: Clone the weights</h1><p id="5d56">To download the weights, go to: <a href="https://huggingface.co/CompVis/stable-diffusion-v-1-4-original">https://huggingface.co/CompVis/stable-diffusion-v-1-4-original</a></p><p id="07d0">Make a Hugging Face account if you don’t have one and accept the terms.</p><p id="9fa9">Then download the weights from: <a href="https://huggingface.co/CompVis/stable-diffusion-v-1-4-original/blob/main/sd-v1-4.ckpt">https://huggingface.co/CompVis/stable-diffusion-v-1-4-original/blob/main/sd-v1-4.ckpt</a></p><p id="65f5">Next, create a folder for the models and create a symlink to the model you just downloaded:</p><div id="3ae1"><pre><span class="hljs-built_in">mkdir</span> -p models/ldm/stable-diffusion-v1/ PATH_TO_CKPT=<span class="hljs-string">"<span class="hljs-variable">HOME</span>/Documents/stable-diffusion-v-1-4-original"</span> <span class="hljs-comment"># or wherever yours is.</span> <span class="hljs-built_in">ln</span> -s <span class="hljs-string">"<span class="hljs-variable">PATH_TO_CKPT</span>/sd-v1-4.ckpt"</span> models/ldm/stable-diffusion-v1/model.ckpt</pre></div><h1 id="645e">Step 6: Prepare the environment</h1><p id="6759">Run the following commands to prepare your environment:</p><div id="df07"><pre>CONDA_SUBDIR=osx-arm64 conda <span class="hljs-built_in">env</span> create -f environment-mac.yaml conda activate ldm

python scripts/preload_models.py</pre></div><p id="1f51">I had to fix the file ‘src/k-diffusion/k_diffusion/sampling.py’, changing line 10 from (hopefully this won’t be needed anymore pretty soon):</p><div id="e083"><pre><span class="hljs-keyword">from</span> typing <span class="hljs-

Options

keyword">import</span> <span class="hljs-type">Optional</span>, <span class="hljs-type">Callable</span>, TypeAlias</pre></div><p id="f447">to:</p><div id="0b3a"><pre><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> <span class="hljs-type">Optional</span>, <span class="hljs-type">Callable</span> <span class="hljs-keyword">from</span> typing_extensions <span class="hljs-keyword">import</span> TypeAlias</pre></div><p id="a112">Then run:</p><div id="2375"><pre>python scripts/dream.py --full_precision <span class="hljs-comment"># half-precision requires autocast and won't work</span></pre></div><p id="01d5">And then you should see the following on your terminal:</p><figure id="8fe2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*vFLHGscSAckkafjtVCQMsQ.png"><figcaption></figcaption></figure><p id="3bbb">Now, try to generate some images! I used this prompt “A portrait photo of Pedro Gomez de Valdivia, architect in Spain, by Annie Leibovitz -n 9” to generate a few photos of invented people.</p><p id="6338">These are some of the results I generated in the “output/img-samples” folder, pretty amazing!</p><figure id="03b0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*p1iL63kjqVs9mt9MVZxanA.png"><figcaption></figcaption></figure><figure id="f420"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*x48AGYFYg6Pu4f6lKm3KsA.png"><figcaption></figcaption></figure><figure id="a052"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*y7MOo1rnYEgjvOUniPrzqQ.png"><figcaption>Portrait photos generated by Stable Diffusion</figcaption></figure><figure id="b436"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*p6v7KBxSu0SywXCer2Fjpg.png"><figcaption></figcaption></figure><figure id="0ba9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*bKLEMNAHzm3bKEMpqyR-tw.png"><figcaption></figcaption></figure><figure id="30db"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*TdDxnbDaXMpME34l3hpE0A.png"><figcaption>Portrait photos generated by Stable Diffusion</figcaption></figure><figure id="5079"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*UkeHIOc7pm_lu6-ppSudpw.png"><figcaption></figcaption></figure><figure id="e8ee"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*WgvtFDYs9nD4un196Qvt8Q.png"><figcaption></figcaption></figure><figure id="4ae8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FMFIFLg1wtTxoXzYyxir2A.png"><figcaption>Portrait photos generated by Stable Diffusion</figcaption></figure><p id="2edb">Enjoy you image generation!</p></article></body>

MacBook M1: How to install and run Stable Diffusion

Stable Diffusion is an open machine learning model developed by Stability AI to generate digital images from natural language descriptions that has become really popular in the last weeks.

Image generated by Stable Diffusion of an alien landscape with futuristic towers

These are the steps I followed to run Stable Diffusion on my laptop with Apple Silicon. These worked for me, and I hope they can work for you too. The first steps are the same I wrote down in my post “MacBook M1: installing TensorFlow and Jupyter Notebook” but I will paste them here for your convenience.

Step 1: Install Xcode

Xcode consists of a suite of tools that developers use to build apps for Apple platforms.

You can install Xcode from the App Store or you can download it from the Developer site.

Step 2: Install the Command Line Tools Package

As defined in the documentation, the Command Line Tools Package is a small self-contained package available for download separately from Xcode and that allows you to do command line development in macOS.

For a fresh install, this command should be enough:

xcode-select --install

Step 3: Install Miniforge

Anaconda is a distribution of the Python and R programming languages for scientific computing, that aims to simplify package management and deployment. However, Anaconda comes with many Python packages included, some of which are not Apple Silicon (i.e. ARM) compatible and thus Anaconda is not ARM compatible. Miniforge has Conda, which means you can install many of the packages you want such as Pandas, Scipy, and Numpy — unlike Anaconda, you just have to do the install manually by running ‘conda install mypackagenamehere’.

Miniforge homepage: https://github.com/conda-forge/miniforge

I downloaded ‘miniforge3-macosx-arm64.sh’ (v4.14.0 at the time) from https://github.com/conda-forge/miniforge/releases/, which is the one for the Apple Silicon architecture.

And to install it, I used the following command:

bash Miniforge3-MacOSX-arm64.sh

I had to accept licensing and terms and when the installation was completed I opened a new terminal and run the following command to verify it worked:

conda

This is what it should appear:

Step 4: Clone the repo

I’ve used Lincoln Stein fork of Stable Diffusion.

First, clone the repo:

git clone https://github.com/lstein/stable-diffusion.git
cd stable-diffusion

Step 5: Clone the weights

To download the weights, go to: https://huggingface.co/CompVis/stable-diffusion-v-1-4-original

Make a Hugging Face account if you don’t have one and accept the terms.

Then download the weights from: https://huggingface.co/CompVis/stable-diffusion-v-1-4-original/blob/main/sd-v1-4.ckpt

Next, create a folder for the models and create a symlink to the model you just downloaded:

mkdir -p models/ldm/stable-diffusion-v1/
PATH_TO_CKPT="$HOME/Documents/stable-diffusion-v-1-4-original"  # or wherever yours is.
ln -s "$PATH_TO_CKPT/sd-v1-4.ckpt" models/ldm/stable-diffusion-v1/model.ckpt

Step 6: Prepare the environment

Run the following commands to prepare your environment:

CONDA_SUBDIR=osx-arm64 conda env create -f environment-mac.yaml
conda activate ldm

python scripts/preload_models.py

I had to fix the file ‘src/k-diffusion/k_diffusion/sampling.py’, changing line 10 from (hopefully this won’t be needed anymore pretty soon):

from typing import Optional, Callable, TypeAlias

to:

from typing import Optional, Callable
from typing_extensions import TypeAlias

Then run:

python scripts/dream.py --full_precision  # half-precision requires autocast and won't work

And then you should see the following on your terminal:

Now, try to generate some images! I used this prompt “A portrait photo of Pedro Gomez de Valdivia, architect in Spain, by Annie Leibovitz -n 9” to generate a few photos of invented people.

These are some of the results I generated in the “output/img-samples” folder, pretty amazing!

Portrait photos generated by Stable Diffusion
Portrait photos generated by Stable Diffusion
Portrait photos generated by Stable Diffusion

Enjoy you image generation!

Stable Diffusion
M2 Macbook
Apple Silicon
M1 Mac
Installation
Recommended from ReadMedium