File size: 7,964 Bytes
bae46d6 17e72cc b12d2e4 17e72cc b12d2e4 17e72cc b12d2e4 17e72cc | 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 198 199 200 201 202 203 204 205 | ---
license: mit
language:
- en
new_version: KAHABKALU/KahabMiniGenT2Im
pipeline_tag: text-to-image
library_name: diffusers
tags:
- '#StableDiffusion'
- '#TextToImage'
- '#MachineLearning'
- '#ImageGeneration'
- '#LightweightModel'
- '#UNet'
- '#DiffusionModel'
- '#HuggingFace'
- '#TextToImageGeneration'
- '#MLModel'
---
# KahabMiniGenT2Im: Lightweight Stable Diffusion Model for Text-to-Image Generation
**Description**: KahabMiniGenT2Im is a lightweight Stable Diffusion model developed by Mohammed Kahab K, an ML engineer, for generating high-quality 256x256 images from text prompts. Trained on a small dataset, this tiny model is optimized for efficiency, making it suitable for low-resource environments like Google Colab or consumer GPUs. Leveraging a custom `UNetConditional` architecture, it provides effective text-to-image generation with minimal computational requirements.
## Model Details
- **Author**: Mohammed Kahab K
- **Architecture**: Custom `UNetConditional` (in_channels=4, base_channels=128, context_dim=768) for latent diffusion.
- **Training**: Trained on a small dataset for efficient text-to-image generation (replace with specific dataset details, e.g., “futuristic images with MSE loss and Adam optimizer”).
- **Output Resolution**: 256x256.
- **Features**:
- Lightweight and tiny model size for fast inference.
- Compatible with Stable Diffusion’s `Diffusers` library components (VAE, CLIP).
- Optimized for low-resource devices with `num_inference_steps=1000` for CUDA.
- **Files**:
- `KahabMinGenT2Im-v1.pt`: Model weights.
- `generate_images_direct.py`: Python script with `UNetConditional` class and generation pipeline.
- `demo.mp4`: Video showcasing model outputs.
- `thumbnail.jpg`: Thumbnail image for video.
## Generated Image...
### Prompt
`Cyberpunk alleyway`

### Prompt
`Fantasy forest`

### Prompt
`A friendly dragon`

## Setup
Install required dependencies for Stable Diffusion and text-to-image generation:
```bash
pip install torch diffusers transformers pillow numpy tqdm huggingface_hub
```
## Downloading Files
### For Google Colab
Download the model and script in a Colab notebook:
```python
from huggingface_hub import hf_hub_download
import sys
import os
# Download the script and model weights
script_path = hf_hub_download(repo_id="KAHABKALU/KahabMiniGenT2Im", filename="generate_images_direct.py")
model_path = hf_hub_download(repo_id="KAHABKALU/KahabMiniGenT2Im", filename="KahabMinGenT2Im-v1.pt")
# Add the script's directory to sys.path
sys.path.append(os.path.dirname(script_path))
```
Alternatively, use `wget`:
```python
!wget https://huggingface.co/KAHABKALU/KahabMiniGenT2Im/resolve/main/generate_images_direct.py -O /content/generate_images_direct.py
!wget https://huggingface.co/KAHABKALU/KahabMiniGenT2Im/resolve/main/KahabMinGenT2Im-v1.pt -O /content/KahabMinGenT2Im-v1.pt
```
### For Other Devices
Install dependencies and download files:
```bash
pip install torch diffusers transformers pillow numpy tqdm huggingface_hub
```
Download using Python:
```python
from huggingface_hub import hf_hub_download
import sys
import os
script_path = hf_hub_download(repo_id="KAHABKALU/KahabMiniGenT2Im", filename="generate_images_direct.py")
model_path = hf_hub_download(repo_id="KAHABKALU/KahabMiniGenT2Im", filename="KahabMinGenT2Im-v1.pt")
sys.path.append(os.path.dirname(script_path))
```
Or use `wget` or `curl`:
```bash
wget https://huggingface.co/KAHABKALU/KahabMiniGenT2Im/resolve/main/generate_images_direct.py -O generate_images_direct.py
wget https://huggingface.co/KAHABKALU/KahabMiniGenT2Im/resolve/main/KahabMinGenT2Im-v1.pt -O KahabMinGenT2Im-v1.pt
```
## Usage
The `generate_images_direct.py` script enables text-to-image generation with custom prompts in Colab, Jupyter, or command-line environments. For optimal quality, especially on CUDA, use `num_inference_steps=1000` (equivalent to `timesteps=1000` in Stable Diffusion terminology).
### Interactive Usage (Colab/Jupyter)
Run in a Colab or Jupyter notebook for interactive prompt input:
```python
import sys
import os
from huggingface_hub import hf_hub_download
import torch
# Download the script and model weights
script_path = hf_hub_download(repo_id="KAHABKALU/KahabMiniGenT2Im", filename="generate_images_direct.py")
model_path = hf_hub_download(repo_id="KAHABKALU/KahabMiniGenT2Im", filename="KahabMinGenT2Im-v1.pt")
sys.path.append(os.path.dirname(script_path))
from generate_images_direct import generate_images_direct
generate_images_direct(
unet_path=model_path,
device="cuda" if torch.cuda.is_available() else "cpu",
output_dir="output",
prompt=None # Prompts for input (e.g., "A glowing cyberpunk city at night")
)
```
### Command-Line Usage
Run with a custom prompt:
```bash
python generate_images_direct.py --prompt "A glowing cyberpunk city at night" --unet_path KahabMinGenT2Im-v1.pt --device cuda --output_dir output
```
### Example Code
Generate a 256x256 image in Colab with a specific prompt:
```python
import sys
import os
import torch
sys.path.append(os.path.dirname(script_path)) # Ensure the script is importable
from generate_images_direct import generate_images_direct
# Generate an image with a custom prompt
generate_images_direct(
unet_path=model_path,
device="cuda" if torch.cuda.is_available() else "cpu",
output_dir="/content/sample_data",
prompt="A delicate glass sculpture",
num_inference_steps=1000 # Optimized for better results, especially on CUDA
)
```
### Viewing Images in Colab
Display generated images in Colab:
```python
from IPython.display import Image
import os
output_dir = "/content/sample_data"
for file in os.listdir(output_dir):
if file.endswith(".png"):
display(Image(filename=os.path.join(output_dir, file)))
```
## Notes
- **File Format**: Ensure `generate_images_direct.py` is a valid Python script (not a Jupyter notebook) to avoid import errors. Use `sys.path.append(os.path.dirname(script_path))` in Colab.
- **Video File**: Keep `demo.mp4` under 10MB to avoid Git LFS issues. Compress using tools like FFmpeg if needed.
- **Compatibility**: The pipeline uses the VAE from `CompVis/stable-diffusion-v1-4` and text encoder from `openai/clip-vit-large-patch14`.
- **Performance**: Set `num_inference_steps=1000` for high-quality images on CUDA. For low-memory devices, try `500` steps to avoid crashes.
- **Large Files**: If `KahabMinGenT2Im-v1.pt` or `demo.mp4` is >5GB, Git LFS is used automatically. For >10GB, contact Hugging Face support.
- **GPU in Colab**: Use a GPU runtime (Runtime > Change runtime type > GPU) for faster generation.
- **Community**: Share your results on X or Reddit with #StableDiffusion, #TextToImage, #MachineLearning to increase visibility.
## License
MIT License
Copyright (c) 2025 Mohammed Kahab K
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |