soiz1 commited on
Commit
bb4506e
·
verified ·
1 Parent(s): 4b5237d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -124
app.py CHANGED
@@ -1,67 +1,60 @@
 
 
1
  import os
 
2
  import numpy as np
3
  import torch
4
  from torch.autograd import Variable
5
  from torchvision import transforms
6
  import torch.nn.functional as F
7
- from flask import Flask, request, jsonify, render_template, send_from_directory
8
- from PIL import Image
9
  import warnings
10
  warnings.filterwarnings("ignore")
11
 
12
- # Clone repository and setup
13
- if not os.path.exists("DIS"):
14
- os.system("git clone https://github.com/xuebinqin/DIS")
15
- os.system("mv DIS/IS-Net/* .")
16
 
17
- # Project imports
18
  from data_loader_cache import normalize, im_reader, im_preprocess
19
  from models import *
20
 
21
- # Setup device
22
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
23
 
24
  # Download official weights
25
  if not os.path.exists("saved_models"):
26
- os.makedirs("saved_models", exist_ok=True)
27
- if not os.path.exists("saved_models/isnet.pth"):
28
- if os.path.exists("isnet.pth"):
29
- os.rename("isnet.pth", "saved_models/isnet.pth")
30
-
31
  class GOSNormalize(object):
 
 
 
32
  def __init__(self, mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225]):
33
  self.mean = mean
34
  self.std = std
35
 
36
- def __call__(self, image):
37
- image = normalize(image, self.mean, self.std)
38
  return image
39
 
40
- transform = transforms.Compose([GOSNormalize([0.5,0.5,0.5],[1.0,1.0,1.0])])
 
41
 
42
  def load_image(im_path, hypar):
43
  im = im_reader(im_path)
44
-
45
- # Convert to RGB if image has alpha channel
46
- if isinstance(im, np.ndarray):
47
- if im.ndim == 3 and im.shape[2] == 4:
48
- im = Image.fromarray(im).convert('RGB')
49
- elif im.ndim == 3:
50
- im = Image.fromarray(im)
51
- elif im.ndim == 2:
52
- im = Image.fromarray(im).convert('RGB')
53
- elif hasattr(im, 'mode') and im.mode == 'RGBA':
54
- im = im.convert('RGB')
55
-
56
  im, im_shp = im_preprocess(im, hypar["cache_size"])
57
- im = torch.divide(im, 255.0)
58
  shape = torch.from_numpy(np.array(im_shp))
59
- return transform(im).unsqueeze(0), shape.unsqueeze(0)
60
 
61
- def build_model(hypar, device):
62
- net = hypar["model"]
63
-
64
- if hypar["model_digit"] == "half":
 
 
65
  net.half()
66
  for layer in net.modules():
67
  if isinstance(layer, nn.BatchNorm2d):
@@ -69,110 +62,92 @@ def build_model(hypar, device):
69
 
70
  net.to(device)
71
 
72
- if hypar["restore_model"] != "":
73
- net.load_state_dict(torch.load(os.path.join(hypar["model_path"], hypar["restore_model"]),
74
- map_location=device))
75
- net.eval()
76
  return net
77
 
78
- def predict(net, inputs_val, shapes_val, hypar, device):
 
 
 
 
79
  net.eval()
80
 
81
- if hypar["model_digit"] == "full":
82
  inputs_val = inputs_val.type(torch.FloatTensor)
83
  else:
84
  inputs_val = inputs_val.type(torch.HalfTensor)
85
 
86
- inputs_val_v = Variable(inputs_val, requires_grad=False).to(device)
87
- ds_val = net(inputs_val_v)[0]
88
- pred_val = ds_val[0][0,:,:,:]
89
-
90
- pred_val = torch.squeeze(F.upsample(torch.unsqueeze(pred_val,0),
91
- (shapes_val[0][0], shapes_val[0][1]), mode='bilinear'))
 
 
 
92
 
93
  ma = torch.max(pred_val)
94
  mi = torch.min(pred_val)
95
- pred_val = (pred_val-mi)/(ma-mi)
96
-
97
- if device == 'cuda':
98
- torch.cuda.empty_cache()
99
- return (pred_val.detach().cpu().numpy()*255).astype(np.uint8)
100
 
 
 
 
101
  # Set Parameters
102
- hypar = {
103
- "model_path": "./saved_models",
104
- "restore_model": "isnet.pth",
105
- "interm_sup": False,
106
- "model_digit": "full",
107
- "seed": 0,
108
- "cache_size": [1024, 1024],
109
- "input_size": [1024, 1024],
110
- "crop_size": [1024, 1024],
111
- "model": ISNetDIS()
112
- }
113
-
114
- # Build Model
115
- net = build_model(hypar, device)
116
 
117
- # Flask app
118
- app = Flask(__name__)
119
- app.config['UPLOAD_FOLDER'] = 'uploads'
120
- app.config['RESULT_FOLDER'] = 'results'
121
 
122
- os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
123
- os.makedirs(app.config['RESULT_FOLDER'], exist_ok=True)
 
124
 
125
- @app.route('/')
126
- def index():
127
- return render_template('index.html')
128
 
129
- @app.route('/api/process', methods=['POST'])
130
- def process_image():
131
- if 'image' not in request.files:
132
- return jsonify({'error': 'No image provided'}), 400
133
-
134
- file = request.files['image']
135
- if file.filename == '':
136
- return jsonify({'error': 'No image selected'}), 400
137
-
138
- # Save uploaded file
139
- upload_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
140
- file.save(upload_path)
141
-
142
- try:
143
- # Process image (same as original inference function)
144
- image_tensor, orig_size = load_image(upload_path, hypar)
145
- mask = predict(net, image_tensor, orig_size, hypar, device)
146
-
147
- # Create results
148
- pil_mask = Image.fromarray(mask).convert('L')
149
- im_rgb = Image.open(upload_path).convert("RGB")
150
- im_rgba = im_rgb.copy()
151
- im_rgba.putalpha(pil_mask)
152
-
153
- # Save results
154
- result_rgba_path = os.path.join(app.config['RESULT_FOLDER'], f"rgba_{file.filename}")
155
- result_mask_path = os.path.join(app.config['RESULT_FOLDER'], f"mask_{file.filename}")
156
-
157
- im_rgba.save(result_rgba_path, format="PNG")
158
- pil_mask.save(result_mask_path, format="PNG")
159
-
160
- return jsonify({
161
- 'original': f"/uploads/{file.filename}",
162
- 'rgba': f"/results/rgba_{file.filename}",
163
- 'mask': f"/results/mask_{file.filename}",
164
- 'filename': file.filename
165
- })
166
- except Exception as e:
167
- return jsonify({'error': str(e)}), 500
168
-
169
- @app.route('/uploads/<filename>')
170
- def serve_upload(filename):
171
- return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
172
-
173
- @app.route('/results/<filename>')
174
- def serve_result(filename):
175
- return send_from_directory(app.config['RESULT_FOLDER'], filename)
176
-
177
- if __name__ == '__main__':
178
- app.run(host='0.0.0.0', port=7860, debug=True)
 
1
+ import cv2
2
+ import gradio as gr
3
  import os
4
+ from PIL import Image
5
  import numpy as np
6
  import torch
7
  from torch.autograd import Variable
8
  from torchvision import transforms
9
  import torch.nn.functional as F
10
+ import gdown
11
+ 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
+ # 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
  # Download official weights
26
  if not os.path.exists("saved_models"):
27
+ os.mkdir("saved_models")
28
+ os.system("mv isnet.pth saved_models/")
29
+
 
 
30
  class GOSNormalize(object):
31
+ '''
32
+ Normalize the Image using torch.transforms
33
+ '''
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
+
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) # make a batch of image, shape
51
 
52
+
53
+ def build_model(hypar,device):
54
+ net = hypar["model"]#GOSNETINC(3,1)
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
 
63
  net.to(device)
64
 
65
+ if(hypar["restore_model"]!=""):
66
+ net.load_state_dict(torch.load(hypar["model_path"]+"/"+hypar["restore_model"], map_location=device))
67
+ net.to(device)
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(hypar["model_digit"]=="full"):
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
+ pred_val = ds_val[0][0,:,:,:] # B x 1 x H x W # we want the first one which is the most accurate prediction
89
+
90
+ ## recover the prediction spatial size to the orignal image size
91
+ pred_val = torch.squeeze(F.upsample(torch.unsqueeze(pred_val,0),(shapes_val[0][0],shapes_val[0][1]),mode='bilinear'))
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': torch.cuda.empty_cache()
98
+ return (pred_val.detach().cpu().numpy()*255).astype(np.uint8) # it is the mask we need
99
+
100
  # Set Parameters
101
+ hypar = {} # paramters for inferencing
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
 
 
 
 
103
 
104
+ hypar["model_path"] ="./saved_models" ## load trained weights from this path
105
+ hypar["restore_model"] = "isnet.pth" ## name of the to-be-loaded weights
106
+ hypar["interm_sup"] = False ## indicate if activate intermediate feature supervision
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://img.shields.io/twitter/follow/DoEvent?label=@DoEvent&style=social)](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)