lombardata
commited on
Commit
•
9d241a6
1
Parent(s):
f5a10fc
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,26 @@
|
|
1 |
-
import numpy as np
|
2 |
-
import gradio as gr
|
3 |
import torch
|
4 |
-
from transformers import
|
5 |
-
|
6 |
-
import
|
7 |
-
import json
|
8 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
9 |
|
10 |
# DEFINE MODEL NAME
|
11 |
model_name = "DinoVdeau-large-2024_04_03-with_data_aug_batch-size32_epochs150_freeze"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# IMPORT CLASSIFICATION MODEL
|
14 |
def create_head(num_features , number_classes ,dropout_prob=0.5 ,activation_func =nn.ReLU):
|
@@ -21,7 +33,6 @@ def create_head(num_features , number_classes ,dropout_prob=0.5 ,activation_func
|
|
21 |
if dropout_prob !=0 : layers.append(nn.Dropout(dropout_prob))
|
22 |
layers.append(nn.Linear(features_lst[-1] , number_classes))
|
23 |
return nn.Sequential(*layers)
|
24 |
-
from transformers import Dinov2Config, Dinov2Model
|
25 |
|
26 |
class NewheadDinov2ForImageClassification(Dinov2ForImageClassification):
|
27 |
def __init__(self, config: Dinov2Config) -> None:
|
@@ -30,55 +41,41 @@ class NewheadDinov2ForImageClassification(Dinov2ForImageClassification):
|
|
30 |
# Classifier head
|
31 |
self.classifier = create_head(config.hidden_size * 2, config.num_labels)
|
32 |
|
33 |
-
checkpoint_name = "lombardata/" + model_name
|
34 |
model = NewheadDinov2ForImageClassification.from_pretrained(checkpoint_name)
|
35 |
-
|
36 |
-
# IMPORT MODEL CONFIG PARAMETERS
|
37 |
-
config_path = hf_hub_download(repo_id=checkpoint_name, filename="config.json")
|
38 |
-
# Opening JSON file
|
39 |
-
config_file = open(config_path)
|
40 |
-
# returns JSON object as
|
41 |
-
config = json.load(config_file)
|
42 |
-
# import parameters
|
43 |
-
id2label = config["id2label"]
|
44 |
-
label2id = config["label2id"]
|
45 |
-
image_size = config["image_size"]
|
46 |
-
classes_names = list(label2id.keys())
|
47 |
-
|
48 |
-
# PREDICTIONS
|
49 |
def sigmoid(_outputs):
|
50 |
return 1.0 / (1.0 + np.exp(-_outputs))
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
image_processor = AutoImageProcessor.from_pretrained(checkpoint_name)
|
54 |
-
# predict
|
55 |
-
inputs = image_processor(input_image, return_tensors="pt")
|
56 |
-
inputs = inputs
|
57 |
with torch.no_grad():
|
58 |
model_outputs = model(**inputs)
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
for
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
return
|
69 |
-
|
70 |
# Define style
|
71 |
title = "Victor - DinoVd'eau image classification"
|
72 |
model_link = "https://huggingface.co/" + checkpoint_name
|
73 |
description = f"This application showcases the capability of artificial intelligence-based systems to identify objects within underwater images. To utilize it, you can either upload your own image or select one of the provided examples for analysis.\nFor predictions, we use this [open-source model]({model_link})"
|
74 |
|
75 |
-
gr.Interface(
|
76 |
fn=predict,
|
77 |
-
inputs=gr.Image(
|
78 |
-
outputs=
|
79 |
title=title,
|
80 |
-
description=description,
|
81 |
examples=["session_GOPR0106.JPG",
|
82 |
"session_2021_08_30_Mayotte_10_image_00066.jpg",
|
83 |
"session_2018_11_17_kite_Le_Morne_Manawa_G0065777.JPG",
|
84 |
-
"session_2023_06_28_caplahoussaye_plancha_body_v1B_00_GP1_3_1327.jpeg"]).launch()
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
from transformers import AutoImageProcessor, Dinov2ForImageClassification, Dinov2Config, Dinov2Model
|
3 |
+
from PIL import Image
|
4 |
+
import gradio as gr
|
|
|
5 |
from huggingface_hub import hf_hub_download
|
6 |
+
import json
|
7 |
+
import torch.nn as nn
|
8 |
+
import numpy as np
|
9 |
|
10 |
# DEFINE MODEL NAME
|
11 |
model_name = "DinoVdeau-large-2024_04_03-with_data_aug_batch-size32_epochs150_freeze"
|
12 |
+
checkpoint_name = "lombardata/" + model_name
|
13 |
+
|
14 |
+
# Load the model configuration and create the model
|
15 |
+
config_path = hf_hub_download(repo_id=checkpoint_name, filename="config.json")
|
16 |
+
with open(config_path, 'r') as config_file:
|
17 |
+
config = json.load(config_file)
|
18 |
+
id2label = config["id2label"]
|
19 |
+
label2id = config["label2id"]
|
20 |
+
image_size = config["image_size"]
|
21 |
+
num_labels = len(id2label)
|
22 |
+
|
23 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
24 |
|
25 |
# IMPORT CLASSIFICATION MODEL
|
26 |
def create_head(num_features , number_classes ,dropout_prob=0.5 ,activation_func =nn.ReLU):
|
|
|
33 |
if dropout_prob !=0 : layers.append(nn.Dropout(dropout_prob))
|
34 |
layers.append(nn.Linear(features_lst[-1] , number_classes))
|
35 |
return nn.Sequential(*layers)
|
|
|
36 |
|
37 |
class NewheadDinov2ForImageClassification(Dinov2ForImageClassification):
|
38 |
def __init__(self, config: Dinov2Config) -> None:
|
|
|
41 |
# Classifier head
|
42 |
self.classifier = create_head(config.hidden_size * 2, config.num_labels)
|
43 |
|
|
|
44 |
model = NewheadDinov2ForImageClassification.from_pretrained(checkpoint_name)
|
45 |
+
model.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
def sigmoid(_outputs):
|
47 |
return 1.0 / (1.0 + np.exp(-_outputs))
|
48 |
+
|
49 |
+
def predict(image, threshold):
|
50 |
+
# Preprocess the image
|
51 |
+
processor = AutoImageProcessor.from_pretrained(checkpoint_name)
|
52 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
53 |
|
54 |
+
# Get model predictions
|
|
|
|
|
|
|
|
|
55 |
with torch.no_grad():
|
56 |
model_outputs = model(**inputs)
|
57 |
+
logits = model_outputs.logits[0]
|
58 |
+
probabilities = torch.sigmoid(logits).cpu().numpy() # Convert to probabilities
|
59 |
+
|
60 |
+
# Create a dictionary of label scores
|
61 |
+
results = {id2label[str(i)]: float(prob) for i, prob in enumerate(probabilities)}
|
62 |
+
|
63 |
+
# Filter out predictions below a certain threshold (e.g., 0.5)
|
64 |
+
filtered_results = {label: prob for label, prob in results.items() if prob > threshold}
|
65 |
+
|
66 |
+
return filtered_results
|
67 |
+
|
68 |
# Define style
|
69 |
title = "Victor - DinoVd'eau image classification"
|
70 |
model_link = "https://huggingface.co/" + checkpoint_name
|
71 |
description = f"This application showcases the capability of artificial intelligence-based systems to identify objects within underwater images. To utilize it, you can either upload your own image or select one of the provided examples for analysis.\nFor predictions, we use this [open-source model]({model_link})"
|
72 |
|
73 |
+
iface = gr.Interface(
|
74 |
fn=predict,
|
75 |
+
inputs=[gr.components.Image(type="pil"), gr.components.Slider(minimum=0, maximum=1, value=0.5, label="Threshold")],
|
76 |
+
outputs=gr.components.Label(),
|
77 |
title=title,
|
|
|
78 |
examples=["session_GOPR0106.JPG",
|
79 |
"session_2021_08_30_Mayotte_10_image_00066.jpg",
|
80 |
"session_2018_11_17_kite_Le_Morne_Manawa_G0065777.JPG",
|
81 |
+
"session_2023_06_28_caplahoussaye_plancha_body_v1B_00_GP1_3_1327.jpeg"]).launch()
|