zseid
commited on
Commit
•
932522d
1
Parent(s):
56259b9
add sd2 and model passing to bias eval
Browse files
app.py
CHANGED
@@ -23,10 +23,14 @@ from datasets import load_dataset
|
|
23 |
from diffusers import DiffusionPipeline, PNDMScheduler,EulerDiscreteScheduler
|
24 |
|
25 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
26 |
-
STABLE_MODELS =
|
27 |
results = dict()
|
28 |
-
results[
|
29 |
-
results[
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# scheduler = PNDMScheduler.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="scheduler", prediction_type="v_prediction",revision="fp16",torch_dtype=torch.float16)
|
32 |
esched = EulerDiscreteScheduler.from_pretrained("stabilityai/stable-diffusion-2-base",subfolder="scheduler")
|
@@ -48,7 +52,8 @@ def fig2img(fig):
|
|
48 |
|
49 |
def trait_graph(model,hist=True):
|
50 |
tda_res,occ_res = results[model]
|
51 |
-
|
|
|
52 |
fig = None
|
53 |
if not hist:
|
54 |
fig = generate_countplot(tda_res, 'tda_sentiment_val', 'gender_detected_val',
|
@@ -77,7 +82,7 @@ def trait_graph(model,hist=True):
|
|
77 |
return pass_skin,pass_gen,fig2img(fig2),fig2img(fig)
|
78 |
def occ_graph(model):
|
79 |
tda_res,occ_result = results[model]
|
80 |
-
pass_gen,pass_skin = evaluate_by_occupation(occupation_df=occ_result)
|
81 |
fig = generate_histplot(occ_result, 'a_median', 'gender_detected_val',
|
82 |
title='Gender Distribution by Median Annualg Salary',
|
83 |
xlabel= 'Median Annual Salary',
|
@@ -100,20 +105,22 @@ def adj_example(adj):
|
|
100 |
return example_analysis(prompt)
|
101 |
def example_analysis(prompt):
|
102 |
pil_img = pipe(prompt,num_inference_steps=20).images[0]
|
103 |
-
# pil_img = Image.open('./
|
104 |
df = process_image_pil(pil_img,prompt)
|
105 |
rgb_tup = (128,128,128)
|
106 |
if "skin color" in df:
|
107 |
rgb_tup = df["skin color"][0]
|
108 |
-
|
109 |
def clamp(x):
|
110 |
return max(0, min(int(x), 255))
|
111 |
def hex_from_tup(in_tup):
|
112 |
return "#{0:02x}{1:02x}{2:02x}".format(clamp(in_tup[0]), clamp(in_tup[1]), clamp(in_tup[2]))
|
113 |
rgb_hex = hex_from_tup(rgb_tup)
|
114 |
intensity_val = rgb_intensity(rgb_tup)
|
|
|
115 |
intense_hex = str(hex(int(intensity_val)))
|
116 |
intense_hex = f"#{intense_hex}{intense_hex}{intense_hex}"
|
|
|
117 |
gender_w = float(df["gender.Woman"][0]) if "gender.Woman" in df else -1
|
118 |
gender_m = float(df["gender.Man"][0]) if "gender.Man" in df else -1
|
119 |
gender_str = f"Male ({gender_m}%)" if gender_m>gender_w else f"Female({gender_w}%)"
|
@@ -131,11 +138,16 @@ def bias_assessment(model):
|
|
131 |
[(f"Skin color {'unbiased' if os else 'biased'} by Income/Occupation",boo_to_str(os))], \
|
132 |
[(f"Gender {'unbiased' if og else 'biased'} by Income/Occupation",boo_to_str(og))],\
|
133 |
osgraph,oggraph
|
134 |
-
|
135 |
-
|
|
|
|
|
136 |
def cached_results(model):
|
137 |
-
|
|
|
138 |
return mj_analysis
|
|
|
|
|
139 |
else:
|
140 |
return sd_analysis
|
141 |
|
@@ -148,7 +160,7 @@ if __name__=='__main__':
|
|
148 |
with gr.Tab("Model Audit"):
|
149 |
with gr.Row():
|
150 |
with gr.Column():
|
151 |
-
model = gr.Dropdown(STABLE_MODELS,label="Text-to-Image Model")
|
152 |
btn = gr.Button("Assess Model Bias")
|
153 |
gr.Markdown("The training set, vocabulary, pre and post processing of generative AI tools don't treat everyone equally. "
|
154 |
"Within a 95% margin of statistical error, the following tests expose bias in gender and skin color. To learn more about this process, <a href=\"http://github.com/TRSS-Research/SAAC.git\"/> Visit the repo</a>")
|
|
|
23 |
from diffusers import DiffusionPipeline, PNDMScheduler,EulerDiscreteScheduler
|
24 |
|
25 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
26 |
+
STABLE_MODELS = {"Stable Diffusion v1.5":'stable', "Midjourney":'midjourney',"Stable Diffusion v2":"sd2"}
|
27 |
results = dict()
|
28 |
+
results["stable"] = process_analysis(os.path.join(EVAL_DATA_DIRECTORY,'raw',"stable_diffusion_raw_processed.csv"),filtered=True,model="stable")
|
29 |
+
results["midjourney"] = process_analysis(os.path.join(EVAL_DATA_DIRECTORY,'raw',"midjourney_deepface_calibrated_equalized_mode.csv"),filtered=True,model='midjourney')
|
30 |
+
results["sd2"] = process_analysis(os.path.join(EVAL_DATA_DIRECTORY,'raw',"sd2_analysis.csv"),filtered=True,model='sd2')
|
31 |
+
for m in results:
|
32 |
+
t,o = results[m]
|
33 |
+
print(m,len(t.index),len(o.index))
|
34 |
|
35 |
# scheduler = PNDMScheduler.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="scheduler", prediction_type="v_prediction",revision="fp16",torch_dtype=torch.float16)
|
36 |
esched = EulerDiscreteScheduler.from_pretrained("stabilityai/stable-diffusion-2-base",subfolder="scheduler")
|
|
|
52 |
|
53 |
def trait_graph(model,hist=True):
|
54 |
tda_res,occ_res = results[model]
|
55 |
+
# print(tda_res)
|
56 |
+
pass_gen,pass_skin = evaluate_by_adjectives(adjective_df=tda_res,model=model)
|
57 |
fig = None
|
58 |
if not hist:
|
59 |
fig = generate_countplot(tda_res, 'tda_sentiment_val', 'gender_detected_val',
|
|
|
82 |
return pass_skin,pass_gen,fig2img(fig2),fig2img(fig)
|
83 |
def occ_graph(model):
|
84 |
tda_res,occ_result = results[model]
|
85 |
+
pass_gen,pass_skin = evaluate_by_occupation(occupation_df=occ_result,model=model)
|
86 |
fig = generate_histplot(occ_result, 'a_median', 'gender_detected_val',
|
87 |
title='Gender Distribution by Median Annualg Salary',
|
88 |
xlabel= 'Median Annual Salary',
|
|
|
105 |
return example_analysis(prompt)
|
106 |
def example_analysis(prompt):
|
107 |
pil_img = pipe(prompt,num_inference_steps=20).images[0]
|
108 |
+
# pil_img = Image.open('./a_anesthesiologist_photorealistic.png')
|
109 |
df = process_image_pil(pil_img,prompt)
|
110 |
rgb_tup = (128,128,128)
|
111 |
if "skin color" in df:
|
112 |
rgb_tup = df["skin color"][0]
|
113 |
+
print('RGB tup',rgb_tup)
|
114 |
def clamp(x):
|
115 |
return max(0, min(int(x), 255))
|
116 |
def hex_from_tup(in_tup):
|
117 |
return "#{0:02x}{1:02x}{2:02x}".format(clamp(in_tup[0]), clamp(in_tup[1]), clamp(in_tup[2]))
|
118 |
rgb_hex = hex_from_tup(rgb_tup)
|
119 |
intensity_val = rgb_intensity(rgb_tup)
|
120 |
+
print('intensity_val',intensity_val)
|
121 |
intense_hex = str(hex(int(intensity_val)))
|
122 |
intense_hex = f"#{intense_hex}{intense_hex}{intense_hex}"
|
123 |
+
print(intense_hex)
|
124 |
gender_w = float(df["gender.Woman"][0]) if "gender.Woman" in df else -1
|
125 |
gender_m = float(df["gender.Man"][0]) if "gender.Man" in df else -1
|
126 |
gender_str = f"Male ({gender_m}%)" if gender_m>gender_w else f"Female({gender_w}%)"
|
|
|
138 |
[(f"Skin color {'unbiased' if os else 'biased'} by Income/Occupation",boo_to_str(os))], \
|
139 |
[(f"Gender {'unbiased' if og else 'biased'} by Income/Occupation",boo_to_str(og))],\
|
140 |
osgraph,oggraph
|
141 |
+
|
142 |
+
mj_analysis = bias_assessment("midjourney")
|
143 |
+
sd_analysis = bias_assessment("stable")
|
144 |
+
sd2_analysis = bias_assessment("sd2")
|
145 |
def cached_results(model):
|
146 |
+
model = STABLE_MODELS[model]
|
147 |
+
if model=="midjourney":
|
148 |
return mj_analysis
|
149 |
+
elif model=="sd2":
|
150 |
+
return sd2_analysis
|
151 |
else:
|
152 |
return sd_analysis
|
153 |
|
|
|
160 |
with gr.Tab("Model Audit"):
|
161 |
with gr.Row():
|
162 |
with gr.Column():
|
163 |
+
model = gr.Dropdown(list(STABLE_MODELS.keys()),label="Text-to-Image Model",value="Midjourney")
|
164 |
btn = gr.Button("Assess Model Bias")
|
165 |
gr.Markdown("The training set, vocabulary, pre and post processing of generative AI tools don't treat everyone equally. "
|
166 |
"Within a 95% margin of statistical error, the following tests expose bias in gender and skin color. To learn more about this process, <a href=\"http://github.com/TRSS-Research/SAAC.git\"/> Visit the repo</a>")
|