File size: 7,735 Bytes
66903f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685ef27
66903f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "!pip intall numpy pandas FlagEmbedding scikit-learn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.metrics import precision_score, recall_score, f1_score\n",
    "from FlagEmbedding import FlagReranker\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_path = '...'\n",
    "qd_df = pd.read_parquet('AutoRAG-example-korean-embedding-benchmark/data/qa_v4.parquet')\n",
    "qd_df['retrieval_gt'] = qd_df['retrieval_gt'].apply(lambda x : x[0][0])\n",
    "\n",
    "corpus_df = pd.read_parquet('AutoRAG-example-korean-embedding-benchmark/data/ocr_corpus_v3.parquet')\n",
    "corpus_id = {}\n",
    "for idx, row in corpus_df.iterrows():\n",
    "    corpus_id[row[0]] =  row[1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_3861538/48936308.py:10: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  corpus_id[row[0]] =  row[1]\n",
      "/tmp/ipykernel_3861538/48936308.py:18: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  query_id[row[0]] =  row[1]\n"
     ]
    }
   ],
   "source": [
    "qd_df = qd_df[['qid','query','generation_gt','retrieval_gt']]\n",
    "\n",
    "query_id = {}\n",
    "for idx, row in qd_df.iterrows():\n",
    "    query_id[row[0]] =  row[1]\n",
    "\n",
    "qrel = qd_df[['qid','retrieval_gt']]\n",
    "qrel_id = {}\n",
    "for idx, row in qrel.iterrows():\n",
    "    q_id = row.iloc[0]\n",
    "    relevant_copus_id = row.iloc[1]\n",
    "    if q_id not in qrel_id:\n",
    "        qrel_id[q_id] = set()\n",
    "    qrel_id[q_id].add(relevant_copus_id)\n",
    "\n",
    "corpus_df = corpus_df[['doc_id','contents']]\n",
    "\n",
    "valid_dict = {}\n",
    "valid_dict['qrel'] =qrel_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "doc_id                          commerce - B2BDigComm.pdf - 1\n",
       "contents    Adobe\\n디지털 커머스 시대,\\nB2B 비즈니스 생존 전략\\nB2B 비즈니스를 ...\n",
       "Name: 0, dtype: object"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "corpus_df.iloc[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['qid', 'query', 'generation_gt', 'retrieval_gt'], dtype='object')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qd_df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "corpus_df = corpus_df.reset_index(drop=True)\n",
    "qd_df = qd_df.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_accuracy(ranks_list, valid_dict, qd_df, k_values=[1, 3, 5]):\n",
    "    accuracies = {k: 0 for k in k_values}\n",
    "    total_queries = len(qd_df)\n",
    "    \n",
    "    for i in range(total_queries):\n",
    "        search_idx = ranks_list[i]\n",
    "        true_doc_idx = corpus_df[corpus_df['doc_id'] == list(valid_dict['qrel'][qd_df.loc[i, 'qid']])[0]].index[0]\n",
    "        \n",
    "        for k in k_values:\n",
    "            top_k_preds = search_idx[:k]\n",
    "            if true_doc_idx in top_k_preds:\n",
    "                accuracies[k] += 1\n",
    "    \n",
    "    return {k: accuracies[k] / total_queries for k in k_values}\n",
    "\n",
    "def calculate_f1_recall_precision(ranks_list, valid_dict, qd_df, k_values=[1, 3, 5]):\n",
    "    f1_scores = {k: 0 for k in k_values}\n",
    "    recall_scores = {k: 0 for k in k_values}\n",
    "    precision_scores = {k: 0 for k in k_values}\n",
    "    \n",
    "    total_queries = len(qd_df)\n",
    "    \n",
    "    for i in range(total_queries):\n",
    "        search_idx = ranks_list[i]\n",
    "        true_doc_idx = corpus_df[corpus_df['doc_id'] == list(valid_dict['qrel'][qd_df.loc[i, 'qid']])[0]].index[0]\n",
    "        \n",
    "        for k in k_values:\n",
    "            top_k_preds = search_idx[:k]\n",
    "            y_true = [1 if idx == true_doc_idx else 0 for idx in top_k_preds]\n",
    "            y_pred = [1] * len(top_k_preds)\n",
    "            \n",
    "            # Precision, Recall, F1\n",
    "            precision_scores[k] += precision_score(y_true, y_pred)\n",
    "            recall_scores[k] += recall_score(y_true, y_pred)\n",
    "            f1_scores[k] += f1_score(y_true, y_pred)\n",
    "    \n",
    "    return {k: f1_scores[k] / total_queries for k in k_values}, \\\n",
    "           {k: recall_scores[k] / total_queries for k in k_values}, \\\n",
    "           {k: precision_scores[k] / total_queries for k in k_values}\n",
    "\n",
    "\n",
    "def evaluate_model(corpus_df, qd_df, valid_dict, reranker):\n",
    "    scores_list = []\n",
    "    ranks_list = []\n",
    "    \n",
    "    for c, query in enumerate(qd_df['query'], start=1):\n",
    "        corpus_df['query'] = query\n",
    "        pair_df = corpus_df[['query', 'contents']]\n",
    "        scores = reranker.compute_score(pair_df.values.tolist(), normalize=True)\n",
    "        scores = np.array(scores)\n",
    "        \n",
    "        sorted_idxs = np.argsort(-scores)\n",
    "        scores_list.append(scores[sorted_idxs])\n",
    "        ranks_list.append(sorted_idxs)\n",
    "        print(f'{c}/{len(qd_df)}')\n",
    "\n",
    "    k_values = [1, 3, 5, 10]\n",
    "    accuracies = calculate_accuracy(ranks_list, valid_dict, qd_df, k_values=k_values)\n",
    "    f1_scores, recalls, precisions = calculate_f1_recall_precision(ranks_list, valid_dict, qd_df, k_values=k_values)\n",
    "    \n",
    "    return accuracies, f1_scores, recalls, precisions\n",
    "\n",
    "\n",
    "# 모델 평가 \n",
    "reranker = FlagReranker(model_path, use_fp16=True)\n",
    "\n",
    "accuracies, f1_scores, recalls, precisions = evaluate_model(\n",
    "    corpus_df.copy(), qd_df, valid_dict, reranker)\n",
    "\n",
    "print(f'Model: {model_path}')\n",
    "for k in [1, 3, 5, 10]:\n",
    "    print(f'Accuracy@{k}: {accuracies[k]:.4f}')\n",
    "    print(f'F1@{k}: {f1_scores[k]:.4f}')\n",
    "    print(f'Recall@{k}: {recalls[k]:.4f}')\n",
    "    print(f'Precision@{k}: {precisions[k]:.4f}')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "sbert3",
   "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.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}