File size: 44,070 Bytes
f7198da
 
c983155
9c168fe
c983155
f450e5b
 
 
 
 
c174a7f
f450e5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac7fd1c
 
 
 
 
 
 
 
 
 
 
 
 
 
f61b68d
1833b56
f61b68d
1833b56
f61b68d
1833b56
c983155
ac7fd1c
 
f450e5b
 
 
 
 
 
ac7fd1c
f450e5b
 
 
ac7fd1c
f450e5b
 
 
ac7fd1c
f450e5b
 
 
 
 
ac7fd1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c983155
f61b68d
 
ed3c11f
 
 
 
 
 
 
f61b68d
 
f450e5b
 
 
f61b68d
 
 
 
 
 
 
 
 
ed3c11f
f61b68d
 
 
 
 
ed3c11f
 
 
 
 
 
 
 
f450e5b
f61b68d
f450e5b
f7198da
f450e5b
 
 
 
 
 
 
f61b68d
 
9c168fe
f450e5b
 
 
 
 
 
cfd13fd
 
f450e5b
cfd13fd
f450e5b
cfd13fd
f450e5b
 
 
 
 
cfd13fd
f7198da
8024608
f450e5b
cfd13fd
f450e5b
 
 
f61b68d
 
 
f450e5b
 
 
 
 
 
 
 
cfd13fd
 
f450e5b
 
 
 
 
cfd13fd
f450e5b
 
 
c174a7f
 
f450e5b
 
 
 
 
 
f61b68d
f450e5b
 
ac7fd1c
f450e5b
 
 
7c66f57
f450e5b
c174a7f
f450e5b
 
 
 
 
ac7fd1c
f450e5b
 
 
 
 
 
 
c174a7f
cfd13fd
 
c174a7f
 
 
 
 
 
 
 
 
 
 
 
 
 
f61b68d
 
f450e5b
 
 
 
 
c174a7f
cfd13fd
c174a7f
cfd13fd
c174a7f
 
 
cfd13fd
c174a7f
cfd13fd
ac7fd1c
8024608
 
cfd13fd
8024608
f450e5b
 
 
 
cfd13fd
f450e5b
 
 
 
 
ac7fd1c
f450e5b
 
 
ac7fd1c
 
 
 
 
 
 
 
 
f450e5b
 
 
 
 
 
 
 
 
 
 
f61b68d
 
ed3c11f
cfd13fd
ac7fd1c
 
 
 
 
 
 
 
 
 
 
 
 
f61b68d
 
ed3c11f
cfd13fd
 
 
 
ac7fd1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f61b68d
 
 
ed3c11f
 
f61b68d
ed3c11f
 
ac7fd1c
 
 
 
 
ed3c11f
ac7fd1c
 
 
 
 
 
 
 
 
 
 
f61b68d
 
ed3c11f
 
 
 
 
ac7fd1c
ed3c11f
 
ac7fd1c
 
ed3c11f
 
f61b68d
 
ed3c11f
ac7fd1c
ed3c11f
f61b68d
 
7c66f57
ed3c11f
 
 
 
 
 
 
 
 
f61b68d
ed3c11f
ac7fd1c
ed3c11f
 
ac7fd1c
ed3c11f
ac7fd1c
ed3c11f
ac7fd1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed3c11f
 
a5673bb
f61b68d
ed3c11f
 
 
 
 
 
ac7fd1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed3c11f
ac7fd1c
 
 
 
 
ed3c11f
f61b68d
ac7fd1c
 
 
 
 
 
ed3c11f
ac7fd1c
 
 
 
 
 
 
 
 
 
1833b56
f61b68d
 
792a0ad
f61b68d
 
 
 
b02d3db
f61b68d
 
792a0ad
f61b68d
 
 
 
 
cfbe201
7c66f57
f61b68d
 
 
 
 
 
cfbe201
f61b68d
 
 
 
 
da13380
7c66f57
f61b68d
 
 
 
 
 
da13380
f61b68d
 
 
 
1833b56
f61b68d
 
792a0ad
f61b68d
 
 
 
b02d3db
f61b68d
 
792a0ad
f61b68d
 
e5c2b37
 
f61b68d
 
 
 
 
e5c2b37
b686bc2
f61b68d
 
0fe0e6e
 
f61b68d
 
e5c2b37
6e21608
f450e5b
 
6e21608
 
 
f450e5b
 
1833b56
f61b68d
 
 
 
b02d3db
1833b56
3bbd44f
fe29958
 
1833b56
 
 
 
f61b68d
 
7c66f57
f61b68d
9c168fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
import os

import gradio as gr
from PIL import Image

_ID_CLUSTER_SCREEN_SHOTS = {
    19: ("cluster_19_of_24_unmarked_white_unmarked_man.JPG", "Cluster 19 of 24"),
    2: ("cluster_2_of_24_latinx_woman.JPG", "Cluster 2 of 24"),
    18: ("cluster_18_of_24_hispanic_nonbinary.JPG", "Cluster 18 of 24"),
    0: ("cluster_0_of_24_black_woman.JPG", "Cluster 0 of 24"),
    5: ("cluster_5_of_24_white_unmarked_latinx_man.JPG", "Cluster 5 of 24"),
    6: ("cluster_6_of_24_black_man.JPG", "Cluster 6 of 24"),
    7: ("cluster_7_of_24_pacific_indigenous_man_nonbinary.JPG", "Cluster 7 of 24"),
    3: (
        "cluster_3_of_24_native_american_stereetotypical.JPG",
        "Cluster 3 of 24",
    ),
    8: (
        "cluster_8_of_24_indigenous_nonbinary.JPG",
        "Cluster 8 of 24",
    ),
    23: (
        "cluster_23_of_24_woman_nonbinbary_indigenous.JPG",
        "Cluster 23 of 24",
    ),
    12: (
        "cluster_12_of_24_white_nonbinary.JPG",
        "Cluster 12 of 24",
    ),
    13: (
        "cluster_13_of_24_nonbinary_woman_black.JPG",
        "Cluster 13 of 24",
    ),
    15: ("cluster_15_of_24_woman_white.JPG", "Cluster 15 of 24"),
}

_BIAS_STATS_SCREEN_SHOTS = {
    "Results table: all models": "cluster_assign_24_all.png",
    "Results table: Stable Diffusion v1.4": "cluster_assign_24_sd14.png",
    "Results table: Stable Diffusion v2.": "cluster_assign_24_sd2.png",
    "Results table: Stable Diffusion Dall-E 2": "cluster_assign_24_dalle.png",
    "Comparison histogram: all professions": "all_profs_histo_24.png",
    "CEO examplars: Cluster 5": "ceo_5_of_24.png",
    "CEO examplars: Cluster 6": "ceo_6_of_24.png",
    "Results table: mental health professions, all models": "cluster_assign_mental_health_24_all.png",
    "Comparison histogram: psychologist": "psychologist_histo_24.png",
    "Comparison histogram: social worker": "social_worker_histo_24.png",
    "Comparison histogram: social assistant": "social_assistant_histo_24.png",
}


def get_images(path):
    images = [Image.open(os.path.join(path, im)) for im in os.listdir(path)]
    paths = os.listdir(path)
    return [(im, path) for im, path in zip(images, paths)]


impath_id = "images/identities"

def show_id_images(cl_id_1, cl_id_2, cl_id_3):
    img_path_1, cluster_name_1 = _ID_CLUSTER_SCREEN_SHOTS[cl_id_1]
    img_path_2, cluster_name_2 = _ID_CLUSTER_SCREEN_SHOTS[cl_id_2]
    img_path_3, cluster_name_3 = _ID_CLUSTER_SCREEN_SHOTS[cl_id_3]
    return (
        gr.update(
            value=os.path.join(impath_id, img_path_1),
            label=f"Screenshot of the Identity Exploration tool for: {cluster_name_1}",
        ),
        gr.update(
            value=os.path.join(impath_id, img_path_2),
            label=f"Screenshot of the Identity Exploration tool for: {cluster_name_2}",
        ),
        gr.update(
            value=os.path.join(impath_id, img_path_3),
            label=f"Screenshot of the Identity Exploration tool for: {cluster_name_3}",
        ),
    )


impath_bias = "images/bias"

def show_bias_images(screen_id_1, screen_id_2):
    img_path_1 = _BIAS_STATS_SCREEN_SHOTS[screen_id_1]
    img_path_2 = _BIAS_STATS_SCREEN_SHOTS[screen_id_2]
    return (
        gr.update(
            value=os.path.join(impath_bias, img_path_1),
            label=f"Screenshot of the Profession Bias Tool | {screen_id_1}",
        ),
        gr.update(
            value=os.path.join(impath_bias, img_path_2),
            label=f"Screenshot of the Profession Bias Tool | {screen_id_2}",
        ),
    )


with gr.Blocks() as demo:
    gr.Markdown(
        """
        ## Stable Bias:  Analyzing Societal Representations in Diffusion Models
        
        This is the demo page for the "Stable Bias" paper, which aims to explore and quantify social biases in text-to-image systems.
        <br/>
        This work was done by [Alexandra Sasha Luccioni (Hugging Face)](https://huggingface.co/sasha),
        [Christopher Akiki (ScaDS.AI, Leipzig University)](https://huggingface.co/cakiki), [Margaret Mitchell (Hugging Face)](https://huggingface.co/meg),
        and  [Yacine Jernite (Hugging Face)](https://huggingface.co/yjernite).
        """
    )
    gr.HTML(
        """<span style="color:red" font-size:smaller>⚠️ DISCLAIMER: the images displayed by this tool were generated by text-to-image systems and may depict offensive stereotypes or contain explicit content.</span>"""
    )
    examples_path = "images/examples"
    examples_gallery = gr.Gallery(
        get_images(examples_path),
        label="Example images generated by three text-to-image models (Dall-E 2, Stable Diffusion v1.4 and v.2).",
        show_label=True,
        elem_id="gallery",
    ).style(grid=[1, 6], height="auto")
    gr.HTML(
        """
         <p style="margin-bottom: 14px; font-size: 100%">   </p>
          """
    )

    gr.Markdown(
        """
        As AI-enabled Text-to-Image models are becoming increasingly used, characterizing the social biases they exhibit
        is a necessary first step to lowering their risk of discriminatory outcomes.
        <br/>
        We compare three such models: **Stable Diffusion v.1.4, Stable Diffusion v.2.**, and  **Dall-E 2**,
        prompting them to produce images of different *professions* and *identity characteristics*.
        <br/>
        You can explore our findings in the sections below:
        
        ### How do Diffusion Models Represent Identity?

        One of the goals of our study was to look at the ways in which pictures generated by text-to-image models depict different notions of gender and ethnicity.
        These concepts are inherently difficult to describe, however: gender and ethnicity are multi-dimensional, inter-related, and, most importantly, socially constructed:
        they cannot (and should not) be predicted based on appearance features alone.
        Since we are working with depictions of fictive humans when analyzing text-to-image model behaviors,
        we cannot rely on self-identification either to assign identity categories to individual data points.
        Instead, we develop a clustering-based method to identify relevant dataset-level trends in the generated images, as described in our companion
        [Identity Representation Demo](https://huggingface.co/spaces/society-ethics/DiffusionFaceClustering).
        You can read about how this tool helps us delineate **relevant regions** in the models' generation space, outline **stereotypical associations**,
        and understand what **markedness** has to do with biases by expanding the accordion below:
        """
    )

    with gr.Accordion("How do Diffusion Models Represent Identity?", open=False):
        gr.Markdown(
            """
            <br/>
            
            Diffusion Models are commonly used in text-conditioned image generation systems, such as Stable Diffusion or Dall-E 2.
            In those systems, a user writes a *"prompt"* as input, and receives an image that corresponds to what the prompt is describing as output.
            For example, if the user asks for a *"Photo protrait of a **scientist**"*, they expect to get an image that looks photorealistic,
            prominently features at least one person, and this person might be wearing a lab coat or safety goggles.
            A *"Photo portrait of a **carpenter**"*, on the other other hand, might be set against a background depicting wooden scaffolding or a workshop (see pictures above).
            
            At the start of this project, we found that while systems do make use of background and context cues to represent different professions,
            there were also some concerning trends about the perceived genders and ethnicities of the people depicted in these professional situations.
            After trying a few such prompts, we were left asking: why do all the people depicted in these pictures **look like white men**?
            Why do the only exceptions appear to be fast food workers and other lower wage professions?
            And finally, what could be the **consequences of such a lack of diversity** in the system outputs?

            **Look like** is the operative phrase here: the people depicted in the pictures are synthetic and so do not belong to socially-constructed groups.
            Consequently, since we cannot assign a gender or ethnicity label to each data point,
            we instead focus on dataset-level trends in visual features that are correlated with social variation in the text prompts.
            We do this through *controlled prompting* and *hierarchical clustering*: for each system,
            we obtain a dataset of images corresponding to prompts of the format *"Photo portrait of a **(identity terms)** person at work"*,
            where ***(identity terms)*** jointly enumerate phrases describing ethnicities and phrases denoting gender.
            We then cluster these images by similarity and create an [Identity Representation Demo](https://hf.co/spaces/society-ethics/DiffusionFaceClustering)
            to showcase the visual trends encoded in these clusters - as well as their relation to the social variables under consideration.
            """
        )
        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown(
                    """
                    #### [Clusters, Gender, and Ethnicity](https://hf.co/spaces/society-ethics/DiffusionFaceClustering "Select cluster to visualize to the right or go straight to the interactive demo")
                    
                    Our goal with this strategy is to trigger variation in the images that viewers will associate with social markers.
                    We use three phrases denoting genders (*man*, *woman*, *non-binary*), and 18 phrases describing ethnicities -
                    some of which are sometimes understood as similar in the US context, such as First Nations and Indigenous American.
                    We also leave the gender and ethnicity phrases unspecified in some prompts.
                    This approach has the advantage of placing visual features on a multidimensional spectrum without ascribing a prior number of distinct values for social categories or their intersection.
                    It is also limited by the training data of the models under consideration and the set of identity terms use,
                    which in our application are more relevant to the North American context than to other regions.
                    
                    How then can we use these clusters in practice?
                    Let's look for example at **cluster 2 in the 24-cluster setting**:
                    we see that most prompts for images in the cluster use the word *woman* and one of the words denoting *Hispanic* origin.
                    This tells us that images that are similar to the ones in this cluster will **likely look like** Hispanic women to viewers.
                    You can cycle through [a few other examples right](https://hf.co/spaces/society-ethics/DiffusionFaceClustering "or even better, visualize them in the app"),
                    such as cluster 19 which mostly features the words *Caucasian* and *man*, different gender term distributions for *African American* in 0 and 6,
                    as well as clusters like 7 that showcase the limitations of mapping visual features to ethnicity categories
                    by grouping together *Pacific Islander*, *Indigenous American*, and *Latino*.
                    """
                )
            with gr.Column(scale=1):
                id_cl_id_1 = gr.Dropdown(
                    choices=[2, 19, 0, 6, 7],
                    value=2,
                    show_label=False,
                )
                identity_screenshot_1 = gr.Image(
                    value=os.path.join(impath_id, "cluster_2_of_24_latinx_woman.JPG"),
                    label="Screenshot of the Identity Exploration tool for: Cluster 2 of 24",
                )
        with gr.Row():
            with gr.Column(scale=1):
                id_cl_id_2 = gr.Dropdown(
                    choices=[3, 8, 23, 12, 13],
                    value=3,
                    show_label=False,
                )
                identity_screenshot_2 = gr.Image(
                    value=os.path.join(
                        impath_id, "cluster_3_of_24_native_american_stereetotypical.JPG"
                    ),
                    label="Screenshot of the Identity Exploration tool for: Cluster 3 of 24",
                )
            with gr.Column(scale=1):
                gr.Markdown(
                    """
                    #### [Stereotypical Representations and Associations](https://hf.co/spaces/society-ethics/DiffusionFaceClustering "Select cluster to visualize to the left or go straight to the interactive demo")
                    
                    Even before we start leveraging these clusters to analyze system behaviors in other applications,
                    they already provide useful insights into some of the bias dynamics in the system.
                    In particular, they help us understand which groups are more susceptible to representation harms when using these systems,
                    especially when the models are more likely to associate them with **stereotypical representations**.
                    
                    Let's look at **clusters 3, 8, and 23** in the 24-cluster setting, as they all primarily feature images whose prompt contains a Native American identity term.
                    Cluster 3 is made up entirely of images of individuals wearing large traditional headdresses.
                    It is also the 4th largest cluster across all generations - which suggests that this one feature has a disproportionate importance in representations of these groups.
                    This behavior further brings to light well known representation issues in the models' training data.
                    It also showcases the necessity of modeling the diversity of how people map visual features to inferred social characteristics,
                    as the images in 8 and 23 are more likely to bear some similarity to depictions of people in contemporary professional settings.
                    
                    With a different focus, **clusters 12, and 13** help us explore stereotypical representations of non-binary identities.
                    While both are made up of over 80% images for which the prompt mentions the word *non-binary*,
                    the similarities in cluster 12 are particularly flagrant upon visual inspection; with all depictions featuring similar glasses and similar haircuts -
                    neither of which should be a strong indicator of gender.
                    """
                )
        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown(
                    """
                    #### [Specification, Markedness, and Bias](https://hf.co/spaces/society-ethics/DiffusionFaceClustering "Select cluster to visualize to the right or go straight to the interactive demo")
                    
                    The second phenomenon we study through the lens of our clustering is that of **markedness**,
                    or *default behavior* of the models when neither gender nor ethnicity is specified.
                    This corresponds to asking the question: "If I **don't explicitly** tell my model to generate people with diverse identity characteristics,
                    will I still see diverse outcomes?"
                    
                    Unsurprisingly, we find that not to be the case.
                    The clusters with the most examples of prompts with unspecified gender and unspecified ethnicity terms are **clusters 5 and 19**,
                    and both are also strongly associated with the words *man*, *White*, and *Causian*.
                    This association holds across genders (as showcased by **cluster 15**, which has a majority of *woman* and *White* prompts along with unspecified ethnicity)
                    and across ethnicities (comparing the proportions of unspecified genders in **clusters 0 and 6**: 18 % and 38% for the clusters with more *woman* and more *man* respectively along with the *African American* phrases).
                    
                    This provides the beginning of an answer to our motivating question: since users rarely specify an explicit gender or ethnicity when using
                    these systems to generate images of people, the high likelihood of defaulting to *Whiteness* and *masculinity* is likely to at least partially explain the observed lack of diversity in the outputs.
                    We compare these behaviors across systems and professions in the next section.
                    """
                )
            with gr.Column(scale=1):
                id_cl_id_3 = gr.Dropdown(
                    choices=[5, 19, 15, 0, 6],
                    value=6,
                    show_label=False,
                )
                identity_screenshot_3 = gr.Image(
                    value=os.path.join(
                        impath_id, "cluster_19_of_24_unmarked_white_unmarked_man.JPG"
                    ),
                    label="Screenshot of the Identity Exploration tool for: Cluster 19 of 24",
                )
        demo.load(
            show_id_images,
            inputs=[id_cl_id_1, id_cl_id_2, id_cl_id_3],
            outputs=[
                identity_screenshot_1,
                identity_screenshot_2,
                identity_screenshot_3,
            ],
        )
        for var in [id_cl_id_1, id_cl_id_2, id_cl_id_3]:
            var.change(
                show_id_images,
                inputs=[id_cl_id_1, id_cl_id_2, id_cl_id_3],
                outputs=[
                    identity_screenshot_1,
                    identity_screenshot_2,
                    identity_screenshot_3,
                ],
            )

    gr.Markdown(
        """
        ### Quantifying Social Biases in Image Generations: Professions

        In the previous section, we provided a method to characterize how text-to-image systems
        associate textual mentions of identity characteristics with visual features in their outputs.
        Exploring the inherent representations learned through our method already gave us insights into some
        of the bias dynamics embedded in the models that support these systems.
        We take the analysis further in the present section by leveraging these represenations to study **social biases tied to a particular application
        setting:** specifically, we consider a setting where users generate pictures of people in professional settings, for example to use in place of
        stock images in articles or websites.
        To that end, we systematically generate a large variety of images for 150 professions for each of 3 systems,
        and compare the distribution of the visual features identified in the [Identity Representation Demo](https://hf.co/spaces/society-ethics/DiffusionFaceClustering)
        across all generations and across generations for each profession.
        You can explore these results in detail in the companion [Profession Bias Tool](https://hf.co/spaces/society-ethics/DiffusionClustering) - 
        in particular, you can read about the resulting **diversity metric** for different models and professions,
        as well as a study case for **comparing the representations of different mental health professions**, by expanding the accordion below:
        """
    )
    with gr.Accordion("Quantifying Social Biases in Image Generations: Professions", open=False):
        gr.Markdown(
            """
            <br/>
            
            Machine Learning models encode and amplify biases that are represented in the data that they are trained on -
            this can include, for instance, stereotypes around the demographic makeup of different professions.
            When the models are used in image generation systems that then contibute to the distribution of web imaged through integration
            in image edition software or stock imagery services, these representation biases can then feed into complex sociotechnical systems.
            For example, previous work has shown that seeing less diverse image search engine results can negatively impact under-represented groups' sense of belonging in their job,
            or reinforce implicit biases in hiring that then lead to further exclusion and in turn again to less diverse representation.
            
            As previously mentioned, measuring the extent of those biases in model outputs is complicated by the lack of inherent social charcteristics
            for the synthetic people depicted in the generated images. In this section, we show how to leverage the [identity clusters](https://hf.co/spaces/society-ethics/DiffusionFaceClustering)
            introduced in the previous section to address this difficulty.
            For each of the 3 image generation systems, we generate a dataset of images corresponding to prompts of the format *"Photo portrait of a **(adjective)** **[profession]**"*.
            The adjectives serve both as a source of additional variability and as the focus of their own analysis (see our paper).
            We use a list of 20 adjectives and 150 professions from the [US Bureau of Labor Statistics (BLS)](https://www.bls.gov/cps/cpsaat11.htm).
            We then assign each image to a cluster based on a dot product between the image embedding and cluster centroid.
            This allows us to answer our motivating question about quantifying whether images depicting a certain professions are **more likely to look like** images
            corresponding to prompts explicitly mentioning specific genders or ethnicities (for example, mostly *man* and mostly *White* for images in cluster 5)
            without assigning an identity charasteristic to an individual generation for *e.g.* a *"Photo portrait of a compassionate CEO"* prompt.
            
            The results are presented in both tabs of the [Profession Bias Tool](https://hf.co/spaces/society-ethics/DiffusionClustering).
            The **Professions Overview** tab lets users select a system (or *All Models*) and a subset of professions (or *all professions* together)
            to print a table showing its distribution over the top 8 identity clusters, its diversity as measured by the entropy of this distribution,
            and the gender ratio for this profession in the US as reported by the [BLS](https://www.bls.gov/cps/cpsaat11.htm "specifically, the reported proportion of women").
            It also provides a summary description of the clusters for convenience.
            The **Profession Focus** tab lets users select a single profession and compares the distribution across identity clusters for all image generation systems in a histogram.
            It also provides examples of images generated for the profession that are assigned to each of the clusters.
            """
        )
        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown(
                    """
                    #### [Diversity and Representation across Models](https://hf.co/spaces/society-ethics/DiffusionClustering "you can cycle through screenshots of the tool in use on the right, or go straight to the interactive demo")
                    
                    We start by looking at the summary statistics provided in the [**Professions Overview** tab](https://hf.co/spaces/society-ethics/DiffusionClustering "select screenshots right or go straight to the demo").
                    **Clusters 5 and 19**, which are mostly made up of images generated for prompts mentioning *man*, *White*, and *Caucasian*,
                    are the most represented across systems and professions, accounting for 53.6 % of generations.
                    This is over-representation (less than 31% of respondents to the 2020 US census checked the boxes for *man* and *White*) is not equally distributed however:
                    these two cluster make up 69.7% of the *CEO* images, but only 35.7 and 22.1% for *fast food worker* and *social worker* respectively.
                    
                    Next, compare the tables for systems Stable Diffusion v1.4, v2, and Dall-E 2.
                    These same clusters are somewhat less over-represented in Stable Siffusion v1.4, accounting for 39.6% of generations across professions and 43.3% for *CEO*,
                    but Dall-E 2 shows the strongest disparities with 68.7 and 87.7 for all professions and *CEO* respectively.
                    Stable Diffusion v2 falls in the middle (52.4 all, 78.1 *CEO*).
                    The same ordering is reflected in the entropy-based diversity metric, with Stable Diffusion v1.4 measured as the most diverse (2.2)
                    followed by v2 (1.9), and Dall-E 2 (1.7).
                    
                    These three systems correspond to different approaches and design choices, especially in terms of filtering of the pre-training data for Stable Diffusion v2 and Dall-E 2,
                    guided by concerns of performance (esthetic value of the images, relevance of the image to the prompt) and risks of generating unwanted content
                    (usually with a focus on sexual themes and violence). While these two aspects are undoubtedly important,
                    the results we present here suggest that much more care needs to be taken in ensuring that these intervention do not exacerbate social dynamics that further erase marginalized populations.
                    """
                )
            with gr.Column(scale=1):
                bias_cl_id_1 = gr.Dropdown(
                    choices=[
                        "Results table: all models",
                        "Results table: Stable Diffusion v1.4",
                        "Results table: Stable Diffusion v2.",
                        "Results table: Stable Diffusion Dall-E 2",
                        "Comparison histogram: all professions",
                        "CEO examplars: Cluster 5",
                        "CEO examplars: Cluster 6",
                    ],
                    value="Results table: all models",
                    show_label=False,
                )
                bias_screenshot_1 = gr.Image(
                    value=os.path.join(impath_bias, "cluster_assign_24_all.png"),
                    label="Screenshot of the Profession Bias Tool | Results table: all models",
                )
        with gr.Row():
            with gr.Column(scale=1):
                bias_cl_id_2 = gr.Dropdown(
                    choices=[
                        "Results table: mental health professions, all models",
                        "Comparison histogram: psychologist",
                        "Comparison histogram: social worker",
                        "Comparison histogram: social assistant",
                    ],
                    value="Results table: mental health professions, all models",
                    show_label=False,
                )
                bias_screenshot_2 = gr.Image(
                    value=os.path.join(impath_bias, "cluster_assign_mental_health_24_all.png"),
                    label="Screenshot of the Profession Bias Tool | Results table: mental health professions, all models",
                )
                mental_health_examlpars = gr.Gallery(
                    [
                        (Image.open(os.path.join(impath_bias, im)), name)
                        for im, name in [
                            ("psychologist_2_of_24.png", "2 - psychologists"),
                            ("psychologist_5_of_24.png", "5 - psychologists"),
                            ("psychologist_15_of_24.png", "15 - psychologists"),
                            ("psychologist_19_of_24.png", "19 - psychologists"),
                            ("psychologist_0_of_24.png", "0 - psychologists"),
                            ("social_assistant_2_of_24.png", "2 - social assistant"),
                            ("social_assistant_5_of_24.png", "5 - social assistant"),
                            ("social_assistant_15_0f_24.png", "15 - social assistant"),
                            ("social_assistant_19_of_24.png", "19 - social assistant"),
                            ("social_assistant_0_of_24.png", "0 - social assistant"),
                            ("social_worker_2_of_24.png", "2 - social worker"),
                            ("social_worker_5_of_24.png", "5 - social worker"),
                            ("social_worker_15_of_24.png", "15 - social worker"),
                            ("social_worker_19_of_24.png", "19 - social worker"),
                            ("social_worker_0_of_24.png", "0 - social worker"),
                        ]
                    ],
                    label="Example images generated by three text-to-image models (Dall-E 2, Stable Diffusion v1.4 and v.2)",
                    show_label=False,
                ).style(grid=[3, 5], height="auto")
            with gr.Column():
                gr.Markdown(
                    """
                    #### [Focused Comparison: Mental Health Professions](https://hf.co/spaces/society-ethics/DiffusionClustering "you can cycle through screenshots of the tool in use on the left and example images below, or go straight to the interactive demo")
                    
                    We can also leverage the [Profession Bias Tool](https://hf.co/spaces/society-ethics/DiffusionClustering) for more focused analysis.
                    For this case study, we use it to compare the distribution over identity clusters for three professions related to mental health and care:
                    [social assistant](https://www.bls.gov/ooh/community-and-social-service/social-and-human-service-assistants.htm),
                    [social worker](https://www.bls.gov/ooh/community-and-social-service/social-workers.htm), and [psychologist](https://www.bls.gov/ooh/life-physical-and-social-science/psychologists.htm).
                    We can see that the BLS reports a significant majority of women for all three professions, from 74.9 to 83.6 percent of the workforce.
                    However, the BLS also reports different degree requirements and significant differences in median income,
                    with *social assistants* at **18$/hour**, *social workers* at **24$/hour**, and *psychologists* at **39$/hour**.
                    Going back to the proportions of clusters 5 and 19, we find that this corresponds to clusters associated with the terms *White* and *man*
                    accounting for **10.5%**, **22.1%**, and **49.8%** of generated images respectively for the prompts mentioning all three professions.
                    These findings underline the urgency of also **including socioeconomic factors in fairness evaluations.**
                    
                    The **Profession Focus** tab provides further information to help interpret these results.
                    First, the comparative histograms (screenshots to the left) show how the trends hold across models.
                    Generations from all models follow the ordering outlined above, although we do see differences in the distribution for the *social worker* prompts
                    between the Stable Diffusion v.2. and Dall-E 2 systems.
                    Second, we show examplars of image generations for each professions that are assigned to each of the identity clusters (screenshot to the left, below).
                    This serves the dual purpose of confirming our intuitions about the visual features encoded in the cluster assignments and of making it easier to find examples
                    of profession image generations that are likely to showcase specific features.
                    For example, we see in the table that **cluster 0**, which is made up primarily of prompts featuring the phrases *woman* and *African American*,
                    is better represented for profession images generated for *social worker* prompts - the tool lets us visually inspect the images for this profession assigned to that cluster.
                    
                    ***Note on the cluster examplar selection:*** In order to also show the limitations of the cluster assignment, we show the three images that are
                    closest to the cluster centroid, two close to the median distance, and finally the three images that are assigned to the cluster with the least confidence.
                    This shows atypical generations often with text printed (or something that looks like text), black and white pictures, pictures with multiple faces, and, in one case, a dog.
                    Given the stochastic nature of the generation, finding surprising generations across the 90,000+ images we generated is not wholly unexpected.
                    We provide more details on how we identified outliers across the image generations in the section entitled **Exploring the Pixel Space of Generated Images**.
                    """
                )
        demo.load(
            show_bias_images,
                inputs=[bias_cl_id_1, bias_cl_id_2],
                outputs=[
                    bias_screenshot_1,
                    bias_screenshot_2,
                ],
        )
        for var in [bias_cl_id_1, bias_cl_id_2]:
            var.change(
                show_bias_images,
                inputs=[bias_cl_id_1, bias_cl_id_2],
                outputs=[
                    bias_screenshot_1,
                    bias_screenshot_2,
                ],
            )

    gr.Markdown(
        """
                ### Comparing Model Generations
            """
    )
    gr.Markdown(
        """
                Above and beyond quantitative analyses, one of the main goals of our project was to create accessible ways for the users to explore the generated images themselves, based on their own interests. For this purpose, we created two interactive tools: the [Diffusion Bias Explorer](https://huggingface.co/spaces/society-ethics/DiffusionBiasExplorer), which can be used to compare two models and the images they generate for a given profession or for a given model across two professions, and the [Average Diffusion Faces Tool](https://huggingface.co/spaces/society-ethics/Average_diffusion_faces), which shows an 'average' representation of faces across professions, based on the images generated by the 3 models.
              """
    )
    with gr.Accordion("Comparing Model Generations", open=False):
        gr.HTML(
            """
         <p style="margin-bottom: 14px; font-size: 100%">  One of the goals of our study was allowing users to compare model generations across professions in an open-ended way, uncovering patterns and trends on their own. This is why we created the <a href='https://huggingface.co/spaces/society-ethics/DiffusionBiasExplorer' style='text-decoration: underline;' target='_blank'> Diffusion Bias Explorer </a> and the <a href='https://huggingface.co/spaces/society-ethics/Average_diffusion_faces' style='text-decoration: underline;' target='_blank'> Average Diffusion Faces </a> tools. We show some of their functionalities below:    </p> """
        )
        with gr.Row():
            with gr.Column():
                explorerpath = "images/biasexplorer"
                biasexplorer_gallery = gr.Gallery(
                    get_images(explorerpath),
                    label="Bias explorer images",
                    show_label=False,
                    elem_id="gallery",
                ).style(grid=[2, 2])
            with gr.Column():
                gr.HTML(
                    """
                 <p style="margin-bottom: 14px; font-size: 100%"> Comparing generations both between two models and within a single model can help uncover trends and patterns that are hard to measure using quantitative approaches. <br> For instance, we can observe that both Dall-E 2 and Stable Diffusion 2 represent both <i> CEOs </i> and <i> nurses </i> as homogenous groups with distinct characteristics, such as ties and scrubs (which makes sense given the results of our clustering, shown above. <br> We can also see that the images of <i> waitresses </i> generated by  Dall-E 2 and Stable Diffusion v.1.4. have different characteristics, both in terms of their clothes as well as their appearance. <br> It's also possible to see harder to describe phenomena, like the fact that portraits of <i> painters </i> often look like paintings themselves. <br> We encourage you to use the <a href='https://huggingface.co/spaces/society-ethics/DiffusionBiasExplorer' style='text-decoration: underline;' target='_blank'> Diffusion Bias Explorer </a> tool to explore these phenomena further! </p>"""
                )
        with gr.Row():
            with gr.Column():
                averagepath = "images/averagefaces"
                average_gallery = gr.Gallery(
                    get_images(averagepath),
                    label="Average Face images",
                    show_label=False,
                    elem_id="gallery",
                ).style(grid=[1, 3], height=560)
            with gr.Column():
                gr.HTML(
                    """
                 <p style="margin-bottom: 14px; font-size: 100%"> Looking at the average faces for a given profession across multiple models can help see the dominant characteristics of that profession, as well as how much variation there is (based on how fuzzy the image is). <br> In the images shown here, we can see that representations of these professions significantly differ across the three models, while sharing common characteristics, e.g. <i> postal workers </i> all wear caps. <br> Also, the average faces of <i> hairdressers </i> seem more fuzzy than the other professions, indicating a higher diversity among the generations compared to other professions. <br> Look at the <a href='https://huggingface.co/spaces/society-ethics/Average_diffusion_faces' style='text-decoration: underline;' target='_blank'> Average Diffusion Faces </a> tool for more examples! </p>"""
                )

    gr.Markdown(
        """
                ### Exploring the Pixel Space of Generated Images
            """
    )
    gr.Markdown(
        """
                Finally, an interesting aspect of the generations of the 3 models are the images themselves, which can be analyzed from different angles on a pixel-level. We explore the images in terms of their colorfulness using the [Colorfulness Profession Explorer](https://huggingface.co/spaces/tti-bias/identities-colorfulness-knn) and the [Colorfulness Identities Explorer](https://huggingface.co/spaces/tti-bias/professions-colorfulness-knn), which allow users to hone in on patterns in terms of colors and shades within the images generated. We also allow exploration of the images in terms of their visual features using the bag-of-visual-words approach (BoVW), which allows users to hone in on visual stereotypical content such as professions that have uniforms of a given color, of elements like glasses and hair styles -- this can be done via the [BoVW Nearest Neighbors Explorer](https://huggingface.co/spaces/tti-bias/identities-bovw-knn) and the [BoVW Professions Explorer](https://huggingface.co/spaces/tti-bias/professions-bovw-knn) -- we also present some of our salient findings in the accordion below.  
              """
    )
    with gr.Accordion("Exploring the Pixel Space of Generated Images", open=False):
        gr.HTML(
            """
         <br>
         <p style="margin-bottom: 14px; font-size: 100%">  With thousands of generated images, we found it useful to provide ways to explore the data in a structured way that did not depend on any external dataset or model. We provide two such tools, one based on <b>colorfulness</b> and one based on a <b>bag-of-visual words</b> model computed using SIFT features.</p>
        """
        )
        with gr.Row():
            gr.HTML(
                """
         <h4>Colorfulness</h4>
         <p style="margin-bottom: 14px; font-size: 100%"> We compute an image's "colorfulness" following <a href="https://doi.org/10.1117/12.477378">this work</a> by David Hasler and Sabine E. Suesstrunk and allow the user to choose a specific prompt and model and explore the neighborhood of that chosen starting point. One interesting orthogonal insight is that images generated by DALL·E 2 are on average the most colorful. Images of men are on average less colorful than all other gender labels, consistently across all three models. Patterns revealed using this explorer include for example the exoticizing depiction of native Americans as can be seen in the very stereotypical gallery of images generated in the example on the right.</p>
        """
            )
            gr.Image("images/colorfulness/nativeamerican_man.png")
        with gr.Row():
            gr.HTML(
                """
         <h4>Bag of Visual Words</h4>
         <p style="margin-bottom: 14px; font-size: 100%">  Another way of providing the means for a structured traversal of the dataset is a nearest-neighbor explorer based on visual features provided by an image's SIFT features, which we quantize into a visual vocabulary to represent the entire image dataset as a TF-IDF matrix. These tools are especially useful in honing in on stereotypical content that is often encoded visually, but also failure modes of the model such as the misinterpetation of the "stocker" profession as an imagined dog-breed. The screenshot to the right shows how SIFT visual patterns tend to cluster together, namely in this instance the booksheves in the background, or the gibberish pseudo-English text that often plagues TTI systems. </p>
        """
            )
            with gr.Column():
                gr.Image("images/bovw/bookshelves.png")
                gr.Image("images/bovw/gibberish.png")
    gr.Markdown(
        """
                ### All of the tools created as part of this project:
            """
    )
    gr.HTML(
        """
     <p style="margin-bottom: 10px; font-size: 110%">
     <a href='https://huggingface.co/spaces/society-ethics/Average_diffusion_faces' style='text-decoration: underline;' target='_blank'> Average Diffusion Faces </a>  <br>
     <a href='https://huggingface.co/spaces/society-ethics/DiffusionBiasExplorer' style='text-decoration: underline;' target='_blank'> Diffusion Bias Explorer </a> <br> 
     <a href='https://huggingface.co/spaces/society-ethics/DiffusionClustering' style='text-decoration: underline;' target='_blank'> Diffusion Cluster Explorer </a> <br>
     <a href='https://huggingface.co/spaces/society-ethics/DiffusionFaceClustering' style='text-decoration: underline;' target='_blank'> Identity Representation Demo </a> <br>
     <a href='https://huggingface.co/spaces/tti-bias/identities-bovw-knn' style='text-decoration: underline;' target='_blank'> BoVW Nearest Neighbors Explorer </a> <br> 
     <a href='https://huggingface.co/spaces/tti-bias/professions-bovw-knn' style='text-decoration: underline;' target='_blank'> BoVW Professions Explorer </a> <br>    
     <a href='https://huggingface.co/spaces/tti-bias/identities-colorfulness-knn' style='text-decoration: underline;' target='_blank'> Colorfulness Profession Explorer </a> <br>      
     <a href='https://huggingface.co/spaces/tti-bias/professions-colorfulness-knn' style='text-decoration: underline;' target='_blank'> Colorfulness Identities Explorer </a> <br> </p>
          """
    )
#    gr.Interface.load("spaces/society-ethics/DiffusionBiasExplorer")

demo.launch(debug=True)