Delete sunfish_classifier.py
Browse files- sunfish_classifier.py +0 -62
sunfish_classifier.py
DELETED
|
@@ -1,62 +0,0 @@
|
|
| 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")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|