|
import gradio as gr |
|
import numpy as np |
|
from PIL import Image |
|
from keras.models import Model |
|
from keras.layers import Input, Conv2D, MaxPooling2D, Conv2DTranspose, concatenate |
|
|
|
def preprocess_image(image, size): |
|
image = image.resize((size, size)) |
|
image = image.convert("L") |
|
image = np.array(image) / 255.0 |
|
return image |
|
|
|
def conv_block(input, num_filters): |
|
conv = Conv2D(num_filters, (3, 3), activation="relu", padding="same", kernel_initializer='he_normal')(input) |
|
conv = Conv2D(num_filters, (3, 3), activation="relu", padding="same", kernel_initializer='he_normal')(conv) |
|
return conv |
|
|
|
def encoder_block(input, num_filters): |
|
conv = conv_block(input, num_filters) |
|
pool = MaxPooling2D((2, 2))(conv) |
|
return conv, pool |
|
|
|
def decoder_block(input, skip_features, num_filters): |
|
uconv = Conv2DTranspose(num_filters, (2, 2), strides=2, padding="same")(input) |
|
con = concatenate([uconv, skip_features]) |
|
conv = conv_block(con, num_filters) |
|
return conv |
|
|
|
def build_model(input_shape, num_output_channels): |
|
input_layer = Input(input_shape) |
|
|
|
s1, p1 = encoder_block(input_layer, 64) |
|
s2, p2 = encoder_block(p1, 128) |
|
s3, p3 = encoder_block(p2, 256) |
|
s4, p4 = encoder_block(p3, 512) |
|
|
|
b1 = conv_block(p4, 1024) |
|
|
|
d1 = decoder_block(b1, s4, 512) |
|
d2 = decoder_block(d1, s3, 256) |
|
d3 = decoder_block(d2, s2, 128) |
|
d4 = decoder_block(d3, s1, 64) |
|
|
|
output_layer = Conv2D(num_output_channels, 1, padding="same", activation="sigmoid")(d4) |
|
model = Model(input_layer, output_layer, name="U-Net") |
|
model.load_weights('modelo.h5') |
|
return model |
|
|
|
def image_segmentation(image, size=128, num_output_channels=1): |
|
image = preprocess_image(image, size) |
|
image = np.expand_dims(image, 0) |
|
output = model.predict(image, verbose=0) |
|
mask_image = output[0] |
|
mask_image = np.squeeze(mask_image, -1) |
|
mask_image *= 255 |
|
mask_image = mask_image.astype(np.uint8) |
|
mask_image = Image.fromarray(mask_image).convert("L") |
|
|
|
positive_pixels = np.count_nonzero(mask_image) |
|
total_pixels = mask_image.size[0] * mask_image.size[1] |
|
percentage = (positive_pixels / total_pixels) * 100 |
|
|
|
class_0_percentage = 100 - percentage |
|
class_1_percentage = percentage |
|
|
|
return mask_image, class_0_percentage, class_1_percentage |
|
|
|
if __name__ == "__main__": |
|
size = 128 |
|
num_output_channels = 1 |
|
model = build_model(input_shape=(size, size, 1), num_output_channels=num_output_channels) |
|
gr.Interface( |
|
fn=image_segmentation, |
|
inputs="image", |
|
outputs=[ |
|
gr.Image(type="pil", label="Cáncer de mama "), |
|
gr.Number(label="Benigno"), |
|
gr.Number(label="Maligno") |
|
], |
|
title='<h1 style="text-align: center;">Cáncer de mama </h1>', |
|
description=""" |
|
Demostración de segmentación de imágenes de ultrasonido de cáncer de mama. |
|
""", |
|
theme="default", |
|
layout="vertical", |
|
verbose=True |
|
).launch(debug=True) |
|
|
|
|