Mastering Image Generation with Stable Diffusion
Raphaël Semeteys
A practical guide to local and controlled image generation using Stable Diffusion, ComfyUI, embeddings, ControlNet, LoRA, inpainting and identity tools.
If the player is unavailable, use the timestamp links.
Read transcript
- [Music]
- Hello everybody.
- Thanks for coming and listening to me.
- Yet another talk on AI. Those images we
- just saw were generated with AI
- apparently. Let's go visual with AI and
- speak about image generation with
- this tool in action with Stable Diffusion
- and ComfyUI. I want to
- generate images of yoga poses. Normally
- my wife should be here next to me
- because it's a side project of both
- of us but she couldn't make it
- unfortunately but in her way she'll be
- present in the presentation. You will
- see why.
- First I'm Raphaël. I work for Worldline
- for quite a few years. I'm head of DevRel
- senior architect open source expert.
- What is important here is I'm also a
- certified yoga teacher, right? Because
- okay, Worldline, who knows Worldline
- in the room just in Belgium.
- Normally people should know a bit. Yes,
- we're in the payment industry a major
- player. We have a lot of engineers.
- We do R&D including AI and we deal with
- heavily regulated transactional flow.
- But this is not the subject. The subject
- is I'm a yoga teacher and with my
- wife who is also a yoga teacher a few
- years back we created this website to
- share our knowledge on yoga and stuff
- but he we always had this issue with
- yoga poses. We wanted them to be
- precise but we didn't want to use
- any pictures because it's a lot of
- hassle. You have to go in the studio and
- take the pictures and then ah the same
- t-shirt as the same time a lot of
- problem. we can't use image from
- the internet also. So with when GenAI
- came out like one year ago, we decided
- to try give it a try. What can we do
- with AI and generate yoga poses, right?
- And because I'm a geek, the this these
- are my functional requirement, right? My
- non-functional requirement are I want
- everything to run on this laptop
- locally. So it's a Nvidia GPU like
- 4070 and only 8 gigabytes of video RAM and
- that's the limiting factor right and I
- want the all the component that I will
- be using to be the more open possible
- because I'm an open source guy right so
- this led me very quickly to Stable Diffusion
- which is a family of open
- model that were born in Germany in 2021
- so it's like four decades ago in the
- GenAI time right they innovated the
- concept of latent diffusion that I will
- explain later on very soon they also
- collaborated with companies Runway,
- EleutherAI and then Stability AI who
- finally hired most of the lead
- developers and now Stability AI is
- the London-based startup that is the
- official maintainer of the these models
- right I said open models because you
- can You can download them. You can
- install that. You can even fine-tune the
- model. But you might have some
- limitation about what you can do or
- cannot do with the model. That's
- why I don't say it's open source. It's
- open. But this openness allows the rise
- of a very very dynamic community sharing
- models, fine-tuned models, tools, tips,
- tutorials, a lot a lot a lot of dynamism
- over there. By the way, if you want to
- understand better what is the difference
- between open weight model, open data,
- open science, open source, openness of a
- model and stuff in this very room.
- tomorrow I have a talk dedicated on
- that. Right. So back to image
- generation. I also selected ComfyUI that is
- an open source tool this time a GPLv3
- license that you download and install
- on your PC. And it is very useful
- because it puts a UI in front of the
- workflow that you could build in Python
- to use Stable Diffusion. It's very
- intuitive, modular, very customizable.
- You can do simple or very complex
- workflow and it's also community based
- meaning that it's extensible with the
- notion of custom nodes kind of plugins.
- There's a lot a lot of people
- contributing custom nodes. So you can do
- a lot a lot of things with this tool
- that becoming more as a platform for me
- not only a tool and actually some people
- are running ComfyUI as a service now right
- now. Okay. So I did selected Stable Diffusion
- as the model or the
- family of model and ComfyUI just to do
- some no code and graphic
- creation of workflow. But let's start
- with a demo. So what I did I recorded
- all the demos on this very PC but then
- it's recorded because you know demo gods
- but also because I have to accelerate
- sometimes the generation time to fit in
- the 30 minutes and not the presentation
- to be too boring. Right. So here
- we are in ComfyUI. I don't know if you can
- see properly this. Oh yeah it works a
- little bit. Yeah. So we are in the
- the UI of ComfyUI right. You can see nodes
- that I interconnect with links. It's
- very graphical. All right. So here I
- connect this node to this other one.
- Here I in green I have the prompt. In
- the prompt basically I write what I
- want. So a girl doing yoga, lotus pose
- in a park, whatever. There's also a
- negative prompt. Then it goes to this
- specific node. We'll go into the details
- of the node later on. Now I just want to
- to see this part is not accelerated. So
- this is the real time right. So you
- can see the image emerging step by step
- in the middle node. You can see the
- consumption of the resource. A lot of
- VRAM is used. The GPU stuff is broken.
- Normally there should be also GPU used
- here in the in the bottom of the screen.
- And then you see the result. Here we
- are. I have a girl.
- She's doing yoga maybe in a park, right?
- So how does it work? Yeah, as you
- saw, it's a step-by-step process. It
- starts with a completely noisy random
- image like the snow on the TV screen,
- you know, when there's no signal, right?
- And then step by step, it will denoise
- the image, remove the noise and
- replace it by features that are guided
- and that want to be aligned with your
- text prompt. Right? Actually, Stable Diffusion
- learned to do the other side
- like how to noise images. And now what
- he's doing is denoising the images
- right important part of that is the
- latent space because even if we you see
- here all the image appearing step by
- step it's not how it goes it goes
- everything is done in a mathematical
- vectoral multi-dimensional vectoral
- space pretty much like LLM actually
- right so it's a compressed version of
- the image that is represented into this
- mathematical space and there in this
- space you have all the matrix
- calculation. So it's all about GPUs
- matrix calculation just like LLM, right?
- So once the image is finished then it's
- encoded back into pixels. So the model
- is not working on pixels, it's working
- on vectoral vectors, vectoral
- dimensions information, right? Let's
- focus on a few of the nodes that we
- saw in our very simple first workflow
- just to explain just a little bit not
- too much models right most important
- part of the stuff obviously so
- models what will they be there will be
- files that I download actually it's the
- weight of the models so the result
- after the training of the model that I
- download and I put on my laptop
- and then with this node I will select
- the model I want to use and then ComfyUI
- will load the model into memory and
- then start using it. Right? The most
- used model by the community the versions
- in Stable Diffusion family are Stable Diffusion 1.5
- and version XL. They have
- a lot a lot a lot of fine-tuned models
- that are provided by many many people
- and communities. You can have some
- of them on a regular generic website
- like Hugging Face but you also have a
- dedicated website to image creation like
- Civitai and here for instance you have some
- of the categories that are used on Civitai
- to you know sort out the model. So we
- have a lot a lot of things possible
- right the other node important node
- here is the prompt okay it's using
- another model that is called CLIP
- developed at the same time 2021 by OpenAI
- right and basically what it will do
- it will do the correspondence between
- the text and the features of the image
- so it understands the prompt and then it
- will guide the generation right you can
- have a positive or negative prompt what
- you want what you don't want in the
- image and you can have a textual
- syntax where you make a sentence
- I want a girl doing yoga sitting
- cross-legged then with blue hair and
- blah blah blah blah blah blah or you can
- have a short syntax with keywords
- separated by commas and you can put
- some weight with the with the brackets
- around to put more weight on a specific
- keyword right
- another important node is the KSampler
- that's where the denoising
- process occur so you see it's very
- visual you have a lot of graphics and
- it's very easy to use but in the
- background there's Python running
- obviously especially in this node right
- so the denoising process is guided by
- two different kind of algorithm the
- first one is the sampler that will guide
- the how the image is the iteration
- process right the scheduler will
- control how the noise at each stage is
- removed so you have a several
- algorithm possible and you can play with
- that it will depend also on the model
- that you use obviously but you have a
- lot of possibility there you also
- have other settings that you can
- tweak them the trick like the number
- of steps so in how many steps the image
- is generated so you could think the
- more step I do the more quality I will
- have this is not true then it's called
- overfitting and then the quality can
- decrease also right you can play with
- CFG the guidance basically it's the
- impact of the prompt in into the
- generation process and you can play with
- the percentage of denoising. So it's
- the percentage of the original
- image that will be removed. Right. Of
- course if you start with a completely
- noisy image you'll want to denoise
- 100%. Of course the seed actually is the
- element the random element that will
- generate this random image. And there is
- something that very important to
- understand here is when you fix the seed
- and when you fix all the parameters in a
- workflow the same parameters the same
- workflow will always generate the same
- image always. It's pure math. There's no
- magic. Everything is going on in the
- latent space. Same parameters, same
- output, right? And the guys who did ComfyUI
- are very clever because of that they
- injected all the workflow information
- into the EXIF metadata of the image. It
- means that when you drag and drop an
- image on a ComfyUI, if it has been
- generated with ComfyUI, you have the
- workflow back. So you are able to
- reproduce the image. Interesting.
- So okay, in this very first workflow,
- what can we do? It's a text to image. I
- put a text description and I have a
- result. So obviously I can tweak the
- prompt. So yes, I can change color of
- the hair, the output, the
- clothes and stuff. I could go even
- further if I want to play with the
- prompt and use textual inversion
- technique or embeddings. Basically, what
- are embeddings? It's just a shortcut, a
- predefined file made by the community
- or yourself. All right. To orient
- the prompt, inject a predefined element
- into the prompt to have a specific
- action or style, theme, texture,
- pose, character, anything depending on
- the embedding file you use. So here for
- instance I have a list of embedded files
- with different styles and then I just
- have to use the keyword in the prompt
- node in ComfyUI as you see here and then
- yes the ComfyUI will apply and inject
- in the prompt all the elements right so
- it's very tiny file so it's very
- easy to use and then I fix everything
- from the previous workflow I
- only change the embedding files and then
- you see I definitely have some impact on
- the style, right? So, it could be
- fantasy, comic, cinematic and stuff. It
- just replace prompt that you do like a
- long a long prompt like that, right? So,
- it's cool. So, I went back to my to my
- wife. I say, "Hey, look what I can do."
- She told me, "Yeah, well, you know,
- maybe the style is good, but the pose,
- you don't have any control on that." By
- the way, lotus pose. The first one, as a
- yoga teacher, you should know it's not a
- lotus pose. Yeah, I know. And this one,
- the tree. Maybe it's because there's a
- tree in the back. I don't know. The
- other one, the camel one. I don't know.
- Maybe it's a bias because she looks
- like oriental. I don't know. Camel
- oriental. I don't know. And the last one
- is completely hallucination. It's not
- the shoulder stand pose. And don't try
- to do that. Actually, it's not a good
- one. Right. So, basically text to image
- for my use case is not enough. So, what
- should we do? Move. Generate an image of
- a woman based on an existing image. I
- have to give something as an input and
- maybe to control a bit more the output.
- Right? So that's what I did here. I just
- update my workflow and I put an image of
- myself doing a specific posture. All
- right. Here you can see
- if I zoom on the prompt, I
- still ask for a girl doing yoga and with
- blue pants. I want her to have blue
- pants, right? So it's an image of me.
- I'm a man supposedly and I want I
- want a girl to be generated. So, let's
- see how it goes here. Okay, you can see
- yes, the pose is okay, but I think
- her name must be Raphaela. She really
- looks like me as a girl. Let's play with
- denoising. This time I say I want to denoise
- only 70%, because in the first
- pass I did 55%. Why? Because if I put
- 100%, of course the whole input
- image has no purpose, right? So you can
- see here it's nice. I can have control
- on the pose. But if I play with the
- denoising, maybe you didn't notice, but I
- did because I am a yoga teacher. And
- the head is tilted. It's not in the axis
- of the spine. So that's not the right
- pose. Not happy, right? So I was kind of
- you know bit disappointed. How
- could we do what would be interesting if
- I could extract only the element of the
- pose from the image not the style not
- the fact that I'm on the on this mat and
- with the background and stuff and
- inject that into the latent space and
- that exactly what the ControlNets are
- there are special specialized neural
- networks that will help to extract
- certain aspect from the image and just
- use that and send it to the latent
- space. So you have different control
- methods depending on the ControlNet.
- Can be structural, the texture, the
- contents, the abstract, etc. Let's have
- an example here. I update my workflow.
- The new nodes are in the blue box here.
- So I still want a girl with white
- t-shirt, whatever, whatever. So I have
- the description of what I want. Here
- are the nodes related to the ControlNet.
- The image will go into a
- pre-processor that will do some image
- processing like here in this case inject
- extract depth information from the
- image and then inject that into the
- generation process. You can see the depth
- image here that will be used here. Now I
- accelerate things a little bit. Now it
- will be quicker. It's in 20 steps. And
- here I have the result. Yes. Now the
- pose is okay. The background is a little
- bit funky. Let's play with the denoising
- value because I want to see how
- stable the image is. All right, so I
- put 75%.
- Okay, the background is okay. The pose
- is still okay. Let's play with the
- prompt. Let's say I want the girl to be
- blonde just to see if when she's blonde,
- the pose is different or not. And I
- tweak a bit also the denoising
- value. I say let's put 80%. All right.
- and
- accelerated again with my 8 gigabyte
- memory. Here we go. So yes, basically
- now I have control on the pose. And
- by the way, you have different kind of
- pre-processors. It can be edge
- detection, color map, scribble, and
- whatever. The OpenPose one was
- promising but not strong enough for
- yoga poses. It's more like for classical
- pose, you know, that kind of stuff, not
- for my use case. But I discovered that
- because you can stack the
- context, you can use several
- by using both edge detection and depth
- then I had a good control on the
- pose. Right? So because I can do that
- abstract the pose from the image, I can
- use abstract images. This is good
- because this is the method I was using
- before for my website. I use these tools
- JustSketchMe. It's basically a virtual
- mannequin, you know. So you make the
- pose you want and then you can have the
- angle the view that you want because
- it's 3D so you export it right. So just
- update the workflow and this time I put
- the abstract image as an input. I
- changed the model I use Juggernaut which
- is a fine tuned version of SDXL. This
- time I have my two ControlNet for depth
- and edges. All right. So obviously
- the more nodes you add the more
- parameters you have to play with it can
- be a little bit dizzy here you see the
- features edges and depth has been
- extracted and then yes I have this
- result okay that is good I have control
- on the pose and I have control on the
- style for the prompt very happy I go
- back to my wife I say job done I mean
- requirement fulfilled she say yeah yeah
- yeah that's very good but you know on
- the website we do sequences, right and
- so we have sequences it's the same
- girl that should do the poses so she
- has to have the same face I say
- but that's that was not in the initial
- requirement you really like customer you
- know like never know so I was like geez
- man and then I thought and I say okay
- okay let's stay motivated and I
- know that people generate deep fakes you
- know of celebrities so it's possible to
- enforce the face of somebody
- all right so let's look into this
- direction. That's how I discovered
- LoRA. So LoRA is for low rank
- adaptation. Basically, if you in plain
- word, it's an update of the model at
- runtime. All right, it has been
- pre-trained only to change small part of
- the model and at the runtime when you
- use the specific keyword, the patch will
- be applied and then it has been trained
- to do different kind of stuff like
- styles, pose, characters or concept.
- Here you can see the style different
- LoRA on the same workflow. But what
- interesting me is the character. All
- right. So let's test it. Very very
- simple workflow. Here in the yellow I
- have downloaded a lot of LoRA file. So
- I select one a celebrity a mysterious
- one. We'll see if we recognize her. The
- prompt. Okay. I put the specific keyword
- to activate the LoRA at runtime. And
- then yes. And then I use a ControlNet
- but with a strength of only 10% because
- I don't want to constraint too much the
- LoRA because I really want how good it
- is to generate the face of the
- celebrity. Right? So let's see.
- All right. Here she so I don't know if
- you recognize her but in the name of the
- LoRA, Scarlett is in it maybe. And by
- changing just the file I can have a Emma
- or a Megan right. So you see the pose
- changes because it's only 10% on the
- ControlNet. So you always have a
- trade-off and you have to play a lot of
- with a lot of parameters. But with that
- yes okay then I can do that very
- easily. I don't even showing you the
- video. I just add my LoRA here and
- then the result I have a control on the
- pose and on the face. So there I go back
- to my wife and I say yay job done. Now I
- can decorate both face pose and
- style. So I have the variation and then
- I can have the homogeneity. Then here is
- your sequence and there is another one.
- So I was kind of excited and she looked
- at me say yeah yeah that's good. I say
- what's going on? Say Scarlett won't be
- happy about that. I say I know but
- well it's not really looking like. So
- there's a trick. Oh you could put 50% of
- Scarlett and then 20% of Megan and then
- you know 30% of Emma right. They said no
- no no you know it's not proper. we have
- to do our own LoRA. So I tried that.
- Okay. First thing on online in a SaaS
- mode with Google Colab with the
- DreamBooth technique. The result were
- not that good. Actually my wife was not
- happy at all because she asked me who is
- this ugly girl. I say it's supposed to
- be you because so she was not happy. I
- read okay back to my initial NFR. Let's
- do everything locally. I changed the
- strategy using Kohya_ss open source
- framework to do that and I injected
- less images but more epochs then I
- got this result. So with this result,
- yes, I can update now my workflow
- and I have a interesting image.
- By the way, I added a node here
- specialized with a associated model just
- to remove the background, right? It's
- just to show you that you can do more
- and more complex workflow. I'm just
- staying on very simple one here. So with
- that, yes, I can have the sequence.
- and when I was going to my retro, I
- mean to the meeting with my wife, right?
- And say in my mind I was like the
- quality is not that good. I mean maybe I
- should have trained more and I preferred
- the Scarlett LoRA. This one is not very
- good. But she killed the solution
- definitely when she asked me. Okay
- that's nice. Let me see what
- it does when you use
- Cheyenne. Cheyenne is a model that is
- specialized to generate comics
- graphic novel style. And I say do you
- have three hour and a half because I
- have to go back and train everything you
- know because it depends on the model.
- And she told me, "Raphaël, it's
- not possible. It's not realistic." All
- right. So I looked again and I
- said, "Okay, are there models techniques
- specialized on facial identities?"
- That's how I discovered FaceID. FaceID
- is a image prompt adapter. It's another
- technique. You can see it very basically
- as a one-image LoRA at
- runtime this time. So just one picture
- that you inject and FaceID is
- specialized in facial recognition.
- All right. So I also discovered in
- while looking for it FaceDetailer which
- is a different kind of model because
- it's a post-processing AI model that is
- specifically trained to enhance facial
- features like nose, lips, facial
- expression and stuff like that. Right?
- So let's update the workflow. And this
- is the final workflow for my use
- case. Here you see the yellow boxes are
- new. I have a big one but it's the blue
- one is for the ControlNet. Now I really
- focused on the facial identity. Here the
- is image of my wife. Here the model you
- have several version as soon as you use
- IP Adapter and another component. You
- have a lot of parameters that you can
- tweak on the strength when it starts
- when it stop and stuff like that. Here
- on the side I have a Juggernaut
- model. I use also LoRA but it's just to
- change the style. I could have dropped
- this one right and then here in the blue
- box you have my the two ControlNet I
- just reorganized the workflow for
- readability but that's all right so
- let's run everything you see pose
- element are extracted now things are
- going on accelerating a little bit then
- I have the result here so good thing but
- it's not finished because now the result
- of this first generation is sent to
- FaceDetailer right you see FaceDetailer
- is also linked with FaceID. It
- means that it will enhance the features
- based on the initial image of my wife
- which is very good. Here's the result.
- You see on the top you have this white
- box. There's a model that detect
- the zone of the image where the face is
- and it will change only that which is
- good because I don't want it to do
- anything else. And here I have a node
- before after. I don't know if you can
- see properly. I zoom a little bit before
- after. So the quality is better with
- FaceDetailer. With that I'm okay. I said,
- "Yes, job done." Job done this time.
- Yes, I'm sure. So, I went to my wife. I
- say, "Hey, here we are. I appreciate."
- Yes, Raphaël, that's fine. We're okay. I
- say, "Yeah, okay. Peace at home. It's
- very nice. And I'm happy. The quality is
- better. It's very and I don't
- need to train anything. It's just a
- simple workflow. I'm very, very happy.
- And by the way, I can change the model
- this time. And the same workflow, same
- parameters. I just change the model,
- tweak a little bit the things. And then
- I have a completely different style. But
- I still have control on the pose. It's
- still looking kind of like her but in a
- cartoon style. Right? So here we go. And
- by the way, because it's only one image,
- I can also change persona and put an
- image of myself. Right? And then it does
- that. Well, I have to admit, at least
- for me, not for my wife, of course, AI
- tends to think I am still the young guy
- I was when I started yoga many, many
- years ago. But whatever that's not the
- point because yes I can do sequencing
- with that. So I'm very happy. What did
- we learn to summarize if you want to
- tweak images have
- some creativity just with prompt and
- text to image you can do things but if
- you want to control more aspect of the
- images you have to input something
- like images. So you can do image to
- image but it's a little bit raw a little
- bit gross. If you want to be more
- subtle, ControlNet helps you by
- extracting some elements only and
- sending them to the into the latent
- space. LoRA for facial identity and
- coherence is okay, but it's a lot of
- work. It's heavy and finally the result
- the quality not okay at least in my case
- but with IP FaceID I mean and
- FaceDetailer yes I was able to do
- something good. So to summarize
- my use case is implementable actually
- implemented we actually publish
- because we used to publish a yoga
- sequence in the yoga journal in French
- in France whatever and why not
- this sequence was generated by AI
- the journal owner was so so excited
- about that. All right. So, we did so we
- have accurate and consistent images of
- poses and sequences, right? And what
- I've learned it's image generation is
- both a science and an art. It's a
- science because as you said, as you as I
- told you, I mean, same parameters, same
- image. It's only mathematical operation
- there. But the more nodes and the more
- component you add in your workflow, the
- more parameters you have and then you're
- lost, you know. So by trial and error
- and you develop a certain sixth sense of
- thing and then you say oh that's the
- LoRA oh no that's the embedding you
- know because my wife sometimes she
- how do you know I feel it's like that so
- it's also an art in that matter
- icing on the cake by changing a few
- elements changing a LoRA or prompt and
- and stuff that was not in the initial
- requirement that's a new feature you
- can deal with inclusivity very very
- easily with that kind of workflow
- Okay.
- we started that one year ago. In the
- meantime, a lot of things changed in
- AI with the rise of new models like
- multimodal models. So, I was like, how
- do they stand regarding my use case? So,
- I did try ChatGPT with GPT-Image-1,
- Nano Banana in Google or Flux in
- Black Forest Labs. For the test I
- did. It's not perfect yet, but it's
- going very very quickly because you
- don't have the same level of control
- basically, you know, the ControlNet,
- the IP Adapter and stuff. So I can't
- have both control on the pose and the
- face. For instance, the first image,
- never do that. It's not good for your
- neck, right? The second one, Nano Banana
- told me that my wife was a
- celebrity. I didn't know that. So I'm
- very happy married to a celebrity. So he
- refused to generate a face, right? But
- what they are very good at is
- editing feature. As soon as you generate
- the image, it's very easy to change. For
- instance, here I change the format. I
- changed the color and I said put Devoxx on
- the t-shirt of the girl
- doing yoga. Very very powerful, right?
- So a lot of amazing thing. Speaking
- about the future, what could be next
- video? What is video except a sequence
- of images, right? So I tried ComfyUI
- locally with Wan from Alibaba a video
- generation model and I feed the model
- with three images generated by the
- previous workflow and I told him
- generate the sequence between the
- images. So here's the result after
- many hours of computing on this. So here
- second pose. Yes. Yeah. She's happy. You
- see? Yes. Yeah. Right. So yeah that's
- possible. And just to wrap everything up
- something that's why I called
- this presentation the yoga of image
- generation because Yoga Sutras is a very
- important text in yoga
- and one of the sutra says the pose to
- meditate to do yoga should be stable
- like Stable Diffusion and comfortable as
- ComfyUI. I was like was it on purpose?
- I don't know. My mind was blown really.
- All right. Thank you very much. I hope
- that you learned a few things. If you
- want to get in touch with me, use the
- first QR code and please feedback
- is a gift. Give me some feedback on
- this very presentation with the second
- QR code. Right. Thank you very much.
Loading slides…