††thanks: anonymized

# Potential Ways to Detect Unfairness in HRI and to Re-establish Positive
Group Dynamics

Astrid Rosenthal-von der Pütten  iTec - Chair Individual and Technology
RWTH Aachen University
Aachen, Germany
<EMAIL_ADDRESS>Stefan Schiffer  iTec - Chair Individual and
Technology
RWTH Aachen University
Aachen, Germany
<EMAIL_ADDRESS>
###### Abstract

This paper focuses on the identification of different algorithm-based biases
in robotic behaviour and their consequences in human-robot mixed groups. We
propose to develop computational models to detect episodes of microaggression,
discrimination, and social exclusion informed by a) observing human coping
behaviours that are used to regain social inclusion and b) using system
inherent information that reveal unequal treatment of human interactants.
Based on this information we can start to develop regulatory mechanisms to
promote fairness and social inclusion in HRI.

###### Index Terms:

human-robot interaction, group dynamics, social rejection, bias, inclusion

##  I Introduction

Social robots are envisioned to be part of our lives as service providers,
team members, companions. Depending on the robots’ tasks and purpose they will
be playing a more or less active role in our social groups and potentially
shape group dynamics - for the better and the worse. Previous research
demonstrated that robots can positively influence social dynamics in small
groups. In free play situations a robot was able to mitigate conflict between
children over toys by providing information on how to compromise [1]. A
robotic microphone positively influenced group discussions by encouraging
those discussion partners to participate more who were more silent than others
[2]. Similarly, a robot giving self-disclosure statements could facilitate to
speak up in a support group session for stressed students and it improved
perceptions of trust among the members of the support group [3]. However,
robots can also cause feelings of social exclusion by leaving humans out of
interactions (e.g. not tossing a ball to the human, [4]) or communication
(e.g. speaking in ”robotic language, [5], or bluntly rejecting the human,
[6]), causing negative consequences such as experiencing negative emotions,
the feeling of being ignored or being meaningless, and lowered self-esteem.
While we assume that developer teams of social robots do not intend to create
robots that socially exclude individuals, social exclusion can still arise in
interactions in human-robot groups because robots may have software components
that are biased against certain groups of humans (e.g., women, PoC) or because
it is unaware of the social situation the robot finds itself in and
unknowingly behaves socially inadequate.

In this paper we want to briefly revisit i) the role groups play in our
(human) lives and how group membership can lead to inter-group bias, ii) the
psychological consequences of social rejection (caused by biased behavior),
iii) sources of algorithmic bias, and iv) how to use system information to
detect bias and start repair mechanisms.

##  II Related work on HRI groups and algorithmic bias

###  II-A What groups mean to us

Groups are highly important to individuals [7]. Since the membership in groups
is one defining part of an individual’s self-concept and consequently an
individual’s self-esteem is partly dependent upon group membership, strategies
to protect the group and differentiate it from other groups are important for
the individual. Positive distinctiveness of the in-group from other groups can
be achieved by simply evaluating groups differently in favour of the in-group
– also referred to as inter-group bias which is “the systematic tendency to
evaluate one’s own membership group (the in-group) or its members more
favourably than a non-membership group (the out-group) or its members” [8]. It
manifests as favouring the in-group (in-group favouritism) or derogating the
out-group (out-group derogation), or both. In-group favouritism entails the
extension of trust, positive regard, cooperation, and empathy to in-group
members, but not to members of the out-group and thus is an initial form of
discrimination. Inter-group bias extends to robots. For instance, humans show
in-group favouritism for an in-group robot in online studies [9, 10] and
assigned “painful” noise blasts to out-group humans to spare in-group robots
in scenarios were interactants were in different rooms [11]. Since humans show
inter-group bias in human-robot mixed groups negative emotional and social
consequences potentially arise for other humans when a robot is favoured
instead of them. Moreover, the robots could also be the source of social
rejection due to algorithmic biases which will be discussed further below.

###  II-B What happens when we feel excluded from a group

Inter-group bias can be perceived as a sign of social exclusion or social
rejection. According to the Temporal-Need-Threat-Model by Williams [12],
social exclusion causes a reflexive pain response accompanied with negative
affect (e.g., sadness, anger) and triggers threats to four fundamental needs:
belonging, self-esteem, control over one’s social environment and meaningful
existence. In a reflective stage, individuals’ attention is directed to the
exclusion episode and they reflect on its meaning and relevance. This may lead
to coping responses such as compliance and conformity or attracting attention,
provoking, and attempts of controlling others to fortify the threatened needs.
Persistent exposure to ostracism over time consumes the resources necessary to
motivate the individual to fortify threatened needs. Eventually, this leads to
resignation, alienation, helplessness, and depression. Since humans are
hypersensitive to ostracism and tend to over-detect ostracism [13], it is
extremely likely that humans detect ostracism in interactions with robots as
well and experience and engage in the described reflexive and reflective
processes. Indeed, recent studies have explored this and found that
participants felt excluded when robots talked in a “robot language” [5] or
when a robot stated it did not want to interact with the human again [6].
Although the need for a paradigm shift from studying dyadic human-robot
interactions in laboratory settings to studying group interactions in complex
environments has been identified and advocated for [14] research in human-
robot mixed groups is still scarce. Social psychological phenomena such as
social exclusion, and ostracism as negative consequences of a robot’s unequal
adaptation to group members through machine learning is yet a new perspective
in research on HRI groups that the community just recently has identified to
be important.

###  II-C Sources of algorithmic bias in HRI

The general notion of unfair or fair AI has been discussed intensively in
recent years. In our modern, digitalized world, we engage more and more in
interactions with algorithms and artificially intelligent systems that learn
and adapt based on these interactions. Our visits, views, clicks, and buying
decisions provide training data for recommender systems on shopping websites
(e.g., Amazon) or video streaming applications (e.g., Netflix). Recently,
voice agents have entered our homes providing us with helpful information, and
services while using these interactions as training data to learn and adapt to
us and generalizing this knowledge to predict preferences and intentions of
groups of users. Especially in the latter area of voice agents, similar biases
may emerge when algorithms try to categorize users into groups and provide
these groups with personalized interactions. Recent research demonstrated in
many application fields (e.g., financial credit, job application management)
that algorithms often discriminate certain groups of people, for instance,
based on gender or skin tone and thereby exhibit unintended and unexpected
biases usually originated in biased training data. While the lack of diversity
in the training data sets that are being used in machine learning originates
from different sources, it unequivocally causes a bias towards certain types
of users at the cost of others. A new topic that has been recently identified
[15] are potential negative consequences arising in HRI by robots that show
unintended biases in favour of certain group members and thereby
discriminating others. Under the term Fair AI, researchers call out the
computer science community to “identify sources of bias, [to] de-bias training
data and [to] develop artificial-intelligence algorithms that are robust to
skews in the data” [16]. Since computer vision and machine-learning are core
technologies for robotic systems, it has been proposed that a similar threat
is posed to HRI [17].

Interestingly, concerns about the negative effects of biased robotics systems
are often seen from a more global societal perspective. For instance,
autonomous cars could put people of colour to a greater risk due to biased
person recognition and medical or service robots might reinforce certain
discriminatory practices due to biased algorithmic decision-making [15].
However, besides the issues already identified, new forms of biases are likely
to emerge when the training data base for machine learning are interactions
with multiple humans over a longer time as we have discussed in previous work
[18]. Robots are expected to learn and adapt to their users, ideally while in
operation during run-time. Hence, robots learning from humans means that
robots learn from interactions and the more interactions the better the
learning outcome. But humans might have more or less time or might be more or
less motivated to provide these interactions that are needed for learning.
Thus, training data sets differ in quantity and quality which has consequences
for the learning outcome (e.g., knowing the user’s preferences) and the
robot’s quality to adapt to different users.

Let is consider the following family scenario, in which the user who spends
more time at home potentially provides the largest training data base for the
robot, is best known to the system, and his/her preferences can be easily
determined and served. A user who spends less time at home might receive
recommendations and interactions matching his/her preferences less often. Or
let us consider a working environment, in which the robot’s implemented goal
is to maximize team performance. The robot will monitor the performance of
every single team member and their contribution to team performance. Based on
the maximization goal, the robot might decide to distribute more resources to
those team members who are high performers in the task, thereby discriminating
low performers. Very likely, low performers will experience negative emotions,
feel threatened in their self-esteem and their need to belong to the group,
and will try to regain social inclusion. Recent work tapped into this issue of
unequal adaptation to users based on performance and algorithm goals in
experimental studies. For instance, in a collaborative tower construction
task, a robot distributed building blocks unequally between two participants
which led to lower satisfaction of the human team members with the team
relationship [19]. In a collaborative Tetris Game, fair distribution (in
contrast to unfair distribution) of resources led participants to trust the
system more and resulted in higher overall team performance [20]. However,
emotional responses and consequences for the self-perception and self-esteem
of the neglected participant were not assessed.

These first results and the scenarios described above demonstrate that besides
the now commonly known problems of biases in natural language processing or
face recognition also interaction-based algorithmic learning can result in,
for instance, perceived (inter-group) bias and social exclusion of individuals
with severe negative outcomes for the emotional state of the individual and
the social dynamics of the group.

##  III How to overcome biased HRI and reach better inclusion

###  III-A First Step - Recognizing the potential for biases in your own work

Researchers in the field of HRI have become more aware of the potential that
their developments and systems might be affected by biases. Earlier this year
a group of HRI scholars discussed ”how pursuing a very typical, data-driven
approach to the development of a robot listener behavior (production of
backchannels, which can serve to indicate attentiveness) resulted in models
that acted differently with participants with different gender identities”
[21]. In their paper the authors discuss design guidelines that may be applied
to avoid embedding gender biases into robot social behavior such as carefully
examining training data sets before using them for modelling. According to
Ntoutsi et al. [22] this recommendation would fall under preprocessing methods
focusing on the data to mitigate bias which focus on creating so-called
balanced data sets. This can be done using different approaches such as equal
sampling from different groups or altering the given data in its
classification, i.e., adapting training sample weights [23].

###  III-B Second Step - Mitigating bias in machine learning before system
deployment

Besides the pre-processing methods to mitigate bias as mentioned before,
Ntoutsi et al. [22] also consider so-called in-processing methods focusing on
the ML algorithm, and post-processing methods focusing on the ML model. Both
types of approaches concentrate on the machine learning process and/or the
inspection and adaptation of the resulting model. For instance, in the latter
case Ntoutsi et al. refer to previous work that post-hoc changed the
confidence of CPAR classification rules [24] or the probabilities in Naïve
Bayes models [25].

###  III-C Third Step - How to use system information to detect bias during
interaction and start repair mechanisms

All the specified approaches above have in common that developers or
researchers are actively involved in curating either data or changing the
algorithm’s specifications which cannot be done during run-time. Moreover, if
the system is further learning based on continuous interactions it can be that
a ”de-biased” algorithm becomes biased again, for instance, because human
interactants behave in stereotypical ways. We have proposed that i)
information on biased components and ii) certain system information that is
produced during ongoing interactions with humans can be used to inform the
system about potential biases emerging. For instance, it is commonly known
that biased speech recognition performance is biased in favour for people
speaking accent-free standard languages due to better training to that user
type. This known pre-existing bias can be used during the development of
interactions with humans. The system should also be enabled to draw
conclusions of internal data to detect bias. For instance, recognition for
human faces or behaviours as well as predictions about human behaviour usually
are hypothesis-based with specifications about the likelihood and confidence
that this hypothesis is true or false. Consistent lower likelihoods connected
with one user could be used (together with other information) as an indicator
for bias. As described above most systems are biased in their speech
recognition performance in favour of people speaking accent-free High German
due to better training to that user type in contrast to people speaking local
dialects or foreign accents. A robot could use system information that is an
indicator for this bias occurring, for instance, when a higher number of
hypotheses exists (cf. n-best lists, [26]; [27]) and/or lower confidence (cf.
[28]) in speech recognition or computer vision for a specific user (e.g.,
interactant with local dialect or foreign language accent). Based on this the
robot would initiate a regulatory mechanism such as apologizing for
misunderstandings and asking the user to speak more slowly.

###  III-D Fourth Step - How to use user behavior to detect bias during
interaction and start repair mechanisms

As explained above there is empirical evidence that when humans detect signs
of social rejection or social exclusion by a robot they will experience
negative emotions and fundamental needs are threatened [6, 4, 29, 5]. This may
lead to coping responses such as compliance and conformity, attracting
attention, provoking, or attempts of controlling others to fortify the
threatened needs. Current studies on social exclusion in HRI scenarios
predominantly look into self-reported experiences. Future work should
systematically investigate which coping behaviours excluded humans exert in
trying to regain social inclusion. One approach is to use behaviour analysis
to identify patterns of verbal and nonverbal behaviour, and interactional
strategies a robot might detect as a sign that social exclusion occurred.
Based on this classification a computational model could be implemented to
detect episodes of social exclusion informed by observing human coping
behaviours that are used to regain social inclusion as well as the
aforementioned system information.

###  III-E Fifth Step - Develop Socially Interactive Agents with Capacity to
Re-establish positive Group Dynamics

The work is not done when we managed to detect biases. We further need a good
concept how to resolve social exclusion episodes for the human and re-
establish positive group dynamics. This means that there is a need to i)
develop conversational or interactional strategies to maintain positive social
group dynamics that can be triggered when potential bias is detected, and ii)
research which conversational and interactional strategies are effective and
regarded as socially adequate in different situations and group
constellations.

##  IV Conclusion

In this paper we outlined why social robots should take into account the
social dynamics in a human-robot mixed group as well as the (negative) social
consequences of its own behaviour in these groups. We discussed why and in
which ways biases can arise in HRI and how we can either de-bias systems or
enable the system to automatically detect bias and engage in repair
mechanisms. We advocate for considering this perspective throughout the
development process of a new system.

## References

  * [1] S. Shen, P. Slovak, and M. F. Jung, “” stop. i see a conflict happening.” a robot mediator for young children’s interpersonal conflict resolution,” in _Proceedings of the 2018 ACM/IEEE international conference on human-robot interaction_ , 2018, pp. 69–77.
  * [2] H. Tennent, S. Shen, and M. Jung, “Micbot: A peripheral robotic object to shape conversational dynamics and team performance,” in _2019 14th ACM/IEEE International Conference on Human-Robot Interaction (HRI)_. IEEE, 2019, pp. 133–142.
  * [3] C. Birmingham, Z. Hu, K. Mahajan, E. Reber, and M. J. Matarić, “Can i trust you? a user study of robot mediation of a support group,” in _2020 IEEE International Conference on Robotics and Automation (ICRA)_. IEEE, 2020, pp. 8019–8026.
  * [4] H. Erel, Y. Cohen, K. Shafrir, S. D. Levy, I. D. Vidra, T. Shem Tov, and O. Zuckerman, “Excluded by robots: Can robot-robot-human interaction lead to ostracism?” in _Proceedings of the 2021 ACM/IEEE International Conference on Human-Robot Interaction_ , 2021, pp. 312–321.
  * [5] A. M. Rosenthal-von der Pütten and N. Bock, “Seriously, what did one robot say to the other? being left out from communication by robots causes feelings of social exclusion,” _Human-Machine Communication_ , vol. 6, no. 1, pp. 117–134, 2023.
  * [6] K. Nash, J. M. Lea, T. Davies, and K. Yogeeswaran, “The bionic blues: Robot rejection lowers self-esteem,” _Computers in human behavior_ , vol. 78, pp. 59–63, 2018.
  * [7] R. F. Baumeister and M. R. Leary, “The need to belong: desire for interpersonal attachments as a fundamental human motivation.” _Psychological bulletin_ , vol. 117, no. 3, p. 497, 1995.
  * [8] M. Hewstone, M. Rubin, and H. Willis, “Intergroup bias,” _Annual review of psychology_ , vol. 53, no. 1, pp. 575–604, 2002.
  * [9] F. Eyssel, D. Kuchenbrandt, S. Bobinger, L. De Ruiter, and F. Hegel, “’if you sound like me, you must be more human’ on the interplay of robot and user features on human-robot acceptance and anthropomorphism,” in _Proceedings of the seventh annual ACM/IEEE international conference on Human-Robot Interaction_ , 2012, pp. 125–126.
  * [10] D. Kuchenbrandt, F. Eyssel, S. Bobinger, and M. Neufeld, “Minimal group-maximal effect? evaluation and anthropomorphization of the humanoid robot nao,” in _Social Robotics: Third International Conference, ICSR 2011, Amsterdam, The Netherlands, November 24-25, 2011. Proceedings 3_. Springer, 2011, pp. 104–113.
  * [11] M. R. Fraune, S. Šabanović, and E. R. Smith, “Some are more equal than others: Ingroup robots gain some but not all benefits of team membership,” _Interaction Studies_ , vol. 21, no. 3, pp. 303–328, 2020.
  * [12] K. D. Williams, “Ostracism: A temporal need-threat model,” _Advances in experimental social psychology_ , vol. 41, pp. 275–314, 2009.
  * [13] L. Zadro, K. D. Williams, and R. Richardson, “How low can you go? ostracism by a computer is sufficient to lower self-reported levels of belonging, control, self-esteem, and meaningful existence,” _Journal of Experimental Social Psychology_ , vol. 40, no. 4, pp. 560–567, 2004.
  * [14] M. Jung and P. Hinds, “Robots in the wild: A time for more robust theories of human-robot interaction,” pp. 1–5, 2018.
  * [15] A. Howard and J. Borenstein, “The ugly truth about ourselves and our robot creations: the problem of bias and social inequity,” _Science and engineering ethics_ , vol. 24, pp. 1521–1536, 2018.
  * [16] J. Zou and L. Schiebinger, “Ai can be sexist and racist—it’s time to make it fair,” 2018.
  * [17] L. Righetti, R. Madhavan, and R. Chatila, “Unintended consequences of biased robotic and artificial intelligence systems [ethical, legal, and societal issues],” _IEEE Robotics & Automation Magazine_, vol. 26, no. 3, pp. 11–13, 2019.
  * [18] A. Rosenthal-von der Pütten and A. M. Abrams, “Social dynamics in human-robot groups–possible consequences of unequal adaptation to group members through machine learning in human-robot groups,” in _Artificial Intelligence in HCI: First International Conference, AI-HCI 2020, Held as Part of the 22nd HCI International Conference, HCII 2020, Copenhagen, Denmark, July 19–24, 2020, Proceedings 22_. Springer, 2020, pp. 396–411.
  * [19] M. F. Jung, D. DiFranzo, S. Shen, B. Stoll, H. Claure, and A. Lawrence, “Robot-assisted tower construction—a method to study the impact of a robot’s allocation behavior on interpersonal dynamics and collaboration in groups,” _ACM Transactions on Human-Robot Interaction (THRI)_ , vol. 10, no. 1, pp. 1–23, 2020.
  * [20] H. Claure, Y. Chen, J. Modi, M. Jung, and S. Nikolaidis, “Multi-armed bandits with fairness constraints for distributing resources to human teammates,” in _Proceedings of the 2020 ACM/IEEE International Conference on Human-Robot Interaction_ , 2020, pp. 299–308.
  * [21] M. T. Parreira, S. Gillet, K. Winkle, and I. Leite, “How did we miss this? a case study on unintended biases in robot social behavior,” in _Companion of the 2023 ACM/IEEE International Conference on Human-Robot Interaction_ , 2023, pp. 11–20.
  * [22] E. Ntoutsi, P. Fafalios, U. Gadiraju, V. Iosifidis, W. Nejdl, M.-E. Vidal, S. Ruggieri, F. Turini, S. Papadopoulos, E. Krasanakis _et al._ , “Bias in data-driven artificial intelligence systems—an introductory survey,” _Wiley Interdisciplinary Reviews: Data Mining and Knowledge Discovery_ , vol. 10, no. 3, p. e1356, 2020.
  * [23] E. Krasanakis, E. Spyromitros-Xioufis, S. Papadopoulos, and Y. Kompatsiaris, “Adaptive sensitive reweighting to mitigate bias in fairness-aware classification,” in _Proceedings of the 2018 world wide web conference_ , 2018, pp. 853–862.
  * [24] D. Pedreschi, S. Ruggieri, and F. Turini, “Measuring discrimination in socially-sensitive decision records,” in _Proceedings of the 2009 SIAM international conference on data mining_. SIAM, 2009, pp. 581–592.
  * [25] T. Calders and S. Verwer, “Three naive bayes approaches for discrimination-free classification,” _Data mining and knowledge discovery_ , vol. 21, pp. 277–292, 2010.
  * [26] T. Baumann, O. Buß, and D. Schlangen, “Evaluating the potential utility of asr n-best lists for incremental spoken dialogue systems,” _Proceedings of Interspeech 2009_ , 2009.
  * [27] J. Verhasselt and H. Dercks, “N-best list rescoring in speech recognition,” _Acoustical Society of America Journal_ , vol. 128, no. 6, p. 3828, 2010.
  * [28] M. D. Breitenstein, F. Reichlin, B. Leibe, E. Koller-Meier, and L. Van Gool, “Robust tracking-by-detection using a detector confidence particle filter,” in _2009 IEEE 12th International Conference on Computer Vision_. IEEE, 2009, pp. 1515–1522.
  * [29] H. Erel, E. Carsenti, and O. Zuckerman, “A carryover effect in hri: Beyond direct social effects in human-robot interaction,” in _2022 17th ACM/IEEE International Conference on Human-Robot Interaction (HRI)_. IEEE, 2022, pp. 342–352.


# Discriminative Diffusion Models
as Few-shot Vision and Language Learners

Xuehai He1 Weixi Feng2 Tsu-Jui Fu2 Varun Jampani3 Arjun Akula3
Pradyumna Narayana3 Sugato Basu3 William Yang Wang2 Xin Eric Wang1
1UC Santa Cruz, 2UC Santa Barbara, 3Google
<EMAIL_ADDRESS>
<EMAIL_ADDRESS>
<EMAIL_ADDRESS>

###### Abstract

Diffusion models, such as Stable Diffusion (Rombach et al., 2022a), have shown
incredible performance on text-to-image generation. Since text-to-image
generation often requires models to generate visual concepts with fine-grained
details and attributes specified in text prompts, can we leverage the powerful
representations learned by pre-trained diffusion models for discriminative
tasks such as image-text matching? To answer this question, we propose a novel
approach, Discriminative Stable Diffusion (DSD), which turns pre-trained text-
to-image diffusion models into few-shot discriminative learners. Our approach
uses the cross-attention score of a Stable Diffusion model to capture the
mutual influence between visual and textual information and fine-tune the
model via attention-based prompt learning to perform image-text matching. By
comparing DSD with state-of-the-art methods on several benchmark datasets, we
demonstrate the potential of using pre-trained diffusion models for
discriminative tasks with superior results on few-shot image-text matching.

##  1 Introduction

> _“What I Cannot Create, I Do Not Understand.”_
>
> Richard Feynman

This quote by Richard Feynman perfectly captures the essence of human learning
techniques. In the context of machine learning, it can be interpreted as the
ability to generate images given text prompts is a strong indicator of
understanding and matching between visual and textual information. Despite the
success of various methods in the image-text matching task (Karpathy and Fei-
Fei, 2015; Lee et al., 2018), there is still a need for more advanced models
that can better capture the fine-grained details, spatial relationships, and
compositionality. Meanwhile, diffusion models (Sohl-Dickstein et al., 2015a;
Rombach et al., 2022a) have been shown to produce high-quality and diverse
images from text descriptions. Therefore, in this paper, we investigate the
idea of leveraging the power of pre-trained Diffusion Models, specifically the
state-of-the-art text-to-image generative model—Stable Diffusion (Rombach et
al., 2022a), for the discriminative image-text matching task, as shown in
Figure 1. The success of Stable Diffusion in generative tasks suggests that it
has a strong understanding of the relationship between visual and textual
information, and we aim to harness this understanding for image-text matching
tasks.

Figure 1:  The upper subfigure in the teaser image illustrates the ability of
Stable Diffusion to generate realistic images given a text prompt. The bottom
subfigure illustrates the process of our proposed method, Discriminative
Stable Diffusion (DSD), for utilizing Stable Diffusion for the image-text
matching task. DSD can output a matching score for a given text prompt and
image, with a higher score indicating a stronger match.

The key advantages of using Stable Diffusion for text-image alignment are two
folds: first, Stable Diffusion uses a pre-trained Variational Autoencoder
(VAE) (Kingma and Welling, 2013) and cross-attention layers in its
architecture, which provides strong compressed representations and shed
information about the alignment of the data from different modalities. Second,
Stable Diffusion has the ability to understand spatial relations and fine-
grained disentangled concepts, so as to generate images per text prompts’
requests, while traditional vision and language model pre-trained on
discriminative tasks such as CLIP (Radford et al., 2021) only allows to model
image-text contextual alignment at coarse-grained contextual (global) level
but ignores the compositional matching of disentangled concepts (i.e., finer-
grained cross-modal alignment at region-word level) (Jiang et al., 2022).

However, to efficiently adapt Stable Diffusion, a pre-trained text-to-image
generation model, to the image-text matching task, two key challenges need to
be addressed: (1) how to disentangle the degree of alignment between the image
and text from the latent space of Stable Diffusion? In text-to-image
generation, the model is trained to generate an image that is semantically
consistent with a given text prompt. However, in image-text matching, the task
is to determine the degree of alignment between a given image and text.
Therefore, it is important to disentangle the degree of alignment between the
image and text in the latent space of Stable Diffusion, to effectively use it
for image-text matching; (2) how to efficiently adapt the model in the few-
shot setting. Fine-tuning a text-to-image generation model like Stable
Diffusion for image-text matching requires adapting the model from a
generative task to a discriminative task, which can be challenging.

To address these challenges, we propose the Discriminative Stable Diffusion
(DSD) method, which includes two key ideas: (1) identifying and leveraging
attention scores from the selected cross-attention maps as the matching score
and (2) using attention-based prompt learning to fine-tune the attention
matrices. DSD can outperform the CLIP-based methods by 2.09% on the
Compositional Visual Genome and 3.19% on the RefCOCOg datasets in terms of
accuracy under the few-shot setting. Our approach reveals the potential of
diffusion models that can broaden their scope of use to discriminative tasks.

Our contributions in this paper are threefold:

  * •

We do a pioneer study using latent text-to-image generation diffusion models
which are initially proposed for generative tasks to address discriminative
tasks such as image-text matching.

  * •

We propose a new method based on exploiting the use of cross-attention maps of
Stable Diffusion across layers and attention-based prompt learning for solving
the image-text matching task.

  * •

We demonstrate the effectiveness of our approach through experimental
evaluation under the few-shot setting on both the Compositional Visual Genome
(Jiang et al., 2022) and the RefCOCOg (Yu et al., 2016) datasets for image-
text matching. We also extend our method to the visual question answering
task, demonstrating its potency on the VQAv2 (Antol et al., 2015) dataset.

##  2 Related Work

Diffusion Probabilistic Models (DPMs) Diffusion probabilistic models (DPMs)
have been widely used as generative models for images in recent years. These
models, which include diffusion (Sohl-Dickstein et al., 2015b) and score-based
generative models (Song and Ermon, 2019), have been shown to outperform
generative adversarial networks (GANs) (Goodfellow et al., 2014) in many
cases. In the past two years, significant progress has been made in the
development of DPMs, with a focus on improving sampling techniques such as
classifier-free guidance (Ho and Salimans, 2021). DPMs are typically
implemented using convolutional U-Net architectures (Ronneberger et al.,
2015a) which contain cross-attention layers. Hertz et al. (2022) finds that
replacing attention maps in the cross-attention module of text-to-image
generation diffusion models can edit image attributes. Just scaling the
attention maps of the respective word can adjust the effect of a particular
word in the prompt. Feng et al. (2022) demonstrates that one can retain the
compositional semantics in the generated image by manipulating the cross-
attention. Kumari et al. (2022) proposes to fine-tune the key and value
mapping from text to latent features in the cross-attention layers of text-to-
image diffusion model to compose multiple new concepts in the image. In the
context of image-text matching, the attention scores between the text and
image representations in the DPMs can reflect the degree of alignment between
them.

Few-shot Learning for Vision and language Tasks Vision and Language
discriminative models pre-trained on large-scale image-text pairs have
demonstrated great potential in multimodal representation learning (Jia et
al., 2021; Yao et al., 2021; Yuan et al., 2021; Radford et al., 2021; He et
al., 2022a). Among them, CLIP (Radford et al., 2021) benefits from 400M
curated data and defines various prompt templates to carry out zero-shot image
classification. Like CLIP, several different few-shot learners were proposed.
GPT (Brown et al., 2020), as a strong few-shot learner, is capable of
performing a new language task by learning from only a few training instances.
Frozen (Tsimpoukelli et al., 2021) is developed based on GPT and made into a
multimodal few-shot learner by expanding the soft prompting to include a
collection of images and text. The concept of prompt learning (Schick and
Schütze, 2020) has been widely explored in natural language processing (NLP)
and computer vision. It allows pre-trained models to adapt to various
downstream tasks with minimal data by introducing a small prompt layer (Schick
and Schütze, 2020; Liu et al., 2021). In the context of image-text matching,
prompt learning has been used to fine-tune pre-trained models for the task (He
et al., 2022b). In our work, instead of adding learnable prompts over the
inputs or between transformer layers (Jia et al., 2022), we introduce
learnable prompts over the attention layers.

Generative Models for Discriminative Tasks There has been a significant amount
of research on using generative models for discriminative tasks in natural
language processing and machine learning. One approach is to fine-tune the
model on a discriminative task. Dai et al. (2021) fine-tunes a large
transformer-based language model on several discriminative tasks, including
named entity recognition and machine translation, and show that fine-tuning
the model leads to improved performance compared to using the model in a
generative only setting. Yang et al. (2019) proposes a model that combines a
generative model for text generation with a discriminative model for sentiment
analysis. Wang et al. (2018) proposes a method for adapting a generative model
for language translation to perform the discriminative task of language
classification, and shows that the adapted model outperforms strong baselines
on the language classification task. Other examples of using generative models
for discriminative tasks include using a generative model to initialize a
discriminative model for improved performance (Mao et al., 2019), and using a
generative model to pre-train a discriminative model for domain adaptation
(Chen et al., 2020b). For diffusion models, recently, Li et al. (2023); Clark
and Jaini (2023) propose to use pre-trained diffusion models for zero-shot
classification. Wei et al. (2023) formulates diffusion models as masked
autoencoders and achieves state-of-the-art classification accuracy on video
tasks.

##  3 Preliminaries on Diffusion Models

In this section, we provide a brief overview of the concepts and techniques in
denoising diffusion models that are necessary to understand our proposed
method. Diffusion models are a class of generative models that are
particularly effective at generating high-quality images (Sohl-Dickstein et
al., 2015b; Nichol et al., 2021; Ramesh et al., 2022; Saharia et al., 2022;
Rombach et al., 2022b). They aim to model a distribution
$p_{\theta}\left(x_{0}\right)$ that approximates the data distribution
$q\left(x_{0}\right)$ and is easy to sample from. DPMs model a "forward
process" in the space of $x_{0}$ from data to noise by adding noise to real
data, and a reverse process that tries to reconstruct the original data from
the noisy version. The forward process is described by the equation

$q(x_{t}|x_{0})=\mathcal{N}(x_{t};\sqrt{\bar{\alpha}_{t}}x_{0},(1-\bar{\alpha}_{t})\mathbf{I}),$
(1)

where $x_{1:T}$ defines a set of noisy images and $x_{0}$ is the initial
image. $\mathcal{N}$ denotes a Gaussian distribution, and $\bar{\alpha}_{t}$
are hyperparameters. The reverse process is modeled by a Gaussian distribution

$p_{\theta}(x_{t-1}|x_{t})=\mathcal{N}(\mu_{\theta}(x_{t}),\Sigma_{\theta}(x_{t})),$
(2)

where neural networks are used to predict the mean and covariance of the
distribution. The parameters of the model, $\theta$, are learned by optimizing
a variational lower bound on the log-likelihood of the real data. Once
trained, new images can be generated by starting from a noise sample and
iteratively sampling from the reverse process distribution until reaching the
final time step. In latent diffusion probabilistic models such as Stable
Diffusion, this two process are similar, while they proceeds in the latent
space: $x_{0}$ is encoded into $z_{0}$ in an efficient, low-dimensional latent
space first and then do the diffusion process. And in the case where a DPM is
conditioned on additional information, such as text information $c$, the
reverse process becomes $p_{\theta}(z_{t-1}|z_{t},y)$, where $y$ is the input
text.

##  4 Discriminative Latent Diffusion Models

Figure 2: Overview of our Discriminative Stable Diffusion framework, which
measures how much the given images and texts matched use the cross-attention
mechanism in the Stable Diffusion. Discriminative Stable Diffusion added
input-dependent prompt embeddings over attention matrices (red boxes). We then
fine-tune the prompt under the few-shoot setting.

###  4.1 Problem Formulation

The problem of image-text matching is formalized as follows: given a text
prompt $y\in\mathcal{Y}$ and a set of images $\mathcal{X}$, we aim to find the
image $x^{*}\in\mathcal{X}$ that is most semantically aligned with the given
text prompt $y$. Formally, we define the image-text matching problem as
finding the function $f:\mathcal{Y}\times\mathcal{X}\rightarrow[0,1]$ that
assigns a score to each image-text pair $(y,x)$ indicating the degree of
semantic alignment between the text and image. The goal is to find the image
$x$ that maximizes the score for a given text prompt $y$, i.e.,
$x^{*}=\arg\max_{x\in\mathcal{X}}f(y,x).$

###  4.2 Method Overview

To learn the function $f$, the main idea is to leverage the powerful
representations learned by a pre-trained Stable Diffusion model to perform
image-text matching. There are three key modules in DSD, cross-attention score
computation, LogSumExp pooling, and attention-based prompt learning, as shown
in Figure 2. The cross-attention score computation module extracts the mutual
influence between visual and textual information by computing the attention
scores from the cross-attention matrix in U-Nets of the Stable Diffusion
model. The LogSumExp pooling module pools these attention scores over all
tokens in the text description to obtain a single matching score. Finally, the
attention-based prompt learning module fine-tunes the model by updating the
key and value mappings from text to latent features in the cross-attention
layers under a few-shot setting. This allows the model to learn new image-text
concepts while retaining the ability to capture complex and nuanced
relationships between images and text. The model outputs a score that measures
the alignment between the image and text, which can be used to adapt the model
from a text-to-image generation task to an image-text matching task.

###  4.3 Cross-attention Score Computation

Cross-attention scores are a measure of the relevance of an image and a text
to each other (Chen et al., 2020a; Li et al., 2019). They are calculated by
taking the dot product of the representations of the image and text in a
latent space, and normalizing by the product of their norms. We propose to
adapt cross-attention scores as a way to better capture the complex
relationships between images and text in the image-text matching task. In the
sequel, we elaborate on our strategy in depth.

Stable Diffusion (Jaegle et al., 2021) is trained to generate images from text
prompts, and as such, it has learned strong compressed representations of both
text and images. We can make use of these representations to learn the
function $f$ for image-text matching.

More specifically, given a text prompt $y$, we first encode it into a
intermediate text representation $r_{y}=\tau_{\theta}(y)\in\mathbb{R}^{M\times
d_{\tau}}$ using the domain specific encoder $\tau_{\theta}$. We then encode
each image $x\in\mathcal{X}$ where $x\in\mathbb{R}^{H\times W\times 3}$ in RGB
space into a latent image representation $z=\mathcal{E}(x)$, where
$\mathcal{E}(x)$ is the encoder. The encoder $\epsilon_{\theta}$ in the U-Net
(Ronneberger et al., 2015b) of the pre-trained text-to-image generation model
then encode $z$ into $r_{x}=\varphi_{i}\left(z_{t}\right)$, where
$\varphi_{i}\left(z_{t}\right)\in\mathbb{R}^{N\times d_{\epsilon}^{i}}$
denotes a (flattened) intermediate representation of the UNet implementing
$\epsilon_{\theta}$, which are then mapped to intermediate layers of the UNet
via a cross-attention layer implementing
$A=\operatorname{softmax}\left(\frac{QK^{T}}{\sqrt{d}}\right)\cdot V$, with
$Q=W^{q^{(i)}}\cdot r_{x},K=W^{k^{(i)}}\cdot r_{y},V=W^{v^{(i)}}\cdot r_{y}.$
Here, $W^{v^{(i)}}\in\mathbb{R}^{d\times
d_{\epsilon}^{i}},W^{q^{(i)}}\in\mathbb{R}^{d\times
d_{\tau}},W^{k^{(i)}}\in\mathbb{R}^{d\times d_{\tau}}$ are learnable
projection matrices (Jaegle et al., 2021; Vaswani et al., 2017), mapping the
inputs to a query, key, and value feature, respectively, and $d^{\prime}$ is
the output dimension of key and query features.

###  4.4 LogSumExp Pooling (LSE)

To compute the function $g$ and quantitatively evaluate the degree of semantic
alignment between an image and a text prompt, we leverage LogSumExp (LSE)
pooling (Blanchard et al., 2021) as a means of aggregating the attention maps
generated by the cross-attention mechanism in our model. By using LSE pooling,
we are able to take into account the relative importance of different image
and text tokens in the attention map, rather than simply averaging or summing
all elements in the map. This has several benefits. Firstly, LSE pooling is
able to handle large values and outliers in the attention map more robustly
than other pooling methods, such as average or sum pooling. Secondly, LSE
pooling is able to better preserve the ordering of values in the attention
map, allowing for more interpretable and accurate matching scores.

The attention map matrix is denoted as $A\in\mathbb{R}^{n\times m}$, where $n$
and $m$ are the number of image and text tokens, respectively. The LSE pooling
operator is defined as:

$S(A)=\frac{1}{\lambda}\log\left(\sum_{i=1}^{n}\exp\left(\lambda
A_{i,:}\right)\right)$ (3)

Where $A_{i,:}$ represents the $i$-th row of the matrix $A$. $\lambda$ is a
factor that determines how much to magnify the importance of the most relevant
pairs of image region features and attended text sentence vectors. The LSE
pooling operator computes the log sum of the exponentiated values in each row
of the matrix, resulting in a vector of length $m$.

The score for the image-text pair $(y,x)$ is then computed by averaging
across-attention maps, denoted by

$f(y,x)=\text{Ave}(S(A))=g(A)$ (4)

where $g:\mathbb{R}^{M\times d}\times\mathbb{R}^{N\times d}\rightarrow[0,1]$
is a scoring function that measures the degree of semantic alignment between
the text and image representations.

Overall, our method combines the strengths of the U-Net architecture and the
attention mechanism of the Stable Diffusion model. We resort to attention-
based prompt learning (Lester et al., 2021) to efficiently adapt the model to
perform image-text matching.

###  4.5 Attention-based Prompt Learning for Stable Diffusion

We aim to adapt the latent diffusion probabilistic model to the image-text
matching task leveraging only a few examples, that is, under the few-shot
setting. The task of fine-tuning aims at updating the mapping from the given
text to the aligned image distribution, and the text features are only input
to $W^{k}$ and $W^{v}$ projection matrix in the cross-attention block.
Therefore, we propose the use of learnable prompts, which are added to the
attention matrices in our model. Specifically, as shown in Figure 2, we
introduce learnable prompt embedding matrices, which are added element-wise to
the key and value attention matrices at the identified layer of the Stable
Diffusion model. Let $m_{\theta}(\cdot)$ denote the mapping network
parameterized by $\theta$, each prompt token is now obtained by $p(x)=p+\pi$
where $\pi=m_{\theta}(x)$ so that the prompt will be conditional on the input
image. As our prompt addition operation applies to all layers and sampled
time-steps, we will omit superscripts $t$ and layer $l$ for notational clarity
and obtains:

$W^{k^{\prime}}=W^{k}+p_{k}(x),W^{v^{\prime}}=W^{v}+p_{v}(x).$ (5)

Both $p_{k}(x)$ and $p_{v}(x)$ are updated during training. This allows the
model to adapt to new instances by attending to relevant information in the
intermediate representation of the text inputs, $\tau_{\theta}(y)$. With the
learned prompt embeddings in the few-shot scenario, we can effectively adapt
the Stable Diffusion to image-text matching performance. The overall algorithm
is shown in Algorithm 1. For optimization, we use the cross-entropy loss
function between the predicted match score and the true match score. Let $L$
be the cross-entropy loss, we have:

$L=-\frac{1}{N}\sum_{n=1}^{N}\left(y_{n}\log\hat{y}_{n}+(1-y_{n})\log(1-\hat{y}_{n})\right)$
(6)

where $N$ is the number of samples in the training dataset and $y_{n}$ and
$\hat{y}_{n}$ are the true and predicted match scores for the $n$-th sample,
respectively.

Algorithm 1 Image-Text Matching with Discriminative Stable Diffusion

1:$\mathbb{I}$: Image space

2:$\mathbb{T}$: Text space

3:$x$: Image

4:$y$: Text

5:$z$: Latent representation

6:$\mathcal{E}$: Encoder

7:$\tau$: Domain-specific encoder

8:$\varphi$: Intermediate representation of the U-Net

9:function $\mathcal{{\color[rgb]{0,0,1}DSD}}$($\mathbb{I},\mathbb{T}$)

10: for $(i,t)$ in the batch do

11: Image latent representation $z_{i}\\!\leftarrow\\!\mathcal{E}(x)$

12: Text latent representation $r_{y}\\!\leftarrow\\!\tau(y)$

13: Intermediate representation $r_{x}\\!\leftarrow\\!\varphi(z_{t})$

14: Add prompts $W^{k^{\prime}}\leftarrow W^{k},W^{v^{\prime}}\leftarrow
W^{v}$ $\triangleright$ Eq. 5

15: Compute attention maps $A\leftarrow r^{y},r^{x}$

16: Compute LSE score $S(A)\\!\leftarrow\\!A$ $\triangleright$ Eq. 3

17: Compute matching score $g(A)\leftarrow A$ $\triangleright$ Eq. 4

18: Compute loss $L\leftarrow\hat{y}_{n}$ $\triangleright$ Eq. 6

19: Update $p_{k}(x),p_{v}(x)$

20: end for

21:end function

Dynamic Attention Head Weighting We propose a method for adjusting the weights
of different heads in the cross-attention of our model. The classifier-free
guidance (Ho and Salimans, 2022) is a technique that encourages the DPM to
sample images that are likely to belong to a certain class by using the
gradient of the log probability of the class given the image. This is done by
adding a term to the loss function that is proportional to the gradient of the
log probability of the class given the image. Inspired by them, where they use
classifier-free guidance for diffusion models and demonstrate that it is
possible to guide the learning of diffusion models by using a representation
that is learned to maximize the mutual information between the input and the
output, we compute the gradient of the output of each attention head with
respect to the input, and use this gradient to weight the contribution of each
head to the final output of the model as follows:

$\operatorname{Attr}_{h}(A)=A_{h}\odot\sum_{k=1}^{H}\frac{\partial{f}(A)}{\partial
A_{h}},$ (7)

where $\odot$ is element-wise multiplication, $A_{h}\in\mathbb{R}^{n\times m}$
denotes the $h$-th head’s attention weight matrix, and $\frac{\partial
f(A)}{\partial A_{h}}$ computes the gradient of model $\mathrm{F}(\cdot)$
along $A_{h}$. The $(i,j)$-th element of $\operatorname{Attr}_{h}(A)$ is
computed for the interaction between input token $x_{i}$ and $x_{j}$ in terms
of the $h$-th attention head. By adjusting these weights during training, we
are able to estimate the gradient flow, keep the most essential token
alignment, guide the matching process between text and image and achieve
better performance in our task.

##  5 Experiments

###  5.1 Datasets

We use the Compositional Visual Genome (ComVG) (Krishna et al., 2017) and
RefCOCOg (Yu et al., 2016) datasets to do image-text matching, which requires
model’s ability to understand fine-grained details, spatial relationships, and
compositionality of image and text pairs, that are more challenging for
traditional vison-language models.

Compositional Visual Genome (ComVG) (Krishna et al., 2017) is a reconstructed
dataset of the Visual Genome (Krishna et al., 2017) dataset, which contains
108,007 images annotated with 2.3 million relationships. These relationships
are represented as subject-predicate-object triplets and include both action
and spatial relationships. ComVG was created by selecting a subset of 542
images from Visual Genome that contain clear relationships, and generating
mutated images by changing a single value in the subject, predicate, or object
of each image. These mutated images were then used as negative examples, while
the original images were used as positive examples, resulting in a total of
5400 data points.

RefCOCOg (Yu et al., 2016) is a reconstructed dataset of the MSCOCO (Lin et
al., 2014) dataset. The dataset was created in a non-interactive setting,
using Amazon Mechanical Turk workers. The process consisted of two stages:
first, workers were asked to write referring expressions for objects in the
images, and then, another set of workers were asked to indicate the referred
object in the image by clicking on it. The final dataset includes 85,474
referring expressions for 54,822 objects in 26,711 images, with a focus on
images containing between 2 and 4 objects of the same category.

VQAv2 (Goyal et al., 2017) The VQAv2 dataset (Goyal et al., 2017) is commonly
converted to a classification task with 3,129 answer classes with frequency
large than 9. In the context of a few-shot learning scenario, we ramdomly
sample 3000 images from VQAv2 training set for training, while reserving 1,000
images from the validation set for the testing. We modify the candidate text
to be the concatenation of question and answer pair for each question and
perform matching with images.

###  5.2 Experimental Setup

We use the Stable Diffusion v2 with the xFormer (Lefaudeux et al., 2022) and
flash attention (Dao et al., 2022) implementation. The Stable Diffusion
utilizes a subset of the LAION-5B (Schuhmann et al., 2022) dataset during pre-
training, specifically 170 million examples, along with LAION-2B-en and LAION-
aesthetics v2 datasets for pre-training. On the RefCOCOg dataset, we sample 10
text prompts from the pool each time, and the model is asked to do the correct
matching given the image and the 10 sampled text prompts. We first evaluate
our method under the zero-shot setting and select the variant with the best
performance (using attention maps with dynamic attention head weighting,
averaged across all U-Net layers, and using the LogSumExp, see the ablation
studies section for details). We then test our Discriminative Stable Diffusion
under the few-shot setting using the best variant where we use 5% data to
train the model.

###  5.3 Baselines

In order to provide a comprehensive evaluation of our Discriminative Stable
Diffusion method, we establish two baselines for comparison.

  * •

Fine-tuning (Radford et al., 2021): The first baseline involves fine-tuning
the CLIP model, with the last linear head being the only component subject to
tuning.

  * •

Prompt learning: The second baseline is based on the prompt learning strategy
applied to CLIP, incorporating learnable prompts to the textual inputs that
are conditioned on individual input images, as described in Zhou et al.
(2022).

###  5.4 Results

To compare the performance of our method to other approaches, we conducted
fine-tuning on CLIP and prompting on CLIP, in addition to our method. The
results of these experiments are summarized in Table 1 on the Compositional
Visual Genome dataset and RefCOCOg dataset. In order to facilitate a fair
comparison, we adapt the use of the DSD model to two distinct settings on
ComVG, given the different resolution of Stable Diffusion and CLIP. The first
setting involves resizing all images in the dataset to a resolution of 224x224
first, and then upsampling and resizing to 512x512 for the use of DSD, so that
DSD will not take advantage of its higher input resolution. The results are
shown in Table 1. The other setting involves directly resizing all images to
224x224 and 512x512 base resolution, which is employed by CLIP and Stable
Diffusion, with the results shown in the Supplements.

Method | Compositional Visual Genome | RefCOCOg
---|---|---
Subjects | Objects | Predicate | Average | Top-1 Acc. | Top-5 Acc.
CLIP (Fine-tuning) | 80.77 | 82.49 | 60.50 | 76.10 | 69.88 | 84.57
CLIP (Prompt Learning) | 78.88 | 79.51 | 60.41 | 74.24 | 69.40 | 84.48
DSD | 80.81 | 83.17 | 63.51 | 78.11 | 73.07 | 84.91

Table 1: Comparison of accuracy (%) on Compositional Visual Genome (ComVG) and Top-1 and Top-5 accuracy (%) on RefCOCOg using CLIP and Discriminative Stable Diffusion (DSD) under the few-shot setting. Our method outperforms CLIP based baselines, demonstrating the superiority of our approach compared with traditional vision and language pre-trained models such as CLIP pre-trained for discriminative tasks. Method | Binary | Other | All
---|---|---|---
CLIP (Fine-tuning) | 66.94 | 5.10 | 17.86
CLIP (Prompt Learning) | 67.32 | 5.16 | 18.03
DSD | 70.59 | 6.77 | 20.43
Table 2: Comparison of accuracy (%) on the VQAv2 dataset under the few-shot
setting. Our method outperforms CLIP, demonstrating the superiority of our
approach compared with traditional vision and language pre-trained models pre-
trained on other vision and language tasks.

From the results, it is clear that our method outperforms both fine-tuning on
CLIP and prompt learning on CLIP on both Compositional Visual Genome across
the three different problem types and RefCOCOg.

We also show the results on the VQAv2 dataset in Table 2. These results
demonstrate the extentiveness of our method to other vision and language
tasks.

###  5.5 Ablation Studies

Effect of Attention Maps from Different Sets of U-Net Layers We investigate
the effect of using different numbers of layers in the U-Net of Stable
Diffusion to compute the attention map. We use two variants of Stable
Diffusion v2 and take the average of the attention maps from different layers
of U-Net. Specifically, we consider the last one, the last two, the last
eight, and all layers. The later layers in the U-Net may contain similar task-
specific information, which can be found from observing that using only the
last two layers also provides a close performance with that of one. The
results, shown in Figure 4, indicate that using all layers in the U-Net gives
the best performance in terms of accuracy, suggesting that the information
from all layers can make use of both the high-level and low-level features
when making predictions and preserve both coarse and fine image details for
the image-text matching task.

Figure 3: Ablation study on the number of attention maps used from layers of
the U-Net (x-axis). Tests on two variants of Stable-Diffusion v2: trained as a
standard noise-prediction model on 512x512 images and 768x768 images.

Figure 4: Ablation study on the number of attention heads (five in total
within the Stable Diffusion) in the U-Net (x-axis) with few-shot performance
(y-axis) under the two scenarios: using the average of all attention maps and
using our dynamic attention head weighting method. The results illustrate the
superiority of our weighting method.

Cosine Similarity vs. Maximum vs. LogSumExp Pooling for Score Computation We
compare the overall accuracy of using Cosine Similarity, Maximum value from
the attention map, and LogSumExp Pooling for score computation in Figure 6. As
can be seen, using LogSumExp performs the best, followed by the maximum value,
and finally the cosine similarity. This suggests that LogSumExp can
effectively capture the overall importance of each element in the attention
map, rather than just relying on the maximum value. Additionally, LogSumExp
can smooth out the influence of individual noisy elements, resulting in more
robust and accurate matching scores. As the dimensions of the image feature
and text feature vectors in Stable Diffusion are not the same, we implement
the cosine similarity by only comparing the shared dimensions of the image and
text feature vectors. Overall, these results highlight the effectiveness of
LogSumExp as a method for computing the matching score in image-text matching
tasks.

Effect of Dynamic Attention Head Weighting We investigate the effect of using
varying numbers of attention heads in Stable Diffusion on image-text matching
performance. To do this, we randomly sample a subset of heads and leave the
rest unused. The results, as shown in Figure 4, indicate that using all heads
(five in total in Stable Diffusion v2) from the Spatial Transformer (Jaderberg
et al., 2015) located in the U-Net of Stable Diffusion performs the best.
Additionally, we compare the results of using dynamic attention head weighting
versus averaging across selected heads (few-shot) to evaluate the impact of
attention head selection on performance, and dynamic attention head weighting
performs better.

Figure 5: Ablation study on using cosine similarity, maximum value from each
column of the attention map, and the smoothed maximum (LogSumExp pooling).

Figure 6: Ablation study on the amount of noise added during the diffusion
process: using consistent noise levels of 0.4, 0.8 and using ensembling.

Ensembling over Noise Levels In diffusion models, the level of noise controls
the variance of the Gaussian noise added during the diffusion process, which
can affect the degree of change in the generated image. To further improve the
performance of our method, we use an ensemble technique inspired by Wolleb et
al. (2022) by averaging the scores over four different noise levels: {0.2,
0.4, 0.6, 0.8}. This is done by first obtaining the score under each noise
level scenario and then averaging them. The results of this comparison are
shown in Figure 6. Our experimental results demonstrate that this ensemble
technique leads to a noticeable improvement in performance.

##  6 Conclusion

In this paper, we proposed a method for aligning text and images in the latent
space of Stable Diffusion. During experiments, we found that VAE determines
the quality of the pixel space, while U-Net and CLIP determine the quality of
the semantic space. Therefore, we fine-tuned the U-Net part of the model by
focusing on the cross-attention between CLIP embeddings and encoder
embeddings, which reflects the alignment between text and image. Our results
show that this fine-tuning approach improves the alignment between text and
image, leading to better performance in image-text matching tasks. Overall,
our proposed method demonstrates a promising approach for aligning text and
images in the latent space of Stable Diffusion, and has the potential to be
widely adopted in these areas. Our results motivate research on simpler
alternatives to adapt Stable Diffusion models, as well as on future methods
for better utilization of them.

## References

  * Antol et al. (2015) Stanislaw Antol, Aishwarya Agrawal, Jiasen Lu, Margaret Mitchell, Dhruv Batra, C Lawrence Zitnick, and Devi Parikh. 2015.  Vqa: Visual question answering.  In _ICCV_.
  * Blanchard et al. (2021) Pierre Blanchard, Desmond J Higham, and Nicholas J Higham. 2021.  Accurately computing the log-sum-exp and softmax functions.  _IMA Journal of Numerical Analysis_ , 41(4):2311–2330.
  * Brown et al. (2020) Tom B Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah, Jared Kaplan, Prafulla Dhariwal, Arvind Neelakantan, Pranav Shyam, Girish Sastry, Amanda Askell, et al. 2020.  Language models are few-shot learners.  _arXiv preprint arXiv:2005.14165_.
  * Chen et al. (2020a) Yen-Chun Chen, Linjie Li, Licheng Yu, Ahmed El Kholy, Faisal Ahmed, Zhe Gan, Yu Cheng, and Jingjing Liu. 2020a.  Uniter: Universal image-text representation learning.  In _European conference on computer vision_ , pages 104–120. Springer.
  * Chen et al. (2020b) Yizhe Chen, Nan Duan, Wenbing Hu, Defeng Lian, Zhoujun Lu, Bin Xu, Yongyang Liu, Bing Dai, Ruslan Salakhutdinov, and Quoc Le. 2020b.  Pre-training with conditional generative adversarial networks for domain adaptation.  _Transactions of the Association for Computational Linguistics_ , 8:237–249.
  * Clark and Jaini (2023) Kevin Clark and Priyank Jaini. 2023.  Text-to-image diffusion models are zero-shot classifiers.  _arXiv preprint arXiv:2303.15233_.
  * Dai et al. (2021) Zhilin Dai, Zihang Yang, Alec Fan, Jing Gao, Jaime Carbonell, and Quoc Le. 2021\.  Fine-tuning generative models for discriminative tasks.  In _Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing_. Association for Computational Linguistics.
  * Dao et al. (2022) Tri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, and Christopher Ré. 2022.  FlashAttention: Fast and memory-efficient exact attention with IO-awareness.  In _Advances in Neural Information Processing Systems_.
  * Feng et al. (2022) Weixi Feng, Xuehai He, Tsu-Jui Fu, Varun Jampani, Arjun Akula, Pradyumna Narayana, Sugato Basu, Xin Eric Wang, and William Yang Wang. 2022.  Training-free structured diffusion guidance for compositional text-to-image synthesis.  _arXiv preprint arXiv:2212.05032_.
  * Goodfellow et al. (2014) Ian Goodfellow, Jean Pouget-Abadie, Mehdi Mirza, Bing Xu, David Warde-Farley, Sherjil Ozair, Aaron Courville, and Yoshua Bengio. 2014.  Generative adversarial nets.  In _Advances in neural information processing systems_ , pages 2672–2680.
  * Goyal et al. (2017) Yash Goyal, Tejas Khot, Douglas Summers-Stay, Dhruv Batra, and Devi Parikh. 2017\.  Making the v in vqa matter: Elevating the role of image understanding in visual question answering.  In _CVPR_.
  * He et al. (2022a) Xuehai He, Chunyuan Li, Pengchuan Zhang, Jianwei Yang, and Xin Eric Wang. 2022a.  Parameter-efficient fine-tuning for vision transformers.  _arXiv preprint arXiv:2203.16329_.
  * He et al. (2022b) Xuehai He, Diji Yang, Weixi Feng, Tsu-Jui Fu, Arjun Akula, Varun Jampani, Pradyumna Narayana, Sugato Basu, William Yang Wang, and Xin Eric Wang. 2022b.  Cpl: Counterfactual prompt learning for vision and language models.  _arXiv preprint arXiv:2210.10362_.
  * Hertz et al. (2022) Amir Hertz, Ron Mokady, Jay Tenenbaum, Kfir Aberman, Yael Pritch, and Daniel Cohen-Or. 2022.  Prompt-to-prompt image editing with cross attention control.  _arXiv preprint arXiv:2208.01626_.
  * Ho and Salimans (2021) Jonathan Ho and Tim Salimans. 2021.  Classifier-free diffusion guidance.  In _NeurIPS 2021 Workshop on Deep Generative Models and Downstream Applications_.
  * Ho and Salimans (2022) Jonathan Ho and Tim Salimans. 2022.  Classifier-free diffusion guidance.  _arXiv preprint arXiv:2207.12598_.
  * Jaderberg et al. (2015) Max Jaderberg, Karen Simonyan, Andrew Zisserman, et al. 2015.  Spatial transformer networks.  _Advances in neural information processing systems_ , 28.
  * Jaegle et al. (2021) Andrew Jaegle, Felix Gimeno, Andy Brock, Oriol Vinyals, Andrew Zisserman, and Joao Carreira. 2021.  Perceiver: General perception with iterative attention.  In _International conference on machine learning_ , pages 4651–4664. PMLR.
  * Jia et al. (2021) Chao Jia, Yinfei Yang, Ye Xia, Yi-Ting Chen, Zarana Parekh, Hieu Pham, Quoc Le, Yun-Hsuan Sung, Zhen Li, and Tom Duerig. 2021.  Scaling up visual and vision-language representation learning with noisy text supervision.  In _International Conference on Machine Learning_ , pages 4904–4916. PMLR.
  * Jia et al. (2022) Menglin Jia, Luming Tang, Bor-Chun Chen, Claire Cardie, Serge Belongie, Bharath Hariharan, and Ser-Nam Lim. 2022.  Visual prompt tuning.  _arXiv preprint arXiv:2203.12119_.
  * Jiang et al. (2022) Kenan Jiang, Xuehai He, Ruize Xu, and Xin Eric Wang. 2022.  Comclip: Training-free compositional image and text matching.  _arXiv preprint arXiv:2211.13854_.
  * Karpathy and Fei-Fei (2015) Andrej Karpathy and Li Fei-Fei. 2015.  Deep visual-semantic alignments for generating image descriptions.  In _Proceedings of the IEEE conference on computer vision and pattern recognition_ , pages 3128–3137.
  * Kingma and Welling (2013) Diederik P Kingma and Max Welling. 2013.  Auto-encoding variational bayes.  _arXiv preprint arXiv:1312.6114_.
  * Krishna et al. (2017) Ranjay Krishna, Yuke Zhu, Oliver Groth, Justin Johnson, Kenji Hata, Joshua Kravitz, Stephanie Chen, Yannis Kalantidis, Li-Jia Li, David A Shamma, et al. 2017\.  Visual genome: Connecting language and vision using crowdsourced dense image annotations.  _International journal of computer vision_ , 123(1):32–73.
  * Kumari et al. (2022) Nupur Kumari, Bingliang Zhang, Richard Zhang, Eli Shechtman, and Jun-Yan Zhu. 2022\.  Multi-concept customization of text-to-image diffusion.  _arXiv preprint arXiv:2212.04488_.
  * Lee et al. (2018) Kuang-Huei Lee, Xi Chen, Gang Hua, Houdong Hu, and Xiaodong He. 2018.  Stacked cross attention for image-text matching.  In _Proceedings of the European Conference on Computer Vision (ECCV)_ , pages 201–216.
  * Lefaudeux et al. (2022) Benjamin Lefaudeux, Francisco Massa, Diana Liskovich, Wenhan Xiong, Vittorio Caggiano, Sean Naren, Min Xu, Jieru Hu, Marta Tintore, Susan Zhang, Patrick Labatut, and Daniel Haziza. 2022.  xformers: A modular and hackable transformer modelling library.  https://github.com/facebookresearch/xformers.
  * Lester et al. (2021) Brian Lester, Rami Al-Rfou, and Noah Constant. 2021.  The power of scale for parameter-efficient prompt tuning.  _arXiv preprint arXiv:2104.08691_.
  * Li et al. (2023) Alexander C Li, Mihir Prabhudesai, Shivam Duggal, Ellis Brown, and Deepak Pathak. 2023.  Your diffusion model is secretly a zero-shot classifier.  _arXiv preprint arXiv:2303.16203_.
  * Li et al. (2019) Liunian Harold Li, Mark Yatskar, Da Yin, Cho-Jui Hsieh, and Kai-Wei Chang. 2019\.  Visualbert: A simple and performant baseline for vision and language.  _arXiv preprint arXiv:1908.03557_.
  * Lin et al. (2014) Tsung-Yi Lin, Michael Maire, Serge Belongie, James Hays, Pietro Perona, Deva Ramanan, Piotr Dollár, and C Lawrence Zitnick. 2014.  Microsoft coco: Common objects in context.  In _ECCV_.
  * Liu et al. (2021) Xiao Liu, Kaixuan Ji, Yicheng Fu, Zhengxiao Du, Zhilin Yang, and Jie Tang. 2021\.  P-tuning v2: Prompt tuning can be comparable to fine-tuning universally across scales and tasks.  _arXiv preprint arXiv:2110.07602_.
  * Mao et al. (2019) Junhua Mao, Bin Xu, Yongyang Liu, Yiming Yang, Bing Dai, Ruslan Salakhutdinov, and Quoc Le. 2019.  Improving discriminative models with generative pretraining.  In _Advances in Neural Information Processing Systems_.
  * Nichol et al. (2021) Alexander Quinn Nichol, Prafulla Dhariwal, Aditya Ramesh, Pranav Shyam, Pamela Mishkin, Bob McGrew, Ilya Sutskever, and Mark Chen. 2021.  Glide: Towards photorealistic image generation and editing with text-guided diffusion models.  _arXiv preprint arXiv:2112.10741_.
  * Radford et al. (2021) Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, et al. 2021.  Learning transferable visual models from natural language supervision.  _arXiv preprint arXiv:2103.00020_.
  * Ramesh et al. (2022) Aditya Ramesh, Prafulla Dhariwal, Alex Nichol, Casey Chu, and Mark Chen. 2022.  Hierarchical text-conditional image generation with clip latents.  _arXiv preprint arXiv:2204.06125_.
  * Rombach et al. (2022a) Robin Rombach, Andreas Blattmann, Dominik Lorenz, Patrick Esser, and Björn Ommer. 2022a.  High-resolution image synthesis with latent diffusion models.  In _Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition_ , pages 10684–10695.
  * Rombach et al. (2022b) Robin Rombach, Andreas Blattmann, Dominik Lorenz, Patrick Esser, and Björn Ommer. 2022b.  High-resolution image synthesis with latent diffusion models.  In _CVPR_.
  * Ronneberger et al. (2015a) Olaf Ronneberger, Philipp Fischer, and Thomas Brox. 2015a.  U-net: Convolutional networks for biomedical image segmentation.  pages 234–241.
  * Ronneberger et al. (2015b) Olaf Ronneberger, Philipp Fischer, and Thomas Brox. 2015b.  U-net: Convolutional networks for biomedical image segmentation.  In _International Conference on Medical image computing and computer-assisted intervention_ , pages 234–241. Springer.
  * Saharia et al. (2022) Chitwan Saharia, William Chan, Saurabh Saxena, Lala Li, Jay Whang, Emily Denton, Seyed Kamyar Seyed Ghasemipour, Burcu Karagol Ayan, S. Sara Mahdavi, Rapha Gontijo Lopes, Tim Salimans, Jonathan Ho, David J Fleet, and Mohammad Norouzi. 2022.  Photorealistic text-to-image diffusion models with deep language understanding.  _arXiv:2205.11487_.
  * Schick and Schütze (2020) Timo Schick and Hinrich Schütze. 2020.  Exploiting cloze questions for few shot text classification and natural language inference.  _arXiv preprint arXiv:2001.07676_.
  * Schuhmann et al. (2022) Christoph Schuhmann, Romain Beaumont, Richard Vencu, Cade Gordon, Ross Wightman, Mehdi Cherti, Theo Coombes, Aarush Katta, Clayton Mullis, Mitchell Wortsman, et al. 2022.  Laion-5b: An open large-scale dataset for training next generation image-text models.  _arXiv preprint arXiv:2210.08402_.
  * Sohl-Dickstein et al. (2015a) Jascha Sohl-Dickstein, Eric Weiss, Niru Maheswaranathan, and Surya Ganguli. 2015a.  Deep unsupervised learning using nonequilibrium thermodynamics.  In _International Conference on Machine Learning_ , pages 2256–2265. PMLR.
  * Sohl-Dickstein et al. (2015b) Jascha Sohl-Dickstein, Eric Weiss, Niru Maheswaranathan, and Surya Ganguli. 2015b.  Deep unsupervised learning using nonequilibrium thermodynamics.  In _ICML_.
  * Song and Ermon (2019) Yang Song and Stefano Ermon. 2019.  Generative modeling by estimating gradients of the data distribution.  In _NeurIPS_.
  * Tsimpoukelli et al. (2021) Maria Tsimpoukelli, Jacob L Menick, Serkan Cabi, SM Eslami, Oriol Vinyals, and Felix Hill. 2021.  Multimodal few-shot learning with frozen language models.  _Advances in Neural Information Processing Systems_ , 34:200–212.
  * Vaswani et al. (2017) Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Łukasz Kaiser, and Illia Polosukhin. 2017.  Attention is all you need.  In _NeurIPS_.
  * Wang et al. (2018) Yiming Wang, Amanpreet Singh, Jian Michael, Felix Hill, Omer Levy, and Samuel Bowman. 2018.  Adapting generative models for discriminative tasks.  In _Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing_. Association for Computational Linguistics.
  * Wei et al. (2023) Chen Wei, Karttikeya Mangalam, Po-Yao Huang, Yanghao Li, Haoqi Fan, Hu Xu, Huiyu Wang, Cihang Xie, Alan Yuille, and Christoph Feichtenhofer. 2023.  Diffusion models as masked autoencoders.  _arXiv preprint arXiv:2304.03283_.
  * Wolleb et al. (2022) Julia Wolleb, Robin Sandkühler, Florentin Bieder, Philippe Valmaggia, and Philippe C Cattin. 2022.  Diffusion models for implicit image segmentation ensembles.  In _International Conference on Medical Imaging with Deep Learning_ , pages 1336–1348. PMLR.
  * Yang et al. (2019) Zichao Yang, Zhilin Dai, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, and Quoc Le. 2019.  Combining generative and discriminative models for improved sentiment analysis.  In _Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing_. Association for Computational Linguistics.
  * Yao et al. (2021) Lewei Yao, Runhui Huang, Lu Hou, Guansong Lu, Minzhe Niu, Hang Xu, Xiaodan Liang, Zhenguo Li, Xin Jiang, and Chunjing Xu. 2021.  Filip: Fine-grained interactive language-image pre-training.  _arXiv preprint arXiv:2111.07783_.
  * Yu et al. (2016) Licheng Yu, Patrick Poirson, Shan Yang, Alexander C Berg, and Tamara L Berg. 2016\.  Modeling context in referring expressions.  In _European Conference on Computer Vision_ , pages 69–85. Springer.
  * Yuan et al. (2021) Lu Yuan, Dongdong Chen, Yi-Ling Chen, Noel Codella, Xiyang Dai, Jianfeng Gao, Houdong Hu, Xuedong Huang, Boxin Li, Chunyuan Li, et al. 2021.  Florence: A new foundation model for computer vision.  _arXiv preprint arXiv:2111.11432_.
  * Zhou et al. (2022) Kaiyang Zhou, Jingkang Yang, Chen Change Loy, and Ziwei Liu. 2022.  Conditional prompt learning for vision-language models.  _arXiv preprint arXiv:2203.05557_.