Update app.py
Browse files
app.py
CHANGED
|
@@ -3,60 +3,43 @@ from tensorflow import keras
|
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
"Gmelina arborea Roxb",
|
| 33 |
-
"Hevea brasiliensis",
|
| 34 |
-
"Hopea",
|
| 35 |
-
"Khaya senegalensis",
|
| 36 |
-
"Khaya senegalensis A.Juss",
|
| 37 |
-
"Lagerstroemia speciosa",
|
| 38 |
-
"Magnolia alba",
|
| 39 |
-
"Mangifera",
|
| 40 |
-
"Melaleuca",
|
| 41 |
-
"Melia azedarach",
|
| 42 |
-
"Musa",
|
| 43 |
-
"Nephelium lappaceum",
|
| 44 |
-
"Persea",
|
| 45 |
-
"Polyalthia longifolia",
|
| 46 |
-
"Prunnus",
|
| 47 |
-
"Prunus salicina",
|
| 48 |
-
"Psidium guajava",
|
| 49 |
-
"Pterocarpus macrocarpus",
|
| 50 |
-
"Senna siamea",
|
| 51 |
-
"Spondias mombin L",
|
| 52 |
-
"Syzygium nervosum",
|
| 53 |
-
"Tamarindus indica",
|
| 54 |
-
"Tectona grandis",
|
| 55 |
-
"Terminalia catappa",
|
| 56 |
-
"Veitchia merrilli",
|
| 57 |
-
"Wrightia",
|
| 58 |
-
"Wrightia religiosa",
|
| 59 |
-
]
|
| 60 |
|
| 61 |
|
| 62 |
def predict(path):
|
|
@@ -65,7 +48,7 @@ def predict(path):
|
|
| 65 |
image = np.expand_dims(image, axis=0)
|
| 66 |
pred = model.predict(image, verbose=0)
|
| 67 |
pred = pred[0]
|
| 68 |
-
confidences = {
|
| 69 |
return confidences
|
| 70 |
|
| 71 |
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Load PyTorch model
|
| 7 |
+
def create_head(num_features , number_classes ,dropout_prob=0.5 ,activation_func =nn.ReLU):
|
| 8 |
+
features_lst = [num_features , num_features//2 , num_features//4]
|
| 9 |
+
layers = []
|
| 10 |
+
for in_f ,out_f in zip(features_lst[:-1] , features_lst[1:]):
|
| 11 |
+
layers.append(nn.Linear(in_f , out_f))
|
| 12 |
+
layers.append(activation_func())
|
| 13 |
+
layers.append(nn.BatchNorm1d(out_f))
|
| 14 |
+
if dropout_prob !=0 : layers.append(nn.Dropout(dropout_prob))
|
| 15 |
+
layers.append(nn.Linear(features_lst[-1] , number_classes))
|
| 16 |
+
return nn.Sequential(*layers)
|
| 17 |
+
from transformers import Dinov2Config, Dinov2Model
|
| 18 |
|
| 19 |
+
class NewheadDinov2ForImageClassification(Dinov2ForImageClassification):
|
| 20 |
+
def __init__(self, config: Dinov2Config) -> None:
|
| 21 |
+
super().__init__(config)
|
| 22 |
+
|
| 23 |
+
self.num_labels = config.num_labels
|
| 24 |
+
self.dinov2 = Dinov2Model(config)
|
| 25 |
+
|
| 26 |
+
# Classifier head
|
| 27 |
+
self.classifier = create_head(config.hidden_size * 2, config.num_labels)
|
| 28 |
+
# IMPORT CLASSIFICATION MODEL
|
| 29 |
+
checkpoint_name = "lombardata/dino-base-2023_11_27-with_custom_head"
|
| 30 |
+
# import labels
|
| 31 |
+
classes_names = ["Acropore_branched", "Acropore_digitised", "Acropore_tabular", "Algae_assembly",
|
| 32 |
+
"Algae_limestone", "Algae_sodding", "Dead_coral", "Fish", "Human_object",
|
| 33 |
+
"Living_coral", "Millepore", "No_acropore_encrusting", "No_acropore_massive",
|
| 34 |
+
"No_acropore_sub_massive", "Rock", "Sand",
|
| 35 |
+
"Scrap", "Sea_cucumber", "Syringodium_isoetifolium",
|
| 36 |
+
"Thalassodendron_ciliatum", "Useless"]
|
| 37 |
+
|
| 38 |
+
classes_nb = list(np.arange(len(classes_names)))
|
| 39 |
+
id2label = {int(classes_nb[i]): classes_names[i] for i in range(len(classes_nb))}
|
| 40 |
+
label2id = {v: k for k, v in id2label.items()}
|
| 41 |
+
|
| 42 |
+
model = NewheadDinov2ForImageClassification.from_pretrained(checkpoint_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
def predict(path):
|
|
|
|
| 48 |
image = np.expand_dims(image, axis=0)
|
| 49 |
pred = model.predict(image, verbose=0)
|
| 50 |
pred = pred[0]
|
| 51 |
+
confidences = {classes_names[i]: round(float(pred[i]), 2) for i in range(50)}
|
| 52 |
return confidences
|
| 53 |
|
| 54 |
|