-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
203 lines (174 loc) · 6.95 KB
/
main.py
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
import os
import datetime
import builtins
import argparse
from source.image_generation import image_generation
from source.utils.evaluation_batch import evaluation_batch
from source.quality.evaluation import quality_evaluation
from source.bias import bias
from source.concept_restoration import concept_restoration
from source.image_translation import image_translation
from source.utils import set_logger, load_config, dict2namespace, merge_args_and_configs
from envs import IMG_DIR, CONFIG_DIR
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, required=True)
parser.add_argument("--method", type=str)
parser.add_argument("--device", type=str)
parser.add_argument("--seed", type=int)
args = parser.parse_args()
# Load config
config = load_config(f"{CONFIG_DIR}/{args.config}")
config = dict2namespace(config)
args = merge_args_and_configs(config, args)
return args
def check_images(task, method, target, seed, device):
# Check if the images exist, if not generate them
dir_path = f"{IMG_DIR}/{task}/{method}/{target}/{seed}"
try:
assert os.path.exists(dir_path), f"Directory {dir_path} does not exist"
assert len(os.listdir(dir_path)) > 0, f"No images in {dir_path}"
if task == "simple_prompt" or task == "diverse_prompt":
assert (
len(os.listdir(dir_path)) == 1000
), f"Expected 1000 images in {dir_path}"
elif task == "over_erasing" or task == "selective_alignment":
assert (
len(os.listdir(dir_path)) == 500
), f"Expected 500 images in {dir_path}"
elif task == "MS-COCO":
assert (
len(os.listdir(dir_path)) == 30000
), f"Expected 30000 image in {dir_path}"
except AssertionError as e:
print(e)
image_generation(task, method, target, seed, device, logger)
if __name__ == "__main__":
args = get_args()
logger = set_logger()
logger.info(args)
################################################################
# 1. Effectiveness and Faithfulness on target concept #
################################################################
# * Simple prompt
if "simple_prompt" in args.tasks:
logger.info(
f"Evaluate images generated from simple prompt (Effectiveness and Faithfulness)"
)
check_images("simple_prompt", args.method, args.target, args.seed, args.device)
evaluation_batch(
"simple_prompt", args.method, args.target, args.seed, logger
) # effectivness
quality_evaluation(
"aesthetic",
"simple_prompt",
args.method,
args.target,
args.seed,
args.device,
logger,
)
# * Diverse prompt
if "diverse_prompt" in args.tasks:
logger.info(
f"Evaluate images generated from diverse prompt (Effectiveness and Faithfulness)"
)
check_images("diverse_prompt", args.method, args.target, args.seed, args.device)
evaluation_batch(
"diverse_prompt", args.method, args.target, args.seed, logger
) # effectivness
quality_evaluation(
"aesthetic",
"diverse_prompt",
args.method,
args.target,
args.seed,
args.device,
logger,
)
################################################################
# 2. Faithfulness and Compliance on MS-COCO #
################################################################
# * MS-COCO prompt
if "MS-COCO" in args.tasks:
logger.info(
f"Evaluate images generated from MS-COCO prompt (Faithfulness and Compliance)"
)
check_images("MS-COCO", args.method, args.target, args.seed, args.device)
quality_evaluation(
"FID", "MS-COCO", args.method, args.target, args.seed, args.device, logger
)
quality_evaluation(
"PickScore",
"MS-COCO",
args.method,
args.target,
args.seed,
args.device,
logger,
)
quality_evaluation(
"ImageReward",
"MS-COCO",
args.method,
args.target,
args.seed,
args.device,
logger,
)
################################################################
# 3. Compliance on target concept #
################################################################
# * Selective alignment
if "selective_alignment" in args.tasks:
logger.info(f"Evaluate selective alignment task")
check_images(
"selective_alignment", args.method, args.target, args.seed, args.device
)
evaluation_batch(
"selective_alignment", args.method, args.target, args.seed, logger
)
################################################################
# 4. Robustness on side effects #
################################################################
# * Over-erasing effect
if "over_erasing" in args.tasks:
logger.info(f"Evaluate over-erasing effect task")
check_images("over_erasing", args.method, args.target, args.seed, args.device)
evaluation_batch("over_erasing", args.method, args.target, args.seed, logger)
# * Model bias
if "bias" in args.tasks:
logger.info(f"Evaluate performance on bias")
bias(
args.method,
args.bias.target_class,
args.bias.n_samples,
args.bias.batch_size,
args.bias.use_cond,
args.seed,
args.device,
)
################################################################
# 5. Consistency in downstream applications #
################################################################
# * 5-1 Sketch-to-image
if "sketch2image" in args.tasks:
logger.info(f"Evaluate performance on sketch to image generation")
image_translation(
args.method, args.target, "sketch2image", args.seed, args.device
)
evaluation_batch("sketch2image", args.method, args.target, args.seed, logger)
# * 5-2 Image-to-image
if "image2image" in args.tasks:
logger.info(f"Evaluate performance on image to image generation")
image_translation(
args.method, args.target, "image2image", args.seed, args.device
)
evaluation_batch("image2image", args.method, args.target, args.seed, logger)
# * 5-3 Concept restoration
if "concept_restoration" in args.tasks:
logger.info(f"Evaluate performance on concept restoration")
for start_t_idx in args.concept_restoration.start_t_idx:
concept_restoration(
args.method, args.target, start_t_idx, args.device, args.seed, logger
)