-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_txt_im.py
71 lines (61 loc) · 2.07 KB
/
generate_txt_im.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
import os
import torch
import argparse
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
from my_utils import create_directory
import matplotlib.pyplot as plt
def parse_args():
parser = argparse.ArgumentParser(description="Generate images from a prompt.")
parser.add_argument(
"--num-images",
type=int,
default=5000,
help="number of images to generate",
)
parser.add_argument("--input", type=str, help="folder to save images to")
parser.add_argument("--folder", type=str, help="folder to save images to")
parser.add_argument(
"--part",
type=int,
help="prompt to generate images from",
)
args, _ = parser.parse_known_args()
return args
def read_sentences_from_file(file_path):
with open(file_path, "r") as file:
sentences = file.readlines()
# Removing newline characters from each sentence
sentences = [sentence.strip() for sentence in sentences]
return sentences
def main(args):
prompt_list = read_sentences_from_file(f"./{args.input}.txt")
output_folder = os.path.join("datasets/stable_diffusion", f"{args.folder}/images")
create_directory(output_folder)
model_id = "stabilityai/stable-diffusion-2"
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
pipe = StableDiffusionPipeline.from_pretrained(
model_id,
scheduler=scheduler,
torch_dtype=torch.float16,
local_files_only=True,
).to("cuda")
# Writing the strings to a file
for j in range(args.num_images):
prompt = prompt_list[j % len(prompt_list)]
image = pipe(
prompt,
height=768,
width=768,
num_inference_steps=50,
guidance_scale=7.5,
).images[0]
image.save(
os.path.join(
output_folder,
f"im_{j+(args.part*args.num_images)}_p{j%len(prompt_list)}.png",
)
)
print(f"image {j} saved")
if __name__ == "__main__":
args = parse_args()
main(args)