-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.py
303 lines (230 loc) · 8.26 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# ML
import torchvision.transforms as transforms
import torch
# Web App
from flask import Flask, render_template, send_file, request, json
app = Flask(__name__)
# Show Images
from PIL import Image
from skimage import color
import io
import numpy as np
import base64
# Biomedical Images
from nd2reader import ND2Reader
from skimage import exposure, img_as_ubyte
# Temp file
from tempfile import NamedTemporaryFile
# List dir
import os
# Suppress all models warnings
import warnings
warnings.filterwarnings('ignore')
# ---------------------Models-----------------------------------
models = {}
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
mypath="/app/models/"
onlyfiles = [f.replace(".pth","") for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f))]
onlyfiles.sort()
for model_name in onlyfiles:
model = torch.jit.load("models/"+model_name+".pth")
model = model.cpu()
model.eval()
models[model_name]=model
# ---------------------Best Model---------------------------------
best_model = ""
for model_name in onlyfiles:
if "best" in model_name:
best_model = model_name.replace("-best","")
models[best_model] = models.pop(model_name)
def normPRED(d):
ma = torch.max(d)
mi = torch.min(d)
dn = (d-mi)/(ma-mi)
return dn
def transform_image(image):
my_transforms = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(
[0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])
image_aux = image
return my_transforms(image_aux).unsqueeze(0).to(device)
u2net_name = "u^2-net"
maskrcnn_name = "mask-rcnn"
def inference(model_name, input):
model=models[model_name]
# Moving to GPU if available
model = model.to(device)
with torch.no_grad():
if u2net_name in model_name.lower():
outputs, _, _, _, _, _, _ = model(input)
elif maskrcnn_name in model_name.lower():
_,outputs = model([input[0]])
else:
outputs = model(input)
if u2net_name in model_name.lower():
outputs=torch.sigmoid(outputs)
outputs=normPRED(outputs)
outputs=outputs>0.5
outputs=outputs.type(torch.uint8)
outputs=outputs[0]
elif maskrcnn_name in model_name.lower():
outputs=outputs[0]
outputs=outputs["masks"]
if outputs.shape[0]>0:
outputs=outputs[0]
outputs=outputs>0.5
outputs=outputs.type(torch.uint8)
else:
outputs = torch.argmax(outputs,1)
# Moving to CPU
model = model.cpu()
return outputs
def mask_into_image(image, mask):
mask = mask*255
prediction = Image.fromarray(np.uint8(mask[0]),"L")
# Save Mask to buffer
buff = io.BytesIO()
image = np.array(image)
output = color.grey2rgb(np.array(prediction))
# Selecting color of tumor
output[np.where((output==[255,255,255]).all(axis=2))] = [0,0,255]
# Changing background to white
output[np.where((output==[0,0,0]).all(axis=2))] = [255,255,255]
# Blending image with it's mask
out_img = np.zeros(image.shape, dtype=image.dtype)
out_img[:,:,:] = (alpha * image[:,:,:]) + ((1-alpha) * output[:,:,:])
out_img = Image.fromarray(out_img)
out_img.save(buff, "PNG")
return buff
def get_prediction_several_models(file, model_names):
img_bytes = file.read()
image = None
if file.filename.endswith(".nd2"):
# Named Temporal File in RAM dir="/dev/shm" does the trick
tmp_file=NamedTemporaryFile(delete=False, suffix=".nd2", dir="/dev/shm")
tmp_file.write(img_bytes)
tmp_file.close()
with ND2Reader(tmp_file.name) as image_reader:
nd2Image = image_reader[0]
# Image Conversion --------------------------------------
# Transforming image from 16bits into 8 bit
nd2Image = img_as_ubyte(nd2Image)
# Reescaling color intensity, if not image gets very dark
nd2Image = exposure.rescale_intensity(nd2Image)
# Converting 1 channel image to 3 channels
nd2Image = color.grey2rgb(nd2Image)
image = Image.fromarray(nd2Image)
# Removing Temporal File
os.unlink(tmp_file.name)
else:
img_bytes = io.BytesIO(img_bytes)
image = Image.open(img_bytes)
# We need to make it before
image = transforms.Resize((1002,1002))(image)
tensor = transform_image(image=image)
data = {}
data["filename"]=file.filename
buff = io.BytesIO()
image.save(buff, "PNG")
data["image"]= base64.encodebytes(buff.getvalue()).decode('ascii')
data["mask"] = []
masks = []
for model_name in model_names:
mask = inference(model_name=model_name, input=tensor)
mask_ndarray = mask.detach().cpu().numpy()
# Releasing CUDA Memory
del mask
masks.append(mask_ndarray)
buff = mask_into_image(image=image, mask=mask_ndarray)
aux_model={}
aux_model["model_name"]=model_name + " Prediction "
aux_model["mask_data"]=base64.encodebytes(buff.getvalue()).decode('ascii')
data["mask"].append(aux_model)
# If more than one prediction do ensemble
if len(masks)>1:
# Convert int mask to bool
for i in range(len(masks)):
masks[i] = masks[i].astype(bool)
# And & OR Ensemble
and_mask=masks[0]
or_mask=masks[0]
for i in range(1,len(masks)):
and_mask=np.logical_and(and_mask,masks[i])
or_mask=np.logical_or(or_mask,masks[i])
and_mask=and_mask.astype(int)
or_mask=or_mask.astype(int)
# And Ensemble
buff = mask_into_image(image=image, mask=and_mask)
aux_and={}
aux_and["model_name"]="Ensemble AND"
aux_and["mask_data"]=base64.encodebytes(buff.getvalue()).decode('ascii')
data["mask"].append(aux_and)
# OR Ensemble
buff = mask_into_image(image=image, mask=or_mask)
aux_or={}
aux_or["model_name"]="Ensemble OR"
aux_or["mask_data"]=base64.encodebytes(buff.getvalue()).decode('ascii')
data["mask"].append(aux_or)
return data
# /
@app.route("/",methods=["GET"])
def main():
return render_template("index.html")
alpha = 0.8
@app.route("/",methods=["POST"])
def best_model_inference():
if request.method == "POST":
files = request.files.getlist("file")
res = []
for file in files:
data = get_prediction_several_models(file, [best_model])
res.append(data)
response = app.response_class(
response=json.dumps(res),
status=200,
mimetype='application/json'
)
return response
# /chooseModel
@app.route("/chooseModel",methods=["GET"])
def get_chooseModel_view():
model_names = list(models.keys())
return render_template("chooseModel.html",model_names=model_names)
@app.route("/chooseModel",methods=["POST"])
def chooseModel_inference():
if request.method == "POST":
files = request.files.getlist("file")
model_name = request.form["model"]
res = []
for file in files:
data = get_prediction_several_models(file, [model_name])
res.append(data)
response = app.response_class(
response=json.dumps(res),
status=200,
mimetype='application/json'
)
return response
# /modelComparison
@app.route("/modelComparison",methods=["GET"])
def get_modelComparison_view():
model_names = list(models.keys())
return render_template("modelComparison.html",model_names=model_names)
@app.route("/modelComparison",methods=["POST"])
def modelComparison_inference():
if request.method == "POST":
files = request.files.getlist("file")
model_names = request.form.getlist("models")
res = []
for file in files:
data = get_prediction_several_models(file,model_names)
res.append(data)
response = app.response_class(
response=json.dumps(res),
status=200,
mimetype='application/json'
)
return response
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0")