chychiu commited on
Commit
77875f7
·
1 Parent(s): fa0fff5

first commit

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