Upload 3 files
Browse files- app.py +22 -0
- requirements.txt +7 -0
- sunfish_classifier.py +62 -0
app.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Gradio app for HuggingFace Spaces deployment
|
| 2 |
+
from fastai.vision.all import *
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import pathlib
|
| 5 |
+
|
| 6 |
+
# Load model (ensure sunfish_model.pkl is in the same directory as this file)
|
| 7 |
+
learn = load_learner('sunfish_model.pkl')
|
| 8 |
+
|
| 9 |
+
def predict(img):
|
| 10 |
+
pred_class, pred_idx, outputs = learn.predict(img)
|
| 11 |
+
return {learn.dls.vocab[i]: float(outputs[i]) for i in range(len(outputs))}
|
| 12 |
+
|
| 13 |
+
# Create Gradio interface
|
| 14 |
+
demo = gr.Interface(
|
| 15 |
+
fn=predict,
|
| 16 |
+
inputs=gr.Image(type="pil"),
|
| 17 |
+
outputs=gr.Label(num_top_classes=3),
|
| 18 |
+
title="Sunfish Classifier",
|
| 19 |
+
description="Classify Lepomis (sunfish) species using fastai and ResNet18"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastai==2.7.14
|
| 2 |
+
torch==2.2.2
|
| 3 |
+
torchvision==0.17.2
|
| 4 |
+
numpy==1.26.4
|
| 5 |
+
gradio
|
| 6 |
+
datasets
|
| 7 |
+
pandas
|
sunfish_classifier.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Sunfish Classifier Training Code (tweaked from cats vs dogs original)
|
| 2 |
+
# For Google Colab: run the !pip lines directly; for local Python, install dependencies via terminal first
|
| 3 |
+
|
| 4 |
+
# Install dependencies (Colab/Jupyter only - remove ! for local Python)
|
| 5 |
+
!pip install -Uqq fastai datasets pandas gradio
|
| 6 |
+
|
| 7 |
+
from fastai.vision.all import *
|
| 8 |
+
from datasets import load_dataset
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
import os
|
| 12 |
+
|
| 13 |
+
# 1. Load Fish-Vista dataset (contains Lepomis/sunfish species)
|
| 14 |
+
print("Loading Fish-Vista dataset...")
|
| 15 |
+
ds = load_dataset("imageomics/fish-vista", name="species_classification", split="train")
|
| 16 |
+
ds = ds.with_format("image")
|
| 17 |
+
|
| 18 |
+
# 2. Filter for sunfish (Lepomis genus)
|
| 19 |
+
print("Filtering for Lepomis/sunfish species...")
|
| 20 |
+
lepomis_labels = [l for l in ds.features['label'].names if 'Lepomis' in l]
|
| 21 |
+
print(f"Found Lepomis labels: {lepomis_labels}")
|
| 22 |
+
lepomis_indices = [i for i, l in enumerate(ds['label']) if ds.features['label'].int2str(l) in lepomis_labels]
|
| 23 |
+
sunfish_ds = ds.select(lepomis_indices)
|
| 24 |
+
print(f"Total sunfish images: {len(sunfish_ds)}")
|
| 25 |
+
|
| 26 |
+
# 3. Save to local folder for fastai
|
| 27 |
+
path = Path('/tmp/sunfish_data')
|
| 28 |
+
if path.exists():
|
| 29 |
+
import shutil
|
| 30 |
+
shutil.rmtree(path)
|
| 31 |
+
path.mkdir(parents=True, exist_ok=True)
|
| 32 |
+
print(f"Saving images to {path}...")
|
| 33 |
+
|
| 34 |
+
labels = [sunfish_ds.features['label'].int2str(l) for l in sunfish_ds['label']]
|
| 35 |
+
image_paths = []
|
| 36 |
+
for i, (img, label) in enumerate(zip(sunfish_ds['image'], labels)):
|
| 37 |
+
# Clean label for folder name (replace spaces/special chars)
|
| 38 |
+
clean_label = label.replace(" ", "_").replace("/", "_")
|
| 39 |
+
class_dir = path / clean_label
|
| 40 |
+
class_dir.mkdir(exist_ok=True)
|
| 41 |
+
img_path = class_dir / f"{i}.jpg"
|
| 42 |
+
img.save(img_path)
|
| 43 |
+
image_paths.append((str(img_path), label))
|
| 44 |
+
|
| 45 |
+
# 4. Create fastai DataLoaders
|
| 46 |
+
print("Creating DataLoaders...")
|
| 47 |
+
df = pd.DataFrame(image_paths, columns=['path', 'label'])
|
| 48 |
+
dls = ImageDataLoaders.from_df(
|
| 49 |
+
df, path='/', valid_pct=0.2, seed=42,
|
| 50 |
+
fn_col='path', label_col='label', item_tfms=Resize(224)
|
| 51 |
+
)
|
| 52 |
+
print("Showing batch...")
|
| 53 |
+
dls.show_batch(max_n=9)
|
| 54 |
+
|
| 55 |
+
# 5. Train model
|
| 56 |
+
print("Training model...")
|
| 57 |
+
learn = vision_learner(dls, resnet18, metrics=error_rate)
|
| 58 |
+
learn.fine_tune(3)
|
| 59 |
+
|
| 60 |
+
# 6. Export model
|
| 61 |
+
learn.export('sunfish_model.pkl')
|
| 62 |
+
print("Model exported to sunfish_model.pkl")
|