Patrick Daniel commited on
Commit
4fc5a19
·
1 Parent(s): f728991

Fixed Transform

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +3 -15
  3. 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 at https://huggingface.co/docs/hub/spaces-config-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, ViTImageProcessor
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=5)
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": "Asterionellopsis_chain",
7
- "5": "Asteromphalus",
8
- "6": "Bad_Beads",
9
- "7": "Bad_blurred",
10
- "8": "Bad_mixed_phyto",
11
- "9": "Bad_setae",
12
- "10": "Centric",
13
- "11": "Centric_fuzzy",
14
- "12": "Ceratium_divaricatum",
15
- "13": "Ceratium_furca",
16
- "14": "Ceratium_lineatum",
17
- "15": "Chaetoceros",
18
- "16": "Ciliate_cutoff",
19
- "17": "Ciliate_large",
20
- "18": "Ciliate_large_2",
21
- "19": "Ciliate_other_morpho_1",
22
- "20": "Clusterflagellate_morpho_1",
23
- "21": "Clusterflagellate_morpho_2",
24
- "22": "Corethron",
25
- "23": "Cryptophyte",
26
- "24": "Cylindrotheca_Nitzschia",
27
- "25": "Detonula_Cerataulina_Lauderia",
28
- "26": "Detritus",
29
- "27": "Detritus_infection",
30
- "28": "Dictyocha",
31
- "29": "Dinoflagellate_morpho_1",
32
- "30": "Dinoflagellate_morpho_2",
33
- "31": "Dinoflagellate_morpho_3",
34
- "32": "Dinophysis",
35
- "33": "Ditylum",
36
- "34": "Entomoneis",
37
- "35": "Eucampia",
38
- "36": "Euglenoid",
39
- "37": "Flagellate_morpho_1",
40
- "38": "Flagellate_morpho_2",
41
- "39": "Flagellate_morpho_3",
42
- "40": "Flagellate_nano_1",
43
- "41": "Flagellate_nano_2",
44
- "42": "Fragilariopsis",
45
- "43": "Guinardia_Dactyliosolen",
46
- "44": "Gymnodinium",
47
- "45": "Gyrodinium",
48
- "46": "Gyrosigma",
49
- "47": "Haptophyte_prymnesium",
50
- "48": "Hemiaulus",
51
- "49": "Hemiselmis",
52
- "50": "Heterocapsa_morpho_1",
53
- "51": "Heterocapsa_morpho_2",
54
- "52": "Heterosigma_akashiwo",
55
- "53": "Laboea",
56
- "54": "Leptocylindrus",
57
- "55": "Lingulodinium",
58
- "56": "Margalefidinium",
59
- "57": "Mesodinium",
60
- "58": "Nano_cluster",
61
- "59": "Nano_p_white",
62
- "60": "Pennate_med",
63
- "61": "Pennate_morpho_1",
64
- "62": "Pennate_short",
65
- "63": "Pennate_wide",
66
- "64": "Peridinium",
67
- "65": "Phaeocystis",
68
- "66": "Pleurosigma",
69
- "67": "Polykrikos",
70
- "68": "Proboscia",
71
- "69": "Prorocentrum_narrow",
72
- "70": "Prorocentrum_wide",
73
- "71": "Pseudo-nitzschia",
74
- "72": "Pyramimonas",
75
- "73": "Rhizosolenia",
76
- "74": "Scrippsiella",
77
- "75": "Skeleonema",
78
- "76": "Skeletonema",
79
- "77": "Spiky_pacman_circular",
80
- "78": "Stombidinium_morpho_1",
81
- "79": "Strombidium_morpho_2",
82
- "80": "Thalassionema",
83
- "81": "Thalassiosira",
84
- "82": "Tiarina",
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
  }