Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding LCM (Latent-Consistency-Models) #5

Merged
merged 2 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/imagen_hub/infermodels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# ==========================================================
# Text-to-Image Generation
from .sd import SD, OpenJourney
from .sd import SD, OpenJourney, LCM
from .sdxl import SDXL
from .deepfloydif import DeepFloydIF
from .dalle import DALLE2, DALLE3, StableUnCLIP
Expand Down
41 changes: 41 additions & 0 deletions src/imagen_hub/infermodels/sd.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,44 @@ def __init__(self, device="cuda", weight="prompthero/openjourney"):

def infer_one_image(self, prompt: str = None, seed: int = 42):
return super().infer_one_image(prompt, seed)

class LCM():
def __init__(self, device="cuda", weight="SimianLuo/LCM_Dreamshaper_v7"):
"""
A class for the Latent Consistency Model. Require diffusers >= 0.22
Reference: https://github.com/luosiallen/latent-consistency-model#latent-consistency-models

Args:
device (str, optional): The device on which the model should run. Default is "cuda".
weight (str, optional): The pretrained model weights for OpenJourney. Default is "SimianLuo/LCM_Dreamshaper_v7".
"""
self.pipe = DiffusionPipeline.from_pretrained(
weight,
torch_dtype=torch.float16,
).to(device)

def infer_one_image(self, prompt: str = None, seed: int = 42, num_inference_steps: int = 4):
"""
Infer an image based on the given prompt and seed.

Args:
prompt (str, optional): The prompt for the image generation. Default is None.
seed (int, optional): The seed for random generator. Default is 42.
num_inference_steps (int, optional): inference steps. Recommend: 1~8 steps. Paper used 4.

Returns:
PIL.Image.Image: The inferred image.

Notes:
num_inference_steps can be set to 1~50 steps. LCM support fast inference even <= 4 steps.
(Max)I personally found 8 steps give a better result but the paper focus on using 4 steps.
"""
generator = torch.manual_seed(seed)
images = self.pipe(prompt=prompt,
num_inference_steps=num_inference_steps,
guidance_scale=8.0,
lcm_origin_steps=50,
generator=generator,
output_type="pil").images
return images[0]