Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,53 +7,51 @@ import torch
|
|
7 |
from torch.autograd import Variable
|
8 |
from torchvision import transforms
|
9 |
import torch.nn.functional as F
|
|
|
10 |
import warnings
|
|
|
11 |
warnings.filterwarnings("ignore")
|
12 |
|
13 |
-
# Sprawdzenie, czy katalog DIS istnieje przed klonowaniem
|
14 |
if not os.path.exists("DIS"):
|
15 |
os.system("git clone https://github.com/xuebinqin/DIS")
|
|
|
16 |
os.system("mv DIS/IS-Net/* .")
|
17 |
|
18 |
# project imports
|
19 |
-
from data_loader_cache import normalize, im_reader, im_preprocess
|
20 |
from models import *
|
21 |
|
22 |
# Helpers
|
23 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
24 |
|
25 |
-
#
|
26 |
if not os.path.exists("saved_models"):
|
27 |
os.mkdir("saved_models")
|
28 |
if not os.path.exists("saved_models/isnet.pth"):
|
29 |
os.system("mv isnet.pth saved_models/")
|
30 |
|
31 |
class GOSNormalize(object):
|
32 |
-
|
33 |
-
Normalize the Image using torch.transforms
|
34 |
-
'''
|
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 |
|
55 |
-
|
56 |
-
if(hypar["model_digit"]=="half"):
|
57 |
net.half()
|
58 |
for layer in net.modules():
|
59 |
if isinstance(layer, nn.BatchNorm2d):
|
@@ -61,53 +59,47 @@ def build_model(hypar,device):
|
|
61 |
|
62 |
net.to(device)
|
63 |
|
64 |
-
if
|
65 |
-
net.load_state_dict(torch.load(hypar["model_path"]+"/"+hypar["restore_model"], map_location=device))
|
66 |
net.to(device)
|
67 |
-
net.eval()
|
68 |
return net
|
69 |
|
70 |
def predict(net, inputs_val, shapes_val, hypar, device):
|
71 |
-
'''
|
72 |
-
Given an Image, predict the mask
|
73 |
-
'''
|
74 |
net.eval()
|
75 |
|
76 |
-
if
|
77 |
inputs_val = inputs_val.type(torch.FloatTensor)
|
78 |
else:
|
79 |
inputs_val = inputs_val.type(torch.HalfTensor)
|
80 |
|
81 |
-
inputs_val_v = Variable(inputs_val, requires_grad=False).to(device)
|
82 |
-
ds_val = net(inputs_val_v)[0]
|
83 |
-
pred_val = ds_val[0][0
|
84 |
|
85 |
-
|
86 |
-
pred_val = torch.squeeze(F.upsample(torch.unsqueeze(pred_val,0),(shapes_val[0][0],shapes_val[0][1]),mode='bilinear'))
|
87 |
|
88 |
ma = torch.max(pred_val)
|
89 |
mi = torch.min(pred_val)
|
90 |
-
pred_val = (pred_val-mi)/(ma-mi)
|
91 |
|
92 |
if device == 'cuda': torch.cuda.empty_cache()
|
93 |
-
return (pred_val.detach().cpu().numpy()*255).astype(np.uint8)
|
94 |
-
|
95 |
# Set Parameters
|
96 |
-
hypar = {}
|
97 |
|
98 |
-
hypar["model_path"] ="./saved_models"
|
99 |
-
hypar["restore_model"] = "isnet.pth"
|
100 |
-
hypar["interm_sup"] = False
|
101 |
|
102 |
-
|
103 |
-
hypar["model_digit"] = "full" ## indicates "half" or "full" accuracy of float number
|
104 |
hypar["seed"] = 0
|
105 |
|
106 |
-
hypar["cache_size"] = [1024, 1024]
|
107 |
|
108 |
-
|
109 |
-
hypar["
|
110 |
-
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
|
111 |
|
112 |
hypar["model"] = ISNetDIS()
|
113 |
|
@@ -115,68 +107,57 @@ hypar["model"] = ISNetDIS()
|
|
115 |
net = build_model(hypar, device)
|
116 |
|
117 |
def inference(image):
|
118 |
-
|
119 |
-
image_tensor, orig_size = load_image(image_path, hypar)
|
120 |
mask = predict(net, image_tensor, orig_size, hypar, device)
|
121 |
-
|
122 |
pil_mask = Image.fromarray(mask).convert('L')
|
123 |
im_rgb = Image.open(image).convert("RGB")
|
124 |
-
|
125 |
im_rgba = im_rgb.copy()
|
126 |
im_rgba.putalpha(pil_mask)
|
127 |
|
128 |
return [im_rgba, pil_mask]
|
129 |
|
130 |
-
#
|
131 |
translations = {
|
132 |
"pl": {
|
133 |
"title": "Zaawansowane Segmentowanie Obraz贸w",
|
134 |
"description": """
|
135 |
-
# Zaawansowane Segmentowanie Obraz贸w
|
136 |
-
|
137 |
**Zaawansowane Segmentowanie Obraz贸w** to zaawansowane narz臋dzie oparte na sztucznej inteligencji, zaprojektowane do precyzyjnego segmentowania obraz贸w. Aplikacja ta wykorzystuje najnowsze technologie g艂臋bokiego uczenia, aby generowa膰 dok艂adne maski dla r贸偶nych typ贸w obraz贸w. Stworzona przez ekspert贸w, oferuje u偶ytkownikom intuicyjny interfejs do przetwarzania obraz贸w. Niezale偶nie od tego, czy jest u偶ywana do cel贸w zawodowych, czy do projekt贸w osobistych, to narz臋dzie zapewnia najwy偶sz膮 jako艣膰 i niezawodno艣膰 w zadaniach segmentacji obraz贸w.
|
|
|
|
|
|
|
|
|
|
|
138 |
""",
|
139 |
-
"article": ""
|
140 |
-
## Technologie
|
141 |
-
|
142 |
-
- **Model**: ISNetDIS
|
143 |
-
- **Stworzony przez**: Rafa艂 Dembski
|
144 |
-
- **Technologie**: PyTorch, Gradio, OpenCV
|
145 |
-
"""
|
146 |
},
|
147 |
"en": {
|
148 |
"title": "Advanced Image Segmentation",
|
149 |
"description": """
|
150 |
-
|
151 |
-
|
152 |
-
**
|
|
|
|
|
|
|
153 |
""",
|
154 |
-
"article": ""
|
155 |
-
## Technologies
|
156 |
-
|
157 |
-
- **Model**: ISNetDIS
|
158 |
-
- **Developed by**: Rafa艂 Dembski
|
159 |
-
- **Technologies**: PyTorch, Gradio, OpenCV
|
160 |
-
"""
|
161 |
},
|
162 |
"de": {
|
163 |
-
"title": "
|
164 |
"description": """
|
165 |
-
|
166 |
-
|
167 |
-
**
|
|
|
|
|
|
|
168 |
""",
|
169 |
-
"article": ""
|
170 |
-
## Technologien
|
171 |
-
|
172 |
-
- **Modell**: ISNetDIS
|
173 |
-
- **Entwickelt von**: Rafa艂 Dembski
|
174 |
-
- **Technologien**: PyTorch, Gradio, OpenCV
|
175 |
-
"""
|
176 |
}
|
177 |
}
|
178 |
|
179 |
-
# Gradio setup with Monochrome theme, logo, and description with language support
|
180 |
css = """
|
181 |
#col-container {
|
182 |
margin: 0 auto;
|
@@ -188,7 +169,10 @@ def change_language(lang):
|
|
188 |
return translations[lang]['title'], translations[lang]['description'], translations[lang]['article']
|
189 |
|
190 |
with gr.Blocks(theme=gr.themes.Monochrome(), css=css) as demo:
|
191 |
-
|
|
|
|
|
|
|
192 |
|
193 |
title = gr.Markdown(translations["en"]["title"])
|
194 |
description = gr.Markdown(translations["en"]["description"])
|
|
|
7 |
from torch.autograd import Variable
|
8 |
from torchvision import transforms
|
9 |
import torch.nn.functional as F
|
10 |
+
import gdown
|
11 |
import warnings
|
12 |
+
|
13 |
warnings.filterwarnings("ignore")
|
14 |
|
|
|
15 |
if not os.path.exists("DIS"):
|
16 |
os.system("git clone https://github.com/xuebinqin/DIS")
|
17 |
+
if not os.path.exists("IS-Net"):
|
18 |
os.system("mv DIS/IS-Net/* .")
|
19 |
|
20 |
# project imports
|
21 |
+
from data_loader_cache import normalize, im_reader, im_preprocess
|
22 |
from models import *
|
23 |
|
24 |
# Helpers
|
25 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
26 |
|
27 |
+
# Download official weights
|
28 |
if not os.path.exists("saved_models"):
|
29 |
os.mkdir("saved_models")
|
30 |
if not os.path.exists("saved_models/isnet.pth"):
|
31 |
os.system("mv isnet.pth saved_models/")
|
32 |
|
33 |
class GOSNormalize(object):
|
34 |
+
def __init__(self, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]):
|
|
|
|
|
|
|
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 |
+
transform = transforms.Compose([GOSNormalize([0.5, 0.5, 0.5], [1.0, 1.0, 1.0])])
|
43 |
|
44 |
def load_image(im_path, hypar):
|
45 |
im = im_reader(im_path)
|
46 |
im, im_shp = im_preprocess(im, hypar["cache_size"])
|
47 |
+
im = torch.divide(im, 255.0)
|
48 |
shape = torch.from_numpy(np.array(im_shp))
|
49 |
+
return transform(im).unsqueeze(0), shape.unsqueeze(0) # make a batch of image, shape
|
50 |
|
51 |
+
def build_model(hypar, device):
|
52 |
net = hypar["model"]
|
53 |
|
54 |
+
if hypar["model_digit"] == "half":
|
|
|
55 |
net.half()
|
56 |
for layer in net.modules():
|
57 |
if isinstance(layer, nn.BatchNorm2d):
|
|
|
59 |
|
60 |
net.to(device)
|
61 |
|
62 |
+
if hypar["restore_model"] != "":
|
63 |
+
net.load_state_dict(torch.load(hypar["model_path"] + "/" + hypar["restore_model"], map_location=device))
|
64 |
net.to(device)
|
65 |
+
net.eval()
|
66 |
return net
|
67 |
|
68 |
def predict(net, inputs_val, shapes_val, hypar, device):
|
|
|
|
|
|
|
69 |
net.eval()
|
70 |
|
71 |
+
if hypar["model_digit"] == "full":
|
72 |
inputs_val = inputs_val.type(torch.FloatTensor)
|
73 |
else:
|
74 |
inputs_val = inputs_val.type(torch.HalfTensor)
|
75 |
|
76 |
+
inputs_val_v = Variable(inputs_val, requires_grad=False).to(device)
|
77 |
+
ds_val = net(inputs_val_v)[0]
|
78 |
+
pred_val = ds_val[0][0, :, :, :]
|
79 |
|
80 |
+
pred_val = torch.squeeze(F.upsample(torch.unsqueeze(pred_val, 0), (shapes_val[0][0], shapes_val[0][1]), mode='bilinear'))
|
|
|
81 |
|
82 |
ma = torch.max(pred_val)
|
83 |
mi = torch.min(pred_val)
|
84 |
+
pred_val = (pred_val - mi) / (ma - mi)
|
85 |
|
86 |
if device == 'cuda': torch.cuda.empty_cache()
|
87 |
+
return (pred_val.detach().cpu().numpy() * 255).astype(np.uint8)
|
88 |
+
|
89 |
# Set Parameters
|
90 |
+
hypar = {}
|
91 |
|
92 |
+
hypar["model_path"] = "./saved_models"
|
93 |
+
hypar["restore_model"] = "isnet.pth"
|
94 |
+
hypar["interm_sup"] = False
|
95 |
|
96 |
+
hypar["model_digit"] = "full"
|
|
|
97 |
hypar["seed"] = 0
|
98 |
|
99 |
+
hypar["cache_size"] = [1024, 1024]
|
100 |
|
101 |
+
hypar["input_size"] = [1024, 1024]
|
102 |
+
hypar["crop_size"] = [1024, 1024]
|
|
|
103 |
|
104 |
hypar["model"] = ISNetDIS()
|
105 |
|
|
|
107 |
net = build_model(hypar, device)
|
108 |
|
109 |
def inference(image):
|
110 |
+
image_tensor, orig_size = load_image(image, hypar)
|
|
|
111 |
mask = predict(net, image_tensor, orig_size, hypar, device)
|
112 |
+
|
113 |
pil_mask = Image.fromarray(mask).convert('L')
|
114 |
im_rgb = Image.open(image).convert("RGB")
|
115 |
+
|
116 |
im_rgba = im_rgb.copy()
|
117 |
im_rgba.putalpha(pil_mask)
|
118 |
|
119 |
return [im_rgba, pil_mask]
|
120 |
|
121 |
+
# Translation texts
|
122 |
translations = {
|
123 |
"pl": {
|
124 |
"title": "Zaawansowane Segmentowanie Obraz贸w",
|
125 |
"description": """
|
|
|
|
|
126 |
**Zaawansowane Segmentowanie Obraz贸w** to zaawansowane narz臋dzie oparte na sztucznej inteligencji, zaprojektowane do precyzyjnego segmentowania obraz贸w. Aplikacja ta wykorzystuje najnowsze technologie g艂臋bokiego uczenia, aby generowa膰 dok艂adne maski dla r贸偶nych typ贸w obraz贸w. Stworzona przez ekspert贸w, oferuje u偶ytkownikom intuicyjny interfejs do przetwarzania obraz贸w. Niezale偶nie od tego, czy jest u偶ywana do cel贸w zawodowych, czy do projekt贸w osobistych, to narz臋dzie zapewnia najwy偶sz膮 jako艣膰 i niezawodno艣膰 w zadaniach segmentacji obraz贸w.
|
127 |
+
|
128 |
+
**Technologie**:
|
129 |
+
- Model: ISNetDIS
|
130 |
+
- Stworzony przez: Rafa艂 Dembski
|
131 |
+
- Technologie: PyTorch, Gradio, OpenCV
|
132 |
""",
|
133 |
+
"article": ""
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
},
|
135 |
"en": {
|
136 |
"title": "Advanced Image Segmentation",
|
137 |
"description": """
|
138 |
+
**Advanced Image Segmentation** is an advanced AI-based tool designed for precise image segmentation. This application utilizes the latest deep learning technologies to generate accurate masks for different types of images. Created by experts, it offers users an intuitive interface for image processing. Whether used for professional purposes or personal projects, this tool ensures the highest quality and reliability in image segmentation tasks.
|
139 |
+
|
140 |
+
**Technologies**:
|
141 |
+
- Model: ISNetDIS
|
142 |
+
- Created by: Rafa艂 Dembski
|
143 |
+
- Technologies: PyTorch, Gradio, OpenCV
|
144 |
""",
|
145 |
+
"article": ""
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
},
|
147 |
"de": {
|
148 |
+
"title": "Fortgeschrittene Bildsegmentierung",
|
149 |
"description": """
|
150 |
+
**Fortgeschrittene Bildsegmentierung** ist ein fortschrittliches, auf k眉nstlicher Intelligenz basierendes Werkzeug, das f眉r die pr盲zise Bildsegmentierung entwickelt wurde. Diese Anwendung nutzt die neuesten Technologien des Deep Learnings, um genaue Masken f眉r verschiedene Bildtypen zu erzeugen. Von Experten erstellt, bietet es den Benutzern eine intuitive Benutzeroberfl盲che f眉r die Bildverarbeitung. Ob f眉r berufliche Zwecke oder pers枚nliche Projekte, dieses Werkzeug gew盲hrleistet h枚chste Qualit盲t und Zuverl盲ssigkeit bei der Bildsegmentierung.
|
151 |
+
|
152 |
+
**Technologien**:
|
153 |
+
- Modell: ISNetDIS
|
154 |
+
- Erstellt von: Rafa艂 Dembski
|
155 |
+
- Technologien: PyTorch, Gradio, OpenCV
|
156 |
""",
|
157 |
+
"article": ""
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
}
|
159 |
}
|
160 |
|
|
|
161 |
css = """
|
162 |
#col-container {
|
163 |
margin: 0 auto;
|
|
|
169 |
return translations[lang]['title'], translations[lang]['description'], translations[lang]['article']
|
170 |
|
171 |
with gr.Blocks(theme=gr.themes.Monochrome(), css=css) as demo:
|
172 |
+
language = gr.State("en")
|
173 |
+
|
174 |
+
with gr.Row():
|
175 |
+
language_selector = gr.Dropdown(choices=["pl", "en", "de"], value="en", label="Wybierz j臋zyk / Select Language / Sprache ausw盲hlen", show_label=True)
|
176 |
|
177 |
title = gr.Markdown(translations["en"]["title"])
|
178 |
description = gr.Markdown(translations["en"]["description"])
|