Transcription
Hello everyone and welcome to lecture 7 of CME 296. So today is a very exciting day because we'll be talking about how to evaluate the quality of the output of the text-to-image generation models that we've been seeing so far. And in particular, if you think about it, in order for you to know what to improve, you first need to know how good your output is. So that's why this lecture is going to be quite important.
But before we start, as usual, we're going to recap what we saw last time. So if you remember last time we focused on the training life cycle of text-to-image generation models and we first started by focusing on the loss that we were using for training and in particular we looked at the time step t that up until then we assumed to be drawn from a uniform distribution and we saw that it was actually not fair to treat all time steps equally. And we saw that there was a distribution that was quite nice in this case which was the logit normal distribution which emphasized on the middle steps which are harder to learn compared to the earlier and the later steps. And this is mainly because early on you have basically no information on which for you to base your prediction. So you just predict the the mean of where all the observations are and then towards the end you have almost denoised your whole image but in the middle you're figuring out where to go and how you want to take those big decisions. So we not only saw this but we also saw that for a given noise level if you have a higher resolution image then you perceive noise is going to be less than a lower resolution image which is the reason why you need need to do what we call a time step shift. And we saw this formula which hopefully is not that haunting right now which is something that is derived by our uncertainty of what the underlying pixel value is.
And then what we did was go through the whole life cycle training life cycle of text-to-image generation models. And we started with pre-training where our goal was to teach our model how to generate images. So we saw curriculum learning where the goal was to teach our model first how to do the easy things before doing the harder things. And then what we did was also look at how we can handle different resolutions. And we saw that given that we have a DIT based model, it's only a matter of having a a longer input at the end of the day. Then we looked at post-training approaches aimed at getting you to generate more beautiful images. And here we saw two main strategies on the supervised uh side which was continued training focusing on acquiring more knowledge which is more relevant to the domain you want to use your model and then supervised fine-tuning which is aimed at just improving the aesthetics of the images that you produce. And we also saw some preference tuning methods which were aimed at capturing negative user signals because up until then we only taught our model what to do but not what to not do. So this is the goal of techniques that are derived from gRPO, DPO and so on. And after that we saw how we could personalize our text-to-image generation model. And we looked at dream booth which is one such method which aims at relying on a rare token and training your model on the specific object or person that you want to capture and then use that tuned model at inference time to be able to leverage what you have learned at training time. And then finally we saw an ar a range of distillation methods which were aimed at shortening the number of steps that you were needing you you were that were needed at inference time. So we saw progressive distillation we saw um distribution matching distillation and here we are.
Today we're going to focus on evaluation which as I mentioned is going to be about assessing how good of a quality the output generated image is. So as as you can maybe think judging of the quality of an image can be quite subjective. So I want us to start with a motivating example and I would like to ask you based on an input in input prompt that I'm going to read which hopefully is familiar by now whether you think that image is a good image or not. So the input prompt is a teddy bear reading a book and here is the generated image. So the question is do you think that this generated image is a good or a not good or bad input output? So who says good? Who says bad? Okay, I think we agree it's uh an output that is not very pleasing to see. And the main reason here is well the aesthetics are not great. You look at the image, it doesn't it doesn't look great. Okay.
So, how about this one? Do you think that this image is a is a good image? Okay. Who says it's a good image? Bad image. Yeah. So, why is it a bad image? Yeah. So, it's indeed not representing the prompt. Uh I agree with you. So it's something that we call prompt adherence where we want the output to follow what we have it as input. And so here although this image is is great to look at well it's not about a teddy bear reading a book. Okay last one. Is this image a good image or not? Okay. So who says good image? Yeah. bad image. Okay. So, why is the bad image? Yeah. So, you have a a very um refined taste, which is great. Um so, let's suppose that this image is is good enough for us because here it's first it's following the prompt. So it's a teddy bear reading a book and then second it's uh it's let's say it's more aesthetically pleasing compared to the other ones. Okay.
So from this small scale exercise what I wanted to point out was that when we look at the output of a text-to-image generation model we typically look at things with respect to two main buckets not exhaustive but two main buckets. The first one is if you take this picture on its own, does this look like a good picture, an aesthetically pleasing picture? So here uh you think about whether what is in there is physically plausible. So for instance, if the book that your teddy bear is reading is on the table and not floating, uh if uh it's realistic, if perceptual quality is there. And then the second big bucket is whether the produced output is indeed following your input. So here it's called prompt adherence. And what we want to see is whether the objects, the people, the locations are indeed present in the image. And we want uh the image to be of a given style. And if for instance I say okay it's reading a book uh about the CME 296 class. I want CME 296 to be on the book. So all of that would be within that bucket. But I mentioned that these two categories are not exhaustive. Of course you have a bunch of other criteria that you can think of. So I'll just name a few. So the first one is safety. So you want to make sure you're not depicting things that are interpreted in a in a unsafe way. You want to make sure that if you generate something based on a prompt, you're not always generating the same thing. So you want something that is a bit diverse. You don't want to memorize the things that you have seen as input. And so here it's trying to have some generation generalization capabilities for your model. And then you have a bunch of other dimensions. So here for instance the bias you don't want your model to always output the same kind of output which could be again misinterpreted. So for all these reasons, what I propose is that this image evaluation problem, we can think of it as us trying to think if the image that we're getting as output is mainly good along these two dimensions which are aesthetics and prompt adherence. So let's assume that these are our two main categories that we care about.
And with that I would like to start with a very simple and natural way of evaluating your output which is to leverage humans. So here the idea is you have your model you have some input prompt that is having your model generate an image and what you're saying is okay given this prompt given this image how would you rate this image and so here one possible option is to have a scale from 1 to five where one is very bad and five is very good. And the idea is to figure out where your your image falls into. So in this case, let's assume it's very good. Then what you would do is in order to measure the performance of your model across a data set, you would take the average of the ratings. So you would do sum of the ratings over the number of ratings which you can can call average rating. So the good thing with this score is it is nuanced because you have a way to distinguish a very good image versus a good image but not a great image and so on. But the main drawback there is given that you're letting more degrees of freedom for humans to decide on which rating that is, it can introduce some noise in the ratings that you're getting. Because humans may think that an image is a five, but someone else can think it's a four, someone else can think it's a three. So everyone has their own way of interpreting the scale and so this can introduce some noise in the ratings. And also if you think about it for a lot of problems it's actually very hard as a person to to say if something is a four versus a five. It's just it's just a hard problem.
And so for that reason, a second method of digging into this issue is instead of having a one to5 scale to reduce this problem into a binary problem which is much easier for humans to handle because here you are given an image and you just need to say whether it's good or bad. So here we assume that the answer is good. And so the score that we can think of uh in this setting is again taking the sum of the ratings over the number of ratings. And here it would be the pass rates. So how many of your images are what is the proportion of your images that that is actually passing the bar. So the good thing here is it is an easier task for humans to work on. So the ratings are less noisy in that sense. But one problem is humans although it's on a binary scale they have a hard time evaluating things on an absolute scale. So it's easier for you to let's say compare two things as opposed to saying whether that something is passing the bar or not because you need to have some reference.
So for that reason I want to propose a third methods of going about doing this which is trying to rate those images by comparing them to some other images. So here it's the pair-wise comparison setting and the setup would be you have your input prompt and what you do is you generate two images and your task is to say which one of these images is better than the other one. So do you agree with me that this task is easier than the absolute scale task? Yeah. So, in this case, you agree that the left one is is a better image than the right one. So, I'm I'm glad you said no for the the other one because I think this illustrates the point that on an absolute scale, people have different expectations. So, they may think that the left one may not be above the bar. But when you compare these two images, it is just obvious that the left one is better than the right one. And so in that sense the ratings that you're getting are going to be even less noisy compared to the absolute scale binary one. Okay.
So how about the metric? So very naively, I think a very natural way would be to just for a given model count the number of wins and divide that by the number of times it was compared to something else. Right? So this one is the win rate. Do you think that's a good metric? So here what we're doing is for each model we're generating outputs and we're comparing these outputs to let's say outputs of another model. What we're what we're saying is in order to quantify the performance of that first model, the model A, what we're doing is we're counting the number of times images produced by this model one over the opponent and we divide that by the number of compar comparisons. So the question is do you think that there's a good way of doing things? Yeah. So the the answer is um so for one reference okay but for many not sure. Well I I think you have the right intuition here. So what I want to say here is winning against a model that is known to be excellent is much harder than winning against a model that is known to be bad. So our intuition here is your proportion of your win proportion should not be the only factor here. You should also consider who you are comparing yourself with. And it's exactly what you mentioned. And I'm not sure if you're tracking the performance of uh text-to-image generation models, but typically you have something like this. you have a leaderboard where you have models that are ranked and one challenge with this setting is that models they come but also they can go. So when a model comes, let's suppose you want to have a win rate that is reflective of the performance of that model, you would need to have the same model be evaluated against all models of that list. And you want all models of that list to be also evaluated against all other models of that list every time the list gets updated. So this is a lot of evaluations. So you don't want to always have to evaluate everyone against everyone every time something gets updated on the list to make things appleto. So for that reason, well, something that we can think of is to adjust that win rate metric by taking into consideration how strong your opponent is within the metric.
How do we want to do that? Well, before we go into exactly how we do that, I just want to illustrate the point I mentioned. So here let's imagine we have our model let's say model A that we want to evaluate against other models. Well if our model wins against a bad model your reaction is okay and so what you you already know that uh it's it's better than that very basic model. So there's nothing to be surprised about. But if your model if the human says your model is winning against another model that is much much stronger then your reaction will be different. So the idea is to capture that within the metric. So let's see how we want to do that. So first of all let's assume that we have a metric per model that we note R. R as in rating. And let's assume that our model, model A, starts with a rating of let's say a thousand. And we're a newcomer. And in that leaderboard, it just happens that there's a model, let's suppose it's a bad model that has a lower rating, a worse rating, which is 600, let's say. So let's have our model A be compared against that bad model. How would we want to update the score? So one idea could be to compute the score that we think would happen if those models they perform like they did in the past. So let's compute the expected score. And for that let's just use this formula which gives us the expected score. And so here the formula would be 1 / 1 + 10 ^ of the rating of the model that you're competing against minus your rating over some arbitrary constant. And let's assume that that constant is 400. Then this quantity would be your expected winning chance. And so here it's 0.9. So 90% chance of winning. So now that you have your baseline, what you want to do is compute your actual score. So actually compare the two models. So here the outcome can be one of three things. So you can either lose, be tied or win over the bad model. And the idea is to use these two quantities. So the expected score and the actual score to compute your element of surprise like how much you went above or how much you went below. And here it would be delta equal to the actual score that you obtained minus the score that you think you would have obtained on average. And here in this case if model A wins it would be plus.1. If it loses then it's minus.9. And if there's a tie it's also a minus minus.4. before. So here by the way the numbers they just come from this formula um that we just plug the ratings into that and then we compute delta. So the good thing with this observation is we see that if we have our model win against the bad model. Well there is not a lot of lot of gains which is aligned with our intuition. And if the model loses then it means it's really really bad. So there is like a big contribution downward contribution. And even if there's a tie, we know that if we're tied with a very bad model, then that's also a bad contribution for our rating. So the fourth step would be to just update our rating by adding K * delta. So that would be the recipe. And so the image that I showed you like a few slides ago was actually this slide where I masked the middle columns. So this is how the leaderboards do it in practice. They compute the score. So you may have heard this score. It's called the ELO score, ELO rating. And it allows you to track the performance of a model by computing the score as a function of how strong your opponent is. And this allows you to avoid having everyone be evaluated against everyone every every time there is a change in who's in the leaderboard. And by the way, ILO is actually the name of the person who came up with the method. It's not an acronym. Um, so I think that's good to note.
But what are the problems with this approach? Well, every time we want to evaluate our model, if we had to always involve humans, well, it to be a very expensive task. So that's the first limitation. Also humans there's always only 24 hours in a day. It's a slow method. It can also be subjective. So I think the example that we had in the very beginning is one example of that. Another example can be okay try to tell me if this image is well lit. I think there are several um notions of something well lit. So again it's a subjective task so answers can differ. And of course, humans are not perfect either. U depending on the time of the day, you may have external circumstances that may also influence on the quality of your ratings. So all of that is, I hope, a motivation for us to look at automated metrics that would allow us to quantify how good the outputs are. And we're going to start with what we call reference-free metrics. And I'm going to tell you in a second what reference-free means.
So as I mentioned, we're in the text-to-image generation case. We have a text as input and we have a model that produces an image. So one thing that I want to tell you is there are just multiple ways you can produce an image for a same prompt like these ones. And for that reason, having a single reference image, a single image as reference is a little bit unfair because you would always try to compare against that same reference. That is not the only way you can do things. And so for that reasons, the metrics that we're going to see are called reference-free as in they're not using a single output reference image as reference. They're not doing that. So that's the reference-free. And so let's go back to our problem. And so here we have an input uh which is a text text prompt. And then we have an image as output. And I told you we could consider our problem as being us trying to quantify how good the image is with respect to aesthetics and with respect to prompt adherence. Well, here let's start with aesthetics and let's take our generated image which is on the right and our real image that is on the left. So as I mentioned it is not fair for us to compare our generated image with a single reference. So one idea can be to actually take many generated images and many real images and instead of comparing your single generated image to the single real image. One idea could be to look at the distribution of both and compare how the two and see how the two compares. So let's do that. Well, how would you do it? Well, in the class we've seen that we have methods to represent images in what we call a latent space or some some space. So here one idea could be to take our let's say real images and have them be inputs into some encoder such that they can be represented in space. So we can do that with the sets of real images and in parallel we can also take our generated images and also let them through that pre-trained encoder so that they can also be represented in that same space. So the idea that we can have here is to try to compare how these two resulting distributions in this space how these two compare. So in order to characterize each distribution you can have the mean mu and the co-variance matrix sigma that can just tell you about how your distribution is shaped. And what you want to do is to compare the two. So you want to compare where they're located. So here you can use the mean and how they're shaped can use the co-variance sigma. So uh the co-variance matrix is a quantity that tells you how spread out your distribution is and also what are the directions of of that spread. So, by the way, why would you also want to quantify the spread? Well, you also want your generated images to be diverse. And so, here the spread would be around quantifying how diverse your generated images are. And so, as I mentioned, what we want is to quantify the distance between these two distributions. And so we can come up with some formula which I'm putting it on the slide. We're going to go through uh each component where the first term is around quantifying the difference in where these distributions are located. So comparing the means of each distribution and also comparing the shape of each of these distribution. So here uh so tr is stands for trace. So it's an operation on a matrix and I think maybe if it's the first time you're seeing this formula your first reaction maybe well why do you have this formula like what does it represent? So the truth is this metric which is called the fche inception distance is a metric that is derived from a distance that is called the vaserstein distance that aims at quantifying how much effort you would have to do to go from one distribution to another one. And it just turns out that if you assume that the distribution of real images and the distribution of gen generated images, they're each gausian. If you assume that then it just happens that this vaserstein distance which is something that typically does not have a closed form solution is something that you can express in a closed form. So that's where the formula comes from. But in terms of interpreting each term, you can think of the first term as being the difference in terms of location and the second one is how the shapes differ. Cool. Uh so it's a distance. So what you want is those distances to be as small as possible. So the lower your FID, the better it is. Yep. Yep.
So the question is, do you can you use VLM to do that? So we're going to see that later in this in this lecture. Yeah. Yeah. So the question is can you uh can you say a little bit about how to interpret these terms and how this relates to this building gausian? So we're not going to go into that. But the underlying idea behind this metric is to quantify how much effort you would have to transport one distribution to another. And this is you can think of it as some formula that has this interpretation. And what I was saying was if you assume that the shape or that your real image distribution and if you assume that your generated image distribution if you assume that each of them is gausian then what we're saying is that the quantity that I mentioned can actually be computed with a rather nice formula that is closed form. So I think this goes into the broader theme of gausian distributions just simplifying a lot of things and and this is one of those instances. So the question is the real here is the data yes. So you would take a set of real images, so non-generated images, and you would compute their representation using the pre-trained encoder and you would use those representations to represent those real images. Yeah. So the question is, can I use it in pixel space? So here's the thing. This metric is there for you to compare how well your model is doing compared to other models out there. So you need to have some space that is the same as what the other ones have mentioned. And so here this metric is called fresh inception distance because actually the pre-trained encoder is the inception network. And so what you want is if you want to compare yourself with someone else, you necessarily mean you necessarily need to take the same representation to make things comparable. Yeah. So the question is if your diffusion model is pixel space, is this is still applicable? Yes. So what you would do is you have your real images that you go through a pre-trained model. Your diff pixel space diffusion no problem. You do your diffusion in pixel space and at the end you get those generated images and you input them into that inception model and then you have your representation. Yeah. Yeah. Oh yeah. Good question. So the question is are these condition on a particular prompt? So it's a great question. Well, you touch a great point which is that you want your model to perform well on what you care about. So for instance, you may care about generating faces, let's say, or generating uh some nature, some um indoor scenes. So depending on what your task is, you may want to compare your generated images with respect to a set of real images that are representative of the thing that you care about. So you have a number of data sets like uh so imaget is one that's quite um uh it's a classic one and then you have I believe ms coco and and so on and so you can think of the images that you generate as being conditioned by a set of prompts that are going to match what is there. So it can be either captions or class conditions. So you have something that allows you to compare yourself to that distribution and typically you would use a data set that is aligned with your task of interest and that data set would contain the thing that you would use as input. Yeah, great question. Yeah, great question. So in practice, so the question is in practice how large is the re the set of real images that you take? So typically 50k and actually speaking of that we've seen in lecture five I'm not sure if you remember we had um which measured its performance on that fid 50k uh scale. So 50k here means that we have a set of 50,000 real images. So here what you would do is generate 50,000 generated images and compare that distribution with the set of 50,000 real images. But I've also seen 30 30,000 typically in the order of tens of thousand. Yeah.
So uh the question is is it fair to use variance and mean to characterize the whole distribution? Is that is that roughly your Oh yeah. So the question is is it fair to assume that the shape and the distance is representative of quality? So it's a great question actually one funny thing is if you look at social media every I would say every week you would see a new paper coming up with the new metric. Uh so this metric is by far not a perfect metric. Uh and I think the community voices that out for sure. Um, so yeah, I agree with you. It is not necessarily indicative of the quality that you want to reach, but it's a good enough proxy or at least that's what the community thinks. Uh, and I would say, so it's a personal take. One of the reasons why it's been hard to change the metric that people use is because when you want to publish a paper, typically you want to show that your method is is good. So you want to compare yourself to some previous method and people have just been using FID. So that's why it it's it's really hard to change from that. But I agree with your point which is it is not necessarily reflective of the quality of your images which is why in papers you would also see a bunch of I mean cherrypicked or not cherry picked images that are shown in the paper just to show how how good the model is. Cool. Great. So I'm looking at the time. Uh I think I will move ahead and uh just say that the location you can think of it as if there's a big gap in the location you can think of it as maybe the quality or the style being different and when it comes to the shape being different if for instance the generated images are is in a very um constrained region maybe your model is not generating diverse sets. So these are the points to watch out for. And in terms of the sample size, so as we mentioned, uh the typical reference is FID 50k of a given data set. Um and of course, so we're looking at FID, which is part of this section of reference-free metrics. And here we're using a reference distribution. So I know that it may sound confusing but I just want you to know that reference-free the title of this section refers to us not comparing one generated output with one real image. It is more about comparing the distribution. And then the last thing is one of the main complaints of people is that the distribution of real images and the distribution of generated images they're typically not gausian. So this formula is typically not 100% valid. So the question is is this formula only for gausian? So the formula is actually derived from this other quantity that's called the vaserstein distance. And so this formula is specifically when you compare two distributions that are gausian. And so that's another limitation and a point that you will see in papers when they're they're like okay fid is not that great of a metric. Cool.
So this is the aesthetics. So FID is the metric that most people use for better or worse to quantify how good of a quality your generated images are. And second, as previously mentioned, we're going to look at prompt adherence. How can we know or how can we quantify that the output image that we're getting is aligned with the input? And so for this actually you may have in mind a method we saw in lecture four clip which stands for contrastive language image pre-training which aimed at comparing the text and an image by encoding each separately through their respective encoders and training the resulting system using a contrastive loss that incentivizes elements that are similar to have a higher score and elements that are not similar to have a lower score. So you could very well use that model where you would put the input text that you use to generate the image along with the generated image. And there's a model out there that's called the clip score that is actually exactly that clip model that allows you to get an actual score in terms of how aligned your text and your image are. So that's called the clip score. And speaking of taking into consideration both the prompt and the image, we can also think of doing so using a clip-like model not in order to predict how aligned the two are, but maybe to align or maybe to predict how good the image is to the input text. So you could very well take such a model and train it on a data set that is around human preferences and that's what people did. So this is the p score. So what they did was they took a clip model and they trained it on preference data where you had image and text and the label was the preference. And here you can get a P score which is a holistic score that combines both the aesthetics and the prompt adherence along with some other things to tell you about the overall satisfaction of the human with respect to the generated output. So this is the last metric I wanted to talk you about.
Now I want us to take a step back. When we talk about evaluation, we may think about the image generation model, but as we saw, it's not actually the only model we're interested in. We've also seen other pieces that do matter. for instance, the VAE which is tasked with reconstructing the input. And in this setting, well, your label is much much clearer because what you want to do, your proxy task is to reconstruct the original input. And so in that sense, you do have a reference to base yourself on. So you could very well in this setting want to compare what you output with what you wanted to output. And this is the reason why in this section we're going to go through such metrics which we're calling reference-based as opposed to reference-free because reference-based metrics have something that you can use to compare your generated image against. So I mentioned the VAE but you have actually other use cases as well. So we've not really gone into details about um image text image to image uh tasks. So image editing tasks. But you may also want to compare how your uh edited uh image is with respect to the input image to make sure you're controlling for things that you don't want to edit and so on. So this is not just something that is valid for VAE but for other use cases as well. Cool.
So just getting a bit more uh rigorous about our notation. So the input is noted X. The output which is an attempt at reconstructing the input is called or is noted uh Xhat. And what you want is to have a quantified metric to compare the two that hopefully would will align with the quality of your reconstruction. So the first metric that you may think about I mean the formula looks scary but it's just about comparing pixels just pixelwise distance and you may also know that uh distance under the name MSE which stands for mean squared error and so this metric what it does is it takes these two images and it just computes the distance pixel wise distance of a given pixel in the reference image and the pixel in your generated image. And it does that for all pixels. And one big drawback is it is very sensitive to exactly the alignment between these two images. And so if you have generated a perfect reconstruction but that is slightly shifted a few pixels let's say to the right then your MSE will be actually terrible. That's one drawback. Uh the second drawback is if we tell you about an MSE like let's say it's 10 I don't know well you don't know what to do with that number because it depends how you encode your pixels. So sometimes it is between zero and one, sometimes it is between 0 and 255 like the scale varies which is the reason why you have another metric that is still pixel wise that is called the peak signal to noise ratio PSNR which normalizes MSE with respect to the maximum value that it can take. So this allows you to at least get a reference or put things into context. So it not only does that, it also wraps the thing into a logarithm. And I just want to pause here for a second with an analogy that to me makes a lot of sense and I hope it will make sense to you as well. So, if we're in a room where there is absolutely no light and if I just turn on one light bulb to you, the difference between the complete darkness and one light bulb is a lot. But if in this room we have all these light bulbs already on and if I turn on yet another light bulb, the perceptual difference between having that extra light bulb in this setting and the one light bulb that is turned on versus darkness is very different. So this is what this log tries to do. So if you you almost don't have any errors, it's very very small value and you have just a little bit more error. From your perspective, it will be more something that you would want to notice in terms of the variation as opposed to if you have an image that is quite wrong that is a little yet again a little bit more wrong with the same delta. So this is what the logarithm tries to do. Well, it's great, but it is still subject to this pixel position. So it is sensitive to any shifting. And this is the reason why these are not the only metrics that people use. And in particular, there is one metric that you may see in papers that looks at how these images are structured. So let me tell you a little bit about what I mean here. So here let's imagine we take the original input and then the generated output which is trying to be that input. So what we're trying to do is to see if let's say we take a given patch that is located in the same um that is in the same location in both uh images. What we're trying to do is whether the two have the same overall structure. And what I mean by that is do the colors have the same intensity? Does it have the same brightness? The second one can be does it have the same contrast? And so here we can think of the variance in pixel values within that patch. And the third one is do pixels vary in the same fashion in the first patch and in the second patch. So these are the three dimensions we care about. And for each of them, the first one would be around computing the mean of the pixels in the patch in order to quantify the the brightness or the color intensity of the patch. The second one, the contrast would be around quantifying how much these pixels vary. So you can very well take the variance of those pixels. And then the third one is regarding the structure. And here you can very well take the co-variance matrix the co-variance between these two uh patches and what you will get when you compare the two is a similarity quantity that here in the luminance case takes in the mean of the first patch and the mean of the second patch and tries to make a number out of them. So, is it the first time you're seeing this formula? Yeah. Okay. Uh, I don't want it to looks look for this to to be looking scary. So, if you're looking at the formula, what you see is there is there are two quantities. So, let's note them A and B in this case. And you have something like 2 a b over a 2 + b square. So you also have a constant that is just for stability purposes just in case both of them are are zero. But in this case you can assume that a and b are greater than zero. So what can you say about this quantity? So from high school you may remember that you have this very general identity which is as follows. So a minus b^ 2 which is a^ 2 - 2 a b + b ^ 2. Well, here you can rewrite this as a 2 + b ^ 2 - a - b^ 2 over a 2 + b ^ 2. So I'm going to replace in place. And so here what I'm going to say is that this is equal to one to say here this is equal to one minus this quantity and here this is positive. So this is bounded by one and a is positive b is positive. This quantity is positive. This is bounded by zero. So this is a coefficient that is between zero and one is what I'm trying to say. So here if a and b are similar. So if a is equal to b this is equal to zero it's equal to one. So what I'm trying to say is this metric is a similarity metric that you may read on the you know papers I think it's called the dice coefficient and the reason why it's a good metric is because it gives you the similarity of two quantities by considering also the values that they're at. So I'm going to illustrate it with with an example. So this quantity so if let's say a is equal to 10 and b is equal to 20 then let's say that quantity that I'm going to I'm going to uh let's say d for dice dice coefficient. So that d will be I think equal to 0.8 8 if you compute it. But if a is equal to 100 and b is equal to let's say 110 then d is going to be much closer to one and b maybe something somewhere around.995. So I computed this before the lecture so don't worry I'm not uh hallucinating things. uh but here what I want to say is that this idea of considering the level that you're at and considering the relative difference with respect to where you're at which we saw with the logarithm and with the light bulb story is also something that is captured here. So even though A and B are 10 apart, here they are less similar than here. So all of that to say that I hope that when you look at this formula, it doesn't look as you know something that came out of the blue. So in this formula on the slide, we're using this way of computing similarity using the mean of the pixels which gives you the luminance similarity. You can do the same for contrast. So here you use the variance and the standard deviation. And you also have one for the structure. Well, this one is a little bit different. um you're actually using the Pearson correlation which gives you a quantity that is between minus1 and one. So let's assume you've done that for luminance, for contrast, for structure. And just for simplicity, let's note them L, C, and S. What we're doing is we're just multiplying everything. So if things are similar, they're equal to one. If things are not similar then L is equal more equal to zero. C is tending to zero and S the correlation is tending to minus one. And so this is exactly what we mentioned and as a result you are getting this formula which you may have seen in textbooks. I think it looks really scary when you just look at this formula firsthand, but with this way of looking at things, I hope it will not look scary anymore. And so you do that operation for all patches and the resulting metric that you obtain is the average of those scores across your patches and you get a metric that is called the SSIM. So the structural similarity which is a score between minus1 and one where the more the closer you're to one the more similar the images should be from a structural perspective. Okay.
So, but you may tell me, well, it's on patches. What if your shift is beyond the patch? And I will say, yeah, you're completely right. This metric although being less pixel dependent than the MSE and the PSNR is still sensitive to that pixel shift which is the reason why there is a final metric I want to talk to you about which we actually have already seen in lecture I believe four which is the LPIPS the learned perceptual image patch similarity and That one is actually quite simple. Instead of comparing your X with your X hat in the pixel space, what you do is you take each quantity and you let them through a pre-trained encoder and you compute the representation that this encoder has, which tells you more about how perceptually similar they are as opposed to exactly how aligned they are from a pixel perspective. And so here you have this distance between 5 L X and 5 L Xhat. So the L represents the layer of that pre-trained encoder. And you also have a coefficient W which is actually a coefficient that was determined such that the resulting metric aligns with human perception. So I believe these were tuned because uh there was like a data set of images and uh the idea was to tune this metric so that it's as aligned as possible with the human perception levels.
And that metric is actually quite commonly used and so you may see it quite often. Uh, the only problem to call out, I mean, only top of mind, is that it's not super interpretable in terms of what is going wrong. So if your uh metric is not doing great, you need to see what's going on. And that was it. Any questions before?
Yep. So the question is, for this last one, which pre-trained model is there to use? So I believe it is VGG among so I think there's AlexNet as well. Um, the reason why we have some preset encoders is because the coefficients WL are dependent on the encoder that you use. So you can use the, you know, libraries and mention which encoder you you you want to leverage, but typically it's either VG or AlexNet.
So the question is, what does the formula mean? So within the distance, what you have is two terms. One is the difference between the feature map of X and feature map of X-hat, and you multiply that element-wise with the coefficient WL. Does that make sense? Perfect.
Yep. So uh, the question is, this was for two images. How do you apply to the batch? So just as a reminder, we're looking at cases where we care about how good of a job we did about reconstructing an input. So what you care about is how far off you are. And so this metric tells you how far off you are in trying to reconstruct an input. And so if you want to aggregate, you can use the traditional aggregations like a mean uh and so on.
Cool. Great. With that, I'll give it to Shervin. Thank you, Ain. So, exactly as we saw, all of these metrics, they're very mathematical, and what we want is have a way to interpret them from an intuition standpoint. So we saw that all these metrics, they were operating at different levels and there are many of them. So what I'm going to do is invite you to do a little detour and visit a class of models that we call multimodal LLMs because they have the nice property of being able to convert text and image data into text. But before we do that, let's do a little summary of the kinds of models we've seen so far in the class.
So we saw the transformer at some point that we know transforms bits of text to text. So this is not something that we can use out of the box. We saw DiT where based on latent noise and potentially a condition, you could generate an image. So here it's a generation of images. This is also a kind of model that we can we cannot reuse as is. And same for MMDIT where the input was guided by potentially text, not just a condition. And here we're in this sibling problem of dealing with both modalities, except that we want image and text at the input in order to find a way to judge them and get text as output. So this is typically the setup that we're interested in. So we have a model, we have um like the picture, let's say here a teddy bear, and we ask a question that would guide the evaluation. Let's say, how cute is this teddy bear? And you would want such an evaluation model to give you the answer.
So before starting all of this, I just want to make the problem a bit more concrete. What do we have at hand? So when we talk about images and when we talk about text, what we can do is convert them to tokens. And at the end of the day, tokens, they're just embeddings. So we're facing the problem of trying to find an architecture that can accept these embeddings and turn them into interpretable text. So one thing that comes to mind, so if you recall the architecture of the transformer, let's say you have a text-based text-based model, one natural choice could be to leverage that cross-attention layer that could enable you to make text tokens attend to anything you want. And that anything you want, the keys and values of cross-attention could very well be images. So this is one possible direction where you use uh text tokens as input of your um of your decoder, and then you keep this cross-attention mechanism in order to make it interact with encoded images. So this is something that people have done uh because it's a very natural thing to do and it's also quite efficient computationally because you leverage a fixed context length only composed of text and then you make it interact with images. So there is one example here that we can cite of a model that did so, Flamingo by Google, where images are given as keys and values, and you have some placeholder token that gates when it's fine to attend to these images. So this is one possibility.
But one caveat of this mindset is that you cannot directly leverage the latest advances in large language models because, as you know, these are decoder-only models and they have gotten rid of that cross-attentional cross-attention altogether. So you cannot use all these latest models that have been tuned and work great out of the out of the box. You need to re-engineer that cross-attention layer in there. So this is why you see a second branch of dealing with multimodal input, which is to directly feed these image and text tokens as input. And one example um of such model is LLaVA. So here you use uh an encoder to to find the right embedding for this uh tokens for for image patches, like a ViT model, and then you you give it to to that decoder-only structure.
Okay, great. And these days, exactly for the reason that I mentioned, like quality but also the fact of reusing wins, this latter technique is what people use in practice. So all the latest LLMs that you might see are based often times on putting both images and text as input of the decoder, not doing cross-attention. And they they have some capabilities that I that I'm mentioning here. We're not going to go into details uh about them, but I want you to be aware of them because we're going to actively use them at the time we want to use these models for judging. So one is working uh with all kinds of images. Yeah.
So there's a question here. Yeah. So the question is, what do you mean by reuse existing wins? So so I think it's an engineering question. I think you could definitely reuse something uh already trained, like have weights frozen and and start from there. This is one possibility. I was more thinking about an architectural standpoint where you have a design that that is proven to work. This is just another modality with a same kind of input that you can just recycle. But then on how you actually come up with such a model, I think it's more of a engineering problem um so I don't give a specific opinion on it, but I I I do think you could definitely initialize weights um and and reuse it, you know, in in a strict sense of the term.
Okay, so we want these models to be um working across all kinds of inputs and in particular, one property that you want your models, your generative models, is to generate text as part of your images. So let's say if you uh want to generate the image of a sign, you want the the text to be readable. So you would want models that do judge these kind of generated images to be aware of uh of characters. So this is why I mentioned OCR. And um and then also one other thing which I think was mentioned here is we do not want to have a black box that just gives us a number. We want to have some rationale behind what grade that we give. So this is why leveraging reasoning abilities of such models is going to be something that we want to use.
Okay, great. So now I want us to look at traditional metrics and then find some of the key concerns we had and see what could be ways to mitigate them. So one thing that we were concerned with with traditional metrics was that you get a number, but at the end of the number, at the end of the day, you don't know what to do with it. Right? Let's say you have a prompt that gave a given image and you have a certain CLIP score. Now, what do you do with it? So this is something that you could fix by taking the prompt and trying to reason about what properties in the generated image could make it such that the image would match the prompt. And this is uh something that you could do by decomposing what you care about into atomic properties. This is what the TIFFA uh paper does. So Text-to-Image Faithfulness Evaluation, where they use few-shot learning to show examples of what interesting decompositions of what you care about in an image might look like, and they apply it to a given prompt to generate a set of questions. And these questions are very simple. They're just yes or no, or things where the answer is obviously right or wrong. So that way, you decompose the like the judging into dimensions that are quite quantitative, and what you do is that you take an LLM model to assess each of these dimensions independently. So, for example, here, uh, this teddy bear reading a book. Is it a teddy bear? Yes. Is it cute? Yes. Is there a book? Yes. And is the teddy bear reading the book? Yes, as well. So, such a metric would give you a score that matches the fact that all of these criteria were met. And it's it it's quite uh interesting because in the in the case of CLIP score, even though you have an image that semantically matches your input prompt, you're not exactly sure what that remaining delta is about. And with a method that decomposes these concerns, you could pinpoint what goes wrong. So, so it's quite simple and um, you know, you could debug where the issue comes from.
But on the other hand, this method of generating dimensions that you care about per image is quite bespoke because you're here at every prompt uh that you input your dataset, you need to have a a grading rubric that is specific to that prompt. So the fact of generating that rubric in the first place is going to be expensive and it might be error-prone. And something else is that you have all of these questions that examine multiple sides of that prompt, but the weighing of each of these claims might not match the importance of what each claim uh to your point of view. So, for example, here, if you uh, if you don't have a book at all, then you might have one or two that goes away, and you would have maybe half of the metric that I mean, the metric would be about half of the max score, but it necessarily might not be what you want. So you might want a way to generate a rubric that is more robust to that. Um, yeah, I'm going to take a pause for a few seconds and see if that makes sense.
Okay, awesome. Uh, so I'm going to state another complaint. Uh, so so here we saw CLIP score, and one great thing about CLIP score is that it can try to convey whether the semantic meaning of a generated image was preserved. But one thing that we observe is that semantically encoding the meaning of a prompt within a vector and then doing some dot products with respect to the embedding of the image might not carry all the semantic subtleties that prompt variations might convey. So here, one thing that we observe in practice is that in the cute bear reading a book example, if you exchange book and teddy bear from a semantic perspective, like to your intuition, it changes things quite a bit, right? You would have a totally another image in mind. But from a CLIP score standpoint, it turns out that the score doesn't change that much. So you could explain this by the fact that you're um that you're projecting all of the image and all of the content of the prompt into vectors, and these are not very richly conveying all these subtleties. But also, one other explanation could reside in the way that uh CLIP is trained because you have you are incentivized to match images and prompts in a way that matches those that correspond with respect to text-image captions. But you have this concept of in-batch negatives that we saw at the time where we introduced CLIP, where you try to make the distance between images and prompts that have nothing to do with that image further apart. So you don't really have an incentive to learn these subtleties. And these are the two main reasons why uh CLIP score is not that great.
So, but you might tell me, okay, Shervin, CLIP was generated with the embedding of the entire sentence. Uh, because as as we remember, you would encode the meaning of a given sentence with a decoder uh and then extract the last embedding, which contains the embedding of the whole sentence. So one thing we can do is have the same um have the same property with a trick where you input both the image and the sentence in the same embedding and then extract the probability of whether both image and sentence are supposed to match or not. So this is something that is called VQA score, where the template is very simple. So you you always follow this same structure where you put the image and then you ask yourself, does this figure show and the content of your prompt? And then what you do is that you look at your probability distribution of your next token and you look at the one that corresponds to the token "yes" and you say that that probability is going to be your score, which means that if the sentence is semantically similar or matching to the image, you're going to have a high probability and lower otherwise. So by doing so, you can alleviate this issue that we had seen at the CLIP score level, where you were comparing vectors in a coarse fashion, and now you're directly reusing the structure of the decoded output in order to embed both modalities in a way that makes sense.
So yeah, so the question, can a regular LLM do it? So very pedantically, no, because you have image modality in there. But let's say the question is for LLMs. Can a standard LLM do it? So the we advocate that yes, because it is trained to yet to understand broader concepts. So it's a zero-shot, it's a zero-shot task. So yeah, that's a great point. It emphasizes one benefit of this technique. You do not need to train anything in a bespoke manner. You just take an off-the-shelf LLM that does uh that does know how to um understand things.
Okay, great. So, but one drawback that you might notice is that this technique relies on you having access to the probability distribution of the next token. And if you take all the latest and best models out there that might be closed source, they do not disclose the probability distribution of the next token. If you are in the field of distillation, you know it's a very sensitive topic because you can learn a lot of things from it. So this is not something that you have access to. And something else that is wasteful is every time you ask such questions, you need to do a dedicated LLM call, and these are quite expensive. So from a user standpoint, you could say, okay, fine, I have a provider. I will do all of them in parallel and wait for the max of all of them to finish to have my result, which is not that bad often times, but it's quite costly as it scales with the number of decomposed dimensions. And everything that I've mentioned here is focusing on the prompt of your dataset. So you're inferring your conditions and dimensions off of the prompt of the dataset.
There seems like a better way to do it. Like you're still stuck in these ancient worlds where you try to engineer everything, even metrics. You try to engineer them in order to make sense of them afterwards. But one shift of paradigm that seems attractive here is instead of being prompt-centric, be concept-centric of what you consider to be good, and then describe that good in a generic way, and then leverage all of the reasoning capabilities of these LLM models to do that sort of reasoning on your behalf and come up with the final score. So, does everyone agree that this strategy sounds attractive?
Okay, great. And this brings us to um a paper that popularized this mindset called VQA score. Visual Instruction Guided Explainable Score, where you give your prompt, you give your generated image, and then you describe a rubric. And Ain, if you remember at the beginning of the lecture, he mentioned two axes where we broadly had an interest in. One is semantic consistency, and the other one is perceptual quality. And this effort formalizes how we could write rubrics and grades for both of them. So you would have such a rubric and then ask an LLM about it, and then since this task is about judging, you call such a model an LLM as a judge. And the good thing about these models is that they are interpretable, and you can ask them to output uh some reasoning before prompting them for a given score, which is what this paper does. And with two dimensions that we care about, so semantic consistency and perceptual quality, respectively, denoting whether the image follows the prompt and whether the image looks good. So you could have two different generations, and you might smile at the second one because it's not very realistic, which is why you might so it's technically still reading the book on the right-hand side, but on uh, the natural aspects, this is where an LLM as a judge might come in and and just, you know, keep us in check.
Okay, great. And just formalizing the fact of asking about an LLM as a judge opinion regarding grading a given dimension. So typically, what you would have is that you first give your input, and then you handcraft a set of guidelines with respect to each of the dimensions of interest, and then you ask your LLM as a judge to output its decision in some given format. One that is popular is JSON, because you can then parse scores and rationals and anything else that you care about in a structured way by just accessing fields. And one question that comes to mind is, how to ensure that the rationals and the scores actually do make sense? So here comes the, I think, most interesting part of the lecture, which is, once you have a problem where you have a judge that you want to use to grade your images, how are you going to make them aligned with what you as a human find good? So this is the first stage where you would take in um, in an actual, you know, in a true setting, you would take your prompts, you would take your generated images, and you would typically ask humans to grade them alongside the rubrics of interest. Let's say you have perceptual quality and semantic consistency. You would first get a sense of what is good versus bad from a human standpoint. And the second step would be to handcraft rubrics that would match human intuition. So these days, handcraft is a bit of a um, you know, it's a bit of a strong word because you wouldn't actually handcraft this. You would ask a model to handcraft instructions that would be such that the resulting ratings would match human intuition. And once you have that, then you can further judge other prompt-image pairs with that trained judge. So this is a three-stage way of solving this problem. So first, just asking your intuition about what is good, then tuning the LLM, the LLM's rubrics about it, and then third one would be uh, directly asking the LLM about new images.
Okay. And um, so you might have multiple settings where you would ask for like for advice. So you have a pointwise setting where you have a given image, a given prompt, and you want to know if if the result is good. You have the pair-wise setting that Ain also mentioned. And you have another one where you have a prompt, you have a list of images, and you want the model to um, to rank them. So going one by one, in practice, pointwise is great when you want to do one-sided evaluation of your of your images. So just to have a rough idea, but also to debug what goes well versus doesn't go well, because as you remember, you have this rational side of the LLM as a judge output that can guide you towards understanding loss modes of um, of your of your model. So it can either lead to you iterating on the image generation model or coming back to the calibration of the judge itself, because it might not correspond to the intuition that you have in mind. So it's great for doing both of them. Uh, another setting is the pair-wise one, which is great when you want to compare models with each other. So typically, if you have a new iteration of the model and you want to know if it's fine to replace it altogether, you would use this kind of mode. And then regarding ranking, even though that's something that's studied in papers, it's typically not used in practice because the ordering might be too sensitive, and so you have a lot of variance, and often times the ranking of several outputs might not be something that you want to do as a one-shot task. So it might not have as broad um, so applications as it as the two other ones.
Uh, so I just want to spend a few minutes to talk about best practices overall when you deal with uh ML, when you deal with LLM as a judge. So here we saw with VQA score that you could decompose what you care about into two dimensions. But in real life, you might have use cases where you have a task-specific rubric that that is not general, but that might be specific to what you care about. So you would typically decompose and isolate that as a separate metric and make the judging be done on all these sets of criteria, including that other one that would be designed to be atomic and isolating what you care about. So another thing is that empirically, you typically want your model to output the rationale before outputting a score. Uh, because in the LLM space, you have all this research around concepts surrounding chains of thought, and how it improves the like performance, and and the intuition for it is that even as a human, when you want to form an opinion on something, it's always good to enumerate facts about it. Just say out loud what you have in mind when you do like a piece of judgment before actually doing the judgment. So this is a good practice. Um, something else is that with LLMs, you have this temperature parameter that you can um, that that controls how creative and how deterministic versus stochastic your output gets. And typically, when you use u like chatbots, these are not set to zero. So outputs are typically non-deterministic because quality is typically better when it's non-deterministic for a range of tasks. But for very precise tasks, like um, like judgment, where you're when you're grounded on the input that you're given and the rubrics that you're given, this is typically um, a use case where it's fine to make it set to zero, and also because you want determinism across your LLM as a judge runs. Uh, something else is that you might see position bias if you are in a pair-wise setting. So it's good to swap the ordering of the two samples that you want to compare. And the last point is something that we mentioned just before. We want to make sure it's aligned with human judgment before actually going ahead and um, and then trusting it blindly.
So I want to spend the last five minutes uh talking about the main benchmarks that cover some of the characteristics that we care about on image generation. So on benchmarks, I I will go through like points that cover overall areas that we care about. So one is, let's say you have a prompt, you want to make sure that whatever is generated contains the objects and some high-level attributes that you mention in your prompt. So this is where um, GenEval, it's it's one possible benchmark that comes to mind, where you have six tasks of increasing difficulty that talks about objects, like counting of objects, colors of objects, and relative position, as well as attribution of colors. So the judges here are object detection uh models that will come in and then detect whether the right objects or the right colors were generated. So this is one example. So you have a photo of a purple backpack and a white umbrella, and you want to make sure that both objects and colors are correctly um attributed.
And something else that you care about that might touch on uh something that you mentioned. So when you deal with dense prompts, so typically prompts that are very detailed, you want to make sure that all of the details are correctly rendered. And this is why this DPGB benchmark has uh a mechanism that looks a bit like what we saw in TIFFA, where you have this big chunk of text that you decompose into a set of atomic yes or no questions. And the interesting thing about this paper is that it groups all of these claims into a graph that is logical. So here you have a big chunk of text, and it decomposes into questions that are centered on attributes, relationships, and uh spatial um, yeah, spatial location. And let's say you want to judge about a given attribute. So here, that long uh bunch of text mentions uh an empty space where an invisible man has this and that. So first, you need the empty space to exist, and this benchmark looks at it in a coarse-to-fine mindset. So if the prerequisite doesn't exist, then you don't even bother looking at the other conditions. So this is what makes this benchmark interestingly uh efficient. And uh conversely, if if one precondition exists, then you move on to the next next pieces of the graph. So this is another area.
So something else that we had mentioned regarding capabilities that we were interested in was OCR as a capability, and on the image generation side, you might want to generate images that have walls of text in there, and you want to know how well they are rendered. So there is a LongTextBench that could be mentioned, where you have um, like scenarios where displaying text might be seen as interesting from an image generation perspective, and the paper uses a VLM uh, like typically an OCR judge that will try to render the text in the generated image, and it will match to the reference text that was used as a generation prompt, and if both match, then we say that the text was correctly generated. So it's one other interesting application.
And then the last one I will mention is on another uh image generation mode that is of interest typically when you want to generate an image. So if you want to operate on top of a given image. So here, like Grounded-Edit Bench. So I put a question mark because it's not explicitly spelled out in the in the paper, but the like the abbreviation meaning, but the grounding part is on the input image that is given. So you ask a task about it, for example, um, like replacing some background, changing colors, and so on. So you have 11 such uh tasks, and you use um, like LLM as a judge model that operates on both perceptual quality and semantic consistency to judge the output. So here's an example from here's the figure from their paper on what it looks like.
And I just want to say one last word about all of this. Sometimes you might be overwhelmed with all these metrics. So we saw at the beginning of the lecture, a bunch of quantitative ones. We saw LLM as a judge ones that are still imperfect because they depend on how well you tune it. So there is no absolute way of knowing how something performs, and you might be prone to only relying on a few sample images as a way to have an opinion about a given uh image uh model. Um, but I want to say that so let's do the following thought experiment. If you consider not the procedure of picking the next image that is generated by your model, but the best of three versus the least well of three, then you see the corresponding distributions are quite different. So from a same image generation model, you could handcraft different distributions just by the way you go about sampling what you want to display to someone. So yeah, I just want to put a caveat on when you see images, it doesn't tell the whole story either.
And with that, thank you very much for coming here despite the long weekend. I hope you have a great one.