Spaces:
Sleeping
Sleeping
Erick Garcia Espinosa
commited on
Commit
路
685396e
1
Parent(s):
9b07e84
Add application file and dependencies
Browse files- ARTmodelo5ns_vit_weights_epoch6.pth +3 -0
- app.py +60 -0
- requirements.txt +5 -0
ARTmodelo5ns_vit_weights_epoch6.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bdb37a9d5130066802689779dc5322787ea94eef1f1d32e4fac23076774346ff
|
3 |
+
size 343269794
|
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
from torchvision import transforms
|
5 |
+
from PIL import Image
|
6 |
+
from timm import create_model
|
7 |
+
|
8 |
+
# Definir el diccionario de mapeo de clases a 铆ndices
|
9 |
+
class_to_idx = {'Monkeypox': 0, 'Melanoma': 1, 'Herpes': 2, 'Sarampion': 3, 'Varicela': 4}
|
10 |
+
|
11 |
+
# Transformaci贸n de datos
|
12 |
+
transform = transforms.Compose([
|
13 |
+
transforms.Resize((224, 224)),
|
14 |
+
transforms.ToTensor(),
|
15 |
+
])
|
16 |
+
|
17 |
+
# Funci贸n para cargar y preprocesar una imagen
|
18 |
+
def load_image(image_path):
|
19 |
+
image = Image.open(image_path).convert('RGB')
|
20 |
+
image = transform(image).unsqueeze(0) # A帽adir dimensi贸n del batch
|
21 |
+
return image
|
22 |
+
|
23 |
+
# Cargar el modelo
|
24 |
+
model_name = 'vit_base_patch16_224'
|
25 |
+
pretrained = True
|
26 |
+
num_classes = len(class_to_idx)
|
27 |
+
model = create_model(model_name, pretrained=pretrained, num_classes=num_classes)
|
28 |
+
model.load_state_dict(torch.load('ARTmodelo5ns_vit_weights_epoch6.pth', map_location='cpu'))
|
29 |
+
model.eval()
|
30 |
+
|
31 |
+
# Definir la funci贸n de predicci贸n
|
32 |
+
def predict_image(img):
|
33 |
+
# Convertir la imagen a PIL.Image si es un numpy array
|
34 |
+
if isinstance(img, np.ndarray):
|
35 |
+
img = Image.fromarray(img)
|
36 |
+
|
37 |
+
# Convertir la imagen a tensor y a帽adir dimensi贸n del batch
|
38 |
+
img_tensor = transform(img).unsqueeze(0)
|
39 |
+
|
40 |
+
# Realizar la predicci贸n
|
41 |
+
with torch.no_grad():
|
42 |
+
output = model(img_tensor)
|
43 |
+
_, predicted = torch.max(output, 1)
|
44 |
+
|
45 |
+
# Obtener la etiqueta predicha
|
46 |
+
predicted_label = list(class_to_idx.keys())[predicted.item()]
|
47 |
+
|
48 |
+
return predicted_label
|
49 |
+
|
50 |
+
# Crear la interfaz de Gradio
|
51 |
+
iface = gr.Interface(
|
52 |
+
fn=predict_image,
|
53 |
+
inputs=gr.inputs.Image(type="file", label="Sube una imagen"),
|
54 |
+
outputs=gr.outputs.Label(label="Predicci贸n"),
|
55 |
+
title="Clasificaci贸n de Im谩genes de Lesiones Cut谩neas",
|
56 |
+
description="Carga una imagen de una lesi贸n cut谩nea para obtener una predicci贸n."
|
57 |
+
)
|
58 |
+
|
59 |
+
# Lanzar la interfaz de Gradio
|
60 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
timm
|
3 |
+
numpy
|
4 |
+
Pillow
|
5 |
+
gradio
|