-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathinfer.py
More file actions
89 lines (73 loc) · 3.02 KB
/
infer.py
File metadata and controls
89 lines (73 loc) · 3.02 KB
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
# Copyright (c) 2026 ByteDance Ltd. and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import warnings
import torch
from PIL import Image
from diffusers.utils import load_image
from dreamlite import DreamLitePipeline
warnings.filterwarnings("ignore")
def parse_args():
parser = argparse.ArgumentParser(description="DreamLite Inference Script")
# Model & Structure
parser.add_argument("--model_id", type=str, default="models/DreamLite-base")
# Inference Params
parser.add_argument("--device", type=str, default="cuda")
parser.add_argument("--weight_dtype", type=str, default="bfloat16", choices=["float16", "bfloat16", "float32"])
parser.add_argument("--num_inference_steps", type=int, default=28)
parser.add_argument("--prompt", type=str, default="a dog running on the grass")
parser.add_argument("--negative_prompt", type=str, default="")
parser.add_argument("--image_path", type=str, default="")
parser.add_argument("--width", type=int, default=1024)
parser.add_argument("--height", type=int, default=1024)
parser.add_argument("--guidance_scale", type=float, default=3.5)
parser.add_argument("--image_guidance_scale", type=float, default=1.0)
return parser.parse_args()
def main():
args = parse_args()
weight_dtype = {
"float16": torch.float16,
"bfloat16": torch.bfloat16,
"float32": torch.float32
}[args.weight_dtype]
print(f"Loading diffusers pipeline from: {args.model_id}")
pipeline = DreamLitePipeline.from_pretrained(
args.model_id,
torch_dtype=weight_dtype,
).to(args.device)
# 3. Setup Data
prompt = args.prompt
input_image = load_image(args.image_path) if args.image_path else None
if input_image is not None:
width, height = input_image.size
else:
width, height = args.width, args.height
print("Generating image...")
image = pipeline(
prompt=prompt,
image=input_image,
height=height,
width=width,
guidance_scale=args.guidance_scale,
image_guidance_scale=args.image_guidance_scale,
num_inference_steps=args.num_inference_steps,
generator=torch.Generator("cpu").manual_seed(42),
).images[0]
if image.size != (width, height):
image = image.resize((width, height), Image.Resampling.LANCZOS)
out_path = f"{prompt.replace(' ', '_')}.png"
image.save(out_path)
print(f"Image saved to {out_path}")
if __name__ == "__main__":
main()