Upload gen_sd3.py
Browse files- gen_sd3.py +50 -0
gen_sd3.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import StableDiffusion3Pipeline
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
import argparse
|
| 7 |
+
|
| 8 |
+
parser = argparse.ArgumentParser(description="Diffusion Pipeline with Arguments")
|
| 9 |
+
|
| 10 |
+
parser.add_argument(
|
| 11 |
+
"--json_filename",
|
| 12 |
+
type=str,
|
| 13 |
+
required=True,
|
| 14 |
+
help="Path to the JSON file containing text data",
|
| 15 |
+
)
|
| 16 |
+
parser.add_argument(
|
| 17 |
+
"--cuda", type=int, required=True, help="CUDA device to use for processing"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
args = parser.parse_args()
|
| 21 |
+
json_filename = args.json_filename
|
| 22 |
+
cuda_device = f"cuda:{args.cuda}"
|
| 23 |
+
print(json_filename, cuda_device)
|
| 24 |
+
|
| 25 |
+
image_dir = "/mnt/petrelfs/zhuchenglin/LLaVA/playground/data/LLaVA-Pretrain/images"
|
| 26 |
+
with open(json_filename, "r") as f:
|
| 27 |
+
json_data = json.load(f)
|
| 28 |
+
|
| 29 |
+
pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16)
|
| 30 |
+
pipe.to('cuda')
|
| 31 |
+
|
| 32 |
+
for text in json_data:
|
| 33 |
+
image = pipe(
|
| 34 |
+
prompt=text["conversations"][1]["value"],
|
| 35 |
+
prompt_3=text["conversations"][1]["value"],
|
| 36 |
+
negative_prompt="",
|
| 37 |
+
num_inference_steps=100,
|
| 38 |
+
height=1024,
|
| 39 |
+
width=1024,
|
| 40 |
+
guidance_scale=10.0,
|
| 41 |
+
max_sequence_length=512,
|
| 42 |
+
).images[0]
|
| 43 |
+
|
| 44 |
+
subdir = text["image"].split("/")[0]
|
| 45 |
+
if not os.path.exists(os.path.join(image_dir, subdir)):
|
| 46 |
+
os.makedirs(os.path.join(image_dir, subdir))
|
| 47 |
+
image_path = os.path.join(image_dir, text["image"])
|
| 48 |
+
image.save(image_path)
|
| 49 |
+
|
| 50 |
+
print("所有图像已成功生成并保存。")
|