Image Classification
LiteRT
LiteRT
ONNX
English
vision
botany
western-australia
dinov3
mixture-of-experts
adaround
fp8
int8
android
biodiversity
flora
Instructions to use thenukegun10x/PLantDetect-WA with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use thenukegun10x/PLantDetect-WA with LiteRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
Remove src/src/models/plant_vit_v2.py
Browse files- src/src/models/plant_vit_v2.py +0 -169
src/src/models/plant_vit_v2.py
DELETED
|
@@ -1,169 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
PlantViT v2 Architecture (PlantDetect v2.0):
|
| 3 |
-
1. DINOv3 ViT-Base Backbone @ 448x448 High-Res (784 patch tokens).
|
| 4 |
-
2. Multi-Layer Feature Pyramid Fusion (Block 8 Micro-Texture + Block 11 Macro-Semantics).
|
| 5 |
-
3. Pure 16 SwiGLU Mixture-of-Experts (MoESwiGLUv2 with Gating Residuals & Context Projection).
|
| 6 |
-
4. Sub-Center ArcFace Metric Head (K=3 Phenology Centers per Class).
|
| 7 |
-
5. Hierarchical Multi-Task Taxonomic Classification Heads (Species + Genus + Family).
|
| 8 |
-
"""
|
| 9 |
-
import torch
|
| 10 |
-
import torch.nn as nn
|
| 11 |
-
import torch.nn.functional as F
|
| 12 |
-
import timm
|
| 13 |
-
|
| 14 |
-
from .moe_swiglu_v2 import MoESwiGLUv2
|
| 15 |
-
from .subcenter_arcface import SubCenterArcFace
|
| 16 |
-
|
| 17 |
-
class MultiLayerFeatureFusion(nn.Module):
|
| 18 |
-
def __init__(self, dim: int = 768, out_dim: int = 512):
|
| 19 |
-
super().__init__()
|
| 20 |
-
self.dim = dim
|
| 21 |
-
self.out_dim = out_dim
|
| 22 |
-
|
| 23 |
-
self.norm_mid = nn.LayerNorm(dim)
|
| 24 |
-
self.norm_deep = nn.LayerNorm(dim)
|
| 25 |
-
|
| 26 |
-
self.fusion_proj = nn.Sequential(
|
| 27 |
-
nn.Linear(dim * 2, out_dim),
|
| 28 |
-
nn.GELU(),
|
| 29 |
-
nn.LayerNorm(out_dim),
|
| 30 |
-
nn.Linear(out_dim, out_dim),
|
| 31 |
-
nn.LayerNorm(out_dim)
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
def forward(self, mid_tokens: torch.Tensor, deep_tokens: torch.Tensor) -> torch.Tensor:
|
| 35 |
-
# Pool spatial tokens over sequence dimension
|
| 36 |
-
mid_pooled = self.norm_mid(mid_tokens.mean(dim=1))
|
| 37 |
-
deep_pooled = self.norm_deep(deep_tokens.mean(dim=1))
|
| 38 |
-
|
| 39 |
-
fused = torch.cat([mid_pooled, deep_pooled], dim=-1)
|
| 40 |
-
emb = self.fusion_proj(fused)
|
| 41 |
-
return F.normalize(emb, dim=-1)
|
| 42 |
-
|
| 43 |
-
class PlantViTv2(nn.Module):
|
| 44 |
-
def __init__(
|
| 45 |
-
self,
|
| 46 |
-
stem_name: str = "vit_base_patch16_dinov3.lvd1689m",
|
| 47 |
-
img_size: int = 448,
|
| 48 |
-
n_species: int = 1000,
|
| 49 |
-
n_genera: int = 358,
|
| 50 |
-
n_families: int = 80,
|
| 51 |
-
embed_dim: int = 512,
|
| 52 |
-
use_moe: bool = True,
|
| 53 |
-
num_ffn: int = 16,
|
| 54 |
-
d_ff: int = 512,
|
| 55 |
-
top_k: int = 2,
|
| 56 |
-
k_subcenters: int = 3,
|
| 57 |
-
pretrained: bool = True,
|
| 58 |
-
freeze_stem: bool = False
|
| 59 |
-
):
|
| 60 |
-
super().__init__()
|
| 61 |
-
self.stem_name = stem_name
|
| 62 |
-
self.img_size = img_size
|
| 63 |
-
self.n_species = n_species
|
| 64 |
-
self.n_genera = n_genera
|
| 65 |
-
self.n_families = n_families
|
| 66 |
-
self.embed_dim = embed_dim
|
| 67 |
-
self.use_moe = use_moe
|
| 68 |
-
|
| 69 |
-
# 1. DINOv3 Backbone
|
| 70 |
-
stem_model_name = stem_name if stem_name.startswith("vit_") else f"{stem_name}.lvd1689m"
|
| 71 |
-
self.stem = timm.create_model(
|
| 72 |
-
stem_model_name,
|
| 73 |
-
pretrained=pretrained,
|
| 74 |
-
img_size=img_size,
|
| 75 |
-
num_classes=0,
|
| 76 |
-
dynamic_img_size=True
|
| 77 |
-
)
|
| 78 |
-
self.dim = self.stem.embed_dim
|
| 79 |
-
|
| 80 |
-
if freeze_stem:
|
| 81 |
-
for p in self.stem.parameters():
|
| 82 |
-
p.requires_grad = False
|
| 83 |
-
|
| 84 |
-
# 2. Pure 16-SwiGLU MoE Layer
|
| 85 |
-
if self.use_moe:
|
| 86 |
-
self.moe_layer = MoESwiGLUv2(
|
| 87 |
-
d_model=self.dim,
|
| 88 |
-
d_ff=d_ff,
|
| 89 |
-
num_ffn=num_ffn,
|
| 90 |
-
top_k=top_k,
|
| 91 |
-
gating_residual=True,
|
| 92 |
-
gate_ctx=True
|
| 93 |
-
)
|
| 94 |
-
self.moe_norm = nn.LayerNorm(self.dim)
|
| 95 |
-
else:
|
| 96 |
-
self.moe_layer = None
|
| 97 |
-
|
| 98 |
-
# 3. Multi-Layer Feature Fusion (Block 8 + Block 11)
|
| 99 |
-
self.fusion = MultiLayerFeatureFusion(dim=self.dim, out_dim=embed_dim)
|
| 100 |
-
|
| 101 |
-
# 4. Multi-Task Taxonomic Heads
|
| 102 |
-
self.head_species = nn.Linear(embed_dim, n_species, bias=False)
|
| 103 |
-
self.head_genus = nn.Linear(embed_dim, n_genera, bias=False)
|
| 104 |
-
self.head_family = nn.Linear(embed_dim, n_families, bias=False)
|
| 105 |
-
|
| 106 |
-
# 5. Sub-Center ArcFace Metric Head (K=3 Sub-Centers)
|
| 107 |
-
self.subcenter_arcface = SubCenterArcFace(
|
| 108 |
-
embed_dim=embed_dim,
|
| 109 |
-
n_classes=n_species,
|
| 110 |
-
k_subcenters=k_subcenters,
|
| 111 |
-
m=0.5,
|
| 112 |
-
s=30.0
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
def _to_tokens(self, feat: torch.Tensor) -> torch.Tensor:
|
| 116 |
-
"""Converts [B, C, H, W] or [B, T, C] to standard [B, T, C]."""
|
| 117 |
-
if feat.dim() == 4:
|
| 118 |
-
return feat.flatten(2).transpose(1, 2)
|
| 119 |
-
return feat
|
| 120 |
-
|
| 121 |
-
def forward_features(self, x: torch.Tensor):
|
| 122 |
-
intermediates, final_feat = self.stem.forward_intermediates(x, indices=[7, 11])
|
| 123 |
-
|
| 124 |
-
if isinstance(intermediates, (list, tuple)):
|
| 125 |
-
mid_raw = intermediates[0]
|
| 126 |
-
deep_raw = intermediates[1] if len(intermediates) > 1 else final_feat
|
| 127 |
-
else:
|
| 128 |
-
mid_raw = intermediates
|
| 129 |
-
deep_raw = final_feat
|
| 130 |
-
|
| 131 |
-
if isinstance(mid_raw, (list, tuple)): mid_raw = mid_raw[0]
|
| 132 |
-
if isinstance(deep_raw, (list, tuple)): deep_raw = deep_raw[0]
|
| 133 |
-
|
| 134 |
-
mid_tokens = self._to_tokens(mid_raw)
|
| 135 |
-
deep_tokens = self._to_tokens(deep_raw)
|
| 136 |
-
|
| 137 |
-
aux_loss = torch.tensor(0.0, device=x.device)
|
| 138 |
-
gate_logits = None
|
| 139 |
-
if self.use_moe and self.moe_layer is not None:
|
| 140 |
-
ctx = deep_tokens[:, 0]
|
| 141 |
-
moe_out, aux_loss, gate_logits = self.moe_layer(self.moe_norm(deep_tokens), ctx=ctx)
|
| 142 |
-
deep_tokens = deep_tokens + moe_out
|
| 143 |
-
|
| 144 |
-
return mid_tokens, deep_tokens, aux_loss, gate_logits
|
| 145 |
-
|
| 146 |
-
def forward(self, x: torch.Tensor, y_species: torch.Tensor = None):
|
| 147 |
-
mid_tokens, deep_tokens, moe_aux, _ = self.forward_features(x)
|
| 148 |
-
|
| 149 |
-
# 1. Multi-Layer Pyramid Fusion (Block 8 Texture + Block 11 Macro)
|
| 150 |
-
emb = self.fusion(mid_tokens, deep_tokens)
|
| 151 |
-
|
| 152 |
-
# 2. Multi-Task Linear Logits
|
| 153 |
-
logits_species = self.head_species(emb)
|
| 154 |
-
logits_genus = self.head_genus(emb)
|
| 155 |
-
logits_family = self.head_family(emb)
|
| 156 |
-
|
| 157 |
-
# 3. Sub-Center ArcFace Margin Logits
|
| 158 |
-
arcface_logits = self.subcenter_arcface(emb, y_species)
|
| 159 |
-
|
| 160 |
-
aux_dict = {"moe_aux": moe_aux}
|
| 161 |
-
|
| 162 |
-
return (
|
| 163 |
-
logits_species,
|
| 164 |
-
logits_genus,
|
| 165 |
-
logits_family,
|
| 166 |
-
arcface_logits,
|
| 167 |
-
emb,
|
| 168 |
-
aux_dict
|
| 169 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|