Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
+
from keras.models import Model
|
6 |
+
from keras.layers import Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate
|
7 |
+
|
8 |
+
size = 128
|
9 |
+
|
10 |
+
def preprocess_image(image, size=128):
|
11 |
+
image = image.resize((size, size))
|
12 |
+
image = image.convert("L")
|
13 |
+
image = np.array(image) / 255.0
|
14 |
+
return image
|
15 |
+
|
16 |
+
def conv_block(input, num_filters):
|
17 |
+
conv = Conv2D(num_filters, (3, 3), activation="relu", padding="same", kernel_initializer='he_normal')(input)
|
18 |
+
conv = Conv2D(num_filters, (3, 3), activation="relu", padding="same", kernel_initializer='he_normal')(conv)
|
19 |
+
return conv
|
20 |
+
|
21 |
+
def encoder_block(input, num_filters):
|
22 |
+
conv = conv_block(input, num_filters)
|
23 |
+
pool = MaxPooling2D((2, 2))(conv)
|
24 |
+
return conv, pool
|
25 |
+
|
26 |
+
def decoder_block(input, skip_features, num_filters):
|
27 |
+
uconv = Conv2DTranspose(num_filters, (2, 2), strides=2, padding="same")(input)
|
28 |
+
con = concatenate([uconv, skip_features])
|
29 |
+
conv = conv_block(con, num_filters)
|
30 |
+
return conv
|
31 |
+
|
32 |
+
def build_model(input_shape):
|
33 |
+
input_layer = Input(input_shape)
|
34 |
+
|
35 |
+
s1, p1 = encoder_block(input_layer, 64)
|
36 |
+
s2, p2 = encoder_block(p1, 128)
|
37 |
+
s3, p3 = encoder_block(p2, 256)
|
38 |
+
s4, p4 = encoder_block(p3, 512)
|
39 |
+
|
40 |
+
b1 = conv_block(p4, 1024)
|
41 |
+
|
42 |
+
d1 = decoder_block(b1, s4, 512)
|
43 |
+
d2 = decoder_block(d1, s3, 256)
|
44 |
+
d3 = decoder_block(d2, s2, 128)
|
45 |
+
d4 = decoder_block(d3, s1, 64)
|
46 |
+
|
47 |
+
output_layer = Conv2D(1, 1, padding="same", activation="sigmoid")(d4)
|
48 |
+
model = Model(input_layer, output_layer, name="U-Net")
|
49 |
+
model.load_weights('BreastCancerSegmentation.h5')
|
50 |
+
return model
|
51 |
+
|
52 |
+
def preprocess_image(image, size=128):
|
53 |
+
image = cv2.resize(image, (size, size))
|
54 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
55 |
+
image = image / 255.
|
56 |
+
return image
|
57 |
+
|
58 |
+
def segment(image):
|
59 |
+
image = preprocess_image(image, size=size)
|
60 |
+
image = np.expand_dims(image, 0)
|
61 |
+
output = model.predict(image, verbose=0)
|
62 |
+
mask_image = output[0]
|
63 |
+
mask_image = np.squeeze(mask_image, -1)
|
64 |
+
mask_image *= 255
|
65 |
+
mask_image = mask_image.astype(np.uint8)
|
66 |
+
mask_image = Image.fromarray(mask_image).convert("L")
|
67 |
+
return mask_image
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
model = build_model(input_shape=(size, size, 1))
|
71 |
+
gr.Interface(
|
72 |
+
fn=segment,
|
73 |
+
inputs="image",
|
74 |
+
outputs=gr.Image(type="pil", label="Breast Cancer Mask"),
|
75 |
+
examples=[["benign(10).png"], ["benign(109).png"]],
|
76 |
+
title = '<h1 style="text-align: center;">Breast Cancer Ultrasound Image Segmentation! 💐 </h1>',
|
77 |
+
description = """
|
78 |
+
Check out this exciting development in the field of breast cancer diagnosis and treatment!
|
79 |
+
A demo of Breast Cancer Ultrasound Image Segmentation has been developed.
|
80 |
+
Upload image file, or try out one of the examples below! 🙌
|
81 |
+
(ผลงานชิ้นนี้เป็นของ นาวสาวสุวีรยา เนินทราย เท่านั้น หากมีผู้อื่นนำไปคัดลอกผลงานต่อ ถือเป็นความผิด)
|
82 |
+
"""
|
83 |
+
).launch(debug=True)
|