Upload DATA/experiment.py with huggingface_hub
Browse files- DATA/experiment.py +110 -0
DATA/experiment.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from PIL import Image
|
3 |
+
import torchvision.transforms.functional as f
|
4 |
+
from utils import load_face_generator
|
5 |
+
from omegaconf import OmegaConf
|
6 |
+
|
7 |
+
def generate_face_image(
|
8 |
+
anything_facemaker,
|
9 |
+
class_concept,
|
10 |
+
face_img_pil=None,
|
11 |
+
controlnet_conditioning_scale=1.0,
|
12 |
+
strength=0.95,
|
13 |
+
):
|
14 |
+
face_img_pil = f.center_crop(
|
15 |
+
f.resize(face_img_pil, 512), 512).convert('RGB')
|
16 |
+
prompt = anything_facemaker.prompt_template.format(class_concept)
|
17 |
+
# # There are four ways to generate a image by now.
|
18 |
+
# pure_generate = anything_facemaker.generate(prompt=prompt, image=face_img_pil, do_inversion=False)
|
19 |
+
# inversion = anything_facemaker.generate(prompt=prompt, image=face_img_pil, strength=strength, do_inversion=True)
|
20 |
+
|
21 |
+
if controlnet_conditioning_scale == None:
|
22 |
+
init_face_pil = anything_facemaker.generate(prompt=prompt)
|
23 |
+
return init_face_pil
|
24 |
+
|
25 |
+
if strength is None:
|
26 |
+
pure_control = anything_facemaker.face_control_generate(prompt=prompt, face_img_pil=face_img_pil, do_inversion=False,
|
27 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale)
|
28 |
+
init_face_pil = pure_control
|
29 |
+
else:
|
30 |
+
control_inversion = anything_facemaker.face_control_generate(prompt=prompt, face_img_pil=face_img_pil, do_inversion=True,
|
31 |
+
strength=strength,
|
32 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale)
|
33 |
+
init_face_pil = control_inversion
|
34 |
+
return init_face_pil
|
35 |
+
|
36 |
+
|
37 |
+
def experiment(anything_facemaker, concepts_path, face_img_path, output_dir,
|
38 |
+
controlnet_conditioning_scale=1., strength=0.95):
|
39 |
+
os.makedirs(output_dir, exist_ok=True)
|
40 |
+
face_img_pil = Image.open(face_img_path)
|
41 |
+
face_img_pil = f.center_crop(
|
42 |
+
f.resize(face_img_pil, 512), 512).convert('RGB')
|
43 |
+
with open(concepts_path) as fr:
|
44 |
+
concepts = fr.read().split('\n')
|
45 |
+
concepts = [concept for concept in concepts if len(concept)!=0]
|
46 |
+
for concept in concepts:
|
47 |
+
save_path = os.path.join(output_dir, f'{concept}.png')
|
48 |
+
if os.path.exists(save_path):
|
49 |
+
continue
|
50 |
+
init_face_pil = generate_face_image(
|
51 |
+
anything_facemaker,
|
52 |
+
class_concept=concept,
|
53 |
+
face_img_pil=face_img_pil,
|
54 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
55 |
+
strength=strength,
|
56 |
+
)
|
57 |
+
|
58 |
+
save_path = os.path.join(output_dir, f'{concept}.png')
|
59 |
+
init_face_pil.save(save_path)
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
if __name__=='__main__':
|
64 |
+
# run this in repo path:
|
65 |
+
# PYTHONPATH=.:$PYTHONPATH python experiments/experiment.py
|
66 |
+
model_config_path = 'resources/models.yaml'
|
67 |
+
# model_config_path = 'resources/models_personality.yaml'
|
68 |
+
model_config = OmegaConf.load(model_config_path)['models']
|
69 |
+
gameicon_config = model_config['GameIconInstitute_mode']
|
70 |
+
|
71 |
+
face_img_path='resources/images/faces/1.jpg'
|
72 |
+
controlnet_conditioning_scale=1.
|
73 |
+
strength=0.95
|
74 |
+
|
75 |
+
for model, model_info in model_config.items():
|
76 |
+
|
77 |
+
anything_facemaker = load_face_generator(
|
78 |
+
model_dir=model_info['model_dir'],
|
79 |
+
lora_path=model_info['lora_path'],
|
80 |
+
prompt_template=model_info['prompt_template'],
|
81 |
+
negative_prompt=model_info['negative_prompt']
|
82 |
+
)
|
83 |
+
output_dir = os.path.join('results/concepts_test/control_inversion', model)
|
84 |
+
os.makedirs(output_dir, exist_ok=True)
|
85 |
+
# concept test, with control and inversion
|
86 |
+
input_dir = 'resources/prompts'
|
87 |
+
for dir, folders, files in os.walk(input_dir):
|
88 |
+
for file in files:
|
89 |
+
input_file = os.path.join(dir, file)
|
90 |
+
file_output_dir = os.path.join(output_dir, file)
|
91 |
+
print(f'input_file: {input_file}')
|
92 |
+
print(f'file_output_dir: {file_output_dir}')
|
93 |
+
experiment(anything_facemaker, input_file, face_img_path, output_dir=file_output_dir,
|
94 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
95 |
+
strength=strength)
|
96 |
+
|
97 |
+
# # concept, with control and inversion
|
98 |
+
# experiment(anything_facemaker, 'resources/concepts.txt', face_img_path, output_dir='results/concepts/control_inversion',
|
99 |
+
# controlnet_conditioning_scale=controlnet_conditioning_scale,
|
100 |
+
# strength=strength)
|
101 |
+
|
102 |
+
# # concept test, no control no inversion
|
103 |
+
# experiment(anything_facemaker, 'resources/concepts_test.txt', face_img_path, output_dir='results/concepts_test/generate',
|
104 |
+
# controlnet_conditioning_scale=None,
|
105 |
+
# strength=strength)
|
106 |
+
|
107 |
+
# # concept, no control no inversion
|
108 |
+
# experiment(anything_facemaker, 'resources/concepts.txt', face_img_path, output_dir='results/concepts/generate',
|
109 |
+
# controlnet_conditioning_scale=None,
|
110 |
+
# strength=strength)
|