Skip to content

Mihawk1891/Unpotatofy

Repository files navigation

Unpotatofy

A robust, modular image enhancement pipeline using lightweight GAN models for deblurring, super-resolution, and colorization with fully offline operation.

🎯 Key Features

  • Intelligent Analysis: Automatically detects blur, resolution, and color characteristics
  • Conditional Processing: Only applies necessary enhancements (no unnecessary computation)
  • Memory Efficient: Sequential model loading/unloading for low-memory systems
  • Modular Architecture: Easy to swap models or add new enhancement types
  • Offline Operation: No internet required after model download
  • Explainable: Detailed logging of analysis and processing steps
  • Robust: Graceful degradation when models are unavailable

🏗️ Architecture

Input Image
    ↓
[Image Analyzer]
    ↓
Quality Analysis
    ├─ Blur Detection (Laplacian Variance)
    ├─ Color Analysis (Grayscale Detection)
    └─ Resolution Analysis
    ↓
[Pipeline Builder]
    ↓
Conditional Enhancement Pipeline
    ├─ Deblur (if blurred)
    ├─ Super-Resolution (always)
    └─ Colorization (if grayscale)
    ↓
[Memory Manager]
    ↓
Sequential Model Processing
    ├─ Load Model
    ├─ Process Image
    └─ Unload Model (free memory)
    ↓
Enhanced Output

📋 Requirements

Hardware

  • Minimum: CPU only, 8GB RAM
  • Recommended: 4GB GPU, 16GB RAM
  • Storage: ~500MB for all models

Software

Python 3.8+
PyTorch 2.0+
OpenCV (cv2)
NumPy
Pillow

🚀 Quick Start

1. Installation

# Clone or download the system
cd Unpotatofy-utility

# Install dependencies
pip install torch torchvision opencv-python numpy pillow

# Create models directory
mkdir models

2. Download Models

See model_integration.py for detailed instructions. Quick overview:

cd models

# Deblurring (NAFNet - 50MB)
NOTE: Please ensure that updated links are used if the existing ones have expired.


wget https://github.com/megvii-research/NAFNet/releases/download/v1.0/NAFNet-GoPro-width64.pth
mv NAFNet-GoPro-width64.pth deblur.pth


# Super-Resolution (Real-ESRGAN - 17MB)
wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth
mv RealESRGAN_x2plus.pth super_resolution.pth


# Colorization (DeOldify - 120MB)
wget https://data.deepai.org/deoldify/ColorizeStable_gen.pth
mv ColorizeStable_gen.pth colorization.pth

cd ..

3. Process Images

Single Image

python main.py input.jpg

With Custom Output

python main.py input.jpg -o enhanced_output.jpg

Batch Processing

python main.py -d ./input_images -o ./output_images

CPU Mode

python main.py input.jpg --device cpu

📖 Usage Examples

Python API

from main import ImageEnhancementPipeline

# Create pipeline
pipeline = ImageEnhancementPipeline(
    models_dir='models',
    device='auto'  # or 'cpu', 'cuda'
)

# Process single image
result = pipeline.process(
    image_path='input.jpg',
    output_path='output.jpg',
    save_intermediate=True
)

print(f"Applied steps: {result['steps_applied']}")
print(f"Analysis: {result['analysis']}")

# Batch process
results = pipeline.batch_process(
    input_dir='./images',
    output_dir='./enhanced'
)

Custom Analysis Thresholds

from main import ImageAnalyzer

# Create analyzer with custom thresholds
analyzer = ImageAnalyzer()
analyzer.blur_threshold = 150.0  # Higher = more sensitive
analyzer.grayscale_threshold = 15.0  # Higher = more sensitive

# Analyze image
import cv2
image = cv2.imread('input.jpg')
analysis = analyzer.analyze(image)

print(f"Is blurred: {analysis['blur']['is_blurred']}")
print(f"Is grayscale: {analysis['color']['is_grayscale']}")

Memory-Efficient Processing

from main import ImageEnhancementPipeline

# Explicit CPU mode for very low memory
pipeline = ImageEnhancementPipeline(device='cpu')

# Process large images in tiles automatically
result = pipeline.process('large_image.jpg')

🔧 Configuration

Model Selection

Edit model_integration.py to change default models:

RECOMMENDED_MODELS = {
    "deblur": {
        "name": "NAFNet",  # or "DeblurGAN-v2"
        ...
    },
    "super_resolution": {
        "name": "Real-ESRGAN",  # or "SwinIR-Light"
        ...
    },
    "colorization": {
        "name": "DeOldify",  # or "ChromaGAN"
        ...
    }
}

Pipeline Customization

from main import ImageEnhancementPipeline

class CustomPipeline(ImageEnhancementPipeline):
    def process(self, image_path, **kwargs):
        # Add custom preprocessing
        image = self.preprocess(image_path)
        
        # Run standard pipeline
        result = super().process(image_path, **kwargs)
        
        # Add custom postprocessing
        self.postprocess(result)
        
        return result

📊 Analysis Metrics

Blur Detection

  • Method: Laplacian Variance
  • Threshold: 100.0 (adjustable)
  • Interpretation: Lower variance = more blur

Grayscale Detection

  • Method: Channel Variance
  • Threshold: 10.0 (adjustable)
  • Interpretation: Lower variance = less color diversity

Resolution Analysis

  • Criterion: Total pixels < 1,000,000
  • Action: Recommend 2x upscaling if below threshold

🎨 Model Details

1. Deblurring (NAFNet)

Purpose: Remove motion blur, defocus, and other blur types

Specifications:

  • Input: RGB image (any size)
  • Output: Deblurred RGB image (same size)
  • Memory: ~1.5GB GPU / ~3GB RAM
  • Speed: ~0.5s per 512x512 image (GPU)

When Applied: Only when blur score < 100.0

2. Super-Resolution (Real-ESRGAN)

Purpose: Enhance resolution and recover fine details

Specifications:

  • Input: RGB image (any size)
  • Output: 2x or 4x upscaled image
  • Memory: ~2GB GPU / ~4GB RAM
  • Speed: ~1s per 512x512 image (GPU)

When Applied: Always (general restoration)

3. Colorization (DeOldify)

Purpose: Add realistic color to grayscale images

Specifications:

  • Input: Grayscale image
  • Output: Colorized RGB image
  • Memory: ~2.5GB GPU / ~5GB RAM
  • Speed: ~2s per 512x512 image (GPU)

When Applied: Only when image is grayscale

💾 Memory Management

Automatic Memory Optimization

  1. Sequential Loading: Models loaded one at a time
  2. Automatic Unloading: Models unloaded after use
  3. Cache Clearing: GPU cache cleared between models
  4. Tile Processing: Large images split into tiles

Manual Memory Control

from main import ModelManager

manager = ModelManager(device='cuda')

# Load specific model
model = manager.load_model('deblur')

# Process
output = model(input_tensor)

# Explicitly unload
manager.unload_model('deblur')

# Clear all models
manager.clear_all()

🧪 Testing and Validation

Test Image Generation

import numpy as np
import cv2

# Create test images
def create_blurred_image():
    img = cv2.imread('sharp.jpg')
    blurred = cv2.GaussianBlur(img, (15, 15), 0)
    cv2.imwrite('test_blurred.jpg', blurred)

def create_grayscale_image():
    img = cv2.imread('color.jpg')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.imwrite('test_grayscale.jpg', gray)

def create_low_res_image():
    img = cv2.imread('high_res.jpg')
    low_res = cv2.resize(img, None, fx=0.25, fy=0.25)
    cv2.imwrite('test_low_res.jpg', low_res)

Quality Metrics

from skimage.metrics import peak_signal_noise_ratio as psnr
from skimage.metrics import structural_similarity as ssim

def evaluate_enhancement(original, enhanced):
    """Compute quality metrics."""
    psnr_value = psnr(original, enhanced)
    ssim_value = ssim(original, enhanced, channel_axis=2)
    
    return {
        'psnr': psnr_value,  # Higher is better (>25 dB good)
        'ssim': ssim_value   # Higher is better (>0.9 good)
    }

🐛 Troubleshooting

Common Issues

1. Out of Memory Error

RuntimeError: CUDA out of memory

Solution:

  • Use CPU mode: --device cpu
  • Reduce image size before processing
  • Process in smaller tiles

2. Model Not Found

Model deblur not found at models/deblur.pth

Solution:

  • Download models as per setup instructions
  • Check model file paths
  • Verify file permissions

3. Slow Processing

Solution:

  • Use GPU if available
  • Reduce image resolution
  • Process in batch mode
  • Close other applications

4. Poor Quality Results

Solution:

  • Check input image quality
  • Verify model checksums
  • Adjust analysis thresholds
  • Try different models

Debug Mode

import logging

logging.basicConfig(level=logging.DEBUG)

pipeline = ImageEnhancementPipeline()
result = pipeline.process('input.jpg')

📈 Performance Benchmarks

GPU (NVIDIA GTX 1650, 4GB)

Image Size Deblur Super-Res Colorize Total
512x512 0.5s 1.0s 2.0s 3.5s
1024x1024 1.8s 3.5s 6.0s 11.3s
1920x1080 3.2s 6.5s 10.0s 19.7s

CPU (Intel i5, 16GB RAM)

Image Size Deblur Super-Res Colorize Total
512x512 5s 10s 20s 35s
1024x1024 18s 35s 60s 113s
1920x1080 32s 65s 100s 197s

🔬 Advanced Features

Custom Model Integration

from main import ImageEnhancer

class CustomEnhancer(ImageEnhancer):
    def custom_enhancement(self, image):
        """Add your custom enhancement."""
        model = self.model_manager.load_model("custom")
        
        # Process
        input_tensor = self._prepare_input(image)
        output = model(input_tensor)
        result = self._postprocess_output(output)
        
        self.model_manager.unload_model("custom")
        return result

Pipeline Hooks

def pre_process_hook(image):
    """Called before processing."""
    print(f"Processing image: {image.shape}")
    return image

def post_process_hook(image, analysis):
    """Called after processing."""
    print(f"Steps applied: {analysis['pipeline_steps']}")
    return image

pipeline = ImageEnhancementPipeline()
pipeline.pre_hook = pre_process_hook
pipeline.post_hook = post_process_hook

📚 References

Papers

Alternative Models

  • Deblur: DeblurGAN-v2, MIMO-UNet, MPRNet
  • Super-Resolution: SwinIR, HAT, EDSR
  • Colorization: ChromaGAN, Colorful Image Colorization

🤝 Contributing

Adding New Models

  1. Create model wrapper in model_integration.py
  2. Add to RECOMMENDED_MODELS dict
  3. Implement in ImageEnhancer class
  4. Update documentation

Testing

# Run test suite
python -m pytest tests/

# Test specific component
python -m pytest tests/test_analyzer.py

📄 License

This system is designed for research and educational purposes. Model licenses:

  • NAFNet: Apache 2.0
  • Real-ESRGAN: BSD 3-Clause
  • DeOldify: MIT

🙏 Acknowledgments

Built with:

  • PyTorch ecosystem
  • OpenCV community
  • GAN research community

Version: 1.0.0
Last Updated: February 2026
Maintainer: Image Enhancement System Team

About

Unpotatofy-utility is a fully offline, memory-efficient image enhancement pipeline that automatically analyzes images and conditionally applies deblurring, super-resolution, and colorization using lightweight GAN models—designed for modularity, low-resource systems, and explainable processing.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors