Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Optimized AI Image Generator for Google Colab

A high-performance, memory-optimized image generation interface built specifically for Google Colab environments. This project provides a user-friendly Gradio interface for generating high-quality images using Hugging Face's Diffusers library with advanced memory management and performance optimizations.

Python PyTorch License Colab

✨ Features

🧠 Smart Memory Management

  • Aggressive cleanup: Automatic GPU memory clearing between operations
  • CPU offloading: Intelligent memory offloading to prevent OOM errors
  • Sequential offloading: Maximum memory efficiency for large models
  • Real-time monitoring: Live GPU memory usage tracking

⚑ Performance Optimizations

  • TF32 acceleration: Faster computation on modern GPUs
  • xFormers attention: Memory-efficient attention mechanisms
  • VAE optimizations: Slicing and tiling for large images
  • CUDA optimizations: Benchmark mode and cache management

🎨 Enhanced User Experience

  • Modern interface: Clean, professional Gradio UI
  • Batch generation: Generate multiple images at once
  • Memory monitor: Real-time system resource tracking
  • Download support: Easy image saving and sharing
  • Error recovery: Graceful handling of memory issues

πŸ”§ Colab-Specific Features

  • Multiple offloading strategies: Balance between speed and memory
  • Session management: Better handling of runtime limitations
  • Automatic device detection: Works on GPU and CPU
  • Share links: Generate public URLs for sharing

πŸš€ Quick Start

Option 1: Direct Colab Launch

Open In Colab

Option 2: Manual Setup

  1. Enable GPU Runtime

    Runtime β†’ Change runtime type β†’ T4 GPU β†’ Save
    
  2. Install Dependencies

    !pip install torch
    !pip install -U xformers --index-url https://download.pytorch.org/whl/cu126
    !pip install diffusers==0.32.2
    !pip install transformers==4.49
  3. Run the Application

    # Download and run the optimized script
    !wget https://github.com/abhi-0203/Optimized-AI-Image-Generator-for-Google-Colab/blob/main/optimized_genai_colab.ipynb
    %run optimized_genai_colab.py

πŸ“‹ Usage

Basic Image Generation

  1. Load a model: Enter a Hugging Face model ID (e.g., runwayml/stable-diffusion-v1-5)
  2. Configure settings:
    • Enable CPU offloading if you encounter memory issues
    • Adjust image dimensions and inference steps
  3. Enter prompts: Write your image description and negative prompts
  4. Generate: Click generate and wait for your image!

Recommended Models

Free Tier (T4 GPU - 15GB VRAM)

runwayml/stable-diffusion-v1-5          # Lightweight and fast
stabilityai/stable-diffusion-2-1-base   # Good quality/speed balance
dreamlike-art/dreamlike-diffusion-1.0   # Artistic style

Pro/Pro+ (A100/V100)

stabilityai/stable-diffusion-xl-base-1.0  # High quality
stabilityai/sdxl-turbo                     # Fast SDXL variant
runwayml/stable-diffusion-v1-5             # Still great for speed

πŸ› οΈ Advanced Configuration

Memory Optimization Levels

# Level 1: GPU only (fastest, highest memory usage)
generator.load_model("model-id", use_cpu_offload=False, use_sequential_offload=False)

# Level 2: CPU offloading (balanced)
generator.load_model("model-id", use_cpu_offload=True, use_sequential_offload=False)

# Level 3: Sequential offloading (slowest, lowest memory usage)
generator.load_model("model-id", use_cpu_offload=False, use_sequential_offload=True)

Generation Parameters

Parameter Recommended Range Description
Steps 15-30 Quality vs speed trade-off
Guidance Scale 7.0-12.0 Prompt adherence strength
Image Size 512-1024px Multiples of 64 work best
Batch Size 1-4 Number of images to generate

πŸ”§ Technical Details

System Requirements

  • Python: 3.10+
  • PyTorch: 2.0+
  • CUDA: 11.8+ (for GPU acceleration)
  • RAM: 8GB+ system RAM
  • VRAM: 6GB+ for most models

Key Dependencies

diffusers>=0.21.0
transformers>=4.25.0
accelerate>=0.16.0
xformers>=0.0.16
gradio>=4.0.0
torch>=2.0.0

Architecture

The application is built with:

  • Core: OptimizedImageGenerator class with smart memory management
  • Interface: Gradio web interface with custom CSS
  • Backend: Hugging Face Diffusers pipeline
  • Optimization: xFormers, CPU offloading, VAE optimizations

πŸ“Š Performance Benchmarks

Configuration Model Image Size Steps Time (T4) Memory Usage
Standard SD 1.5 512x512 20 ~8s ~4GB
Optimized SD 1.5 512x512 20 ~6s ~2.5GB
CPU Offload SDXL 1024x1024 25 ~45s ~8GB
Sequential SDXL 1024x1024 25 ~60s ~4GB

πŸ› Troubleshooting

Common Issues

Out of Memory Errors

# Solutions (in order of preference):
1. Enable CPU offloading
2. Reduce image dimensions (1024β†’768β†’512)
3. Lower inference steps (30β†’20β†’15)
4. Use sequential offloading
5. Restart runtime and try again

Model Loading Issues

# Common fixes:
1. Check internet connection
2. Verify model ID spelling
3. Clear memory before loading new model
4. Try alternative model variants

Slow Generation

# Optimization steps:
1. Ensure GPU runtime is enabled
2. Verify CUDA availability
3. Reduce image size and steps
4. Use CPU offloading only when necessary

Memory Management Tips

  1. Monitor usage: Use the built-in memory monitor
  2. Clear regularly: Click "Clear Memory" between model switches
  3. Start small: Begin with 768x768 images
  4. Use offloading: Enable for models >6GB

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Development Setup

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes
  4. Test in Colab: Ensure compatibility
  5. Commit changes: git commit -m 'Add amazing feature'
  6. Push to branch: git push origin feature/amazing-feature
  7. Open a Pull Request

Code Style

  • Follow PEP 8 guidelines
  • Use type hints where appropriate
  • Add docstrings for new functions
  • Test changes in Google Colab

πŸ™ Acknowledgments

  • Hugging Face: For the amazing Diffusers library
  • Stability AI: For Stable Diffusion models
  • Google Colab: For providing free GPU access
  • Gradio: For the excellent web interface framework

πŸ“ž Support

πŸ“ˆ Roadmap

  • Support for ControlNet models
  • Inpainting and outpainting features
  • Image-to-image generation
  • Advanced prompt engineering tools
  • Multi-model comparison interface
  • API endpoint for programmatic access

Star ⭐ this repository if you find it helpful!

Made with ❀️ for the AI community

About

High-performance AI image generator optimized for Google Colab. Features smart memory management, multiple model support, and easy-to-use web interface.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages