farrosalferro24
commited on
Commit
•
30c1de8
1
Parent(s):
de3a6b2
Upload model and script
Browse files- pytorch_model.bin +3 -0
- script.py +109 -0
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bed2ba9abe11ad59b65d0ec5521f1ddb5d73d71988986a135322e7a53f77557e
|
3 |
+
size 1222078221
|
script.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
20 |
+
model_path: str,
|
21 |
+
model_name: str,
|
22 |
+
number_of_categories: int = 1784):
|
23 |
+
|
24 |
+
def _load_model(model_name, model_path):
|
25 |
+
|
26 |
+
print("Setting up Pytorch Model")
|
27 |
+
self.device = torch.device(
|
28 |
+
"cuda:0" if torch.cuda.is_available() else "cpu")
|
29 |
+
print(f"Using devide: {self.device}")
|
30 |
+
|
31 |
+
model = timm.create_model(model_name,
|
32 |
+
num_classes=number_of_categories,
|
33 |
+
pretrained=True)
|
34 |
+
|
35 |
+
# if not torch.cuda.is_available():
|
36 |
+
# model_ckpt = torch.load(model_path, map_location=torch.device("cpu"))
|
37 |
+
# else:
|
38 |
+
# model_ckpt = torch.load(model_path)
|
39 |
+
|
40 |
+
model_ckpt = torch.load(model_path, map_location=self.device)
|
41 |
+
model.load_state_dict(model_ckpt)
|
42 |
+
|
43 |
+
return model.to(self.device).eval()
|
44 |
+
|
45 |
+
self.model = _load_model(model_name, model_path)
|
46 |
+
|
47 |
+
self.transforms = T.Compose([
|
48 |
+
T.Resize((336, 336)),
|
49 |
+
T.ToTensor(),
|
50 |
+
T.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
|
51 |
+
])
|
52 |
+
|
53 |
+
def predict_image(self, image: np.ndarray) -> list():
|
54 |
+
"""Run inference using ONNX runtime.
|
55 |
+
|
56 |
+
:param image: Input image as numpy array.
|
57 |
+
:return: A list with logits and confidences.
|
58 |
+
"""
|
59 |
+
|
60 |
+
logits = self.model(
|
61 |
+
self.transforms(image).unsqueeze(0).to(self.device))
|
62 |
+
|
63 |
+
return logits.tolist()
|
64 |
+
|
65 |
+
|
66 |
+
def make_submission(test_metadata, model_path, model_name, output_csv_path="./submission.csv", images_root_path="/tmp/data/private_testset"):
|
67 |
+
"""Make submission with given """
|
68 |
+
|
69 |
+
model = PytorchWorker(model_path, model_name)
|
70 |
+
|
71 |
+
predictions = []
|
72 |
+
|
73 |
+
for _, row in tqdm(test_metadata.iterrows(), total=len(test_metadata)):
|
74 |
+
image_path = os.path.join(images_root_path, row.image_path)
|
75 |
+
|
76 |
+
test_image = Image.open(image_path).convert("RGB")
|
77 |
+
|
78 |
+
logits = model.predict_image(test_image)
|
79 |
+
|
80 |
+
predictions.append(np.argmax(logits))
|
81 |
+
|
82 |
+
|
83 |
+
test_metadata["class_id"] = predictions
|
84 |
+
|
85 |
+
user_pred_df = test_metadata.drop_duplicates("observation_id", keep="first")
|
86 |
+
user_pred_df[["observation_id", "class_id"]].to_csv(output_csv_path, index=None)
|
87 |
+
|
88 |
+
if __name__ == "__main__":
|
89 |
+
|
90 |
+
import zipfile
|
91 |
+
|
92 |
+
with zipfile.ZipFile("/tmp/data/private_testset.zip", 'r') as zip_ref:
|
93 |
+
zip_ref.extractall("/tmp/data")
|
94 |
+
|
95 |
+
MODEL_PATH = "pytorch_model.bin"
|
96 |
+
MODEL_NAME = "hf-hub:timm/eva02_large_patch14_clip_336.merged2b_ft_inat21"
|
97 |
+
|
98 |
+
metadata_file_path = "./SnakeCLEF2024_TestMetadata.csv"
|
99 |
+
test_metadata = pd.read_csv(metadata_file_path)
|
100 |
+
|
101 |
+
# metadata_file_path = "./unit_test.csv"
|
102 |
+
# test_metadata = pd.read_csv(metadata_file_path)
|
103 |
+
# images_root_path = "SnakeCLEF2023-large_size"
|
104 |
+
|
105 |
+
make_submission(
|
106 |
+
test_metadata=test_metadata,
|
107 |
+
model_path=MODEL_PATH,
|
108 |
+
model_name=MODEL_NAME,
|
109 |
+
)
|