How AI Image Generators Work (Stable Diffusion / Dall-E) – Computerphile

By | October 4, 2022

AI image generators are massive, but how are they creating such interesting images? Dr Mike Pound explains what’s going on.


Generating images using diffusion what Is that right so I should probably find Out it’s just things like Dolly and Dolly too yeah Imogen from Google stable Diffusion now as well I’ve spent quite a Long time messing about a stable Diffusion I’m having quite a lot of fun With that so what I thought I’d do is I Download the code I’d you know read the Paper with work out what’s going on and Then we can talk about it I delved into this code and realized It’s actually quite a lot to these these Things right it’s not so much that They’re complicated it’s just there’s a Lot of a lot of moving Parts Um so let’s just have a quick reminder Of generative adversarial networks which Are I suppose before now the the Standard way for generating images and Then we can talk about how it’s Different and why we’re doing it using Diffusion having a network or some you Know deep Network train to Just Produce The same image over and over again not Very interesting so we have some kind of Random noise that we’re using to make it Different each time we have some kind of Very large generator Network which is This is just I’m gonna give this black Box big neural network right that turn That turns out an image that hopefully Looks nice like at the like the thing We’re trying to produce faces Landscapes

People you know is this that how those Anonymous people on this person does not Exist is this one yeah that’s exactly How they work yeah if that’s using I Think style Gan right and it’s that Exact idea and that’s trained on a large Corpus of faces and it just generates Faces right at random right or at least Mostly at random the way we train this Is we have you know millions and Millions of pictures of something that We’re trying to produce so we produce we Give it noise We produce an image and we have to tell Is that good or is that bad right we Need to give this network some Instruction on if this image is actually Looking like a face right otherwise it’s Not going to train it so what we do is We have another Network here which is Sort of like the opposite and this says Is it a real or is it a fake image and So we’re giving this half a time we’re Giving it fake images and half a time We’re giving it real faces so this Trains and gets better at discriminating Between the fake images produced here And the real images produced from the Training set and in doing so this has to Get better at faking them and so on and So forth and the hope is that they just Get better and better and better all Right now that that kind of works the The problem is that

Um they’re very hard to train right you Have a lot of problems with things like Mode collapse where it just produces the Same face if it produces a face that Fools this every time there’s not a lot Of incentive for this network to do Anything interesting right because it Does solve the problem right it’s beaten This let’s move on right and so if You’re not careful with your training Process it’s these kind of things can Happen and I suppose intuitively it’s Quite difficult to go from this bit of Noise to a really beautiful looking Image in high resolution Without there being some Oddities right And some things that go a bit more so What we’re going to do is in diffusion Models is try and simplify this process Into a kind of iterative small step Situation where the work that this Network has to do is slightly smaller And you just run it a few times to try And make the process better right we’ll Start again on the paper so we can clean Things up a bit so we’ve got an image Right let’s say it’s an image of a Rabbit right we add some noise so we’ve Got a rabbit Which is the same right and you add some Noise to it now it’s not speckly noise But I can’t draw gaussian noise right And then we add another bit of noise Right and the rabbit it’s the same shape

Rabbit there’s a bit more noise right And then we come over here and we come Over here And we end up with just noise looks like Nonsense and so the question is like how Do we craft some kind of training Algorithm some kind of what we call Inference you know how do we actually Deploy a network that can undo this Process the first question is how much Noise do we add why don’t we just add Loads of noise Right so just delete all these images And doesn’t really don’t need to worry About that add loads of noise and then Say like give me that and then you’ve Got a pair of training examples you Could use and the answer is it’ll kind Of work but that’s about a very Difficult job and you’ve sort of in the Same problem with the Gant you’re trying To do everything in one go right the Intuition perhaps is that it’s maybe Slightly easier to go from this one to This one just remove a little bit of Noise and then from this one to this one A little bit more noise well in Traditional like image processing you do There are noise removal techniques rise Yeah it’s not difficult to do that is it No I mean it’s it’s difficult in a sense That you don’t know what the original Image was so What we’re trying to do is train a

Network to undo this process That’s the idea and if we can do that Then we can start with random noise a Bit like I can and we can just iterate This process and produce an image right Now there’s a lot of missing parts here Right so we’ll start building up the Complexity a little bit Okay so the first thing is is let’s go Back to our question of how much noise Do we add right so we could add a small Amount of noise And then the same amount again I’ve been The same amount again and we could keep Adding it until we have essentially what Looks like random noise over here right And that will be what we would call a Linear Schedule right for that is the same same Amount of noise each time basically Right and it’s not interesting but it Works the other thing you could do is You could add very little noise at the Beginning and then ramp up the amount of Noise you add later right and so there Are different strategies depending on What paper you read about the best Approach for adding noise But it’s called the schedule right so The idea is you have a schedule that Says right given this image so this is An image at uh at time T equals naught This is T equals one blah blah blah T Equals some capital T which is like the

Final number of steps you’ve got right And this represents essentially all the Noise and this represents some amount of Noise and you can change how much each Step has right and then the nice thing Is you can then very easily produce Because gaussians add together very Nicely so you can say well I want T Equals seven and you don’t have to Produce all the images you can just jump Straight to t7 add the exact right Amount of noise and then hand that back To the network so when you train this You can give it image random images from Your training set with random amounts of Noise added based on this schedule right Varying randomly between 1 and T right And you can say okay here’s a really Noisy image Undo It here’s a little less Noisy image undo it right so what you do Is you take your noise image image Right I’m going to keep going with this Rabbit it’s taller than it was before Right you take your noisy image at some Time let’s say t equals five right you Have a giant unit shaped Network we’ve Talked about encoder decoder networks Before there’s nothing particularly Surprising about this one And then you also put in the time right Because if we’re running a funny Schedule where your at different times Have different amounts of noise you need To tell the network where it is so that

It knows okay I’m gonna have to remove a Lot of noise this time or just a little Bit of noise what do we produce here So we could go for the whole hog and we Just say we’ll just produce the original Rabbit image but then you’ve got a Situation where you have to go from here All the way back to the rabbit that’s a Little bit difficult right Mathematically it works out a little bit Easier if we just try and predict the Noise we want to know what is the noise That was added to this image That you could use to get back to the Original image so this is all the noise From T1234 and five so you just get noise Basically out here like this right with No rabbit that’s the hope And then theoretically you could take That away from this and you get the Rabbit back right now if you did that From here you would find that it’s a Little bit iffy right because you know You’re predicting the noise all the way Back to this rabbit is maybe quite Difficult but if you did it from here it May be not quite so difficult we want to Predict the noise so what we could do is Predict the noise at let’s say time T Equals five and to say give me the noise It takes us back to T equals four right And then T equals three and T equals two The problem if you do that is that

You’re very Stuck doing the exact time steps of the Schedule used right if you used a Thousand time steps for training now You’ve got to use a thousand time steps Of inference right you can’t speed it up So what we might try and do instead is Say well okay whatever time step you’re At you’ve got some amount of noise Remove it all predict me all the noise In the image and just give me back that Noise that I can take away and get back To the original image and so that’s what We do so during training we pick a Random Source image we pick a random Time step and we add based on our Schedule that amount of noise right so We have A noisy image A Time step T we put that into the Network And we say what was the noise That We’ve just added to that image right now We haven’t given it the original image Right so that’s what’s Difficult about This we we have the original image Without any noise on it that we’re not Showing it and we added some noise and We want that noise back right so we can Do that very easily we’ve got millions Of images in our or billions of images In our data set right we can add random Bits of noise and we can say what was

That noise right and over time it starts To build up a picture of what that noise Is so it sounds like a really good kind Of plug-in for Photoshop or something Right it’s going to be noise removal Plug-in how does that turn into creating New images yeah so actually in some Sense that’s the clever bit right is how We use this network that produces noise To undo the noise right We’ve got a network which given an image With some noise added to it and a Time Step that represents how much noise that Is roughly or where we are in the Noising process we have a network which Produces an estimate for what that noise Is in total and theoretically if we take That noise away from this we get back to The original image now that is not a Perfect process right this network is Not going to be perfect and so if you Give it an incredibly noisy image And you take away what it predicts You’ll get like a sort of Maybe like a vague shape and so what we Want to do is take it a little bit more Slowly okay so we take this noise and we Subtract it from our image Right to get an estimate of what the Original image was right T naught okay So we take this And we take this and we do subtraction And we get another image which is our Estimate for T equals naught

Right and it’s not going to look very Good the first time But then we add a bunch of this noise Back again and we get to a t that’s Slightly less than this one so maybe This was like T10 T equals 10. maybe we Add like nine tenths of a noise back and We get to what we roughly T equals nine Right so now we have a slightly less Noisy image and we can repeat this Process so we put the slightly less Noisy image in we predict how to get Back to T naught and we add back most But not all of the noise And then we repeat the process right and So each time we Loop this We get a little bit closer to the Original image it was very difficult to Predict the noise at T equals 10. it’s Slightly easier to predict the noise at T equals nine and very easy at T equals One because it’s both mostly the image With a little bit of noise on it and so If we just sort of feel our way towards It by taking off little bits of noise at A time we can actually produce an image Right so you start off with a noisy Image You predict all the noise and remove it And then add back most of it right and So then you get and so at each step you Have an estimate for what the original Image was and you have a next image Which is just a little bit less noisy

Than the one before and you Loop this a Number of times right and that’s Basically how the image generation Process works so you take your noisy Image you Loop it and you gradually Remove noise until you end up back at What the network thinks was the original Image and you’re doing this by Predicting the noise and taking it away Rather than spitting out an image with Less noise right and that mathematically Works out a lot easier to train and it’s A lot more stable than again there’s an Elephant in the room here there is You’re kind of talking about how to make Random images effectively how do we Direct this so that’s where the Complexity starts ramping up right we’ve Got a structure where we can train a Network to produce random images but It’s not guided there’s no way of saying I want a frog rabbit hybrid right which I’ve done and it’s very weird so how do We do that the answer is we base Condition this network that’s the word We would use we’ll basically give access To the text as well all right so let’s Actually infer on an image on my piece Of paper right I bear in mind the output Is going to be hand drawn by me so it’s Going to be terrible you start off with A random noise image right so this is Just an image that you’ve generated by Taking random gaussian noise

Mathematically this is centered around Zero so you have negative and positive Numbers you don’t go from zero to two Five five because it’s just easier for The network to train You put in your time step so you Generate a you put in a times that let’s Say you’re going to do 50 iterations Right so we put in a Time step that’s Maybe right at the end of our schedule But it says like time step equals you Know 50 which is our most noised image Right and then you pass it through the Network and say estimate me the noise And we also take our string which is Frogs Frogs on stilts I’ll have to have to try that later oh Look right what’s this one anyway we Could spend let’s say another 20 30 Minutes producing fogs on stills We embed this Right by using our GPT style Transformer Embedding and we’d stick that in as well And then it produces an estimate of how Much noise it thinks is in that image So that estimate on T equals 50 is going To be a bit average right it’s not going To produce you a frog on a stilt picture It’s going to produce you like a gray Image or a brown image or something like That because that is a very very Difficult problem to solve however if You subtract

This noise from this image you get your First estimate for what your first image Is right and when you add back a bunch Of noise and you get to T equals 49 Right so now we’ve got slightly less Noise and maybe they’re like the biggest Outline of a frog on a stilt right and This is T equals 49 you take your Embedding and you put this in as well Right and you get another maybe slightly Better estimate of the noise in the Image and then we Loop right it’s a for Loop right we’ve done those before you Take this output you subtract it you add Noise back and you repeat this process And you keep adding this text embedding Now there’s one final trick that they Use to make things a little bit better If you do this you will get a picture That maybe looks slightly frog-like Maybe there’s a stilt in it right but it Won’t look anything like the images you See on the internet that have been Produced by these tools because they do Another trick to make the output even More tied to the text what you do is Something called classifier free Guidance so you actually put this image In twice once you include the embeddings Of the text and once you don’t right so This method this network is maybe Slightly better when it has a text Estimating the noise so you actually put In

Two images Right this one’s with the embedding And this one’s no embedding right and This one is maybe slightly more random Noise and this one’s slightly more Frog-like right or it’s better better It’s slightly moving towards the right Thing And we can calculate the difference Between these two noises and amplify That signal right and then feed that Back so what we essentially do is we say Okay if this network wasn’t given any Information on what was in the image and Then this version of a network was What’s the difference between those two Predictions and can we amplify that when We loot this to really Target this kind Of output right and the idea is Basically you’re really forcing this Network or this this Loop to really Point in direction of the of the scene We want right Um and that’s called classify free Guidance and it is somewhat of a hack at The end of the network but it does work Right if you turn it off which I’ve done It doesn’t it produces you vague sort of Structures that kind of look right it’s Not it’s not terrible I mean I think I Did like a muppet cooking in the kitchen And it just produced me a picture of a Generic kitchen with no Muppet in it Right but if you do this then you

Suddenly are really targeting what you Want standard question got to ask it is This something people can play with Without just going to one of these Websites and typing some words well yeah I mean that’s the thing is is that Um is that it costs hundreds of Thousands of dollars to try one of these Networks because of how many images they Use and how much processing power they Use um the good news is that there are Ones like stable diffusion that are um That are available to use for free right And you can use them through things like Google colab Now I I did this through Google collab Um and it works really really well Um and maybe we’ll talk about that in Another video where we delve into the Code and see all of these bits happening Within the code right I blew through my Uh free Google allowance very very Quickly I had to pay my eight pounds for Uh for premium Google access so um you Know eight pounds eight pounds Thank you yeah so you know never let it Be said I don’t spare expense I I know I Spare no expense on um on on computer File uh getting access to proper compute Hardware but um Could beasts do something like that it Could yeah almost of our servers could I’m just a bit lazy and haven’t set them Up to do so um but actually the code is

Quite easy to run that the the sort of The entry-level version of a code you Literally can just like basically call One python function and it will produce You an image I’m using a code which is Perhaps a little bit more detailed it’s Got the full loop in it and I can go in And inject things and change things so I Can understand it better and we’ll talk Through that next you know perhaps next Time The only other interesting thing about The current neural networks is that the Weights here and here and here are Shared so they are the same because Otherwise this one here would always be The time to make one sandwich but you’ve Got two people doing it so they make Twice as many sandwiches each time they Make a sandwich same with the computer We could either make the computer Processor faster or

Leave a Reply