Spaces:
Runtime error
Runtime error
test commit
Browse files- app.py +133 -150
- examples/example1.png +0 -0
- model/best_deblur_model.pth +3 -0
app.py
CHANGED
|
@@ -1,154 +1,137 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
-
import random
|
| 4 |
-
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 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 |
-
inputs=[
|
| 141 |
-
prompt,
|
| 142 |
-
negative_prompt,
|
| 143 |
-
seed,
|
| 144 |
-
randomize_seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
-
guidance_scale,
|
| 148 |
-
num_inference_steps,
|
| 149 |
-
],
|
| 150 |
-
outputs=[result, seed],
|
| 151 |
-
)
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torchvision.transforms as transforms
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import numpy as np
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Check if CUDA is available
|
| 10 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 11 |
+
print(f"Using device: {device}")
|
| 12 |
+
|
| 13 |
+
class DeblurNet(nn.Module):
|
| 14 |
+
def __init__(self):
|
| 15 |
+
super(DeblurNet, self).__init__()
|
| 16 |
+
|
| 17 |
+
# Encoder
|
| 18 |
+
self.enc_conv1 = self.conv_block(3, 64)
|
| 19 |
+
self.enc_conv2 = self.conv_block(64, 128)
|
| 20 |
+
self.enc_conv3 = self.conv_block(128, 256)
|
| 21 |
+
|
| 22 |
+
# Bottleneck
|
| 23 |
+
self.bottleneck = self.conv_block(256, 512)
|
| 24 |
+
|
| 25 |
+
# Decoder with residual connections
|
| 26 |
+
self.dec_conv1 = self.conv_block(512 + 256, 256)
|
| 27 |
+
self.dec_conv2 = self.conv_block(256 + 128, 128)
|
| 28 |
+
self.dec_conv3 = self.conv_block(128 + 64, 64)
|
| 29 |
+
self.final_conv = nn.Conv2d(64, 3, kernel_size=3, padding=1)
|
| 30 |
+
|
| 31 |
+
# Pooling and upsampling
|
| 32 |
+
self.pool = nn.MaxPool2d(2, 2)
|
| 33 |
+
self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
|
| 34 |
+
|
| 35 |
+
def conv_block(self, in_channels, out_channels):
|
| 36 |
+
return nn.Sequential(
|
| 37 |
+
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
|
| 38 |
+
nn.ReLU(inplace=True),
|
| 39 |
+
nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1),
|
| 40 |
+
nn.ReLU(inplace=True)
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
def forward(self, x):
|
| 44 |
+
# Encoder
|
| 45 |
+
x1 = self.enc_conv1(x)
|
| 46 |
+
x2 = self.pool(x1)
|
| 47 |
+
|
| 48 |
+
x2 = self.enc_conv2(x2)
|
| 49 |
+
x3 = self.pool(x2)
|
| 50 |
+
|
| 51 |
+
x3 = self.enc_conv3(x3)
|
| 52 |
+
x4 = self.pool(x3)
|
| 53 |
+
|
| 54 |
+
# Bottleneck
|
| 55 |
+
x4 = self.bottleneck(x4)
|
| 56 |
+
|
| 57 |
+
# Decoder with skip connections
|
| 58 |
+
x = self.upsample(x4)
|
| 59 |
+
x = torch.cat([x, x3], dim=1)
|
| 60 |
+
x = self.dec_conv1(x)
|
| 61 |
+
|
| 62 |
+
x = self.upsample(x)
|
| 63 |
+
x = torch.cat([x, x2], dim=1)
|
| 64 |
+
x = self.dec_conv2(x)
|
| 65 |
+
|
| 66 |
+
x = self.upsample(x)
|
| 67 |
+
x = torch.cat([x, x1], dim=1)
|
| 68 |
+
x = self.dec_conv3(x)
|
| 69 |
+
|
| 70 |
+
x = self.final_conv(x)
|
| 71 |
+
return torch.tanh(x)
|
| 72 |
+
|
| 73 |
+
# Load model
|
| 74 |
+
model = DeblurNet().to(device)
|
| 75 |
+
model_path = os.path.join('model', 'best_deblur_model.pth')
|
| 76 |
+
model.load_state_dict(torch.load(model_path, map_location=device))
|
| 77 |
+
model.eval()
|
| 78 |
+
|
| 79 |
+
# Image processing functions
|
| 80 |
+
transform = transforms.Compose([
|
| 81 |
+
transforms.Resize((256, 256)),
|
| 82 |
+
transforms.ToTensor(),
|
| 83 |
+
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
|
| 84 |
+
])
|
| 85 |
+
|
| 86 |
+
def postprocess_image(tensor):
|
| 87 |
+
tensor = tensor * 0.5 + 0.5
|
| 88 |
+
tensor = torch.clamp(tensor, 0, 1)
|
| 89 |
+
image = tensor.cpu().detach().numpy()
|
| 90 |
+
image = np.transpose(image, (1, 2, 0))
|
| 91 |
+
return (image * 255).astype(np.uint8)
|
| 92 |
+
|
| 93 |
+
def deblur_image(input_image):
|
| 94 |
+
if input_image is None:
|
| 95 |
+
return None
|
| 96 |
+
|
| 97 |
+
try:
|
| 98 |
+
# Convert to PIL Image
|
| 99 |
+
if isinstance(input_image, np.ndarray):
|
| 100 |
+
input_image = Image.fromarray(input_image)
|
| 101 |
+
|
| 102 |
+
# Save original size
|
| 103 |
+
original_size = input_image.size
|
| 104 |
+
|
| 105 |
+
# Preprocess
|
| 106 |
+
input_tensor = transform(input_image).unsqueeze(0).to(device)
|
| 107 |
+
|
| 108 |
+
# Inference
|
| 109 |
+
with torch.no_grad():
|
| 110 |
+
output_tensor = model(input_tensor)
|
| 111 |
+
|
| 112 |
+
# Postprocess
|
| 113 |
+
output_image = postprocess_image(output_tensor[0])
|
| 114 |
+
|
| 115 |
+
# Resize back to original size
|
| 116 |
+
output_image = Image.fromarray(output_image).resize(original_size)
|
| 117 |
+
return np.array(output_image)
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(f"Error processing image: {e}")
|
| 121 |
+
return None
|
| 122 |
+
|
| 123 |
+
# Create Gradio interface
|
| 124 |
+
demo = gr.Interface(
|
| 125 |
+
fn=deblur_image,
|
| 126 |
+
inputs=gr.Image(type="numpy", label="Upload Blurry Image"),
|
| 127 |
+
outputs=gr.Image(type="numpy", label="Deblurred Result"),
|
| 128 |
+
title="Image Deblurring",
|
| 129 |
+
description="Upload a blurry image and get it deblurred using deep learning.",
|
| 130 |
+
examples=[
|
| 131 |
+
["examples/example1.jpg"],
|
| 132 |
+
["examples/example2.jpg"]
|
| 133 |
+
]
|
| 134 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
+
demo.launch()
|
examples/example1.png
ADDED
|
model/best_deblur_model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:652ed502848fee131c3812b46cf5e72dc7a8c1d37f74c21b2a90eee031ac4d21
|
| 3 |
+
size 31149520
|