Views
No views yet
├── main/
│ ├── attention.py # Multi-head attention implementation
│ ├── clip.py # CLIP text encoder
│ ├── ddpm.py # DDPM sampling algorithm
│ ├── decoder.py # VAE decoder for image reconstruction
│ ├── diffusion.py # U-Net diffusion model
│ ├── encoder.py # VAE encoder for image compression
│ ├── model_converter.py # Converts checkpoint files to PyTorch format
│ ├── model_loader.py # Loads and manages model weights
│ ├── pipeline.py # Main generation pipeline
│ └── demo.py # Example usage and demonstration
├── data/ # Model weights and tokenizer files
└── images/ # Input/output images1git clone https://github.com/https://github.com/ApoorvBrooklyn/Stable-Diffusion
2cd pytorch-stable-diffusion1python -m venv venv
2source venv/bin/activate # On Windows: venv\Scripts\activate1pip install torch torchvision torchaudio
2pip install transformers pillow numpy tqdmvocab.json and merges.txt from Stable Diffusion v1.5 tokenizerv1-5-pruned-emaonly.ckpt from Stable Diffusion v1.5data/ folder1import model_loader
2import pipeline
3from transformers import CLIPTokenizer
4
5# Initialize tokenizer and load models
6tokenizer = CLIPTokenizer("data/vocab.json", merges_file="data/merges.txt")
7models = model_loader.preload_models_from_standard_weights("data/v1-5-pruned-emaonly.ckpt", "cpu")
8
9# Generate image from text
10output_image = pipeline.generate(
11 prompt="A beautiful sunset over mountains, highly detailed, 8k resolution",
12 uncond_prompt="", # Negative prompt
13 do_cfg=True,
14 cfg_scale=8,
15 sampler_name="ddpm",
16 n_inference_steps=50,
17 seed=42,
18 models=models,
19 device="cpu",
20 tokenizer=tokenizer
21)1from PIL import Image
2
3# Load input image
4input_image = Image.open("images/input.jpg")
5
6# Generate transformed image
7output_image = pipeline.generate(
8 prompt="Transform this into a watercolor painting",
9 input_image=input_image,
10 strength=0.8, # Controls how much to change the input
11 # ... other parameters
12)model_converter.py script converts Stable Diffusion checkpoint files to PyTorch format:python main/model_converter.py --checkpoint_path data/v1-5-pruned-emaonly.ckpt --output_dir converted_models/idle_device="cpu" to free GPU memory