atom-predict/egnn_v2/.ipynb_checkpoints/MetricsE2E-checkpoint.ipynb

300 lines
7.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "b7f09d7e-dc1e-4962-a031-0a55e5b67a90",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import cv2\n",
"import glob\n",
"import json\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from tqdm import tqdm\n",
"from PIL import Image\n",
"from utils.e2e_metrics import get_metrics\n",
"from core.data import get_y_3\n",
"from core.data import load_data\n",
"from sklearn.metrics import accuracy_score, f1_score, recall_score, precision_score, confusion_matrix"
]
},
{
"cell_type": "markdown",
"id": "bde4590a-e868-4668-8b88-9b7ae6741c02",
"metadata": {},
"source": [
"# Update Class"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7aaf0964-d720-4d61-bf66-4e21d58d8c9c",
"metadata": {},
"outputs": [],
"source": [
"class_dict = {\n",
" 1: 'Norm', \n",
" 2: 'SV',\n",
" 3: 'LineSV',\n",
"}\n",
"\n",
"class_dict_rev = {\n",
" 'Norm': 1, \n",
" 'SV': 2,\n",
" 'LineSV': 3,\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "41873800-6b74-4fa4-8c8b-3cecd7089518",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"with open('./logs/0/version_0/e2e.json') as f:\n",
" data = json.load(f)\n",
"\n",
"name = np.array(data['name'])\n",
"pred = np.argmax(np.array(data['pred']), axis=1)\n",
"pred_dict = dict(zip(name, pred))\n",
"\n",
"json_lst = glob.glob('../../data/linesv/gnn_data/e2e/raw/*.json', recursive=True); len(json_lst)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b002854a-b36e-4423-a016-dd088a344681",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:17<00:00, 2.17s/it]\n"
]
}
],
"source": [
"for json_path in tqdm(json_lst):\n",
" base_name = json_path.split('/')[-1].split('.')[0]\n",
" points, edge_index, _, _ = load_data(json_path)\n",
" labels = np.array([pred_dict['{}_{}'.format(base_name, '_'.join(np.array(point, np.str_)))] for point in points])\n",
" \n",
" with open(json_path) as f:\n",
" data = json.load(f)\n",
"\n",
" for i in range(len(labels)):\n",
" data['shapes'][i]['label'] = class_dict[labels[i] + 1]\n",
" \n",
" with open(json_path, 'w') as f:\n",
" json.dump(data, f)"
]
},
{
"cell_type": "markdown",
"id": "ac107f45-f949-4fcd-8950-9235b23d07ba",
"metadata": {},
"source": [
"# Metrics"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "139d5824-cf33-45d4-8fce-e525245295ea",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"json_lst = glob.glob('../../data/linesv/gnn_data/e2e/raw/*.json', recursive=True); len(json_lst)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "8ccaf1cb-0202-4f92-9757-ba2eaf6be30a",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [02:21<00:00, 17.73s/it]\n"
]
}
],
"source": [
"res = []\n",
"\n",
"for json_path in tqdm(json_lst):\n",
" base_name = json_path.split('/')[-1].split('.')[0]\n",
" points, edge_index, labels, _ = load_data(json_path)\n",
" \n",
" mask_pd = np.zeros((2048, 2048))\n",
" mask_pd[points[:, 0], points[:, 1]] = labels + 1\n",
" mask_pd = np.array(mask_pd, np.uint8)\n",
" \n",
" mask_gt = np.array(Image.open(json_path.replace('.json', '.png')), np.uint8)\n",
" \n",
" for i in range(1, 4):\n",
" res += [get_metrics(mask_gt == i, mask_pd == i)]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "cca8debf-9de3-4473-a7eb-bd3ca8884a2e",
"metadata": {},
"outputs": [],
"source": [
"res = np.array(res)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "5d92bb3b-e3c2-4373-8ae1-2bbdc901476b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.99363791, 0.99729265, 0.99546084])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Norm\n",
"np.mean(res[::3, :], axis=0)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "995b9bab-768c-47e2-9199-91fa3572380b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.70249967, 0.93076288, 0.79977477])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# SV\n",
"np.mean(res[1::3, :], axis=0)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "34a00da7-8576-4102-a3a8-e78e15db0d45",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.95990162, 0.82609145, 0.88743022])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# LineSV\n",
"np.mean(res[2::3, :], axis=0)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "4926aea8-b9d2-4878-b8db-5f676d067bc1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.8853464 , 0.918049 , 0.89422194])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.mean(res, axis=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24043672-21c7-40ef-91a1-5c612cfd6c78",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "cmae",
"language": "python",
"name": "cmae"
},
"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.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}