Commit
•
b342394
1
Parent(s):
a37893b
Upload 2 files
Browse files- script.py +88 -0
- swinv2_tiny_window16_256.onnx +3 -0
script.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import onnxruntime as ort
|
5 |
+
import os
|
6 |
+
from tqdm import tqdm
|
7 |
+
|
8 |
+
|
9 |
+
def is_gpu_available():
|
10 |
+
"""Check if the python package `onnxruntime-gpu` is installed."""
|
11 |
+
return ort.get_device() == "GPU"
|
12 |
+
|
13 |
+
|
14 |
+
class ONNXWorker:
|
15 |
+
"""Run inference using ONNX runtime."""
|
16 |
+
|
17 |
+
def __init__(self, onnx_path: str):
|
18 |
+
print("Setting up ONNX runtime session.")
|
19 |
+
self.use_gpu = is_gpu_available()
|
20 |
+
if self.use_gpu:
|
21 |
+
providers = ["CUDAExecutionProvider", "CPUExecutionProvider"]
|
22 |
+
else:
|
23 |
+
providers = ["CPUExecutionProvider"]
|
24 |
+
|
25 |
+
print(f"Using {providers}")
|
26 |
+
self.ort_session = ort.InferenceSession(onnx_path, providers=providers)
|
27 |
+
|
28 |
+
def _resize_image(self, image: np.ndarray) -> np.ndarray:
|
29 |
+
"""
|
30 |
+
|
31 |
+
:param image:
|
32 |
+
:return:
|
33 |
+
"""
|
34 |
+
|
35 |
+
newsize = (300, 300)
|
36 |
+
im1 = im1.resize(newsize)
|
37 |
+
|
38 |
+
def predict_image(self, image: np.ndarray) -> list():
|
39 |
+
"""Run inference using ONNX runtime.
|
40 |
+
|
41 |
+
:param image: Input image as numpy array.
|
42 |
+
:return: A list with logits and confidences.
|
43 |
+
"""
|
44 |
+
|
45 |
+
logits, _ = self.ort_session.run(None, {"input": image.astype(dtype=np.uint8)})
|
46 |
+
|
47 |
+
return logits.tolist()
|
48 |
+
|
49 |
+
|
50 |
+
def make_submission(test_metadata, model_path, output_csv_path="./submission.csv", images_root_path="/tmp/data/private_testset"):
|
51 |
+
"""Make submission with given """
|
52 |
+
|
53 |
+
model = ONNXWorker(model_path)
|
54 |
+
|
55 |
+
predictions = []
|
56 |
+
|
57 |
+
for _, row in tqdm(test_metadata.iterrows(), total=len(test_metadata)):
|
58 |
+
image_path = os.path.join(images_root_path, row.filename)
|
59 |
+
|
60 |
+
test_image = Image.open(image_path).convert("RGB")
|
61 |
+
test_image_resized = np.asarray(test_image.resize((256, 256)))
|
62 |
+
|
63 |
+
logits = model.predict_image(test_image_resized)
|
64 |
+
|
65 |
+
predictions.append(np.argmax(logits))
|
66 |
+
|
67 |
+
test_metadata["class_id"] = predictions
|
68 |
+
|
69 |
+
user_pred_df = test_metadata.drop_duplicates("observation_id", keep="first")
|
70 |
+
user_pred_df[["observation_id", "class_id"]].to_csv(output_csv_path, index=None)
|
71 |
+
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
|
75 |
+
import zipfile
|
76 |
+
|
77 |
+
with zipfile.ZipFile("/tmp/data/private_testset.zip", 'r') as zip_ref:
|
78 |
+
zip_ref.extractall("/tmp/data")
|
79 |
+
|
80 |
+
ONNX_MODEL_PATH = "./swinv2_tiny_window16_256.onnx"
|
81 |
+
|
82 |
+
metadata_file_path = "./SnakeCLEF2024-TestMetadata.csv"
|
83 |
+
test_metadata = pd.read_csv(metadata_file_path)
|
84 |
+
|
85 |
+
make_submission(
|
86 |
+
test_metadata=test_metadata,
|
87 |
+
model_path=ONNX_MODEL_PATH,
|
88 |
+
)
|
swinv2_tiny_window16_256.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ff53172bace1485a6e582e1c3dc9719fa4b6acf7ba4481061a220220faaa2eb2
|
3 |
+
size 122122210
|