Stefan Wolf
commited on
Commit
·
5d666d5
1
Parent(s):
20e6d37
Add competition inference script.
Browse files- script.py +52 -0
- tools/test_generate_result_pre-consensus.py +2 -2
script.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import os
|
4 |
+
import subprocess
|
5 |
+
import sys
|
6 |
+
from tqdm import tqdm
|
7 |
+
import timm
|
8 |
+
import torchvision.transforms as T
|
9 |
+
from PIL import Image
|
10 |
+
import torch
|
11 |
+
|
12 |
+
|
13 |
+
# custom script arguments
|
14 |
+
CONFIG_PATH = 'models/swinv2_base_w24_b16x4-fp16_fungi+val_res_384_cb_epochs_6.py'
|
15 |
+
CHECKPOINT_PATH = "models/swinv2_base_w24_b16x4-fp16_fungi+val_res_384_cb_epochs_6_epoch_6_20240514-de00365e.pth"
|
16 |
+
SCORE_THRESHOLD = 0.2
|
17 |
+
|
18 |
+
def run_inference(input_csv, output_csv, data_root_path):
|
19 |
+
"""Load model and dataloader and run inference."""
|
20 |
+
|
21 |
+
if not data_root_path.endswith('/'):
|
22 |
+
data_root_path += '/'
|
23 |
+
data_cfg_opts = [
|
24 |
+
f'test_dataloader.dataset.data_root=',
|
25 |
+
f'test_dataloader.dataset.ann_file={input_csv}',
|
26 |
+
f'test_dataloader.dataset.data_prefix={data_root_path}']
|
27 |
+
|
28 |
+
inference = subprocess.Popen([
|
29 |
+
'python', '-m',
|
30 |
+
'tools.test_generate_result_pre-consensus',
|
31 |
+
CONFIG_PATH, CHECKPOINT_PATH,
|
32 |
+
output_csv,
|
33 |
+
'--threshold', str(SCORE_THRESHOLD),
|
34 |
+
'--no-scores',
|
35 |
+
'--cfg-options'] + data_cfg_opts)
|
36 |
+
return_code = inference.wait()
|
37 |
+
if return_code != 0:
|
38 |
+
print(f'Inference crashed with exit code {return_code}')
|
39 |
+
sys.exit(return_code)
|
40 |
+
print(f'Written {output_csv}')
|
41 |
+
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
|
45 |
+
# import zipfile
|
46 |
+
|
47 |
+
# with zipfile.ZipFile("/tmp/data/private_testset.zip", 'r') as zip_ref:
|
48 |
+
# zip_ref.extractall("/tmp/data")
|
49 |
+
|
50 |
+
metadata_file_path = "./data/fungi2024/FungiCLEF2023_val_metadata_PRODUCTION.csv" #"./FungiCLEF2024_TestMetadata.csv"
|
51 |
+
|
52 |
+
run_inference(metadata_file_path, "./submission.csv", "./data/fungi2024/DF21/")#"/tmp/data/private_testset")
|
tools/test_generate_result_pre-consensus.py
CHANGED
@@ -95,7 +95,7 @@ def single_gpu_test(model,
|
|
95 |
observation_ids = []
|
96 |
for i, data in enumerate(data_loader):
|
97 |
with torch.no_grad():
|
98 |
-
|
99 |
imgs = data['inputs'].cuda()
|
100 |
result = model.module.extract_feat(imgs)
|
101 |
|
@@ -209,7 +209,7 @@ def main():
|
|
209 |
dropped = 0
|
210 |
total = 0
|
211 |
with open(args.out, 'w') as f:
|
212 |
-
f.write('
|
213 |
for obs_id, result in results.items():
|
214 |
avg_feats = torch.mean(torch.stack(result, dim=0), dim=0, keepdim=True)
|
215 |
scores = model.module.head(avg_feats)
|
|
|
95 |
observation_ids = []
|
96 |
for i, data in enumerate(data_loader):
|
97 |
with torch.no_grad():
|
98 |
+
data = model.module.data_preprocessor(data, training=False)
|
99 |
imgs = data['inputs'].cuda()
|
100 |
result = model.module.extract_feat(imgs)
|
101 |
|
|
|
209 |
dropped = 0
|
210 |
total = 0
|
211 |
with open(args.out, 'w') as f:
|
212 |
+
f.write('observation_id,class_id\n')
|
213 |
for obs_id, result in results.items():
|
214 |
avg_feats = torch.mean(torch.stack(result, dim=0), dim=0, keepdim=True)
|
215 |
scores = model.module.head(avg_feats)
|