Spaces:
Sleeping
Sleeping
import fastai | |
import fastai.vision | |
import PIL | |
import gradio | |
import matplotlib | |
import numpy | |
import pandas | |
from fastai.vision.all import * | |
# Crear la clase | |
class ADA_SKIN(object): | |
# Inicializar el objeto | |
def __init__(self, name="Wallaby", verbose=True, *args, **kwargs): | |
super(ADA_SKIN, self).__init__(*args, **kwargs) | |
self.author = "Jey" | |
self.name = name | |
if verbose: | |
self._ph() | |
self._pp("Hola desde la clase", str(self.__class__) + " Clase: " + str(self.__class__.__name__)) | |
self._pp("Nombre del código", self.name) | |
self._pp("Autor", self.author) | |
self._ph() | |
self.article = '<h3>Predice las siguientes patologias en piel</h3><ol>' | |
self.article += '<li>Enfermedad de Bowen (AKIEC)</li>' | |
self.article += '<li>Carcinoma de células basales</li>' | |
self.article += '<li>Lesiones benignas similares a queratosis</li>' | |
self.article += '<li>Dermatofibroma</li>' | |
self.article += '<li>Melanoma</li>' | |
self.article += '<li>Lunares melanocíticos</li>' | |
self.article += '<li>Carcinoma de células escamosas</li>' | |
self.article += '<li>Lesiones vasculares</li>' | |
self.article += '<li>Benigno</li>' | |
self.article += '<li></li></ol>' | |
self.article += '<h3> Prueba Jey(2023)</h3><ul>' | |
self.examples = ['akiec1.jpg','bcc1.jpg','bkl1.jpg','df1.jpg','mel1.jpg', | |
'nevi1.jpg','scc1.jpg','vl1.jpg','benign1.jpg','benign3.jpg'] | |
self.title = "Predicción Cáncer de Piel prueba " | |
return | |
# Imprimir de manera legible el nombre y valor de una línea | |
def _pp(self, a, b): | |
print("%34s : %s" % (str(a), str(b))) | |
return | |
# Imprimir la línea de encabezado o pie de página | |
def _ph(self): | |
print("-" * 34, ":", "-" * 34) | |
return | |
def _predict_image(self, img, cat): | |
pred, idx, probs = learn.predict(img) | |
return dict(zip(cat, map(float, probs))) | |
def _predict_image2(self, img, cat): | |
pred, idx, probs = learn2.predict(img) | |
return dict(zip(cat, map(float, probs))) | |
def _draw_pred(self, df_pred, df2): | |
canvas, pic = matplotlib.pyplot.subplots(1, 2, figsize=(12, 6)) | |
ti = df_pred["vocab"].head(3).values | |
ti2 = df2["vocab"].head(2).values | |
try: | |
df_pred["pred"].head(3).plot(ax=pic[0], kind="pie", | |
cmap="Set2", labels=ti, explode=(0.02, 0, 0), | |
wedgeprops=dict(width=.4), | |
normalize=False) | |
df2["pred"].head(2).plot(ax=pic[1], kind="pie", | |
colors=["cornflowerblue", "darkorange"], labels=ti2, explode=(0.02, 0), | |
wedgeprops=dict(width=.4), | |
normalize=False) | |
except: | |
df_pred["pred"].head(3).plot(ax=pic[0], kind="pie", | |
cmap="Set2", labels=ti, explode=(0.02, 0, 0), | |
wedgeprops=dict(width=.4)) | |
df2["pred"].head(2).plot(ax=pic[1], kind="pie", | |
colors=["cornflowerblue", "darkorange"], labels=ti2, explode=(0.02, 0), | |
wedgeprops=dict(width=.4)) | |
t = str(ti[0]) + ": " + str(numpy.round(df_pred.head(1).pred.values[0] * 100, 2)) + "% de predicción" | |
pic[0].set_title(t, fontsize=14.0, fontweight="bold") | |
pic[0].axis('off') | |
pic[0].legend(ti, loc="lower right", title="Cáncer de Piel: ") | |
k0 = numpy.round(df2.head(1).pred.values[0] * 100, 2) | |
k1 = numpy.round(df2.tail(1).pred.values[0] * 100, 2) | |
if k0 > k1: | |
t2 = str(ti2[0]) + ": " + str(k0) + "% de predicción" | |
else: | |
t2 = str(ti2[1]) + ": " + str(k1) + "% de predicción" | |
pic[1].set_title(t2, fontsize=14.0, fontweight="bold") | |
pic[1].axis('off') | |
pic[1].legend(ti2, loc="lower right", title="Prediccíon Cáncer de Piel:") | |
canvas.tight_layout() | |
return canvas | |
def predict_donut(self, img): | |
d = self._predict_image(img, self.categories) | |
df = pandas.DataFrame(d, index=[0]) | |
df = df.transpose().reset_index() | |
df.columns = ["vocab", "pred"] | |
df.sort_values("pred", inplace=True, ascending=False, ignore_index=True) | |
d2 = self._predict_image2(img, self.categories2) | |
df2 = pandas.DataFrame(d2, index=[0]) | |
df2 = df2.transpose().reset_index() | |
df2.columns = ["vocab", "pred"] | |
canvas = self._draw_pred(df, df2) | |
return canvas | |
maxi = ADA_SKIN(verbose=False) | |
learn = fastai.learner.load_learner('ada_learn_skin_norm2000.pkl') | |
learn2 = fastai.learner.load_learner('ada_learn_malben.pkl') | |
maxi.categories = learn.dls.vocab | |
maxi.categories2 = learn2.dls.vocab | |
hf_image = gradio.inputs.Image(shape=(192, 192)) | |
hf_label = gradio.outputs.Label() | |
intf = gradio.Interface(fn=maxi.predict_donut, | |
inputs=hf_image, | |
outputs=["plot"], | |
examples=maxi.examples, | |
title=maxi.title, | |
live=True, | |
article=maxi.article) | |
intf.launch(inline=False, share=True) | |