sieberm commited on
Commit
8941f8e
1 Parent(s): b5d8317

Upload 9 files

Browse files
README.md ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ ---
2
+ license: mit
3
+ ---
SnakeCLEF2024-TestMetadata.csv ADDED
The diff for this file is too large to render. See raw diff
 
best_accuracy_boost.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7b774f03ccae3d4a2297113440fa9ead4f851cab133ab6a10e42a656a8c746a2
3
+ size 115886846
best_loss_boost.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b53cefa2f22078af664cf3316dbe5f2a0eb2eec159c92e3eda13c6c7b3654a97
3
+ size 115885946
epoch_50.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:55e8ab332495fd7f8cbb74222449d37913300575e831df23e62a5d53ea424f15
3
+ size 115885721
main.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # model_ckpt = torch.load(model_path, map_location=torch.device("cpu"))
31
+ # else:
32
+ # model_ckpt = torch.load(model_path)
33
+
34
+ model_ckpt = torch.load(model_path, map_location=self.device)
35
+ model.load_state_dict(model_ckpt)
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((256, 256)),
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="/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.image_path)
67
+
68
+ test_image = Image.open(image_path).convert("RGB")
69
+
70
+ logits = model.predict_image(test_image)
71
+
72
+ predictions.append(np.argmax(logits))
73
+
74
+ test_metadata["class_id"] = predictions
75
+
76
+ user_pred_df = test_metadata.drop_duplicates("observation_id", keep="first")
77
+ user_pred_df[["observation_id", "class_id"]].to_csv(output_csv_path, index=None)
78
+
79
+
80
+ if __name__ == "__main__":
81
+
82
+ # import zipfile
83
+ #
84
+ # with zipfile.ZipFile("/tmp/data/private_testset.zip", 'r') as zip_ref:
85
+ # zip_ref.extractall("/tmp/data")
86
+
87
+ # MODEL_PATH = "pytorch_model.bin"
88
+ MODEL_PATH = "best_accuracy.pth"
89
+ # MODEL_NAME = "tf_efficientnet_b1.ap_in1k"
90
+ MODEL_NAME = "swinv2_tiny_window16_256.ms_in1k"
91
+
92
+ metadata_file_path = "./FungiCLEF2024_TestMetadata.csv"
93
+ # metadata_file_path = "/home/zeleznyt/mnt/data-ntis/projects/korpusy_cv/SnakeCLEF2024/SnakeCLEF2023-ValMetadata.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
+ # images_root_path='/home/zeleznyt/mnt/data-ntis/projects/korpusy_cv/SnakeCLEF2024/val/SnakeCLEF2023-medium_size'
101
+ )
script.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ from torch import nn
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
+ class PytorchWorker:
16
+ """Run inference using ONNX runtime."""
17
+
18
+ def __init__(self, model_path: str, model_name: str, number_of_categories: int = 1784):
19
+
20
+ def _load_model(model_name, model_path):
21
+
22
+ print("Setting up Pytorch Model")
23
+ self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
24
+ print(f"Using devide: {self.device}")
25
+
26
+ model = timm.create_model(model_name, num_classes=number_of_categories, pretrained=False)
27
+
28
+ # if not torch.cuda.is_available():
29
+ # model_ckpt = torch.load(model_path, map_location=torch.device("cpu"))
30
+ # else:
31
+ # model_ckpt = torch.load(model_path)
32
+
33
+ model_ckpt = torch.load(model_path, map_location=self.device)
34
+ model.load_state_dict(model_ckpt)
35
+
36
+ return model.to(self.device).eval()
37
+
38
+ self.model = _load_model(model_name, model_path)
39
+
40
+ self.transforms = T.Compose([T.Resize((256, 256)),
41
+ T.ToTensor(),
42
+ T.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
43
+
44
+
45
+ def predict_image(self, image: np.ndarray) -> list():
46
+ """Run inference using ONNX runtime.
47
+
48
+ :param image: Input image as numpy array.
49
+ :return: A list with logits and confidences.
50
+ """
51
+
52
+ logits = self.model(self.transforms(image).unsqueeze(0).to(self.device))
53
+
54
+ return logits.tolist()
55
+
56
+
57
+ def make_submission(test_metadata, model_path, model_name, output_csv_path="./submission.csv", images_root_path="/tmp/data/private_testset"):
58
+ """Make submission with given """
59
+
60
+ model = PytorchWorker(model_path, model_name)
61
+
62
+ predictions = []
63
+
64
+ for _, row in tqdm(test_metadata.iterrows(), total=len(test_metadata)):
65
+ image_path = os.path.join(images_root_path, row.filename)
66
+
67
+ test_image = Image.open(image_path).convert("RGB")
68
+
69
+ logits = model.predict_image(test_image)
70
+
71
+ predictions.append(np.argmax(logits))
72
+
73
+ test_metadata["class_id"] = predictions
74
+
75
+ user_pred_df = test_metadata.drop_duplicates("observation_id", keep="first")
76
+ user_pred_df[["observation_id", "class_id"]].to_csv(output_csv_path, index=None)
77
+
78
+
79
+ if __name__ == "__main__":
80
+
81
+ import zipfile
82
+
83
+ with zipfile.ZipFile("/tmp/data/private_testset.zip", 'r') as zip_ref:
84
+ zip_ref.extractall("/tmp/data")
85
+
86
+ MODEL_PATH = "best_accuracy_boost.pth"
87
+ # MODEL_PATH= "best_loss_boost.pth"
88
+ # MODEL_PATH = "epoch_50.pth"
89
+ MODEL_NAME = "swinv2_tiny_window16_256.ms_in1k"
90
+
91
+ metadata_file_path = "./SnakeCLEF2024-TestMetadata.csv"
92
+ test_metadata = pd.read_csv(metadata_file_path)
93
+
94
+ make_submission(
95
+ test_metadata=test_metadata,
96
+ model_path=MODEL_PATH,
97
+ model_name=MODEL_NAME,
98
+ # images_root_path='/home/zeleznyt/mnt/data-ntis/projects/korpusy_cv/SnakeCLEF2024/val/SnakeCLEF2023-medium_size'
99
+ )
submission.csv ADDED
The diff for this file is too large to render. See raw diff
 
swinv2_tiny_window16_256.ms_in1k.yaml ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # data
2
+ augmentations: 'vit_heavy'
3
+ image_size: [256, 256] # [height, width]
4
+ dataset: 'SnakeCLEF2023'
5
+
6
+ # model
7
+ architecture: 'swinv2_tiny_window16_256.ms_in1k'
8
+
9
+ # training
10
+ loss: 'SeeSawLoss'
11
+ optimizer: 'SGD'
12
+ scheduler: 'plateau'
13
+ epochs: 100
14
+ learning_rate: 0.01
15
+ batch_size: 32
16
+ accumulation_steps: 4
17
+
18
+ # other
19
+ random_seed: 777
20
+ workers: 1
21
+ multigpu: False
22
+ tags: ["Fine-tuning"] # W&B Run tags
23
+ root_path: "./"