alfredplpl/Japanese-photos
Viewer • Updated • 756 • 330 • 4
How to use alfredplpl/F-Lite-Japanese-LoRA with Diffusers:
pip install -U diffusers transformers accelerate
import torch
from diffusers import DiffusionPipeline
# switch to "mps" for apple devices
pipe = DiffusionPipeline.from_pretrained("alfredplpl/F-Lite-Japanese-LoRA", dtype=torch.bfloat16, device_map="cuda")
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
image = pipe(prompt).images[0]Copyright-safeと言っているF-Liteを日本っぽくするLoRAつくりました。 学習データはCC-0なので、これもCopyright-safeとなります。
例えば、学習前のF-Liteにマグロを含む日本の寿司というプロンプトで画像を作ります。
すると、若干、CGっぽくなります。そこでこのLoRAを適用すると、
と写真っぽくなりました。このように日本のものが写真っぽくなります。
Copyright-safeであるならば、生成結果に対して著作権侵害を気にしない画像生成の改造もできるのではないでしょうか。 これにより、マスメディアで気軽に画像生成AIを使えるようになると期待しています。
import torch
from f_lite import FLitePipeline
from peft import LoraConfig, set_peft_model_state_dict
from diffusers.pipelines.pipeline_loading_utils import LOADABLE_CLASSES, ALL_IMPORTABLE_CLASSES
# Trick required because it is not a native diffusers model
from diffusers.pipelines.pipeline_loading_utils import LOADABLE_CLASSES, ALL_IMPORTABLE_CLASSES
LOADABLE_CLASSES["f_lite"] = LOADABLE_CLASSES["f_lite.model"] = {"DiT": ["save_pretrained", "from_pretrained"]}
ALL_IMPORTABLE_CLASSES["DiT"] = ["save_pretrained", "from_pretrained"]
pipeline = FLitePipeline.from_pretrained("Freepik/F-Lite", torch_dtype=torch.bfloat16)
pipeline.enable_model_cpu_offload() # For less memory consumption. Alternatively, pipeline.to("cuda")
pipeline.vae.enable_slicing()
pipeline.vae.enable_tiling()
# Set LoRA configuration
target_modules = ["qkv", "context_kv", "proj", "q"] # Target modules detected from LoRA weights
lora_rank = 128 # LoRA rank of your lora weights
# Create LoRA configuration
lora_config = LoraConfig(
r=lora_rank,
lora_alpha=lora_rank, # Typically set to the same value as rank
target_modules=target_modules,
bias="none",
init_lora_weights=False, # Don't initialize weights, use loaded ones
)
# Add LoRA adapter to the model
pipeline.dit_model.add_adapter(lora_config)
# Load LoRA weights
lora_state_dict = torch.load("lora_weights.pt", weights_only=True)
set_peft_model_state_dict(pipeline.dit_model, lora_state_dict)
# Generate an image
output = pipeline(
prompt="A photo of Japanese sushi including tuna.",
height=1024,
width=1024,
num_inference_steps=30,
guidance_scale=3.0,
negative_prompt=None,
)
# Save the generated image
output.images[0].save("generated_image.png")
Base model
Freepik/F-Lite