picekl commited on
Commit
443dd28
1 Parent(s): b6c8c53

feat: initial commit with 'working' version

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. FungiCLEF2024_TestMetadata.csv +3 -0
  3. README.md +3 -0
  4. script.py +82 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ FungiCLEF2024_TestMetadata.csv filter=lfs diff=lfs merge=lfs -text
FungiCLEF2024_TestMetadata.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4e6c2d444ca9fa21fa3f648466513184a5cb9dc0a6e4abf64c74c5139e9ff3ec
3
+ size 4503122
README.md ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ ---
2
+ license: mit
3
+ ---
script.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, onnx_path: str):
20
+ print("Setting up Pytorch Model")
21
+
22
+ self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
23
+
24
+ print(f"Using devide: {self.device}")
25
+ self.model = timm.create_model("hf-hub:BVRA/tf_efficientnet_b3.in1k_ft_df20_224", pretrained=True)
26
+ self.model = self.model.eval()
27
+ self.model.to(self.device)
28
+
29
+ self.transforms = T.Compose([T.Resize((224, 224)),
30
+ T.ToTensor(),
31
+ T.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
32
+
33
+ def predict_image(self, image: np.ndarray) -> list():
34
+ """Run inference using ONNX runtime.
35
+
36
+ :param image: Input image as numpy array.
37
+ :return: A list with logits and confidences.
38
+ """
39
+
40
+ logits = self.model(self.transforms(image).unsqueeze(0))
41
+
42
+ return logits.tolist()
43
+
44
+
45
+ def make_submission(test_metadata, model_path, output_csv_path="./submission.csv", images_root_path="/tmp/data/private_testset"):
46
+ """Make submission with given """
47
+
48
+ model = PytorchWorker(model_path)
49
+
50
+ predictions = []
51
+
52
+ for _, row in tqdm(test_metadata.iterrows(), total=len(test_metadata)):
53
+ image_path = os.path.join(images_root_path, row.image_path)
54
+
55
+ test_image = Image.open(image_path).convert("RGB")
56
+
57
+ logits = model.predict_image(test_image)
58
+
59
+ predictions.append(np.argmax(logits))
60
+
61
+ test_metadata["class_id"] = predictions
62
+
63
+ user_pred_df = test_metadata.drop_duplicates("observation_id", keep="first")
64
+ user_pred_df[["observation_id", "class_id"]].to_csv(output_csv_path, index=None)
65
+
66
+
67
+ if __name__ == "__main__":
68
+
69
+ import zipfile
70
+
71
+ with zipfile.ZipFile("/tmp/data/private_testset.zip", 'r') as zip_ref:
72
+ zip_ref.extractall("/tmp/data")
73
+
74
+ HFHUB_MODEL_PATH = "hf-hub:BVRA/tf_efficientnet_b3.in1k_ft_df20_224"
75
+
76
+ metadata_file_path = "./FungiCLEF2024_TestMetadata.csv"
77
+ test_metadata = pd.read_csv(metadata_file_path)
78
+
79
+ make_submission(
80
+ test_metadata=test_metadata,
81
+ model_path=HFHUB_MODEL_PATH,
82
+ )