Spaces:
Sleeping
Sleeping
Multi targer model added
Browse files- app-old.py +22 -0
- app.py +78 -14
- mammals-of-india-v0.pkl → final-v0.pkl +2 -2
- flagged/Behavior Predictions/tmp7ncvgp3m.json +1 -0
- flagged/Behavior Predictions/tmpctb7dywo.json +1 -0
- flagged/Species Predictions/tmpdx0rhnon.json +1 -0
- flagged/Species Predictions/tmpxv6q0zdy.json +1 -0
- flagged/Upload Image/16d731eabe5e13037f5656d5786479e687986f96/tmpis3qgsoq.png +0 -0
- flagged/Upload Image/e0899f5af61b54022c2e1e48bb8c93c8b9c31732/tmpa52lof9c.png +0 -0
- flagged/log.csv +3 -0
- test images/Antilope cervicapra_23.jpg +0 -0
- test images/Antilope cervicapra_33.jpg +0 -0
- test images/Gazella bennettii_01.jpg +0 -0
- test images/Loris lydekkerianus_11.jpg +0 -0
- test images/Pteropus giganteus_67.jpg +0 -0
- test images/Screenshot 2023-09-15 112403.png +0 -0
- test images/Screenshot 2023-09-15 141743.png +0 -0
- test images/Semnopithecus ajax_19.jpg +0 -0
- test images/Viverra zibetha_51.jpg +0 -0
- test.py +55 -0
app-old.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastai.vision.all import load_learner
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
#import pathlib
|
5 |
+
#temp = pathlib.PosixPath
|
6 |
+
#pathlib.PosixPath = pathlib.WindowsPath
|
7 |
+
|
8 |
+
labels = ['Acinonyx jubatus', 'Ailurus fulgens', 'Antilope cervicapra', 'Aonyx cinereus', 'Arctictis binturong', 'Axis porcinus', 'Bos frontalis', 'Bos grunniens', 'Bos taurus', 'Boselaphus tragocamelus', 'Bubalus bubalis', 'Budorcas taxicolor', 'Callosciurus erythraeus', 'Canis aureus', 'Canis lupus', 'Capra falconeri', 'Capra sibirica', 'Caracal caracal', 'Catopuma temminckii', 'Cervus elaphus', 'Cuon alpinus', 'Cynopterus sphinx', 'Dicerorhinus sumatrensis', 'Elephas maximus', 'Equus hemionus', 'Equus kiang', 'Felis catus', 'Felis chaus', 'Felis manul', 'Felis silvestris', 'Funambulus palmarum', 'Funambulus pennantii', 'Gazella bennettii', 'Helarctos malayanus', 'Hemitragus jemlahicus', 'Herpestes edwardsi', 'Herpestes javanicus', 'Herpestes smithii', 'Herpestes urva', 'Herpestes vitticollis', 'Hipposideros ater', 'Hipposideros speoris', 'Hoolock hoolock', 'Hyaena hyaena', 'Kerivoula picta', 'Lepus nigricollis', 'Loris lydekkerianus', 'Lutra lutra', 'Lutrogale perspicillata', 'Lynx lynx', 'Macaca silenus', 'Manis crassicaudata', 'Martes flavigula', 'Martes foina', 'Mellivora capensis', 'Melursus ursinus', 'Moschiola indica', 'Muntiacus muntjak', 'Mustela altaica', 'Mustela erminea', 'Mustela sibirica', 'Naemorhedus goral', 'Naemorhedus griseus', 'Neofelis nebulosa', 'Nilgiritragus hylocrius', 'Nycticebus bengalensis', 'Ochotona roylei', 'Orcinus orca', 'Panthera leo', 'Panthera pardus', 'Panthera tigris', 'Pantholops hodgsonii', 'Pardofelis marmorata', 'Pipistrellus coromandra', 'Prionailurus bengalensis', 'Prionailurus rubiginosus', 'Prionailurus viverrinus', 'Pteropus faunulus', 'Pteropus giganteus', 'Pteropus hypomelanus', 'Pteropus vampyrus', 'Ratufa bicolor', 'Ratufa indica', 'Rhinoceros unicornis', 'Rhinolophus lepidus', 'Rucervus duvaucelii', 'Rucervus eldii', 'Rusa unicolor', 'Semnopithecus ajax', 'Sus scrofa', 'Tamiops mcclellandii', 'Tetracerus quadricornis', 'Trachypithecus phayrei', 'Trachypithecus pileatus', 'Tupaia belangeri', 'Uncia uncia', 'Ursus arctos', 'Ursus thibetanus', 'Viverra zibetha', 'Viverricula indica', 'Vulpes bengalensis', 'Vulpes vulpes', 'Ziphius cavirostris']
|
9 |
+
|
10 |
+
model = load_learner("mammals-of-india-v0.pkl")
|
11 |
+
|
12 |
+
def recognize_image(image):
|
13 |
+
pred, idx, probs = model.predict(image)
|
14 |
+
print(pred, idx, probs)
|
15 |
+
return dict(zip(labels, map(float, probs)))
|
16 |
+
|
17 |
+
|
18 |
+
image = gr.inputs.Image(shape=(192, 192))
|
19 |
+
label = gr.outputs.Label(num_top_classes=5)
|
20 |
+
|
21 |
+
iface = gr.Interface(fn=recognize_image, inputs=image, outputs=label)
|
22 |
+
iface.launch(inline=False)
|
app.py
CHANGED
@@ -1,22 +1,86 @@
|
|
1 |
-
from fastai.vision.all import load_learner
|
2 |
import gradio as gr
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
|
8 |
-
|
|
|
9 |
|
10 |
-
|
|
|
11 |
|
12 |
-
|
13 |
-
pred, idx, probs = model.predict(image)
|
14 |
-
print(pred, idx, probs)
|
15 |
-
return dict(zip(labels, map(float, probs)))
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from fastai.vision.all import *
|
3 |
|
4 |
+
import pathlib
|
5 |
+
temp = pathlib.PosixPath
|
6 |
+
pathlib.PosixPath = pathlib.WindowsPath
|
7 |
|
8 |
+
# Load your multi-target model
|
9 |
+
model = load_learner("final-v0.pkl")
|
10 |
|
11 |
+
# List of behavior labels
|
12 |
+
behavior_labels = ['F', 'H', 'M', 'M', 'P', 'R', 'T']
|
13 |
|
14 |
+
all_labels = model.dls.vocab
|
|
|
|
|
|
|
15 |
|
16 |
+
def recognize_image(input_image, top_k=4):
|
17 |
+
# Make predictions
|
18 |
+
_, _, probabilities = model.predict(input_image)
|
19 |
+
|
20 |
+
# Get the indices of the top-k predicted labels based on probabilities
|
21 |
+
top_indices = (-probabilities).argsort()[:top_k]
|
22 |
+
|
23 |
+
# Get the labels corresponding to the top indices from the vocabulary
|
24 |
+
top_labels = [all_labels[i] for i in top_indices]
|
25 |
+
|
26 |
+
# Get the probabilities for the top-k labels
|
27 |
+
top_probabilities = [float(probabilities[i]) for i in top_indices]
|
28 |
+
|
29 |
+
# Separate labels into behavior and species categories
|
30 |
+
behavior_predictions = [label for label in top_labels if label in behavior_labels]
|
31 |
+
species_predictions = [label for label in top_labels if label not in behavior_labels]
|
32 |
+
|
33 |
+
# Create dictionaries for species and behavior predictions with their probabilities
|
34 |
+
species_result = {
|
35 |
+
'top_species_labels': species_predictions,
|
36 |
+
'top_species_probabilities': [prob for label, prob in zip(top_labels, top_probabilities) if label not in behavior_labels],
|
37 |
+
}
|
38 |
+
|
39 |
+
behavior_result = {
|
40 |
+
'top_behavior_labels': behavior_predictions,
|
41 |
+
'top_behavior_probabilities': [prob for label, prob in zip(top_labels, top_probabilities) if label in behavior_labels],
|
42 |
+
}
|
43 |
+
|
44 |
+
# Format the results into a more readable format
|
45 |
+
formatted_species_result = {
|
46 |
+
'Species Predictions': dict(zip(species_predictions, [round(prob, 4) for prob in species_result['top_species_probabilities']])),
|
47 |
+
}
|
48 |
+
|
49 |
+
formatted_behavior_result = {
|
50 |
+
'Behavior Predictions': dict(zip(behavior_predictions, [round(prob, 4) for prob in behavior_result['top_behavior_probabilities']])),
|
51 |
+
}
|
52 |
+
|
53 |
+
return formatted_species_result, formatted_behavior_result
|
54 |
|
55 |
+
def get_outputs(input_image):
|
56 |
+
species_predictions, behavior_predictions = recognize_image(input_image)
|
57 |
+
print("Species Predictions:")
|
58 |
+
for species, probability in species_predictions['Species Predictions'].items():
|
59 |
+
print(f"{species}: {probability}")
|
60 |
|
61 |
+
print("Behavior Predictions:")
|
62 |
+
for behavior, probability in behavior_predictions['Behavior Predictions'].items():
|
63 |
+
print(f"{behavior}: {probability}")
|
64 |
+
|
65 |
+
return species_predictions['Species Predictions'], behavior_predictions['Behavior Predictions']
|
66 |
+
|
67 |
+
path = 'test images/'
|
68 |
+
|
69 |
+
image_path = []
|
70 |
+
|
71 |
+
for i in os.listdir(path):
|
72 |
+
image_path.append(path+i)
|
73 |
+
|
74 |
+
# Gradio interface
|
75 |
+
input_image = gr.inputs.Image(label='Upload Image')
|
76 |
+
output_species = gr.outputs.Label(label='Species Predictions')
|
77 |
+
output_behavior = gr.outputs.Label(label='Behavior Predictions')
|
78 |
+
|
79 |
+
gr.Interface(
|
80 |
+
fn=get_outputs,
|
81 |
+
inputs=input_image,
|
82 |
+
outputs=[output_species, output_behavior],
|
83 |
+
examples = image_path,
|
84 |
+
title='Species and Behavior Recognition',
|
85 |
+
description='Upload an image and get predictions for species and behavior.'
|
86 |
+
).launch()
|
mammals-of-india-v0.pkl → final-v0.pkl
RENAMED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d5842c76985bba8c577ed8a6b257da99dfc43321e069fed4a765e24ec77ade74
|
3 |
+
size 184385371
|
flagged/Behavior Predictions/tmp7ncvgp3m.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"label": "Behavior Predictions", "confidences": [{"label": "Behavior Predictions", "confidence": {"R": 0.4824, "F": 0.4729}}]}
|
flagged/Behavior Predictions/tmpctb7dywo.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"label": "Behavior Predictions", "confidences": [{"label": "Behavior Predictions", "confidence": {"F": 0.9, "H": 0.1451}}]}
|
flagged/Species Predictions/tmpdx0rhnon.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"label": "Species Predictions", "confidences": [{"label": "Species Predictions", "confidence": {"Canis aureus": 0.1907, "Herpestes edwardsi": 0.0618}}]}
|
flagged/Species Predictions/tmpxv6q0zdy.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"label": "Species Predictions", "confidences": [{"label": "Species Predictions", "confidence": {"Herpestes urva": 0.0978, "Sus scrofa": 0.0637}}]}
|
flagged/Upload Image/16d731eabe5e13037f5656d5786479e687986f96/tmpis3qgsoq.png
ADDED
![]() |
flagged/Upload Image/e0899f5af61b54022c2e1e48bb8c93c8b9c31732/tmpa52lof9c.png
ADDED
![]() |
flagged/log.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
Upload Image,Species Predictions,Behavior Predictions,flag,username,timestamp
|
2 |
+
C:\Users\mmdip\Dropbox\BioDiversity Project\image recogniser\mammals-of-india\flagged\Upload Image\e0899f5af61b54022c2e1e48bb8c93c8b9c31732\tmpa52lof9c.png,C:\Users\mmdip\Dropbox\BioDiversity Project\image recogniser\mammals-of-india\flagged\Species Predictions\tmpxv6q0zdy.json,C:\Users\mmdip\Dropbox\BioDiversity Project\image recogniser\mammals-of-india\flagged\Behavior Predictions\tmpctb7dywo.json,,,2023-09-19 16:24:13.959194
|
3 |
+
C:\Users\mmdip\Dropbox\BioDiversity Project\image recogniser\mammals-of-india\flagged\Upload Image\16d731eabe5e13037f5656d5786479e687986f96\tmpis3qgsoq.png,C:\Users\mmdip\Dropbox\BioDiversity Project\image recogniser\mammals-of-india\flagged\Species Predictions\tmpdx0rhnon.json,C:\Users\mmdip\Dropbox\BioDiversity Project\image recogniser\mammals-of-india\flagged\Behavior Predictions\tmp7ncvgp3m.json,,,2023-09-19 16:35:02.900288
|
test images/Antilope cervicapra_23.jpg
ADDED
![]() |
test images/Antilope cervicapra_33.jpg
ADDED
![]() |
test images/Gazella bennettii_01.jpg
ADDED
![]() |
test images/Loris lydekkerianus_11.jpg
ADDED
![]() |
test images/Pteropus giganteus_67.jpg
ADDED
![]() |
test images/Screenshot 2023-09-15 112403.png
ADDED
![]() |
test images/Screenshot 2023-09-15 141743.png
ADDED
![]() |
test images/Semnopithecus ajax_19.jpg
ADDED
![]() |
test images/Viverra zibetha_51.jpg
ADDED
![]() |
test.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastai.vision.all import load_learner
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
import pathlib
|
6 |
+
temp = pathlib.PosixPath
|
7 |
+
pathlib.PosixPath = pathlib.WindowsPath
|
8 |
+
|
9 |
+
# Function for recognizing species and behavior from an image
|
10 |
+
def recognize_image(input_image):
|
11 |
+
# Add your model loading code here if not already loaded
|
12 |
+
model = load_learner("final-v0.pkl")
|
13 |
+
|
14 |
+
# Make predictions
|
15 |
+
pred, idx, probabilities = model.predict(input_image)
|
16 |
+
|
17 |
+
# Get all labels from the model's vocabulary
|
18 |
+
all_labels = model.dls.vocab
|
19 |
+
|
20 |
+
# List of behavior labels
|
21 |
+
behavior_labels = ['F', 'H', 'M', 'M', 'P', 'R', 'T']
|
22 |
+
|
23 |
+
# Get the top-k predicted labels
|
24 |
+
top_k = 4
|
25 |
+
top_indices = (-probabilities).argsort()[:top_k]
|
26 |
+
top_labels = [all_labels[i] for i in top_indices]
|
27 |
+
top_probabilities = [float(probabilities[i]) for i in top_indices]
|
28 |
+
|
29 |
+
# Separate labels into behavior and species categories
|
30 |
+
behavior_predictions = [label for label in top_labels if label in behavior_labels]
|
31 |
+
species_predictions = [label for label in top_labels if label not in behavior_labels]
|
32 |
+
|
33 |
+
# Create dictionaries for species and behavior predictions with their probabilities
|
34 |
+
species_result = {
|
35 |
+
'Species Predictions': dict(zip(species_predictions, [round(prob, 4) for prob in top_probabilities if prob not in behavior_labels])),
|
36 |
+
}
|
37 |
+
|
38 |
+
behavior_result = {
|
39 |
+
'Behavior Predictions': dict(zip(behavior_predictions, [round(prob, 4) for prob in top_probabilities if prob in behavior_labels])),
|
40 |
+
}
|
41 |
+
|
42 |
+
return species_result, behavior_result
|
43 |
+
|
44 |
+
# Gradio interface
|
45 |
+
input_image = gr.inputs.Image(type='numpy', label='Upload Image')
|
46 |
+
output_species = gr.outputs.Label(label='Species Predictions')
|
47 |
+
output_behavior = gr.outputs.Label(label='Behavior Predictions')
|
48 |
+
|
49 |
+
gr.Interface(
|
50 |
+
fn=recognize_image,
|
51 |
+
inputs=input_image,
|
52 |
+
outputs=[output_species, output_behavior],
|
53 |
+
title='Species and Behavior Recognition',
|
54 |
+
description='Upload an image and get predictions for species and behavior.'
|
55 |
+
).launch()
|