Giulio Rossi
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torchvision import transforms
|
3 |
+
from PIL import Image, ImageStat
|
4 |
+
import gradio as gr
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
# Carica il tuo modello pre-addestrato (aggiorna il percorso del modello come necessario)
|
8 |
+
model = torch.load('model_v11.pt', map_location=torch.device('cpu'))
|
9 |
+
model.eval()
|
10 |
+
|
11 |
+
# Funzione per loggare i colori dell'immagine
|
12 |
+
def log_image_color_stats(image):
|
13 |
+
stat = ImageStat.Stat(image)
|
14 |
+
mean_rgb = stat.mean
|
15 |
+
print(f"Color Stats - R: {mean_rgb[0] / 255.0}, G: {mean_rgb[1] / 255.0}, B: {mean_rgb[2] / 255.0}")
|
16 |
+
|
17 |
+
# Funzione per loggare i dati del tensore
|
18 |
+
def log_tensor_data(tensor):
|
19 |
+
print(f"Tensor shape: {tensor.shape}")
|
20 |
+
print(f"Tensor data (first 10 values): {tensor.flatten()[:10]}")
|
21 |
+
|
22 |
+
# Preprocessing per l'immagine con normalizzazione
|
23 |
+
preprocess = transforms.Compose([
|
24 |
+
transforms.Resize((224, 224)),
|
25 |
+
transforms.ToTensor(),
|
26 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
27 |
+
])
|
28 |
+
|
29 |
+
# Preprocessing senza normalizzazione (separato)
|
30 |
+
preprocess_without_normalize = transforms.Compose([
|
31 |
+
transforms.Resize((224, 224)),
|
32 |
+
transforms.ToTensor()
|
33 |
+
])
|
34 |
+
|
35 |
+
# Funzione di previsione
|
36 |
+
def classify_image(image):
|
37 |
+
# Logga i colori dell'immagine originale
|
38 |
+
log_image_color_stats(image)
|
39 |
+
|
40 |
+
# Ridimensiona l'immagine per coerenza
|
41 |
+
img_resized = image.resize((224, 224))
|
42 |
+
print(f"Resized Bitmap dimensions: {img_resized.size}")
|
43 |
+
|
44 |
+
# Preprocessa e ottieni il tensore non normalizzato
|
45 |
+
img_tensor_no_normalize = preprocess_without_normalize(image).unsqueeze(0)
|
46 |
+
print("Tensor data before normalization:")
|
47 |
+
log_tensor_data(img_tensor_no_normalize)
|
48 |
+
|
49 |
+
# Preprocessa e normalizza
|
50 |
+
img_tensor = preprocess(image).unsqueeze(0)
|
51 |
+
log_tensor_data(img_tensor)
|
52 |
+
|
53 |
+
# Esegui la previsione
|
54 |
+
with torch.no_grad():
|
55 |
+
output = model(img_tensor)
|
56 |
+
|
57 |
+
# Logga i punteggi raw
|
58 |
+
print(f"Raw output scores: {output}")
|
59 |
+
|
60 |
+
# Calcola le probabilità
|
61 |
+
probabilities = torch.softmax(output, dim=1)
|
62 |
+
|
63 |
+
# Estrai le probabilità e l'indice della classe prevista
|
64 |
+
predicted_class_index = probabilities.argmax()
|
65 |
+
predicted_probability = probabilities[0][predicted_class_index]
|
66 |
+
|
67 |
+
# Logga le probabilità di ciascuna classe
|
68 |
+
print("Probabilities for each class:")
|
69 |
+
for index, prob in enumerate(probabilities[0]):
|
70 |
+
print(f"Class {index}: {prob.item():.4f}")
|
71 |
+
|
72 |
+
# Restituisci l'indice della classe e la probabilità come output
|
73 |
+
return f"Predicted class index: {predicted_class_index.item()}, Probability: {predicted_probability.item():.4f}"
|
74 |
+
|
75 |
+
# Interfaccia Gradio
|
76 |
+
gr.Interface(fn=classify_image, inputs="image", outputs="text").launch()
|