Commit
·
b404ed5
1
Parent(s):
03a9dc8
Initial upload of Gradio app and baseline model
Browse files- app.py +116 -0
- baseline.py +9 -0
- models/herbarium_convnext_v2_base.pth +3 -0
- requirements.txt +4 -0
- species_list.txt +100 -0
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
+
import pandas as pd
|
| 7 |
+
|
| 8 |
+
# --- Model Loading ---
|
| 9 |
+
def get_model_paths():
|
| 10 |
+
"""Returns a dictionary of model names to their file paths."""
|
| 11 |
+
model_dir = "models"
|
| 12 |
+
model_files = [f for f in os.listdir(model_dir) if f.endswith(".pth")]
|
| 13 |
+
# You can create more descriptive names here if you want
|
| 14 |
+
model_names = [os.path.splitext(f)[0] for f in model_files]
|
| 15 |
+
return dict(zip(model_names, [os.path.join(model_dir, f) for f in model_files]))
|
| 16 |
+
|
| 17 |
+
MODEL_PATHS = get_model_paths()
|
| 18 |
+
# Add placeholder paths for the other two models
|
| 19 |
+
MODEL_PATHS["Future Model 1"] = "models/future_model_1.pth"
|
| 20 |
+
MODEL_PATHS["Future Model 2"] = "models/future_model_2.pth"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# This is a placeholder for your actual model loading logic
|
| 24 |
+
# You will need to replace this with the code to load your specific model architecture
|
| 25 |
+
def load_model(model_path):
|
| 26 |
+
"""Loads a model from the given path."""
|
| 27 |
+
# Example:
|
| 28 |
+
# model = torch.load(model_path)
|
| 29 |
+
# model.eval()
|
| 30 |
+
# return model
|
| 31 |
+
# For now, returning a dummy object
|
| 32 |
+
print(f"Loading model from: {model_path}")
|
| 33 |
+
if not os.path.exists(model_path):
|
| 34 |
+
print("Warning: Model file does not exist. Using a dummy model.")
|
| 35 |
+
return None
|
| 36 |
+
# Replace with your actual model loading
|
| 37 |
+
try:
|
| 38 |
+
# This is a guess, you'll need to replace with your actual model class
|
| 39 |
+
from baseline import convnext_v2_base
|
| 40 |
+
model = convnext_v2_base(num_classes=10) # Or whatever your number of classes is
|
| 41 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
| 42 |
+
model.eval()
|
| 43 |
+
return model
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"Error loading model: {e}")
|
| 46 |
+
print("Using a dummy model.")
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# --- Image Preprocessing ---
|
| 51 |
+
# You'll need to adjust this to match the preprocessing your model expects
|
| 52 |
+
preprocess = transforms.Compose([
|
| 53 |
+
transforms.Resize(256),
|
| 54 |
+
transforms.CenterCrop(224),
|
| 55 |
+
transforms.ToTensor(),
|
| 56 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 57 |
+
])
|
| 58 |
+
|
| 59 |
+
# --- Prediction ---
|
| 60 |
+
# Load species list
|
| 61 |
+
species_df = pd.read_csv('species_list.txt', sep=';', header=None, names=['class_id', 'species_name'])
|
| 62 |
+
idx_to_class = {i: row['class_id'] for i, row in species_df.iterrows()}
|
| 63 |
+
class_id_to_name = {row['class_id']: row['species_name'] for i, row in species_df.iterrows()}
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def predict(model_name, image):
|
| 67 |
+
"""Makes a prediction on an image using the selected model."""
|
| 68 |
+
model_path = MODEL_PATHS[model_name]
|
| 69 |
+
|
| 70 |
+
if not os.path.exists(model_path):
|
| 71 |
+
return f"Model '{model_name}' not found. Please upload the model file."
|
| 72 |
+
|
| 73 |
+
model = load_model(model_path)
|
| 74 |
+
if model is None:
|
| 75 |
+
return f"Could not load model '{model_name}'."
|
| 76 |
+
|
| 77 |
+
pil_image = Image.fromarray(image.astype('uint8'), 'RGB')
|
| 78 |
+
processed_image = preprocess(pil_image).unsqueeze(0)
|
| 79 |
+
|
| 80 |
+
with torch.no_grad():
|
| 81 |
+
outputs = model(processed_image).logits
|
| 82 |
+
_, predicted_idx = torch.max(outputs, 1)
|
| 83 |
+
class_id = idx_to_class[predicted_idx.item()]
|
| 84 |
+
class_name = class_id_to_name[class_id]
|
| 85 |
+
return f"Prediction: {class_name}"
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# --- Gradio Interface ---
|
| 89 |
+
with gr.Blocks() as demo:
|
| 90 |
+
gr.Markdown("# Plant Classification")
|
| 91 |
+
gr.Markdown("Select a model and upload an image to classify.")
|
| 92 |
+
|
| 93 |
+
with gr.Row():
|
| 94 |
+
model_dropdown = gr.Dropdown(
|
| 95 |
+
choices=list(MODEL_PATHS.keys()),
|
| 96 |
+
label="Select Model",
|
| 97 |
+
value=list(MODEL_PATHS.keys())[0] if MODEL_PATHS else None
|
| 98 |
+
)
|
| 99 |
+
image_input = gr.Image(type="numpy")
|
| 100 |
+
|
| 101 |
+
output_text = gr.Textbox(label="Prediction")
|
| 102 |
+
|
| 103 |
+
image_input.change(
|
| 104 |
+
fn=predict,
|
| 105 |
+
inputs=[model_dropdown, image_input],
|
| 106 |
+
outputs=output_text
|
| 107 |
+
)
|
| 108 |
+
model_dropdown.change(
|
| 109 |
+
fn=predict,
|
| 110 |
+
inputs=[model_dropdown, image_input],
|
| 111 |
+
outputs=output_text
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
demo.launch()
|
baseline.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import ConvNextV2ForImageClassification
|
| 2 |
+
|
| 3 |
+
def convnext_v2_base(num_classes):
|
| 4 |
+
model = ConvNextV2ForImageClassification.from_pretrained(
|
| 5 |
+
"facebook/convnextv2-base-22k-224",
|
| 6 |
+
num_labels=num_classes,
|
| 7 |
+
ignore_mismatched_sizes=True
|
| 8 |
+
)
|
| 9 |
+
return model
|
models/herbarium_convnext_v2_base.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:837cca126e235c0ae822770470e38a3621b81b0ba7e915aaef2b15a7f66914e6
|
| 3 |
+
size 351335085
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
gradio
|
| 4 |
+
Pillow
|
species_list.txt
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
105951; Maripa glabra Choisy
|
| 2 |
+
106023; Merremia umbellata (L.) Hallier f.
|
| 3 |
+
106387; Costus arabicus L.
|
| 4 |
+
106461; Costus scaber Ruiz Pav.
|
| 5 |
+
106466; Costus spiralis (Jacq.) Roscoe
|
| 6 |
+
110432; Evodianthus funifer (Poit.) Lindm.
|
| 7 |
+
116853; Pteridium arachnoideum (Kaulf.) Maxon
|
| 8 |
+
119986; Olfersia cervina (L.) Kunze
|
| 9 |
+
120497; Diospyros capreifolia Mart. ex Hiern
|
| 10 |
+
121836; Sloanea grandiflora Sm.
|
| 11 |
+
121841; Sloanea guianensis (Aubl.) Benth.
|
| 12 |
+
12254; Anacardium occidentale L.
|
| 13 |
+
12518; Mangifera indica L.
|
| 14 |
+
125412; Sphyrospermum cordifolium Benth.
|
| 15 |
+
126895; Syngonanthus caulescens (Poir.) Ruhland
|
| 16 |
+
127007; Tonina fluviatilis Aubl.
|
| 17 |
+
127097; Erythroxylum fimbriatum Peyr.
|
| 18 |
+
127151; Erythroxylum macrophyllum Cav.
|
| 19 |
+
127242; Erythroxylum squamatum Sw.
|
| 20 |
+
12910; Spondias mombin L.
|
| 21 |
+
12922; Tapirira guianensis Aubl.
|
| 22 |
+
129645; Croton schiedeanus Schltdl.
|
| 23 |
+
130657; Euphorbia cotinifolia L.
|
| 24 |
+
131079; Euphorbia heterophylla L.
|
| 25 |
+
131736; Euphorbia prostrata Aiton
|
| 26 |
+
132107; Euphorbia thymifolia L.
|
| 27 |
+
132113; Euphorbia tithymaloides L.
|
| 28 |
+
132431; Hura crepitans L.
|
| 29 |
+
132476; Jatropha curcas L.
|
| 30 |
+
132501; Jatropha gossypiifolia L.
|
| 31 |
+
13276; Annona ambotay Aubl.
|
| 32 |
+
13325; Annona foetida Mart.
|
| 33 |
+
13330; Annona glabra L.
|
| 34 |
+
133595; Ricinus communis L.
|
| 35 |
+
133617; Sapium glandulosum (L.) Morong
|
| 36 |
+
13370; Annona muricata L.
|
| 37 |
+
136761; Potalia amara Aubl.
|
| 38 |
+
138662; Chrysothemis pulchella (Donn ex Sims) Decne.
|
| 39 |
+
140367; Lembocarpus amoenus Leeuwenb.
|
| 40 |
+
141068; Sinningia incarnata (Aubl.) D.L.Denham
|
| 41 |
+
141332; Dicranopteris flexuosa (Schrad.) Underw.
|
| 42 |
+
141336; Dicranopteris pectinata (Willd.) Underw.
|
| 43 |
+
142550; Heliconia chartacea Lane ex Barreiros
|
| 44 |
+
142736; Hernandia guianensis Aubl.
|
| 45 |
+
143496; Hymenophyllum hirsutum (L.) Sw.
|
| 46 |
+
14353; Guatteria ouregou (Aubl.) Dunal
|
| 47 |
+
143706; Trichomanes diversifrons (Bory) Mett. ex Sadeb.
|
| 48 |
+
143758; Trichomanes punctatum Poir.
|
| 49 |
+
14401; Guatteria scandens Ducke
|
| 50 |
+
144394; Didymochlaena truncatula (Sw.) J. Sm.
|
| 51 |
+
145020; Cipura paludosa Aubl.
|
| 52 |
+
148220; Aegiphila macrantha Ducke
|
| 53 |
+
148977; Clerodendrum paniculatum L.
|
| 54 |
+
149264; Congea tomentosa Roxb.
|
| 55 |
+
149682; Gmelina philippensis Cham.
|
| 56 |
+
149919; Holmskioldia sanguinea Retz.
|
| 57 |
+
150135; Hyptis lanceolata Poir.
|
| 58 |
+
15014; Rollinia mucosa (Jacq.) Baill.
|
| 59 |
+
151469; Ocimum campechianum Mill.
|
| 60 |
+
151593; Orthosiphon aristatus (Blume) Miq.
|
| 61 |
+
15318; Xylopia aromatica (Lam.) Mart.
|
| 62 |
+
15330; Xylopia cayennensis Maas
|
| 63 |
+
15355; Xylopia frutescens Aubl.
|
| 64 |
+
156516; Aniba guianensis Aubl.
|
| 65 |
+
156526; Aniba megaphylla Mez
|
| 66 |
+
158341; Nectandra cissiflora Nees
|
| 67 |
+
158592; Ocotea cernua (Nees) Mez
|
| 68 |
+
158653; Ocotea floribunda (Sw.) Mez
|
| 69 |
+
158736; Ocotea longifolia Kunth
|
| 70 |
+
158793; Ocotea oblonga (Meisn.) Mez
|
| 71 |
+
158833; Ocotea puberula (Rich.) Nees
|
| 72 |
+
159434; Couratari guianensis Aubl.
|
| 73 |
+
159516; Eschweilera parviflora (Aubl.) Miers
|
| 74 |
+
159518; Eschweilera pedicellata (Rich.) S.A.Mori
|
| 75 |
+
160570; Acacia mangium Willd.
|
| 76 |
+
166822; Caesalpinia pulcherrima (L.) Sw.
|
| 77 |
+
166869; Cajanus cajan (L.) Millsp.
|
| 78 |
+
169293; Crotalaria retusa L.
|
| 79 |
+
171727; Erythrina fusca Lour.
|
| 80 |
+
173914; Inga alba (Sw.) Willd.
|
| 81 |
+
173972; Inga capitata Desv.
|
| 82 |
+
174017; Inga edulis Mart.
|
| 83 |
+
177730; Mimosa pigra L.
|
| 84 |
+
177775; Mimosa pudica L.
|
| 85 |
+
189669; Punica granatum L.
|
| 86 |
+
191642; Adansonia digitata L.
|
| 87 |
+
19165; Allamanda cathartica L.
|
| 88 |
+
192311; Ceiba pentandra (L.) Gaertn.
|
| 89 |
+
194035; Hibiscus rosa-sinensis L.
|
| 90 |
+
19489; Asclepias curassavica L.
|
| 91 |
+
209328; Psidium guineense Sw.
|
| 92 |
+
211059; Nephrolepis biserrata (Sw.) Schott
|
| 93 |
+
244705; Averrhoa carambola L.
|
| 94 |
+
248392; Turnera ulmifolia L.
|
| 95 |
+
254180; Piper peltatum L.
|
| 96 |
+
275029; Eichhornia crassipes (Mart.) Solms
|
| 97 |
+
280085; Ceratopteris thalictroides (L.) Brongn.
|
| 98 |
+
280698; Pityrogramma calomelanos (L.) Link
|
| 99 |
+
285398; Cassipourea guianensis Aubl.
|
| 100 |
+
29686; Oreopanax capitatus (Jacq.) Decne. Planch.
|