File size: 5,573 Bytes
5236ead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5cf20b5b",
   "metadata": {},
   "source": [
    "## Get Transformation file\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe5a09ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import math\n",
    "import json\n",
    "import os\n",
    "\n",
    "categories = ['bottle2', 'lightbulb', 'lighter', 'eyeglasses', 'magnifying_glass', 'spray']\n",
    "fill_rate = ['100', '75', '50', '25', '0']\n",
    "result_path = './Fast-Robust-ICP/Result/'\n",
    "\n",
    "# assign your folder \n",
    "\n",
    "category =  categories[0]\n",
    "\n",
    "\n",
    "result_path =result_path +  category\n",
    "\n",
    "json_path = result_path + \"ply_files.json\"\n",
    "\n",
    "\n",
    "### Generating T matrix list.\n",
    "\n",
    "# bring the filename json file.\n",
    "try: \n",
    "    with open(json_path, \"r\", encoding=\"utf-8\") as f:\n",
    "        categorized_files = json.load(f)\n",
    "\n",
    "except FileNotFoundError:\n",
    "    print(f\"오류: '{json_path}' 파일을 찾을 수 없습니다. 먼저 파일 분류 코드를 실행해 주세요.\")\n",
    "    exit() # 파일이 없으면 프로그램 종료\n",
    "\n",
    "\n",
    "## get GT\n",
    "gt = []\n",
    "\n",
    "gt_T =[]\n",
    "\n",
    "for fill in fill_rate:\n",
    "    filenames = categorized_files.get(fill, [])\n",
    "    T_array = []\n",
    "\n",
    "    for file in filenames:\n",
    "        gt_name = f\"gt_{file}.txt\"\n",
    "        matrix = np.loadtxt(gt_name)\n",
    "        T_array.append(matrix)\n",
    "    gt_T.append(T_array)\n",
    "\n",
    "\n",
    "\n",
    "print(np.gt_T)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# get T matrix array\n",
    "overall_T =[]\n",
    "\n",
    "for fill in fill_rate:\n",
    "    filenames = categorized_files.get(fill, [])\n",
    "    T_array = []\n",
    "\n",
    "    for file in filenames:\n",
    "        matrix_path = result_path + file+\".txt\"\n",
    "        matrix = np.loadtxt(matrix_path)\n",
    "        T_array.append(matrix)\n",
    "    overall_T.append(T_array)\n",
    "\n",
    "\n",
    "\n",
    "print(np.overall_T.shape)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a7cf4b9",
   "metadata": {},
   "source": [
    "# compute RMSE\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "758cc248",
   "metadata": {},
   "outputs": [],
   "source": [
    "def RMSE(T_star, T):\n",
    "    diff = T_star - T\n",
    "    sq_norms = np.sum(diff**2, axis =1)\n",
    "\n",
    "    r = np.sqrt(np.mean(sq_norms))\n",
    "\n",
    "    return r\n",
    "\n",
    "def mean(array):\n",
    "    return np.mean(array)\n",
    "\n",
    "\n",
    "RMSE_mean = []\n",
    "for gt, overall in zip(gt_T, overall_T):\n",
    "    rmse = []\n",
    "    for T_star, T in zip(gt, overall):\n",
    "        r= RMSE(T_star, T)\n",
    "        rmse.append(r)\n",
    "    RMSE_mean.append(mean(rmse))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b859fdc3",
   "metadata": {},
   "source": [
    "## Save in json\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8c0faa07",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'RMSE_mean' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[2], line 8\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m cat \u001b[38;5;129;01min\u001b[39;00m categories:\n\u001b[1;32m      7\u001b[0m     rmse_dict[cat] \u001b[38;5;241m=\u001b[39m {}\n\u001b[0;32m----> 8\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m mean, fr \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(RMSE_mean,fill_rate):\n\u001b[1;32m      9\u001b[0m         rmse_dict[cat][fr] \u001b[38;5;241m=\u001b[39m mean\n\u001b[1;32m     11\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrmse_Results.json\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mw\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f:\n",
      "\u001b[0;31mNameError\u001b[0m: name 'RMSE_mean' is not defined"
     ]
    }
   ],
   "source": [
    "categories = ['bottle2', 'lightbulb', 'lighter', 'eyeglasses', 'magnifying_glass', 'spray']\n",
    "fill_rate = ['100', '75', '50', '25', '0']\n",
    "\n",
    "rmse_dict = {}\n",
    "\n",
    "for cat in categories:\n",
    "    rmse_dict[cat] = {}\n",
    "    for mean, fr in zip(RMSE_mean,fill_rate):\n",
    "        rmse_dict[cat][fr] = mean\n",
    "\n",
    "with open('rmse_Results.json', 'w') as f:\n",
    "    json.dump(rmse_dict, f, indent=4)\n",
    "        \n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}