lombardata
commited on
Commit
•
95ddf62
1
Parent(s):
7989a0a
Update app.py
Browse files
app.py
CHANGED
@@ -42,8 +42,31 @@ label2id = {v: k for k, v in id2label.items()}
|
|
42 |
|
43 |
model = NewheadDinov2ForImageClassification.from_pretrained(checkpoint_name)
|
44 |
|
45 |
-
|
|
|
|
|
46 |
def predict(input_image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
image_processor = AutoImageProcessor.from_pretrained(checkpoint_name)
|
48 |
# predict
|
49 |
inputs = image_processor(input_image, return_tensors="pt")
|
@@ -52,7 +75,7 @@ def predict(input_image):
|
|
52 |
pred = pred[0]
|
53 |
confidences = {classes_names[i]: round(float(pred[i]), 2) for i in range(50)}
|
54 |
return confidences
|
55 |
-
|
56 |
|
57 |
gr.Interface(
|
58 |
fn=predict,
|
|
|
42 |
|
43 |
model = NewheadDinov2ForImageClassification.from_pretrained(checkpoint_name)
|
44 |
|
45 |
+
def sigmoid(_outputs):
|
46 |
+
return 1.0 / (1.0 + np.exp(-_outputs))
|
47 |
+
|
48 |
def predict(input_image):
|
49 |
+
image_processor = AutoImageProcessor.from_pretrained(checkpoint_name)
|
50 |
+
# predict
|
51 |
+
inputs = image_processor(input_image, return_tensors="pt")
|
52 |
+
inputs = inputs
|
53 |
+
with torch.no_grad():
|
54 |
+
model_outputs = model(**inputs)
|
55 |
+
outputs = model_outputs["logits"][0]
|
56 |
+
scores = sigmoid(outputs)
|
57 |
+
result = {}
|
58 |
+
i = 0
|
59 |
+
for score in scores:
|
60 |
+
label = id2label[i]
|
61 |
+
result[label] = float(score)
|
62 |
+
i += 1
|
63 |
+
result = {key: result[key] for key in result if result[key] > 0.5}
|
64 |
+
return result
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
'''
|
70 |
image_processor = AutoImageProcessor.from_pretrained(checkpoint_name)
|
71 |
# predict
|
72 |
inputs = image_processor(input_image, return_tensors="pt")
|
|
|
75 |
pred = pred[0]
|
76 |
confidences = {classes_names[i]: round(float(pred[i]), 2) for i in range(50)}
|
77 |
return confidences
|
78 |
+
'''
|
79 |
|
80 |
gr.Interface(
|
81 |
fn=predict,
|