Upload 3 files
Browse files- SnakeCLEF2024_TestMetadata.csv +0 -0
- model_best.pth.tar +3 -0
- script.py +100 -0
SnakeCLEF2024_TestMetadata.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
model_best.pth.tar
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:db69fa1f486a020aee617ef4eb536b3a0e1a05c57961f7d5c60773891f924f8f
|
3 |
+
size 217679664
|
script.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
# import onnxruntime as ort
|
4 |
+
import os
|
5 |
+
from tqdm import tqdm
|
6 |
+
import timm
|
7 |
+
import torchvision.transforms as T
|
8 |
+
from PIL import Image
|
9 |
+
import torch
|
10 |
+
|
11 |
+
def is_gpu_available():
|
12 |
+
"""Check if the python package `onnxruntime-gpu` is installed."""
|
13 |
+
return torch.cuda.is_available()
|
14 |
+
|
15 |
+
|
16 |
+
class PytorchWorker:
|
17 |
+
"""Run inference using ONNX runtime."""
|
18 |
+
|
19 |
+
def __init__(self, model_path: str, model_name: str, number_of_categories: int = 1784):
|
20 |
+
|
21 |
+
def _load_model(model_name, model_path):
|
22 |
+
|
23 |
+
print("Setting up Pytorch Model")
|
24 |
+
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
25 |
+
print(f"Using devide: {self.device}")
|
26 |
+
|
27 |
+
model = timm.create_model(model_name, num_classes=number_of_categories, pretrained=False)
|
28 |
+
|
29 |
+
# if not torch.cuda.is_available():
|
30 |
+
# checkpoint = torch.load(model_path, map_location=torch.device("cpu"))
|
31 |
+
# else:
|
32 |
+
# checkpoint = torch.load(model_path)
|
33 |
+
|
34 |
+
checkpoint = torch.load(model_path, map_location=self.device)
|
35 |
+
model.load_state_dict(checkpoint['state_dict'], strict=False)
|
36 |
+
|
37 |
+
return model.to(self.device).eval()
|
38 |
+
|
39 |
+
self.model = _load_model(model_name, model_path)
|
40 |
+
|
41 |
+
self.transforms = T.Compose([T.Resize((299, 299)),
|
42 |
+
T.ToTensor(),
|
43 |
+
T.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
|
44 |
+
|
45 |
+
|
46 |
+
def predict_image(self, image: np.ndarray) -> list():
|
47 |
+
"""Run inference using ONNX runtime.
|
48 |
+
|
49 |
+
:param image: Input image as numpy array.
|
50 |
+
:return: A list with logits and confidences.
|
51 |
+
"""
|
52 |
+
|
53 |
+
logits = self.model(self.transforms(image).unsqueeze(0).to(self.device))
|
54 |
+
|
55 |
+
return logits.tolist()
|
56 |
+
|
57 |
+
|
58 |
+
def make_submission(test_metadata, model_path, model_name, output_csv_path="./submission.csv", images_root_path=r"/tmp/data/private_testset"):
|
59 |
+
"""Make submission with given """
|
60 |
+
|
61 |
+
model = PytorchWorker(model_path, model_name)
|
62 |
+
|
63 |
+
predictions = []
|
64 |
+
|
65 |
+
for _, row in tqdm(test_metadata.iterrows(), total=len(test_metadata)):
|
66 |
+
image_path = os.path.join(images_root_path, row.filename)
|
67 |
+
try:
|
68 |
+
|
69 |
+
test_image = Image.open(image_path).convert("RGB")
|
70 |
+
|
71 |
+
logits = model.predict_image(test_image)
|
72 |
+
|
73 |
+
predictions.append(np.argmax(logits))
|
74 |
+
except:
|
75 |
+
print(image_path," Not found")
|
76 |
+
|
77 |
+
test_metadata["class_id"] = predictions
|
78 |
+
|
79 |
+
user_pred_df = test_metadata.drop_duplicates("observation_id", keep="first")
|
80 |
+
user_pred_df[["observation_id", "class_id"]].to_csv(output_csv_path, index=None)
|
81 |
+
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
|
85 |
+
import zipfile
|
86 |
+
|
87 |
+
with zipfile.ZipFile("/tmp/data/private_testset.zip", 'r') as zip_ref:
|
88 |
+
zip_ref.extractall("/tmp/data")
|
89 |
+
|
90 |
+
MODEL_PATH = "./model_best.pth.tar"
|
91 |
+
MODEL_NAME = "resnet50"
|
92 |
+
|
93 |
+
metadata_file_path = "./SnakeCLEF2024_TestMetadata.csv"
|
94 |
+
test_metadata = pd.read_csv(metadata_file_path)
|
95 |
+
|
96 |
+
make_submission(
|
97 |
+
test_metadata=test_metadata,
|
98 |
+
model_path=MODEL_PATH,
|
99 |
+
model_name=MODEL_NAME
|
100 |
+
)
|