Spaces:
Sleeping
Sleeping
Patrick Daniel
commited on
Commit
·
4fc5a19
1
Parent(s):
f728991
Fixed Transform
Browse files- README.md +1 -1
- app.py +3 -15
- label_names.json +79 -85
README.md
CHANGED
|
@@ -11,4 +11,4 @@ license: mit
|
|
| 11 |
short_description: Classify Imaging FlowCytobot images
|
| 12 |
---
|
| 13 |
|
| 14 |
-
Check out the configuration reference
|
|
|
|
| 11 |
short_description: Classify Imaging FlowCytobot images
|
| 12 |
---
|
| 13 |
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -1,9 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import ViTForImageClassification
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import requests
|
| 6 |
-
from io import BytesIO
|
| 7 |
import os
|
| 8 |
from safetensors.torch import load_file
|
| 9 |
from huggingface_hub import hf_hub_download
|
|
@@ -48,6 +45,7 @@ def predict(image):
|
|
| 48 |
try:
|
| 49 |
transform = transforms.Compose([
|
| 50 |
transforms.Resize((224, 224)), # match ViT input size
|
|
|
|
| 51 |
transforms.Normalize(mean=(0.485, 0.456, 0.406),
|
| 52 |
std=(0.229, 0.224, 0.225))
|
| 53 |
])
|
|
@@ -59,7 +57,7 @@ def predict(image):
|
|
| 59 |
probs = torch.nn.functional.softmax(logits, dim=1).squeeze()
|
| 60 |
|
| 61 |
|
| 62 |
-
topk = torch.topk(probs, k=
|
| 63 |
top_indices = topk.indices.tolist()
|
| 64 |
top_scores = topk.values.tolist()
|
| 65 |
|
|
@@ -72,15 +70,6 @@ def predict(image):
|
|
| 72 |
print(traceback.format_exc())
|
| 73 |
return {"Error": str(e)}
|
| 74 |
|
| 75 |
-
# Optional: allow input via URL
|
| 76 |
-
def classify_from_url(url):
|
| 77 |
-
try:
|
| 78 |
-
response = requests.get(url)
|
| 79 |
-
image = Image.open(BytesIO(response.content))
|
| 80 |
-
return predict(image)
|
| 81 |
-
except Exception as e:
|
| 82 |
-
return {"Error": f"Could not load image from URL. {e}"}
|
| 83 |
-
|
| 84 |
# Gradio UI
|
| 85 |
with gr.Blocks() as demo:
|
| 86 |
gr.Markdown("# PhytoViT - IFCB Phytoplankton Classifier")
|
|
@@ -94,6 +83,5 @@ with gr.Blocks() as demo:
|
|
| 94 |
label_output = gr.Label(label="Top 5 Predictions")
|
| 95 |
|
| 96 |
predict_btn.click(fn=predict, inputs=image_input, outputs=label_output)
|
| 97 |
-
url_input.change(fn=classify_from_url, inputs=url_input, outputs=label_output)
|
| 98 |
|
| 99 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import ViTForImageClassification
|
|
|
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
from safetensors.torch import load_file
|
| 6 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 45 |
try:
|
| 46 |
transform = transforms.Compose([
|
| 47 |
transforms.Resize((224, 224)), # match ViT input size
|
| 48 |
+
transforms.ToTensor(), # Converts PIL.Image to torch.Tensor
|
| 49 |
transforms.Normalize(mean=(0.485, 0.456, 0.406),
|
| 50 |
std=(0.229, 0.224, 0.225))
|
| 51 |
])
|
|
|
|
| 57 |
probs = torch.nn.functional.softmax(logits, dim=1).squeeze()
|
| 58 |
|
| 59 |
|
| 60 |
+
topk = torch.topk(probs, k=3)
|
| 61 |
top_indices = topk.indices.tolist()
|
| 62 |
top_scores = topk.values.tolist()
|
| 63 |
|
|
|
|
| 70 |
print(traceback.format_exc())
|
| 71 |
return {"Error": str(e)}
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
# Gradio UI
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
gr.Markdown("# PhytoViT - IFCB Phytoplankton Classifier")
|
|
|
|
| 83 |
label_output = gr.Label(label="Top 5 Predictions")
|
| 84 |
|
| 85 |
predict_btn.click(fn=predict, inputs=image_input, outputs=label_output)
|
|
|
|
| 86 |
|
| 87 |
demo.launch()
|
label_names.json
CHANGED
|
@@ -3,89 +3,83 @@
|
|
| 3 |
"1": "Alexandrium",
|
| 4 |
"2": "Amylax_Gonyaulax_Protoceratium",
|
| 5 |
"3": "Asterionellopsis",
|
| 6 |
-
"4": "
|
| 7 |
-
"5": "
|
| 8 |
-
"6": "
|
| 9 |
-
"7": "
|
| 10 |
-
"8": "
|
| 11 |
-
"9": "
|
| 12 |
-
"10": "
|
| 13 |
-
"11": "
|
| 14 |
-
"12": "
|
| 15 |
-
"13": "
|
| 16 |
-
"14": "
|
| 17 |
-
"15": "
|
| 18 |
-
"16": "
|
| 19 |
-
"17": "
|
| 20 |
-
"18": "
|
| 21 |
-
"19": "
|
| 22 |
-
"20": "
|
| 23 |
-
"21": "
|
| 24 |
-
"22": "
|
| 25 |
-
"23": "
|
| 26 |
-
"24": "
|
| 27 |
-
"25": "
|
| 28 |
-
"26": "
|
| 29 |
-
"27": "
|
| 30 |
-
"28": "
|
| 31 |
-
"29": "
|
| 32 |
-
"30": "
|
| 33 |
-
"31": "
|
| 34 |
-
"32": "
|
| 35 |
-
"33": "
|
| 36 |
-
"34": "
|
| 37 |
-
"35": "
|
| 38 |
-
"36": "
|
| 39 |
-
"37": "
|
| 40 |
-
"38": "
|
| 41 |
-
"39": "
|
| 42 |
-
"40": "
|
| 43 |
-
"41": "
|
| 44 |
-
"42": "
|
| 45 |
-
"43": "
|
| 46 |
-
"44": "
|
| 47 |
-
"45": "
|
| 48 |
-
"46": "
|
| 49 |
-
"47": "
|
| 50 |
-
"48": "
|
| 51 |
-
"49": "
|
| 52 |
-
"50": "
|
| 53 |
-
"51": "
|
| 54 |
-
"52": "
|
| 55 |
-
"53": "
|
| 56 |
-
"54": "
|
| 57 |
-
"55": "
|
| 58 |
-
"56": "
|
| 59 |
-
"57": "
|
| 60 |
-
"58": "
|
| 61 |
-
"59": "
|
| 62 |
-
"60": "
|
| 63 |
-
"61": "
|
| 64 |
-
"62": "
|
| 65 |
-
"63": "
|
| 66 |
-
"64": "
|
| 67 |
-
"65": "
|
| 68 |
-
"66": "
|
| 69 |
-
"67": "
|
| 70 |
-
"68": "
|
| 71 |
-
"69": "
|
| 72 |
-
"70": "
|
| 73 |
-
"71": "
|
| 74 |
-
"72": "
|
| 75 |
-
"73": "
|
| 76 |
-
"74": "
|
| 77 |
-
"75": "
|
| 78 |
-
"76": "
|
| 79 |
-
"77": "
|
| 80 |
-
"78": "
|
| 81 |
-
"79": "
|
| 82 |
-
"80": "
|
| 83 |
-
"81": "
|
| 84 |
-
"82": "
|
| 85 |
-
"83": "Tintinnid",
|
| 86 |
-
"84": "Tontonia",
|
| 87 |
-
"85": "Torodinium",
|
| 88 |
-
"86": "Tropidoneis",
|
| 89 |
-
"87": "Unknown_morpho_1",
|
| 90 |
-
"88": "Vicicitus"
|
| 91 |
}
|
|
|
|
| 3 |
"1": "Alexandrium",
|
| 4 |
"2": "Amylax_Gonyaulax_Protoceratium",
|
| 5 |
"3": "Asterionellopsis",
|
| 6 |
+
"4": "Asteromphalus",
|
| 7 |
+
"5": "Bad_setae",
|
| 8 |
+
"6": "Centric",
|
| 9 |
+
"7": "Ceratium_divaricatum",
|
| 10 |
+
"8": "Ceratium_furca",
|
| 11 |
+
"9": "Ceratium_lineatum",
|
| 12 |
+
"10": "Chaetoceros",
|
| 13 |
+
"11": "Ciliate_large",
|
| 14 |
+
"12": "Ciliate_large_2",
|
| 15 |
+
"13": "Ciliate_other_morpho_1",
|
| 16 |
+
"14": "Clusterflagellate_morpho_1",
|
| 17 |
+
"15": "Clusterflagellate_morpho_2",
|
| 18 |
+
"16": "Corethron",
|
| 19 |
+
"17": "Cryptophyte",
|
| 20 |
+
"18": "Cylindrotheca",
|
| 21 |
+
"19": "Detonula_Cerataulina_Lauderia",
|
| 22 |
+
"20": "Detritus",
|
| 23 |
+
"21": "Detritus_infection",
|
| 24 |
+
"22": "Dictyocha",
|
| 25 |
+
"23": "Dinoflagellate_cyst",
|
| 26 |
+
"24": "Dinoflagellate_morpho_1",
|
| 27 |
+
"25": "Dinoflagellate_morpho_2",
|
| 28 |
+
"26": "Dinophysis",
|
| 29 |
+
"27": "Ditylum",
|
| 30 |
+
"28": "Entomoneis",
|
| 31 |
+
"29": "Eucampia",
|
| 32 |
+
"30": "Euglenoid",
|
| 33 |
+
"31": "Flagellate_morpho_1",
|
| 34 |
+
"32": "Flagellate_morpho_2",
|
| 35 |
+
"33": "Flagellate_morpho_3",
|
| 36 |
+
"34": "Flagellate_nano_1",
|
| 37 |
+
"35": "Flagellate_nano_2",
|
| 38 |
+
"36": "Fragilariopsis",
|
| 39 |
+
"37": "Guinardia_Dactyliosolen",
|
| 40 |
+
"38": "Gymnodinium",
|
| 41 |
+
"39": "Gyrodinium",
|
| 42 |
+
"40": "Gyrosigma",
|
| 43 |
+
"41": "Haptophyte_prymnesium",
|
| 44 |
+
"42": "Hemiaulus",
|
| 45 |
+
"43": "Hemiselmis",
|
| 46 |
+
"44": "Heterocapsa_long",
|
| 47 |
+
"45": "Heterocapsa_rotundata",
|
| 48 |
+
"46": "Heterocapsa_triquetra",
|
| 49 |
+
"47": "Heterosigma_akashiwo",
|
| 50 |
+
"48": "Laboea",
|
| 51 |
+
"49": "Leptocylindrus",
|
| 52 |
+
"50": "Margalefidinium",
|
| 53 |
+
"51": "Mesodinium",
|
| 54 |
+
"52": "Nano_cluster",
|
| 55 |
+
"53": "Nano_p_white",
|
| 56 |
+
"54": "Noctiluca",
|
| 57 |
+
"55": "Odontella",
|
| 58 |
+
"56": "Pennate",
|
| 59 |
+
"57": "Pennate_Tropidoneis",
|
| 60 |
+
"58": "Pennate_Unknown",
|
| 61 |
+
"59": "Pennate_small",
|
| 62 |
+
"60": "Peridinium",
|
| 63 |
+
"61": "Phaeocystis",
|
| 64 |
+
"62": "Pleurosigma",
|
| 65 |
+
"63": "Polykrikos",
|
| 66 |
+
"64": "Proboscia",
|
| 67 |
+
"65": "Prorocentrum_narrow",
|
| 68 |
+
"66": "Prorocentrum_wide",
|
| 69 |
+
"67": "Pseudo-nitzschia",
|
| 70 |
+
"68": "Pyramimonas",
|
| 71 |
+
"69": "Rhizosolenia",
|
| 72 |
+
"70": "Scrippsiella",
|
| 73 |
+
"71": "Skeletonema",
|
| 74 |
+
"72": "Spiky_pacman",
|
| 75 |
+
"73": "Stombidinium_morpho_1",
|
| 76 |
+
"74": "Strombidinum_morpho_2",
|
| 77 |
+
"75": "Thalassionema",
|
| 78 |
+
"76": "Thalassiosira",
|
| 79 |
+
"77": "Tiarina",
|
| 80 |
+
"78": "Tontonia",
|
| 81 |
+
"79": "Torodinium",
|
| 82 |
+
"80": "Tropidoneis",
|
| 83 |
+
"81": "Vicicitus",
|
| 84 |
+
"82": "haptophyte_ucynA_host"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
}
|