Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
size = 128
|
5 |
|
6 |
def build_model(input_shape):
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
return model
|
9 |
|
10 |
model = build_model(input_shape=(size, size, 1))
|
11 |
model.load_weights('BreastCancerSegmentation.h5')
|
12 |
|
13 |
-
def preprocess_image(image, size
|
14 |
-
image = cv2.resize(image, (size,size))
|
15 |
-
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
16 |
-
image = image/255.
|
17 |
return image
|
18 |
-
|
19 |
def segment(image):
|
20 |
image = preprocess_image(image, size=size)
|
21 |
image = np.expand_dims(image, 0)
|
@@ -28,11 +49,11 @@ def segment(image):
|
|
28 |
return mask_image
|
29 |
|
30 |
if __name__ == "__main__":
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from tensorflow.keras.models import load_model
|
6 |
+
from tensorflow.keras.layers import Input, Conv2D
|
7 |
+
from tensorflow.keras.models import Model
|
8 |
|
9 |
size = 128
|
10 |
|
11 |
def build_model(input_shape):
|
12 |
+
input_layer = Input(input_shape)
|
13 |
+
|
14 |
+
s1, p1 = encoder_block(input_layer, 64)
|
15 |
+
s2, p2 = encoder_block(p1, 128)
|
16 |
+
s3, p3 = encoder_block(p2, 256)
|
17 |
+
s4, p4 = encoder_block(p3, 512)
|
18 |
+
|
19 |
+
b1 = conv_block(p4, 1024)
|
20 |
+
|
21 |
+
d1 = decoder_block(b1, s4, 512)
|
22 |
+
d2 = decoder_block(d1, s3, 256)
|
23 |
+
d3 = decoder_block(d2, s2, 128)
|
24 |
+
d4 = decoder_block(d3, s1, 64)
|
25 |
+
|
26 |
+
output_layer = Conv2D(1, 1, padding="same", activation="sigmoid")(d4)
|
27 |
+
|
28 |
+
model = Model(input_layer, output_layer, name="U-Net")
|
29 |
return model
|
30 |
|
31 |
model = build_model(input_shape=(size, size, 1))
|
32 |
model.load_weights('BreastCancerSegmentation.h5')
|
33 |
|
34 |
+
def preprocess_image(image, size=128):
|
35 |
+
image = cv2.resize(image, (size, size))
|
36 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
37 |
+
image = image / 255.
|
38 |
return image
|
39 |
+
|
40 |
def segment(image):
|
41 |
image = preprocess_image(image, size=size)
|
42 |
image = np.expand_dims(image, 0)
|
|
|
49 |
return mask_image
|
50 |
|
51 |
if __name__ == "__main__":
|
52 |
+
gr.Interface(
|
53 |
+
fn=segment,
|
54 |
+
inputs="image",
|
55 |
+
outputs=gr.Image(type="pil", label="Breast Cancer Mask"),
|
56 |
+
examples=[["/content/benign(10).png"], ["/content/benign(109).png"]],
|
57 |
+
title="Breast Cancer Ultrasound Image Segmentation",
|
58 |
+
description="Check out this exciting development in the field of breast cancer diagnosis and treatment! A demo of Breast Cancer Ultrasound Image Segmentation has been developed. Upload an image file, or try out one of the examples below!"
|
59 |
+
).launch(share=True, debug=True)
|