File size: 5,892 Bytes
55500d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# η”Ÿζˆζ•ˆζžœζŠ½ζŸ₯"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 40/40 [00:00<00:00, 171.67it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{\"Q\": \"What color is the reporter's coat during the interview?',\n",
      "  'Options': [\n",
      "      'A. Red.',\n",
      "      'B. Black.',\n",
      "      'C. Blue.',\n",
      "      'D. White.'\n",
      "  ],\n",
      "  'Answer': 'B\"}]\n",
      "[{\"Q\": \"What color is the man's jacket in the video?', 'Options': ['A. Red.', 'B. Blue.', 'C. Green.', 'D. Black.'], 'Answer': 'B\"}]\n",
      "[{\"Q\": \"What is the person's hands manipulating in the video clip?', 'Options': ['A. A small electronic component.', 'B. A large mechanical part.', 'C. A piece of paper.', 'D. A tool from the workspace.'], 'Answer': 'A\"}]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import os\n",
    "import random\n",
    "import shutil\n",
    "from tqdm import tqdm\n",
    "import re\n",
    "\n",
    "def parse(generated_text):\n",
    "    generated_text = generated_text.strip()\n",
    "    if \"```json\" in generated_text:\n",
    "        generated_text = re.sub(r\"^```json\\s*|\\s*```$\", \"\", generated_text.strip())\n",
    "    try:\n",
    "        data = eval(generated_text)\n",
    "    except:\n",
    "        generated_text = generated_text.replace('\\'Q\\': \\'', \"\\\"Q\\\": \\\"\").replace('\\', \\'A\\': \\'', \"\\\", \\\"A\\\": \\\"\").replace('\\'}', \"\\\"}\")\n",
    "        print(generated_text)\n",
    "        data = eval(generated_text)\n",
    "\n",
    "    return data\n",
    "\n",
    "def load_jsonl(path):\n",
    "    datas = []\n",
    "    with open(path, 'r') as file:\n",
    "        for line in file:\n",
    "            data = json.loads(line)\n",
    "            datas.append(data)\n",
    "    return datas\n",
    "\n",
    "\n",
    "results_dir = '/share/minghao/VideoProjects/Sythesis2/ObjectRecognition/Results/object_0_40'\n",
    "file_names = os.listdir(results_dir)\n",
    "all_datas = []\n",
    "for name in file_names:\n",
    "    file_path = os.path.join(results_dir, name)\n",
    "    if os.path.isdir(file_path):\n",
    "        continue\n",
    "    datas = load_jsonl(file_path)\n",
    "    all_datas.extend(datas)\n",
    "\n",
    "total_size = len(all_datas)\n",
    "check_size = total_size//10\n",
    "check_size = 40\n",
    "check_datas = random.sample(all_datas, check_size)\n",
    "observation_dir = '/share/minghao/VideoProjects/Sythesis2/ObjectRecognition/Observations'\n",
    "\n",
    "for data in tqdm(check_datas):\n",
    "    video_id = data['video_id']\n",
    "    generated_text = data['generated_qa']\n",
    "    try:\n",
    "        result = parse(generated_text)\n",
    "    except:\n",
    "        continue\n",
    "    with open(os.path.join(observation_dir, video_id+'.json'), 'w') as file:\n",
    "        json.dump(result, file, indent=4)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 2700/2700 [00:00<00:00, 1360151.43it/s]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "from tqdm import tqdm\n",
    "\n",
    "path = '/share/minghao/VideoProjects/VideoChat-Flash/lmms-eval_videochat/videochat-flash-7B@448_eval_log_videomme.json'\n",
    "\n",
    "with open(path, 'r') as f:\n",
    "    datas = json.load(f)\n",
    "\n",
    "score_task_type = {}\n",
    "score_duration_task_type = {\n",
    "    'short': {},\n",
    "    'medium': {},\n",
    "    'long': {}\n",
    "}\n",
    "\n",
    "record_error = {\n",
    "    'short': [],\n",
    "    'medium': [],\n",
    "    'long': []\n",
    "}\n",
    "\n",
    "for data in tqdm(datas['logs']):\n",
    "    duration = data['doc']['duration']\n",
    "    task_type = data['doc']['task_type']\n",
    "    if task_type != 'Action Reasoning':\n",
    "        continue\n",
    "    pred_answer = data['videomme_percetion_score']['pred_answer']\n",
    "    answer = data['videomme_percetion_score']['answer']\n",
    "    if pred_answer == answer:\n",
    "        correct_flag = True\n",
    "    else:\n",
    "        correct_flag = False\n",
    "\n",
    "    if not correct_flag:\n",
    "        info = {\n",
    "            'url' : data['doc']['url'],\n",
    "            'Q': data['doc']['question'],\n",
    "            'Options': data['doc']['options'],\n",
    "            'Answer': answer,\n",
    "            'pred': pred_answer\n",
    "        }\n",
    "\n",
    "\n",
    "        record_error[duration].append(info)\n",
    "\n",
    "import os\n",
    "\n",
    "ob_dir = '/share/minghao/VideoProjects/Sythesis/ActionReasoning/ErrorObservation'\n",
    "for duration, error_infos in record_error.items():\n",
    "    ob_path = os.path.join(ob_dir, f'{duration}.json')\n",
    "    with open(ob_path, 'w') as file:\n",
    "        json.dump(error_infos, file, indent=4)\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}