Skip to content

Pranay22077/Image-Super-Resolution

Repository files navigation

Real-ESRGAN Image Super-Resolution

Python PyTorch CUDA

A Python implementation for upscaling low-resolution images using Real-ESRGAN (Real-Enhanced Super-Resolution Generative Adversarial Networks). This project provides an easy-to-use interface for enhancing image quality with 2x, 4x, or 8x upscaling.

πŸš€ Features

πŸ” Multiple Scale Options

  • Support for 2x, 4x, and 8x image upscaling
  • Flexible model selection

⚑ GPU Acceleration

  • Automatic CUDA detection
  • Faster processing with GPU support

πŸ“¦ Batch Processing

  • Process multiple images automatically
  • Error handling for robust operation

πŸ‘οΈ Visual Comparison

  • Side-by-side image comparison
  • Low-res vs Super-resolved vs Ground truth

πŸ–ΌοΈ Format Support

  • PNG, JPG, JPEG compatibility
  • Automatic format detection

πŸ› οΈ Easy Integration

  • Simple API interface
  • Minimal setup required

πŸ“‹ Installation

Quick Install

pip install git+https://github.com/sberbank-ai/Real-ESRGAN.git
pip install huggingface_hub==0.9.0

Required Dependencies

numpy
opencv-python
Pillow
torch>=1.7
torchvision>=0.8.0
tqdm
huggingface-hub
matplotlib (for visualization)

🎯 Usage

Basic Setup

from RealESRGAN import RealESRGAN
from PIL import Image
import numpy as np
import torch

# Initialize device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('device:', device)

# Create model with desired scale (2, 4, or 8)
model_scale = 4
model = RealESRGAN(device, scale=model_scale)
model.load_weights(f'weights/RealESRGAN_x{model_scale}.pth')

Processing Single Image

# Load and process image
lr_image = Image.open('path/to/low_res_image.jpg').convert('RGB')
lr_np = np.array(lr_image)

# Generate super-resolved image
sr_output = model.predict(lr_np)

# Convert to PIL Image and save
if isinstance(sr_output, Image.Image):
    sr_image = sr_output
else:
    sr_image = Image.fromarray(np.clip(sr_output, 0, 255).astype(np.uint8))

sr_image.save('path/to/output_image.jpg')

Batch Processing

import os
import matplotlib.pyplot as plt

# Set up folders
lr_folder = 'path/to/low_resolution_images'
hr_folder = 'path/to/high_resolution_ground_truth'  # Optional for comparison
sr_folder = 'results'
os.makedirs(sr_folder, exist_ok=True)

# Get image list
image_names = sorted([f for f in os.listdir(lr_folder) 
                     if f.lower().endswith(('.png', '.jpg', '.jpeg'))])

# Process all images
for img_name in image_names:
    print(f"Processing: {img_name}")
    lr_path = os.path.join(lr_folder, img_name)
    
    try:
        # Load and process
        lr_image = Image.open(lr_path).convert('RGB')
        lr_np = np.array(lr_image)
        sr_output = model.predict(lr_np)
        
        # Save result
        if isinstance(sr_output, Image.Image):
            sr_image_pil = sr_output
        else:
            sr_image_pil = Image.fromarray(np.clip(sr_output, 0, 255).astype(np.uint8))
        
        sr_image_pil.save(os.path.join(sr_folder, img_name))
        
    except Exception as e:
        print(f"Error processing {img_name}: {e}")

Visual Comparison

def show_side_by_side(lr, sr, hr, title):
    fig, ax = plt.subplots(1, 3, figsize=(15, 5))
    
    ax[0].imshow(lr)
    ax[0].set_title('Low-Res')
    ax[0].axis('off')
    
    ax[1].imshow(sr)
    ax[1].set_title('Super-Resolved (4x)')
    ax[1].axis('off')
    
    ax[2].imshow(hr)
    ax[2].set_title('High-Res (Ground Truth)')
    ax[2].axis('off')
    
    plt.suptitle(title)
    plt.tight_layout()
    plt.show()

# Use in processing loop
show_side_by_side(lr_image, sr_image_pil, hr_image, img_name)

πŸ“Š Model Scales

Scale Description Use Case
2x Doubles the resolution Quick enhancement, real-time processing
4x Quadruples the resolution Balanced quality and performance
8x 8x resolution increase Maximum quality, resource intensive

πŸ’» Hardware Requirements

Component Requirement Notes
GPU CUDA-compatible recommended Significantly faster processing
CPU Any modern processor Fallback option if no GPU
Memory 4GB+ RAM/VRAM Depends on image size and batch size

πŸ“ File Structure

project/
β”œβ”€β”€ weights/
β”‚   β”œβ”€β”€ RealESRGAN_x2.pth
β”‚   β”œβ”€β”€ RealESRGAN_x4.pth
β”‚   └── RealESRGAN_x8.pth
β”œβ”€β”€ input_images/
β”œβ”€β”€ results/
└── your_script.py

⚑ Performance Notes

πŸ’‘ Pro Tips

  • Model weights are automatically downloaded on first use
  • Processing time varies based on image size and hardware
  • GPU acceleration provides 5-10x speed improvement
  • Larger scale factors (8x) require more computational resources

πŸ”§ Troubleshooting

Common Issues

🚨 CUDA Out of Memory

Solution:

  • Reduce batch size
  • Process smaller images
  • Use lower scale factor (2x instead of 8x)
πŸ“¦ Dependency Conflicts

Solution:

  • Use the specified huggingface_hub==0.9.0 version
  • Create a fresh virtual environment
  • Install dependencies in the exact order provided
⚠️ Model Loading Errors

Solution:

  • Ensure stable internet connection for weight download
  • Check available disk space
  • Verify CUDA installation if using GPU

Error Handling

The implementation includes robust error handling for individual image processing failures, allowing batch operations to continue even if some images fail to process.

🎨 Output Quality

Real-ESRGAN excels at enhancing:

πŸ–ΌοΈ
Natural Images
Photographs, landscapes
🎨
Artwork
Illustrations, drawings
πŸ’»
Digital Content
Screenshots, UI elements
πŸ”
Fine Details
Textures, patterns

✨ Quality Promise: The model preserves important visual details while reducing artifacts commonly found in traditional upscaling methods.


🌟 Star this repository if you found it helpful! 🌟

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages