sieberm commited on
Commit
49bec47
·
verified ·
1 Parent(s): a3b966e

Upload 7 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_BCE_CE.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1d05a3823ff0104619dfc17a79ef1a28a99f185ef2034257e7977778c881c4a6
3
+ size 115896262
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,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 CustomModel(nn.Module):
16
+ def __init__(self, base_model_name, num_classes1, num_classes2):
17
+ super(CustomModel, self).__init__()
18
+ self.base_model = timm.create_model(base_model_name, pretrained=False)
19
+ in_features = self.base_model.get_classifier().in_features
20
+ self.base_model.reset_classifier(0) # Remove the original classification layer
21
+
22
+ self.fc1 = nn.Linear(in_features, num_classes1) # Binary classification output
23
+ self.fc2 = nn.Linear(in_features, num_classes2) # Categorical classification output
24
+
25
+ def forward(self, x):
26
+ x = self.base_model(x)
27
+ out1 = torch.sigmoid(self.fc1(x)) # Binary output
28
+ out2 = self.fc2(x) # Categorical output
29
+ return out2
30
+
31
+ class PytorchWorker:
32
+ """Run inference using ONNX runtime."""
33
+
34
+ def __init__(self, model_path: str, model_name: str, number_of_categories: int = 1784):
35
+
36
+ def _load_model(model_name, model_path):
37
+
38
+ print("Setting up Pytorch Model")
39
+ self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
40
+ print(f"Using devide: {self.device}")
41
+
42
+ model = CustomModel(model_name, 1, number_of_categories)
43
+
44
+ # if not torch.cuda.is_available():
45
+ # model_ckpt = torch.load(model_path, map_location=torch.device("cpu"))
46
+ # else:
47
+ # model_ckpt = torch.load(model_path)
48
+
49
+ model_ckpt = torch.load(model_path, map_location=self.device)
50
+ model.load_state_dict(model_ckpt)
51
+
52
+ return model.to(self.device).eval()
53
+
54
+ self.model = _load_model(model_name, model_path)
55
+
56
+ self.transforms = T.Compose([T.Resize((256, 256)),
57
+ T.ToTensor(),
58
+ T.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
59
+
60
+
61
+ def predict_image(self, image: np.ndarray) -> list():
62
+ """Run inference using ONNX runtime.
63
+
64
+ :param image: Input image as numpy array.
65
+ :return: A list with logits and confidences.
66
+ """
67
+
68
+ logits = self.model(self.transforms(image).unsqueeze(0).to(self.device))
69
+
70
+ return logits.tolist()
71
+
72
+
73
+ def make_submission(test_metadata, model_path, model_name, output_csv_path="./submission.csv", images_root_path="/tmp/data/private_testset"):
74
+ """Make submission with given """
75
+
76
+ model = PytorchWorker(model_path, model_name)
77
+
78
+ predictions = []
79
+
80
+ for _, row in tqdm(test_metadata.iterrows(), total=len(test_metadata)):
81
+ image_path = os.path.join(images_root_path, row.filename)
82
+
83
+ test_image = Image.open(image_path).convert("RGB")
84
+
85
+ logits = model.predict_image(test_image)
86
+
87
+ predictions.append(np.argmax(logits))
88
+
89
+ test_metadata["class_id"] = predictions
90
+
91
+ user_pred_df = test_metadata.drop_duplicates("observation_id", keep="first")
92
+ user_pred_df[["observation_id", "class_id"]].to_csv(output_csv_path, index=None)
93
+
94
+
95
+ if __name__ == "__main__":
96
+
97
+ import zipfile
98
+
99
+ with zipfile.ZipFile("/tmp/data/private_testset.zip", 'r') as zip_ref:
100
+ zip_ref.extractall("/tmp/data")
101
+
102
+ MODEL_PATH = "best_accuracy_BCE_CE.pth"
103
+ MODEL_NAME = "swinv2_tiny_window16_256.ms_in1k"
104
+
105
+ metadata_file_path = "./SnakeCLEF2024-TestMetadata.csv"
106
+ test_metadata = pd.read_csv(metadata_file_path)
107
+
108
+ make_submission(
109
+ test_metadata=test_metadata,
110
+ model_path=MODEL_PATH,
111
+ model_name=MODEL_NAME,
112
+ # images_root_path='/home/zeleznyt/mnt/data-ntis/projects/korpusy_cv/SnakeCLEF2024/val/SnakeCLEF2023-medium_size'
113
+ )
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: "./"