Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,153 +1,107 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import os
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
-
import
|
| 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 |
-
self.
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
transform =
|
| 44 |
-
|
| 45 |
-
def load_image(im_path, hypar):
|
| 46 |
-
im = im_reader(im_path)
|
| 47 |
-
im, im_shp = im_preprocess(im, hypar["cache_size"])
|
| 48 |
-
im = torch.divide(im,255.0)
|
| 49 |
-
shape = torch.from_numpy(np.array(im_shp))
|
| 50 |
-
return transform(im).unsqueeze(0), shape.unsqueeze(0)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
net
|
| 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 |
-
## choose floating point accuracy --
|
| 109 |
-
hypar["model_digit"] = "full" ## indicates "half" or "full" accuracy of float number
|
| 110 |
-
hypar["seed"] = 0
|
| 111 |
-
|
| 112 |
-
hypar["cache_size"] = [1024, 1024] ## cached input spatial resolution, can be configured into different size
|
| 113 |
-
|
| 114 |
-
## data augmentation parameters ---
|
| 115 |
-
hypar["input_size"] = [1024, 1024] ## mdoel input spatial size, usually use the same value hypar["cache_size"], which means we don't further resize the images
|
| 116 |
-
hypar["crop_size"] = [1024, 1024] ## random crop size from the input, it is usually set as smaller than hypar["cache_size"], e.g., [920,920] for data augmentation
|
| 117 |
-
|
| 118 |
-
hypar["model"] = ISNetDIS()
|
| 119 |
-
|
| 120 |
-
# Build Model
|
| 121 |
-
net = build_model(hypar, device)
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
def inference(image):
|
| 125 |
-
image_path = image
|
| 126 |
-
|
| 127 |
-
image_tensor, orig_size = load_image(image_path, hypar)
|
| 128 |
-
mask = predict(net, image_tensor, orig_size, hypar, device)
|
| 129 |
-
|
| 130 |
-
pil_mask = Image.fromarray(mask).convert('L')
|
| 131 |
-
im_rgb = Image.open(image).convert("RGB")
|
| 132 |
-
|
| 133 |
-
im_rgba = im_rgb.copy()
|
| 134 |
-
im_rgba.putalpha(pil_mask)
|
| 135 |
-
|
| 136 |
-
return [im_rgba, pil_mask]
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
title = "Highly Accurate Dichotomous Image Segmentation"
|
| 140 |
-
description = "This is an unofficial demo for DIS, a model that can remove the background from a given image. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below.<br>GitHub: https://github.com/xuebinqin/DIS<br>Telegram bot: https://t.me/restoration_photo_bot<br>[](https://twitter.com/DoEvent)"
|
| 141 |
-
article = "<div><center><img src='https://visitor-badge.glitch.me/badge?page_id=max_skobeev_dis_cmp_public' alt='visitor badge'></center></div>"
|
| 142 |
-
|
| 143 |
-
interface = gr.Interface(
|
| 144 |
-
fn=inference,
|
| 145 |
-
inputs=gr.Image(type='filepath'),
|
| 146 |
-
outputs=[gr.Image(type='filepath', format="png"), gr.Image(type='filepath', format="png")],
|
| 147 |
-
examples=[['robot.png'], ['ship.png']],
|
| 148 |
-
title=title,
|
| 149 |
-
description=description,
|
| 150 |
-
article=article,
|
| 151 |
-
flagging_mode="never",
|
| 152 |
-
cache_mode="lazy",
|
| 153 |
-
).queue(api_open=True).launch(show_error=True, show_api=True)
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
from huggingface_hub import hf_hub_download # Added for Hugging Face download
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import numpy as np
|
| 7 |
+
import torch
|
| 8 |
+
from torch.autograd import Variable
|
| 9 |
+
from torchvision import transforms
|
| 10 |
+
import torch.nn.functional as F
|
| 11 |
+
|
| 12 |
+
# Project imports (assumes data_loader_cache.py and models.py are uploaded)
|
| 13 |
+
from data_loader_cache import normalize, im_reader, im_preprocess
|
| 14 |
+
from models import *
|
| 15 |
+
|
| 16 |
+
# Helpers
|
| 17 |
+
device = 'cpu' # Free Hugging Face Space uses CPU
|
| 18 |
+
|
| 19 |
+
# Create directory for model weights
|
| 20 |
+
if not os.path.exists("saved_models"):
|
| 21 |
+
os.makedirs("saved_models")
|
| 22 |
+
|
| 23 |
+
# Automatically download isnet.pth from ECCV2022/dis-background-removal if not present
|
| 24 |
+
isnet_path = "saved_models/isnet.pth"
|
| 25 |
+
if not os.path.exists(isnet_path):
|
| 26 |
+
print("Downloading isnet.pth from ECCV2022/dis-background-removal...")
|
| 27 |
+
hf_hub_download(
|
| 28 |
+
repo_id="ECCV2022/dis-background-removal",
|
| 29 |
+
filename="isnet.pth",
|
| 30 |
+
local_dir="saved_models",
|
| 31 |
+
local_dir_use_symlinks=False
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
class GOSNormalize(object):
|
| 35 |
+
def __init__(self, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]):
|
| 36 |
+
self.mean = mean
|
| 37 |
+
self.std = std
|
| 38 |
+
|
| 39 |
+
def __call__(self, image):
|
| 40 |
+
image = normalize(image, self.mean, self.std)
|
| 41 |
+
return image
|
| 42 |
+
|
| 43 |
+
transform = transforms.Compose([GOSNormalize([0.5, 0.5, 0.5], [1.0, 1.0, 1.0])])
|
| 44 |
+
|
| 45 |
+
def load_image(im_path, hypar):
|
| 46 |
+
im = im_reader(im_path)
|
| 47 |
+
im, im_shp = im_preprocess(im, hypar["cache_size"])
|
| 48 |
+
im = torch.divide(im, 255.0)
|
| 49 |
+
shape = torch.from_numpy(np.array(im_shp))
|
| 50 |
+
return transform(im).unsqueeze(0), shape.unsqueeze(0)
|
| 51 |
+
|
| 52 |
+
def build_model(hypar, device):
|
| 53 |
+
net = hypar["model"]
|
| 54 |
+
net.to(device)
|
| 55 |
+
if hypar["restore_model"]:
|
| 56 |
+
net.load_state_dict(torch.load(os.path.join(hypar["model_path"], hypar["restore_model"]), map_location=device))
|
| 57 |
+
net.eval()
|
| 58 |
+
return net
|
| 59 |
+
|
| 60 |
+
def predict(net, inputs_val, shapes_val, hypar, device):
|
| 61 |
+
net.eval()
|
| 62 |
+
inputs_val = inputs_val.type(torch.FloatTensor).to(device)
|
| 63 |
+
with torch.no_grad(): # Reduce memory usage
|
| 64 |
+
inputs_val_v = Variable(inputs_val)
|
| 65 |
+
ds_val = net(inputs_val_v)[0]
|
| 66 |
+
pred_val = ds_val[0][0, :, :, :] # B x 1 x H x W
|
| 67 |
+
pred_val = torch.squeeze(F.upsample(torch.unsqueeze(pred_val, 0), (shapes_val[0][0], shapes_val[0][1]), mode='bilinear'))
|
| 68 |
+
ma = torch.max(pred_val)
|
| 69 |
+
mi = torch.min(pred_val)
|
| 70 |
+
pred_val = (pred_val - mi) / (ma - mi) # Normalize to [0, 1]
|
| 71 |
+
return (pred_val.cpu().numpy() * 255).astype(np.uint8)
|
| 72 |
+
|
| 73 |
+
# Set Parameters
|
| 74 |
+
hypar = {
|
| 75 |
+
"model_path": "saved_models",
|
| 76 |
+
"restore_model": "isnet.pth",
|
| 77 |
+
"cache_size": [512, 512], # Optimized for CPU
|
| 78 |
+
"input_size": [512, 512],
|
| 79 |
+
"crop_size": [512, 512],
|
| 80 |
+
"model": ISNetDIS()
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
# Build Model
|
| 84 |
+
net = build_model(hypar, device)
|
| 85 |
+
|
| 86 |
+
def inference(image):
|
| 87 |
+
image_path = image
|
| 88 |
+
image_tensor, orig_size = load_image(image_path, hypar)
|
| 89 |
+
mask = predict(net, image_tensor, orig_size, hypar, device)
|
| 90 |
+
pil_mask = Image.fromarray(mask).convert('L')
|
| 91 |
+
im_rgb = Image.open(image).convert("RGB")
|
| 92 |
+
im_rgba = im_rgb.copy()
|
| 93 |
+
im_rgba.putalpha(pil_mask)
|
| 94 |
+
return [im_rgba, pil_mask]
|
| 95 |
+
|
| 96 |
+
title = "Dichotomous Image Segmentation"
|
| 97 |
+
description = "Upload an image to remove its background."
|
| 98 |
+
|
| 99 |
+
interface = gr.Interface(
|
| 100 |
+
fn=inference,
|
| 101 |
+
inputs=gr.Image(type='filepath'),
|
| 102 |
+
outputs=[gr.Image(type='filepath', format="png"), gr.Image(type='filepath', format="png")],
|
| 103 |
+
title=title,
|
| 104 |
+
description=description,
|
| 105 |
+
flagging_mode="never",
|
| 106 |
+
cache_mode="lazy"
|
| 107 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|