-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgendata.py
124 lines (104 loc) · 3.46 KB
/
gendata.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
#! usr/bin/env python
from db.write import write_image_to_db
from pathlib import Path
import click
import os
import numpy as np
import pandas as pd
from tqdm import tqdm
from big_sleep import Imagine as bsImagine
from deep_daze import Imagine as ddImagine
import signal
from shutil import rmtree
import base64
def _get_base64_string(image_path):
with open(image_path, "rb") as image:
return base64.b64encode(image.read())
@click.group()
def generate():
pass
@generate.command()
@click.argument('prompt_file', type=click.Path(exists=True, resolve_path=True, path_type=Path))
@click.argument('output_dir', type=click.Path(resolve_path=True, path_type=Path))
def deepdaze(prompt_file, output_dir):
"""
Generate data for the prompt-response task.
"""
print("Generating image/prompt data")
print("-"*50)
if output_dir.exists():
overwrite=click.confirm("Output directory already exists. Erase and Overwrite?", default=False)
if not overwrite:
print("Exiting.")
exit(1)
else:
print(f"Erasing and overwriting {output_dir}.")
rmtree(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
os.chdir(output_dir)
print(f'Generating data in {output_dir}...')
df = pd.read_csv(prompt_file)
for i, (prompt, cat) in tqdm(df.iterrows(), total=df.shape[0]):
subdir = output_dir / cat
subdir.mkdir(parents=True, exist_ok=True)
os.chdir(subdir)
imagine = ddImagine(
text=prompt.lower(),
open_folder=False,
gradient_accumulate_every=4,
epochs=2,
save_progress=False,
num_layers=42,
batch_size=64,
iterations=100
)
imagine()
os.chdir(output_dir)
@generate.command()
@click.argument('prompt_file', type=click.Path(exists=True, resolve_path=True, path_type=Path))
@click.argument('output_dir', type=click.Path(resolve_path=True, path_type=Path))
def bigsleep(prompt_file, output_dir):
"""
Generate data for the prompt-response task.
"""
print("Generating image/prompt data")
print("-"*50)
if output_dir.exists():
overwrite=click.confirm("Output directory already exists. Erase and Overwrite?", default=False)
if not overwrite:
print("Exiting.")
exit(1)
else:
print(f"Erasing and overwriting {output_dir}.")
rmtree(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
os.chdir(output_dir)
print(f'Generating data in {output_dir}...')
df = pd.read_csv(prompt_file)
imagine = bsImagine(
text_min="blur|zoom",
open_folder=False,
larger_clip=False,
gradient_accumulate_every=3,
epochs=1,
save_best=True,
iterations=100
)
for i, (prompt, cat) in tqdm(df.iterrows(), total=df.shape[0]):
subdir = output_dir / cat
subdir.mkdir(parents=True, exist_ok=True)
os.chdir(subdir)
imagine.reset()
imagine.set_text(prompt.lower() + "|" + cat.lower())
imagine.img = prompt.lower() + ".png"
imagine()
image_string = _get_base64_string(imagine.text_path + ".best.png")
write_image_to_db(
prompt.lower(),
image_string.decode('utf-8'),
image_source='BigSleep',
theme=cat.lower().replace(" ", "_")
)
os.chdir(output_dir)
if __name__ == '__main__':
generate()