Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,21 +12,27 @@ import matplotlib.pyplot as plt
|
|
| 12 |
import warnings
|
| 13 |
warnings.filterwarnings("ignore")
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
os.system("git clone https://github.com/xuebinqin/DIS")
|
| 16 |
os.system("mv DIS/IS-Net/* .")
|
| 17 |
|
| 18 |
-
#
|
| 19 |
from data_loader_cache import normalize, im_reader, im_preprocess
|
| 20 |
from models import *
|
| 21 |
|
| 22 |
-
#
|
| 23 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 24 |
|
| 25 |
-
#
|
| 26 |
if not os.path.exists("saved_models"):
|
| 27 |
-
os.
|
| 28 |
-
os.
|
| 29 |
-
|
|
|
|
| 30 |
class GOSNormalize(object):
|
| 31 |
'''
|
| 32 |
Normalize the Image using torch.transforms
|
|
@@ -35,26 +41,24 @@ class GOSNormalize(object):
|
|
| 35 |
self.mean = mean
|
| 36 |
self.std = std
|
| 37 |
|
| 38 |
-
def __call__(self,image):
|
| 39 |
-
image = normalize(image,self.mean,self.std)
|
| 40 |
return image
|
| 41 |
|
| 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 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
# convert to half precision
|
| 57 |
-
if(hypar["model_digit"]=="half"):
|
| 58 |
net.half()
|
| 59 |
for layer in net.modules():
|
| 60 |
if isinstance(layer, nn.BatchNorm2d):
|
|
@@ -62,92 +66,109 @@ def build_model(hypar,device):
|
|
| 62 |
|
| 63 |
net.to(device)
|
| 64 |
|
| 65 |
-
if
|
| 66 |
-
net.load_state_dict(torch.load(
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
net.eval()
|
| 69 |
return net
|
| 70 |
|
| 71 |
-
|
| 72 |
-
def predict(net, inputs_val, shapes_val, hypar, device):
|
| 73 |
-
'''
|
| 74 |
-
Given an Image, predict the mask
|
| 75 |
-
'''
|
| 76 |
net.eval()
|
| 77 |
|
| 78 |
-
if
|
| 79 |
inputs_val = inputs_val.type(torch.FloatTensor)
|
| 80 |
else:
|
| 81 |
inputs_val = inputs_val.type(torch.HalfTensor)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
inputs_val_v = Variable(inputs_val, requires_grad=False).to(device) # wrap inputs in Variable
|
| 85 |
-
|
| 86 |
ds_val = net(inputs_val_v)[0] # list of 6 results
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
|
| 93 |
ma = torch.max(pred_val)
|
| 94 |
mi = torch.min(pred_val)
|
| 95 |
pred_val = (pred_val-mi)/(ma-mi) # max = 1
|
| 96 |
|
| 97 |
-
if device == 'cuda':
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 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 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
title = "Highly Accurate Dichotomous Image Segmentation"
|
| 140 |
-
description = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=
|
| 146 |
-
outputs=[
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
title=title,
|
| 149 |
description=description,
|
| 150 |
article=article,
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
import warnings
|
| 13 |
warnings.filterwarnings("ignore")
|
| 14 |
|
| 15 |
+
# Clean up any previous runs
|
| 16 |
+
if os.path.exists("DIS"):
|
| 17 |
+
os.system("rm -rf DIS")
|
| 18 |
+
|
| 19 |
+
# Clone and setup the model
|
| 20 |
os.system("git clone https://github.com/xuebinqin/DIS")
|
| 21 |
os.system("mv DIS/IS-Net/* .")
|
| 22 |
|
| 23 |
+
# Project imports
|
| 24 |
from data_loader_cache import normalize, im_reader, im_preprocess
|
| 25 |
from models import *
|
| 26 |
|
| 27 |
+
# Device configuration
|
| 28 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 29 |
|
| 30 |
+
# Setup model directory and weights
|
| 31 |
if not os.path.exists("saved_models"):
|
| 32 |
+
os.makedirs("saved_models", exist_ok=True)
|
| 33 |
+
if os.path.exists("isnet.pth"):
|
| 34 |
+
os.system("mv isnet.pth saved_models/")
|
| 35 |
+
|
| 36 |
class GOSNormalize(object):
|
| 37 |
'''
|
| 38 |
Normalize the Image using torch.transforms
|
|
|
|
| 41 |
self.mean = mean
|
| 42 |
self.std = std
|
| 43 |
|
| 44 |
+
def __call__(self, image):
|
| 45 |
+
image = normalize(image, self.mean, self.std)
|
| 46 |
return image
|
| 47 |
|
| 48 |
+
transform = transforms.Compose([GOSNormalize([0.5,0.5,0.5],[1.0,1.0,1.0])])
|
|
|
|
| 49 |
|
| 50 |
def load_image(im_path, hypar):
|
| 51 |
im = im_reader(im_path)
|
| 52 |
im, im_shp = im_preprocess(im, hypar["cache_size"])
|
| 53 |
+
im = torch.divide(im, 255.0)
|
| 54 |
shape = torch.from_numpy(np.array(im_shp))
|
| 55 |
+
return transform(im).unsqueeze(0), shape.unsqueeze(0)
|
| 56 |
|
| 57 |
+
def build_model(hypar, device):
|
| 58 |
+
net = hypar["model"] # GOSNETINC(3,1)
|
| 59 |
|
| 60 |
+
# Convert to half precision
|
| 61 |
+
if hypar["model_digit"] == "half":
|
|
|
|
|
|
|
|
|
|
| 62 |
net.half()
|
| 63 |
for layer in net.modules():
|
| 64 |
if isinstance(layer, nn.BatchNorm2d):
|
|
|
|
| 66 |
|
| 67 |
net.to(device)
|
| 68 |
|
| 69 |
+
if hypar["restore_model"] != "":
|
| 70 |
+
net.load_state_dict(torch.load(
|
| 71 |
+
hypar["model_path"]+"/"+hypar["restore_model"],
|
| 72 |
+
map_location=device
|
| 73 |
+
))
|
| 74 |
net.eval()
|
| 75 |
return net
|
| 76 |
|
| 77 |
+
def predict(net, inputs_val, shapes_val, hypar, device):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
net.eval()
|
| 79 |
|
| 80 |
+
if hypar["model_digit"] == "full":
|
| 81 |
inputs_val = inputs_val.type(torch.FloatTensor)
|
| 82 |
else:
|
| 83 |
inputs_val = inputs_val.type(torch.HalfTensor)
|
| 84 |
|
| 85 |
+
inputs_val_v = Variable(inputs_val, requires_grad=False).to(device)
|
|
|
|
|
|
|
| 86 |
ds_val = net(inputs_val_v)[0] # list of 6 results
|
| 87 |
+
pred_val = ds_val[0][0,:,:,:] # B x 1 x H x W
|
| 88 |
|
| 89 |
+
# Recover the prediction spatial size to the original image size
|
| 90 |
+
pred_val = torch.squeeze(F.upsample(
|
| 91 |
+
torch.unsqueeze(pred_val, 0),
|
| 92 |
+
(shapes_val[0][0], shapes_val[0][1]),
|
| 93 |
+
mode='bilinear'
|
| 94 |
+
))
|
| 95 |
|
| 96 |
ma = torch.max(pred_val)
|
| 97 |
mi = torch.min(pred_val)
|
| 98 |
pred_val = (pred_val-mi)/(ma-mi) # max = 1
|
| 99 |
|
| 100 |
+
if device == 'cuda':
|
| 101 |
+
torch.cuda.empty_cache()
|
| 102 |
+
return (pred_val.detach().cpu().numpy()*255).astype(np.uint8)
|
| 103 |
+
|
| 104 |
+
# Set parameters
|
| 105 |
+
hypar = {
|
| 106 |
+
"model_path": "./saved_models",
|
| 107 |
+
"restore_model": "isnet.pth",
|
| 108 |
+
"interm_sup": False,
|
| 109 |
+
"model_digit": "full",
|
| 110 |
+
"seed": 0,
|
| 111 |
+
"cache_size": [1024, 1024],
|
| 112 |
+
"input_size": [1024, 1024],
|
| 113 |
+
"crop_size": [1024, 1024],
|
| 114 |
+
"model": ISNetDIS()
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
# Build model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
net = build_model(hypar, device)
|
| 119 |
|
|
|
|
| 120 |
def inference(image):
|
| 121 |
+
try:
|
| 122 |
+
image_path = image.name if hasattr(image, 'name') else image
|
| 123 |
+
|
| 124 |
+
image_tensor, orig_size = load_image(image_path, hypar)
|
| 125 |
+
mask = predict(net, image_tensor, orig_size, hypar, device)
|
| 126 |
+
|
| 127 |
+
pil_mask = Image.fromarray(mask).convert('L')
|
| 128 |
+
im_rgb = Image.open(image_path).convert("RGB")
|
| 129 |
+
|
| 130 |
+
im_rgba = im_rgb.copy()
|
| 131 |
+
im_rgba.putalpha(pil_mask)
|
| 132 |
+
|
| 133 |
+
return [im_rgba, pil_mask]
|
| 134 |
+
except Exception as e:
|
| 135 |
+
print(f"Error during inference: {str(e)}")
|
| 136 |
+
raise e
|
| 137 |
|
| 138 |
title = "Highly Accurate Dichotomous Image Segmentation"
|
| 139 |
+
description = """
|
| 140 |
+
This is an unofficial demo for DIS, a model that can remove the background from a given image.
|
| 141 |
+
To use it, simply upload your image, or click one of the examples to load them.
|
| 142 |
+
<br>GitHub: https://github.com/xuebinqin/DIS
|
| 143 |
+
<br>Telegram bot: https://t.me/restoration_photo_bot
|
| 144 |
+
[](https://twitter.com/DoEvent)
|
| 145 |
+
"""
|
| 146 |
article = "<div><center><img src='https://visitor-badge.glitch.me/badge?page_id=max_skobeev_dis_cmp_public' alt='visitor badge'></center></div>"
|
| 147 |
|
| 148 |
+
# Create interface
|
| 149 |
interface = gr.Interface(
|
| 150 |
fn=inference,
|
| 151 |
+
inputs=gr.Image(type="filepath"),
|
| 152 |
+
outputs=[
|
| 153 |
+
gr.Image(type="pil", label="Image with Transparency"),
|
| 154 |
+
gr.Image(type="pil", label="Mask Only")
|
| 155 |
+
],
|
| 156 |
+
examples=[
|
| 157 |
+
["robot.png"],
|
| 158 |
+
["ship.png"]
|
| 159 |
+
],
|
| 160 |
title=title,
|
| 161 |
description=description,
|
| 162 |
article=article,
|
| 163 |
+
allow_flagging="never"
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
# Launch with more robust settings
|
| 167 |
+
interface.launch(
|
| 168 |
+
server_name="0.0.0.0",
|
| 169 |
+
server_port=7860,
|
| 170 |
+
enable_queue=True,
|
| 171 |
+
share=False,
|
| 172 |
+
debug=True,
|
| 173 |
+
show_error=True
|
| 174 |
+
)
|