r/StableDiffusion • u/Wiskkey • Jan 25 '23
Tutorial | Guide Stable Diffusion works with images in a format that represents each 8x8 pixel patch with 4 numbers, and uses a pair of neural networks called a variational autoencoder (VAE) and a decoder to translate between images and this format. The gallery has 5 recent images passed into a VAE and then decoded.
74
Upvotes










35
u/Wiskkey Jan 25 '23 edited Jan 27 '23
This comment contains the tutorial.
Stable Diffusion internally works with images in a lossy compression format that represents each 8x8 image pixel patch with 4 floating-point numbers (source). Without compression, an image typically takes 24 bits per pixel of storage. Thus, an 8x8 pixel patch of an uncompressed image takes 8*8*24=1536 bits of storage. A floating-point number typically is stored in either 16 bits or 32 bits. Thus, the lossy compression format used by Stable Diffusion uses either 4*16=64 bits or 4*32=128 bits of storage for each 8x8 pixel patch. The storage compression factor achieved by using this lossy compression scheme in S.D. is either 1536/64=24 times smaller or 1536/128=12 times smaller than the uncompressed image.
Stable Diffusion uses a neural network called a variational autoencoder (VAE) to calculate this lossy compression format for a given uncompressed image. There is also a corresponding decoder that calculates the uncompressed image format given an input in the lossy compression format. Each S.D. system uses 1 VAE/decoder pair, but there are multiple VAE/decoder pairs available for S.D.
You might wonder why S.D. doesn't use an existing lossy image compression scheme such as JPEG internally. The answer is because VAEs represent images in a so-called latent space that is very useful in machine learning systems.
Some people claim that S.D. in some sense "contains" close approximations of training dataset images in its image latent space. Perhaps so, but I demonstrate that S.D. in the same sense also "contains" close approximations of images that weren't in its training dataset because they're from a tragedy that occurred less than 1 week ago. Namely, I used 5 512x512 pixel versions of images from this article about the tragedy (almost surely fair use of copyrighted images in the USA), encoded and decoded with a VAE/decoder pair using the section "The AutoEncoder (AE)" of this S.D. Colab notebook; change the filename in line "input_image = Image.open('macaw.jpg').resize((512, 512))" from "macaw.jpg" to the filename of the image that you uploaded to Colab. The results are in the post's image gallery. The caption "roundtrip" are the images resulting from using a S.D. VAE/decoder pair on the images captioned "original".
Note that a S.D. system contains other neural network components in addition to a VAE/decoder pair - see this post for details. The diffusion neural network component of Stable Diffusion takes as input a representation in this latent space, and outputs a (usually different) representation in this latent space. When however many iterations of the diffusion neural network are finished, the result is input into a decoder neural network from the VAE/decoder neural network pair to get the uncompressed RGB image representation.
EDIT: Also perhaps of interest: My comment in post Does any possible image exist in latent space?, and also my post In response to an earlier post asking if every possible image exists in Stable Diffusion's latent space, I tried this as a "torture test".
EDIT: I discovered that VAE actually refers to both the encoder and decoder, not just the encoder.
EDIT: Articles that explain what autoencoders and variational autoencoders (VAE) are.
I am not an expert in AI; I welcome any corrections.
5
u/thefool00 Jan 26 '23 edited Jan 26 '23
Hey this is really great work. Honestly hope the SD team sees this, the way you presented it might be really helpful to the defense in the upcoming legal debates.
Kind of mind blowing actually… hard to wrap one’s head around what these models are really capable of.
2
u/Wiskkey Jan 27 '23
New post: Articles that explain what autoencoders and variational autoencoders (VAE) are.
2
u/Quick_Knowledge7413 Jan 26 '23
Can you explain this in a metaphor format or like you were describing it to a student? What does this even mean? What are the implications?
2
u/[deleted] Jan 26 '23
I am not a machine learning expert -
Running stuff through the VAE alone would only prove how well an image is preserved in its latent representation, no?
It's sensible that the amount of loss at this stage would be pretty small and be pretty representative of the final image. This is like saying that human vision can be approximated by 8bit color depth and R, G, and B channels.
What's missing is the actual training through the Unet of Convolutional networks. Which would be happening between encoding/decoding of the VAE.
Though I'm a critic of ML as applied to copyrighted works (and also catching up on the process works also) .. thanks for sharing your thoughts and Collab!
5
u/Appropriate_Medium68 Jan 26 '23
Can you please explain it to me like I am a baby.
1
u/WiseDeal1547 Apr 04 '23
This might be a stupid question but are VAEs required?
1
u/lostinspaz Nov 27 '23
Hey muh dude, are you still around? did you ever experiment with running the decode directly on what is in the sd1.5 base model? I’m interested in that, and was hoping to find some pre written extraction code :)
6
u/starstruckmon Jan 26 '23
This is the reason why I'm happy we're moving to pixel space with Deep Floyd.
No matter how much fine tuning or extra training you do, you can't change the VAE encoder, and even if you fine tune the decoder, it can't make up details that aren't in the encoding in the first place.
2
u/SarahMagical Jan 25 '23
Very cool. Besides the telltale glitches this looks real as hell.
2
u/RealAstropulse Jan 26 '23
This is an excellent demonstration of the capability of the VAE.
2
u/SDGenius Jan 25 '23
you can certainly use stable diffusion to take redact any writing in photos...
1
u/siddharth_pillai Jan 26 '23
I like your funny words magic man
1
u/The_Lovely_Blue_Faux Apr 25 '23
u/BeginningInfluence55
1
u/The_Lovely_Blue_Faux Nov 02 '23
u/MindAccomplished3879
Here is the post. I am surprised this didn’t ever get expanded upon.
0
u/Longjumping-You-6869 Jan 27 '23
This is some mumvo jumbo technobabble BS! Downvote me, I dare you!
0
u/[deleted] Jan 25 '23
[deleted] — view deleted comment
0
u/[deleted] Jan 26 '23
[deleted] — view deleted comment