Add andrena classifier
Browse files- classifier-andrena-classmapping.txt +50 -0
- classifier-andrena.onnx +3 -0
- pytorch-to-onnx.py +53 -0
classifier-andrena-classmapping.txt
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
11172350
|
| 2 |
+
11178877
|
| 3 |
+
1356887
|
| 4 |
+
1356889
|
| 5 |
+
1356976
|
| 6 |
+
1356981
|
| 7 |
+
1357001
|
| 8 |
+
1357040
|
| 9 |
+
1357066
|
| 10 |
+
1357077
|
| 11 |
+
1357111
|
| 12 |
+
1357120
|
| 13 |
+
1357156
|
| 14 |
+
1357176
|
| 15 |
+
1357260
|
| 16 |
+
1357265
|
| 17 |
+
1357279
|
| 18 |
+
1357310
|
| 19 |
+
1357338
|
| 20 |
+
1357364
|
| 21 |
+
9740714
|
| 22 |
+
1357406
|
| 23 |
+
1357486
|
| 24 |
+
1357531
|
| 25 |
+
1357545
|
| 26 |
+
1357548
|
| 27 |
+
1357652
|
| 28 |
+
1357663
|
| 29 |
+
1357665
|
| 30 |
+
1357703
|
| 31 |
+
1357773
|
| 32 |
+
1357835
|
| 33 |
+
1357872
|
| 34 |
+
1357893
|
| 35 |
+
1357907
|
| 36 |
+
1357937
|
| 37 |
+
1357950
|
| 38 |
+
1358038
|
| 39 |
+
1358070
|
| 40 |
+
1358080
|
| 41 |
+
1358088
|
| 42 |
+
1358128
|
| 43 |
+
1358155
|
| 44 |
+
1358165
|
| 45 |
+
1358224
|
| 46 |
+
1358225
|
| 47 |
+
1358232
|
| 48 |
+
1358240
|
| 49 |
+
8460383
|
| 50 |
+
9805254
|
classifier-andrena.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c891ce12e38af92df1a0bdf75a43db93132f14e4eba7e1b52f02c32fc5091492
|
| 3 |
+
size 1063717
|
pytorch-to-onnx.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.12"
|
| 3 |
+
# dependencies = [
|
| 4 |
+
# "onnx",
|
| 5 |
+
# "onnxscript",
|
| 6 |
+
# "rich",
|
| 7 |
+
# "timm",
|
| 8 |
+
# "torch",
|
| 9 |
+
# ]
|
| 10 |
+
# ///
|
| 11 |
+
import sys
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
|
| 14 |
+
import timm
|
| 15 |
+
import torch
|
| 16 |
+
from rich import print
|
| 17 |
+
|
| 18 |
+
if len(sys.argv) < 3:
|
| 19 |
+
print(
|
| 20 |
+
"""Usage: uv run pytorch-to-onnx.py BASE_MODEL PATH_TO_PTH PATH_TO_CLASSMAPPING_TXT IMAGE_INPUT_SIZE
|
| 21 |
+
|
| 22 |
+
BASE_MODEL: The name of the base model to use (e.g., resnet50.a1_in1k). Should be available in the timm library.
|
| 23 |
+
PATH_TO_PTH: can be a .tar containing a .pth folder, or a .pth file
|
| 24 |
+
PATH_TO_CLASSMAPPING_TXT: A text file containing the class names, one per line. Only used to determine the number of classes for the model.
|
| 25 |
+
IMAGE_INPUT_SIZE: The size of the input images (e.g., 224). The model will expect input tensors of shape [1, 3, IMAGE_INPUT_SIZE, IMAGE_INPUT_SIZE].
|
| 26 |
+
|
| 27 |
+
"""
|
| 28 |
+
)
|
| 29 |
+
sys.exit()
|
| 30 |
+
|
| 31 |
+
classmapping = list(Path(sys.argv[3]).read_text().splitlines())
|
| 32 |
+
|
| 33 |
+
base_model = timm.create_model(
|
| 34 |
+
sys.argv[1], pretrained=True, num_classes=len(classmapping)
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
filename = Path(sys.argv[2])
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
image_input_size = int(sys.argv[4])
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
state = torch.load(filename, map_location=torch.device("cpu"), weights_only=False)
|
| 44 |
+
base_model.load_state_dict(state["state_dict"])
|
| 45 |
+
|
| 46 |
+
base_model = torch.nn.Sequential(base_model, torch.nn.Softmax(dim=1))
|
| 47 |
+
base_model.eval()
|
| 48 |
+
|
| 49 |
+
torch.onnx.export(
|
| 50 |
+
base_model,
|
| 51 |
+
args=(torch.zeros([1, 3, image_input_size, image_input_size]),),
|
| 52 |
+
f=filename.with_suffix(".onnx"),
|
| 53 |
+
)
|