-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInterAug_Step2_T2I.py
65 lines (52 loc) · 1.71 KB
/
InterAug_Step2_T2I.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
import os
import argparse
from diffusers import StableDiffusionPipeline
import torch
import json
def parse_args():
parser = argparse.ArgumentParser(description="GPT-3.5.")
parser.add_argument(
"--start_index",
type=int,
required=True,
)
parser.add_argument(
"--end_index",
type=int,
required=True,
) # number of total argumented reports
parser.add_argument(
"--save_path",
type=str,
default='data/InterAug/InterAug_images',
required=False,
)
args = parser.parse_args()
return args
args = parse_args()
txt_path = 'data/InterAug/InterAug_reports'
model_path = "pretrained/roentgen"
pipe = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
pipe.to("cuda")
for _, _, files in os.walk(txt_path):
final_files = files
print('finish data processing')
start_index = -1
os.makedirs(args.save_path, exist_ok=True)
times = 1 ### number of generated images for each report
while start_index < len(final_files):
start_index += 1
if start_index >= args.start_index:
try:
f = open(os.path.join(txt_path, final_files[start_index]), "r")
prompt = f.read()
name = final_files[start_index]
for t in range(times):
image = pipe([prompt], num_inference_steps=50, height=512, width=512, guidance_scale=4).images[0]
image.save(os.path.join(args.save_path, name[0:-4] + '_t' + str(t) + ".png"))
print(name)
if start_index > args.end_index:
break
except Exception as e:
print(f"Error encountered as {e}")
print("FINISHED")