--- 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` ![Generated image](example1.jpg) ### Prompt `Fantasy forest` ![Generated image](example2.jpg) ### Prompt `A friendly dragon` ![Generated image](example3.jpg) ## 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.