-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from boostcampaitech3/feature/soft-voting
Feature/soft voting
- Loading branch information
Showing
2 changed files
with
241 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"import glob\n", | ||
"import os\n", | ||
"import pandas as pd\n", | ||
"from tqdm import tqdm" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"624it [00:52, 11.78it/s]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"data_root = 'out'\n", | ||
"path_list = ['test','psa_80k']\n", | ||
"\n", | ||
"pt_lists = []\n", | ||
"for p in path_list:\n", | ||
" pt_list = sorted(glob.glob(f'{data_root}/{p}/*.pt'))\n", | ||
" pt_lists.append(pt_list)\n", | ||
"\n", | ||
"os.makedirs(f'{data_root}/essamble', exist_ok=True)\n", | ||
"\n", | ||
"for p in tqdm(zip(*pt_lists),total=len(pt_list)):\n", | ||
" logits = list(map(lambda x: torch.load(x),p))\n", | ||
" logit_sum = sum(logits)\n", | ||
" result = torch.argmax(logit_sum,dim=1)\n", | ||
" file_name = p[0].split('/')[-1].replace('.pt','.txt')\n", | ||
" with open(f'{data_root}/essamble/{file_name}', 'w') as f:\n", | ||
" for r in result[0]:\n", | ||
" r_list = r.tolist()\n", | ||
" f.write(' '.join(map(str,r_list)) + '\\n')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
" 3%|▎ | 17/624 [00:00<00:03, 162.46it/s]" | ||
] | ||
}, | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"\n", | ||
"making submission ...\n" | ||
] | ||
}, | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"100%|██████████| 624/624 [00:04<00:00, 146.46it/s]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print('\\nmaking submission ...')\n", | ||
"dic = {\"image_id\":[],\"PredictionString\":[]}\n", | ||
"for path in tqdm(sorted(glob.glob(f'{data_root}/essamble/*.txt'))):\n", | ||
" image_id = path.replace(f'{data_root}/essamble/','').replace('_03_','_03/').replace('vt_','vt/').replace('.txt','.jpg')\n", | ||
" with open(path, 'r') as f:\n", | ||
" value_list = f.readlines()\n", | ||
" new_value_list = []\n", | ||
" for i in range(0,len(value_list),2):\n", | ||
" value = value_list[i].split(' ')\n", | ||
" value = ' '.join(value[0::2])\n", | ||
" new_value_list.append(value)\n", | ||
"\n", | ||
" dic[\"image_id\"].append(image_id)\n", | ||
" dic[\"PredictionString\"].append(' '.join(new_value_list))\n", | ||
"\n", | ||
"df = pd.DataFrame(dic)\n", | ||
"df.to_csv(f'{data_root}/essamble/_submission.csv',index=False)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "d4d1e4263499bec80672ea0156c357c1ee493ec2b1c70f0acce89fc37c4a6abe" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3.8.5 ('base')", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.5" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters