Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,113 @@
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
|
|
|
| 3 |
from torchvision import transforms
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
import gradio as gr
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def __init__(self):
|
| 11 |
-
super(
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
self.
|
| 15 |
-
self.
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def forward(self, x):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
])
|
| 34 |
-
img = transform(image).unsqueeze(0) # add batch dimension
|
| 35 |
|
| 36 |
with torch.no_grad():
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
output_img = output.squeeze(0).permute(1, 2, 0).numpy()
|
| 41 |
-
output_img = (output_img * 255).astype(np.uint8)
|
| 42 |
-
return Image.fromarray(output_img)
|
| 43 |
|
| 44 |
-
#
|
|
|
|
|
|
|
| 45 |
iface = gr.Interface(
|
| 46 |
-
fn=
|
| 47 |
inputs=gr.Image(type="pil"),
|
| 48 |
-
outputs=
|
| 49 |
-
title="U-Net
|
| 50 |
-
description="Upload
|
| 51 |
)
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
from torchvision import transforms
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
import gradio as gr
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
+
# =========================
|
| 12 |
+
# 1. DEVICE CONFIG
|
| 13 |
+
# =========================
|
| 14 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
+
MODEL_PATH = "sharp_edge_unet.pth" # Path to trained model
|
| 16 |
+
|
| 17 |
+
# =========================
|
| 18 |
+
# 2. DEFINE SHARP UNET
|
| 19 |
+
# =========================
|
| 20 |
+
class SharpUNet(nn.Module):
|
| 21 |
def __init__(self):
|
| 22 |
+
super(SharpUNet, self).__init__()
|
| 23 |
+
self.enc1 = self.conv_block(3, 64)
|
| 24 |
+
self.enc2 = self.conv_block(64, 128)
|
| 25 |
+
self.enc3 = self.conv_block(128, 256)
|
| 26 |
+
self.bottleneck = self.conv_block(256, 512)
|
| 27 |
+
self.up1 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
|
| 28 |
+
self.dec1 = self.conv_block(512 + 256, 256)
|
| 29 |
+
self.up2 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
|
| 30 |
+
self.dec2 = self.conv_block(256 + 128, 128)
|
| 31 |
+
self.up3 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
|
| 32 |
+
self.dec3 = self.conv_block(128 + 64, 64)
|
| 33 |
+
self.final = nn.Conv2d(64, 3, kernel_size=1)
|
| 34 |
+
|
| 35 |
+
def conv_block(self, in_c, out_c):
|
| 36 |
+
return nn.Sequential(
|
| 37 |
+
nn.Conv2d(in_c, out_c, 3, padding=1),
|
| 38 |
+
nn.BatchNorm2d(out_c),
|
| 39 |
+
nn.ReLU(inplace=True),
|
| 40 |
+
nn.Conv2d(out_c, out_c, 3, padding=1),
|
| 41 |
+
nn.BatchNorm2d(out_c),
|
| 42 |
+
nn.ReLU(inplace=True)
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
def forward(self, x):
|
| 46 |
+
e1 = self.enc1(x)
|
| 47 |
+
e2 = self.enc2(F.max_pool2d(e1, 2))
|
| 48 |
+
e3 = self.enc3(F.max_pool2d(e2, 2))
|
| 49 |
+
b = self.bottleneck(F.max_pool2d(e3, 2))
|
| 50 |
+
d1 = self.up1(b)
|
| 51 |
+
if d1.size() != e3.size(): d1 = F.interpolate(d1, size=e3.shape[2:])
|
| 52 |
+
d1 = torch.cat((d1, e3), dim=1)
|
| 53 |
+
d1 = self.dec1(d1)
|
| 54 |
+
d2 = self.up2(d1)
|
| 55 |
+
if d2.size() != e2.size(): d2 = F.interpolate(d2, size=e2.shape[2:])
|
| 56 |
+
d2 = torch.cat((d2, e2), dim=1)
|
| 57 |
+
d2 = self.dec2(d2)
|
| 58 |
+
d3 = self.up3(d2)
|
| 59 |
+
if d3.size() != e1.size(): d3 = F.interpolate(d3, size=e1.shape[2:])
|
| 60 |
+
d3 = torch.cat((d3, e1), dim=1)
|
| 61 |
+
d3 = self.dec3(d3)
|
| 62 |
+
return torch.sigmoid(self.final(d3))
|
| 63 |
|
| 64 |
+
# =========================
|
| 65 |
+
# 3. LOAD MODEL
|
| 66 |
+
# =========================
|
| 67 |
+
model = SharpUNet().to(DEVICE)
|
| 68 |
+
if not os.path.exists(MODEL_PATH):
|
| 69 |
+
raise FileNotFoundError(f"Model not found at {MODEL_PATH}")
|
| 70 |
+
model.load_state_dict(torch.load(MODEL_PATH, map_location=DEVICE))
|
| 71 |
model.eval()
|
| 72 |
+
print("Model loaded successfully.")
|
| 73 |
+
|
| 74 |
+
# =========================
|
| 75 |
+
# 4. INFERENCE FUNCTION
|
| 76 |
+
# =========================
|
| 77 |
+
transform = transforms.Compose([
|
| 78 |
+
transforms.Resize((256, 256)),
|
| 79 |
+
transforms.ToTensor()
|
| 80 |
+
])
|
| 81 |
|
| 82 |
+
def infer_image(user_img):
|
| 83 |
+
# Convert uploaded image to RGB PIL
|
| 84 |
+
img = user_img.convert("RGB")
|
| 85 |
+
|
| 86 |
+
# Preprocess
|
| 87 |
+
img_tensor = transform(img).unsqueeze(0).to(DEVICE)
|
|
|
|
|
|
|
| 88 |
|
| 89 |
with torch.no_grad():
|
| 90 |
+
pred = model(img_tensor)
|
| 91 |
+
|
| 92 |
+
pred_np = pred.squeeze(0).cpu().permute(1, 2, 0).numpy()
|
| 93 |
+
pred_clean = pred_np.copy()
|
| 94 |
+
pred_clean[pred_clean < 0.1] = 0 # Remove weak pixels for sharp display
|
| 95 |
+
|
| 96 |
+
# Resize original image to match output size
|
| 97 |
+
img_resized = img.resize((256, 256))
|
| 98 |
|
| 99 |
+
return img_resized, Image.fromarray((pred_clean * 255).astype(np.uint8))
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
# =========================
|
| 102 |
+
# 5. GRADIO INTERFACE
|
| 103 |
+
# =========================
|
| 104 |
iface = gr.Interface(
|
| 105 |
+
fn=infer_image,
|
| 106 |
inputs=gr.Image(type="pil"),
|
| 107 |
+
outputs=["image", "image"],
|
| 108 |
+
title="Sharp U-Net Edge Colorization",
|
| 109 |
+
description="Upload any RGB image and see the predicted pseudo-colored edges."
|
| 110 |
)
|
| 111 |
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
iface.launch()
|