Skip to content

Commit

Permalink
Merge pull request #6 from boostcampaitech4lv23cv3/add_ensemble
Browse files Browse the repository at this point in the history
Add ensemble_code to utils directory
  • Loading branch information
nanpuhaha authored Nov 29, 2022
2 parents c80e0d5 + 30555df commit 647348d
Showing 1 changed file with 205 additions and 0 deletions.
205 changes: 205 additions & 0 deletions mmdetection/configs/_trash_/_base_/utils/ensemble_code.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from ensemble_boxes import *\n",
"import numpy as np\n",
"from pycocotools.coco import COCO"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loading annotations into memory...\n",
"Done (t=0.01s)\n",
"creating index...\n",
"index created!\n"
]
}
],
"source": [
"# submission csv 파일 경로를 넣어주세요.\n",
"submission_files = ['/opt/level2/level2_objectdetection_cv-level2-cv-17/mmdetection/work_dirs/csv_result/swin_1-1-aug.csv',\n",
" '/opt/level2/level2_objectdetection_cv-level2-cv-17/mmdetection/work_dirs/csv_result/swin_1-1.csv'] \n",
"\n",
"submission_df = [pd.read_csv(file) for file in submission_files]\n",
"\n",
"image_ids = submission_df[0]['image_id'].tolist()\n",
"\n",
"annotation = '/opt/ml/dataset/test.json'\n",
"coco = COCO(annotation)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"prediction_strings = []\n",
"file_names = []\n",
"\n",
"for i, image_id in enumerate(image_ids):\n",
" prediction_string = ''\n",
" boxes_list = []\n",
" scores_list = []\n",
" labels_list = []\n",
" image_info = coco.loadImgs(i)[0]\n",
" \n",
" for df in submission_df:\n",
" predict_string = df[df['image_id'] == image_id]['PredictionString'].tolist()[0]\n",
" predict_list = str(predict_string).split()\n",
" \n",
" if len(predict_list)==0 or len(predict_list)==1:\n",
" continue\n",
" \n",
" predict_list = np.reshape(predict_list, (-1, 6))\n",
" box_list = []\n",
" \n",
" for box in predict_list[:, 2:6].tolist():\n",
" box[0] = float(box[0]) / image_info['width']\n",
" box[1] = float(box[1]) / image_info['height']\n",
" box[2] = float(box[2]) / image_info['width']\n",
" box[3] = float(box[3]) / image_info['height']\n",
" box_list.append(box)\n",
" \n",
" boxes_list.append(box_list)\n",
" scores_list.append(list(map(float, predict_list[:, 1].tolist())))\n",
" labels_list.append(list(map(int, predict_list[:, 0].tolist())))\n",
" \n",
" if len(boxes_list):\n",
" #boxes, scores, labels = nms(boxes_list, scores_list, labels_list,iou_thr=0.5) # NMS ensemble\n",
" boxes, scores, labels = weighted_boxes_fusion(boxes_list, scores_list, labels_list, iou_thr=0.5) # WBF ensemble\n",
"\n",
" for box, score, label in zip(boxes, scores, labels):\n",
" prediction_string += str(int(label)) + ' ' + str(score) + ' ' + str(box[0] * image_info['width']) + ' ' + str(box[1] * image_info['height']) + ' ' + str(box[2] * image_info['width']) + ' ' + str(box[3] * image_info['height']) + ' '\n",
" \n",
" prediction_strings.append(prediction_string)\n",
" file_names.append(image_id)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>PredictionString</th>\n",
" <th>image_id</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>7 0.9999401569366455 217.22557067871094 47.987...</td>\n",
" <td>test/0000.jpg</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>5 0.9988875389099121 127.82943725585938 0.0364...</td>\n",
" <td>test/0001.jpg</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1 0.9978135824203491 287.3660583496094 308.768...</td>\n",
" <td>test/0002.jpg</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>9 0.9301331043243408 59.82720947265625 140.238...</td>\n",
" <td>test/0003.jpg</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1 0.9995791912078857 203.21682739257812 251.33...</td>\n",
" <td>test/0004.jpg</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" PredictionString image_id\n",
"0 7 0.9999401569366455 217.22557067871094 47.987... test/0000.jpg\n",
"1 5 0.9988875389099121 127.82943725585938 0.0364... test/0001.jpg\n",
"2 1 0.9978135824203491 287.3660583496094 308.768... test/0002.jpg\n",
"3 9 0.9301331043243408 59.82720947265625 140.238... test/0003.jpg\n",
"4 1 0.9995791912078857 203.21682739257812 251.33... test/0004.jpg"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"submission = pd.DataFrame()\n",
"submission['PredictionString'] = prediction_strings\n",
"submission['image_id'] = file_names\n",
"\n",
"# 저장 경로를 넣어주세요.\n",
"# 현재 코드에서는 utils 디렉토리의 submission_ensemble.csv로 저장됩니다.\n",
"submission.to_csv('/opt/level2/level2_objectdetection_cv-level2-cv-17/mmdetection/configs/_trash_/_base_/utils/submission_ensemble.csv') \n",
"submission.head()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.7.15 ('detection')",
"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.7.15"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "b94c6de4bce9a87a354a5fa9998691adc0532adddb9d4140f5ba941d00b01fae"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 647348d

Please sign in to comment.