File size: 6,112 Bytes
a983e6e 6c76e8d 0ef4d1f a983e6e 0c5282d a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 4f90b4a a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 a983e6e 7ca1745 |
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
---
license: mit
datasets:
- uwunish/ghibli-dataset
language:
- en
base_model:
- stabilityai/stable-diffusion-2-1-base
pipeline_tag: text-to-image
library_name: diffusers
tags:
- ghibli
- text2image
- finetune
- sd-2.1
---
<div align="center">
<h1>
Ghibli Fine-Tuned Stable Diffusion 2.1
</h1>
</div>
## Dataset
Avalible at: https://huggingface.co/datasets/uwunish/ghibli-dataset.
## Hyperparameters
The fine-tuning process was optimized with the following hyperparameters:
| Hyperparameter | Value |
| --- | --- |
| `learning_rate` | 1e-05 |
| `num_train_epochs` | 40 |
| `train_batch_size` | 2 |
| `gradient_accumulation_steps` | 2 |
| `mixed_precision` | "fp16" |
| `resolution` | 512 |
| `max_grad_norm` | 1 |
| `lr_scheduler` | "constant" |
| `lr_warmup_steps` | 0 |
| `checkpoints_total_limit` | 1 |
| `use_ema` | True |
| `use_8bit_adam` | True |
| `center_crop` | True |
| `random_flip` | True |
| `gradient_checkpointing` | True |
These parameters were carefully selected to balance training efficiency and model performance, leveraging techniques like mixed precision and gradient checkpointing.
## Metrics
The fine-tuning process achieved a final loss of **0.0345**, indicating excellent convergence and high fidelity to the Ghibli art style.
## Usage
### Step 1: Import Required Libraries
Begin by importing the necessary libraries to power the image generation pipeline.
```python
import torch
from PIL import Image
import numpy as np
from transformers import CLIPTextModel, CLIPTokenizer
from diffusers import AutoencoderKL, UNet2DConditionModel, PNDMScheduler
from tqdm import tqdm
```
### Step 2: Configure the Model
Set up the device, data type, and load the pre-trained Ghibli-fine-tuned Stable Diffusion model.
```python
# Configure device and data type
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
# Model path
model_name = "danhtran2mind/ghibli-fine-tuned-sd-2.1"
# Load model components
vae = AutoencoderKL.from_pretrained(model_name, subfolder="vae", torch_dtype=dtype).to(device)
tokenizer = CLIPTokenizer.from_pretrained(model_name, subfolder="tokenizer")
text_encoder = CLIPTextModel.from_pretrained(model_name, subfolder="text_encoder", torch_dtype=dtype).to(device)
unet = UNet2DConditionModel.from_pretrained(model_name, subfolder="unet", torch_dtype=dtype).to(device)
scheduler = PNDMScheduler.from_pretrained(model_name, subfolder="scheduler")
```
### Step 3: Define the Image Generation Function
Use the following function to generate Ghibli-style images based on your text prompts.
```python
def generate_image(prompt, height=512, width=512, num_inference_steps=50, guidance_scale=3.5, seed=42):
"""Generate a Ghibli-style image from a text prompt."""
# Set random seed for reproducibility
generator = torch.Generator(device=device).manual_seed(int(seed))
# Tokenize and encode the prompt
text_input = tokenizer(
[prompt], padding="max_length", max_length=tokenizer.model_max_length, truncation=True, return_tensors="pt"
)
with torch.no_grad():
text_embeddings = text_encoder(text_input.input_ids.to(device))[0].to(dtype=dtype)
# Encode an empty prompt for classifier-free guidance
uncond_input = tokenizer(
[""], padding="max_length", max_length=text_input.input_ids.shape[-1], return_tensors="pt"
)
with torch.no_grad():
uncond_embeddings = text_encoder(uncond_input.input_ids.to(device))[0].to(dtype=dtype)
text_embeddings = torch.cat([uncond_embeddings, text_embeddings])
# Initialize latent representations
latents = torch.randn(
(1, unet.config.in_channels, height // 8, width // 8),
generator=generator,
dtype=dtype,
device=device
)
# Configure scheduler timesteps
scheduler.set_timesteps(num_inference_steps)
latents = latents * scheduler.init_noise_sigma
# Denoising loop
for t in tqdm(scheduler.timesteps, desc="Generating image"):
latent_model_input = torch.cat([latents] * 2)
latent_model_input = scheduler.scale_model_input(latent_model_input, t)
with torch.no_grad():
if device.type == "cuda":
with torch.autocast(device_type="cuda", dtype=torch.float16):
noise_pred = unet(latent_model_input, t, encoder_hidden_states=text_embeddings).sample
else:
noise_pred = unet(latent_model_input, t, encoder_hidden_states=text_embeddings).sample
# Apply classifier-free guidance
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
latents = scheduler.step(noise_pred, t, latents).prev_sample
# Decode latents to image
with torch.no_grad():
latents = latents / vae.config.scaling_factor
image = vae.decode(latents).sample
# Convert to PIL Image
image = (image / 2 + 0.5).clamp(0, 1)
image = image.detach().cpu().permute(0, 2, 3, 1).numpy()
image = (image * 255).round().astype("uint8")
return Image.fromarray(image[0])
```
### Step 4: Generate Your Image
Craft a vivid prompt and generate your Ghibli-style masterpiece.
```python
# Example prompt
prompt = "a serene landscape in Ghibli style"
# Generate the image
image = generate_image(
prompt=prompt,
height=512,
width=512,
num_inference_steps=50,
guidance_scale=3.5,
seed=42
)
# Display or save the image
image.show() # Or image.save("ghibli_landscape.png")
```
## Environment
The project was developed and tested in the following environment:
- **Python Version**: 3.11.11
- **Dependencies**:
| Library | Version |
| --- | --- |
| huggingface-hub | 0.30.2 |
| accelerate | 1.3.0 |
| bitsandbytes | 0.45.5 |
| torch | 2.5.1 |
| Pillow | 11.1.0 |
| numpy | 1.26.4 |
| transformers | 4.51.1 |
| torchvision | 0.20.1 |
| diffusers | 0.33.1 |
| gradio | Latest |
Ensure your environment matches these specifications to avoid compatibility issues. |