GitHub Actions commited on
Commit
621c96d
·
1 Parent(s): ee461be

Sync from GitHub Actions

Browse files
notebooks/01_dataset_experiment.ipynb DELETED
@@ -1,786 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": 1,
6
- "id": "27ea1a10",
7
- "metadata": {},
8
- "outputs": [],
9
- "source": [
10
- "!pip install -q torch torchvision transformers datasets pillow pandas scikit-learn tqdm huggingface_hub matplotlib"
11
- ]
12
- },
13
- {
14
- "cell_type": "code",
15
- "execution_count": 2,
16
- "id": "d246862c",
17
- "metadata": {},
18
- "outputs": [],
19
- "source": [
20
- "import os\n",
21
- "import json\n",
22
- "import random\n",
23
- "import time\n",
24
- "from pathlib import Path\n",
25
- "\n",
26
- "import numpy as np\n",
27
- "import pandas as pd\n",
28
- "import torch\n",
29
- "import torch.nn as nn\n",
30
- "import torch.nn.functional as F\n",
31
- "\n",
32
- "from PIL import Image\n",
33
- "from tqdm.auto import tqdm\n",
34
- "from datasets import load_dataset\n",
35
- "from torch.utils.data import Dataset, DataLoader\n",
36
- "from transformers import CLIPModel, CLIPImageProcessor, get_cosine_schedule_with_warmup\n",
37
- "\n",
38
- "from sklearn.model_selection import train_test_split\n",
39
- "from sklearn.metrics import accuracy_score, f1_score, confusion_matrix, classification_report"
40
- ]
41
- },
42
- {
43
- "cell_type": "markdown",
44
- "id": "7e09b0d2",
45
- "metadata": {},
46
- "source": [
47
- "_Config_"
48
- ]
49
- },
50
- {
51
- "cell_type": "code",
52
- "execution_count": 3,
53
- "id": "697fa289",
54
- "metadata": {},
55
- "outputs": [],
56
- "source": [
57
- "DATASET_NAME = \"ashraq/fashion-product-images-small\"\n",
58
- "MODEL_NAME = \"openai/clip-vit-base-patch32\"\n",
59
- "TASKS = [\n",
60
- " \"gender\",\n",
61
- " \"masterCategory\",\n",
62
- " \"subCategory\",\n",
63
- " \"articleType\",\n",
64
- " \"baseColour\",\n",
65
- " \"season\",\n",
66
- " \"usage\"\n",
67
- "]\n",
68
- "\n",
69
- "SEED = 42\n",
70
- "TRAIN_RATIO = 0.70\n",
71
- "VAL_RATIO = 0.15\n",
72
- "TEST_RATIO = 0.15\n",
73
- "\n",
74
- "BATCH_SIZE = 32\n",
75
- "EPOCHS = 5\n",
76
- "\n",
77
- "HEAD_LR = 3e-4\n",
78
- "BACKBONE_LR = 1e-5\n",
79
- "WEIGHT_DECAY = 1e-2\n",
80
- "\n",
81
- "HIDDEN_DIM = 512\n",
82
- "DROPOUT = 0.20\n",
83
- "\n",
84
- "UNFREEZE_LAST_N_VISION_LAYERS = 2\n",
85
- "\n",
86
- "USE_CLASS_WEIGHTS = True\n",
87
- "USE_AMP = True\n",
88
- "\n",
89
- "MAX_GRAD_NORM = 1.0\n",
90
- "EARLY_STOPPING_PATIENCE = 2\n",
91
- "NUM_WORKERS = 2\n",
92
- "DEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\""
93
- ]
94
- },
95
- {
96
- "cell_type": "code",
97
- "execution_count": 4,
98
- "id": "ef7f600c",
99
- "metadata": {},
100
- "outputs": [
101
- {
102
- "name": "stdout",
103
- "output_type": "stream",
104
- "text": [
105
- "Device: cuda\n",
106
- "Model: openai/clip-vit-base-patch32\n"
107
- ]
108
- }
109
- ],
110
- "source": [
111
- "OUTPUT_DIR = Path(\"artifacts/models/autocatalogai_clip\")\n",
112
- "EVAL_DIR = Path(\"artifacts/evaluation\")\n",
113
- "PLOT_DIR = Path(\"artifacts/plots\")\n",
114
- "PROCESSED_DIR = Path(\"data/processed\")\n",
115
- "\n",
116
- "for directory in [OUTPUT_DIR, EVAL_DIR, PLOT_DIR, PROCESSED_DIR]:\n",
117
- " directory.mkdir(parents=True, exist_ok=True)\n",
118
- "\n",
119
- "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
120
- "\n",
121
- "print(\"Device:\", DEVICE)\n",
122
- "print(\"Model:\", MODEL_NAME)"
123
- ]
124
- },
125
- {
126
- "cell_type": "code",
127
- "execution_count": 5,
128
- "id": "d5242fdc",
129
- "metadata": {},
130
- "outputs": [],
131
- "source": [
132
- "def set_seed(seed):\n",
133
- " random.seed(seed)\n",
134
- " np.random.seed(seed)\n",
135
- " torch.manual_seed(seed)\n",
136
- " \n",
137
- " if torch.cuda.is_available():\n",
138
- " torch.cuda.manual_seed_all(seed)\n",
139
- " torch.backends.cudnn.benchmark = True\n",
140
- "\n",
141
- "\n",
142
- "set_seed(SEED)"
143
- ]
144
- },
145
- {
146
- "cell_type": "markdown",
147
- "id": "f07779e1",
148
- "metadata": {},
149
- "source": [
150
- "#### Load Full Dataset"
151
- ]
152
- },
153
- {
154
- "cell_type": "code",
155
- "execution_count": 6,
156
- "id": "280fb69a",
157
- "metadata": {},
158
- "outputs": [
159
- {
160
- "name": "stderr",
161
- "output_type": "stream",
162
- "text": [
163
- "/usr/local/lib/python3.12/dist-packages/huggingface_hub/utils/_auth.py:138: UserWarning: \n",
164
- "Error while fetching `HF_TOKEN` secret value from your vault: 'Requesting secret HF_TOKEN timed out. Secrets can only be fetched when running from the Colab UI.'.\n",
165
- "You are not authenticated with the Hugging Face Hub in this notebook.\n",
166
- "If the error persists, please let us know by opening an issue on GitHub (https://github.com/huggingface/huggingface_hub/issues/new).\n",
167
- " warnings.warn(\n"
168
- ]
169
- },
170
- {
171
- "data": {
172
- "application/vnd.jupyter.widget-view+json": {
173
- "model_id": "f72208f4dc6348fc87d90c6c22769b0f",
174
- "version_major": 2,
175
- "version_minor": 0
176
- },
177
- "text/plain": [
178
- "README.md: 0%| | 0.00/867 [00:00<?, ?B/s]"
179
- ]
180
- },
181
- "metadata": {},
182
- "output_type": "display_data"
183
- },
184
- {
185
- "name": "stderr",
186
- "output_type": "stream",
187
- "text": [
188
- "Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.\n",
189
- "WARNING:huggingface_hub.utils._http:Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.\n"
190
- ]
191
- },
192
- {
193
- "data": {
194
- "application/vnd.jupyter.widget-view+json": {
195
- "model_id": "ee563a2427bc4eb9b91f160c593b5477",
196
- "version_major": 2,
197
- "version_minor": 0
198
- },
199
- "text/plain": [
200
- "data/train-00000-of-00002-6cff4c59f91661(…): 0%| | 0.00/136M [00:00<?, ?B/s]"
201
- ]
202
- },
203
- "metadata": {},
204
- "output_type": "display_data"
205
- },
206
- {
207
- "data": {
208
- "application/vnd.jupyter.widget-view+json": {
209
- "model_id": "2960cb54d2ba4573a7ef82894e897ad6",
210
- "version_major": 2,
211
- "version_minor": 0
212
- },
213
- "text/plain": [
214
- "data/train-00001-of-00002-bb459e5ac5f01e(…): 0%| | 0.00/135M [00:00<?, ?B/s]"
215
- ]
216
- },
217
- "metadata": {},
218
- "output_type": "display_data"
219
- },
220
- {
221
- "data": {
222
- "application/vnd.jupyter.widget-view+json": {
223
- "model_id": "7aa26aab19ff40e1a74b28de564323e2",
224
- "version_major": 2,
225
- "version_minor": 0
226
- },
227
- "text/plain": [
228
- "Generating train split: 0%| | 0/44072 [00:00<?, ? examples/s]"
229
- ]
230
- },
231
- "metadata": {},
232
- "output_type": "display_data"
233
- },
234
- {
235
- "name": "stdout",
236
- "output_type": "stream",
237
- "text": [
238
- "Dataset({\n",
239
- " features: ['id', 'gender', 'masterCategory', 'subCategory', 'articleType', 'baseColour', 'season', 'year', 'usage', 'productDisplayName', 'image'],\n",
240
- " num_rows: 44072\n",
241
- "})\n",
242
- "['id', 'gender', 'masterCategory', 'subCategory', 'articleType', 'baseColour', 'season', 'year', 'usage', 'productDisplayName', 'image']\n",
243
- "Total rows: 44072\n"
244
- ]
245
- }
246
- ],
247
- "source": [
248
- "raw_dataset = load_dataset(DATASET_NAME, split=\"train\")\n",
249
- "\n",
250
- "print(raw_dataset)\n",
251
- "print(raw_dataset.column_names)\n",
252
- "print(\"Total rows:\", len(raw_dataset))"
253
- ]
254
- },
255
- {
256
- "cell_type": "markdown",
257
- "id": "5edb84b5",
258
- "metadata": {},
259
- "source": [
260
- "#### Clean Dataset"
261
- ]
262
- },
263
- {
264
- "cell_type": "code",
265
- "execution_count": 7,
266
- "id": "bdf04d04",
267
- "metadata": {},
268
- "outputs": [],
269
- "source": [
270
- "missing_columns = [task for task in TASKS if task not in raw_dataset.column_names]\n",
271
- "\n",
272
- "if \"image\" not in raw_dataset.column_names:\n",
273
- " raise ValueError(f\"Dataset must contain image column. Found: {raw_dataset.column_names}\")\n",
274
- "\n",
275
- "if missing_columns:\n",
276
- " raise ValueError(f\"Missing task columns: {missing_columns}\")"
277
- ]
278
- },
279
- {
280
- "cell_type": "code",
281
- "execution_count": 8,
282
- "id": "721ec563",
283
- "metadata": {},
284
- "outputs": [
285
- {
286
- "data": {
287
- "application/vnd.jupyter.widget-view+json": {
288
- "model_id": "49bb2b38bea040a5896ceaf814335e21",
289
- "version_major": 2,
290
- "version_minor": 0
291
- },
292
- "text/plain": [
293
- "Filter: 0%| | 0/44072 [00:00<?, ? examples/s]"
294
- ]
295
- },
296
- "metadata": {},
297
- "output_type": "display_data"
298
- },
299
- {
300
- "name": "stdout",
301
- "output_type": "stream",
302
- "text": [
303
- "Before cleaning: 44072\n",
304
- "After cleaning: 44072\n"
305
- ]
306
- }
307
- ],
308
- "source": [
309
- "def is_valid_row(row):\n",
310
- " for task in TASKS:\n",
311
- " value = row.get(task)\n",
312
- " if value is None:\n",
313
- " return False\n",
314
- " if str(value).strip() == \"\":\n",
315
- " return False\n",
316
- " \n",
317
- " return row.get(\"image\") is not None\n",
318
- "\n",
319
- "\n",
320
- "clean_dataset = raw_dataset.filter(is_valid_row)\n",
321
- "print(\"Before cleaning:\", len(raw_dataset))\n",
322
- "print(\"After cleaning:\", len(clean_dataset))"
323
- ]
324
- },
325
- {
326
- "cell_type": "markdown",
327
- "id": "59e5f003",
328
- "metadata": {},
329
- "source": [
330
- "#### Create Metadata DataFrame"
331
- ]
332
- },
333
- {
334
- "cell_type": "code",
335
- "execution_count": 9,
336
- "id": "17fecce2",
337
- "metadata": {},
338
- "outputs": [
339
- {
340
- "data": {
341
- "text/html": [
342
- "\n",
343
- " <div id=\"df-819ad3e2-5ed3-4458-823f-695ea26d18b3\" class=\"colab-df-container\">\n",
344
- " <div>\n",
345
- "<style scoped>\n",
346
- " .dataframe tbody tr th:only-of-type {\n",
347
- " vertical-align: middle;\n",
348
- " }\n",
349
- "\n",
350
- " .dataframe tbody tr th {\n",
351
- " vertical-align: top;\n",
352
- " }\n",
353
- "\n",
354
- " .dataframe thead th {\n",
355
- " text-align: right;\n",
356
- " }\n",
357
- "</style>\n",
358
- "<table border=\"1\" class=\"dataframe\">\n",
359
- " <thead>\n",
360
- " <tr style=\"text-align: right;\">\n",
361
- " <th></th>\n",
362
- " <th>gender</th>\n",
363
- " <th>masterCategory</th>\n",
364
- " <th>subCategory</th>\n",
365
- " <th>articleType</th>\n",
366
- " <th>baseColour</th>\n",
367
- " <th>season</th>\n",
368
- " <th>usage</th>\n",
369
- " <th>id</th>\n",
370
- " <th>productDisplayName</th>\n",
371
- " <th>dataset_idx</th>\n",
372
- " </tr>\n",
373
- " </thead>\n",
374
- " <tbody>\n",
375
- " <tr>\n",
376
- " <th>0</th>\n",
377
- " <td>Men</td>\n",
378
- " <td>Apparel</td>\n",
379
- " <td>Topwear</td>\n",
380
- " <td>Shirts</td>\n",
381
- " <td>Navy Blue</td>\n",
382
- " <td>Fall</td>\n",
383
- " <td>Casual</td>\n",
384
- " <td>15970</td>\n",
385
- " <td>Turtle Check Men Navy Blue Shirt</td>\n",
386
- " <td>0</td>\n",
387
- " </tr>\n",
388
- " <tr>\n",
389
- " <th>1</th>\n",
390
- " <td>Men</td>\n",
391
- " <td>Apparel</td>\n",
392
- " <td>Bottomwear</td>\n",
393
- " <td>Jeans</td>\n",
394
- " <td>Blue</td>\n",
395
- " <td>Summer</td>\n",
396
- " <td>Casual</td>\n",
397
- " <td>39386</td>\n",
398
- " <td>Peter England Men Party Blue Jeans</td>\n",
399
- " <td>1</td>\n",
400
- " </tr>\n",
401
- " <tr>\n",
402
- " <th>2</th>\n",
403
- " <td>Women</td>\n",
404
- " <td>Accessories</td>\n",
405
- " <td>Watches</td>\n",
406
- " <td>Watches</td>\n",
407
- " <td>Silver</td>\n",
408
- " <td>Winter</td>\n",
409
- " <td>Casual</td>\n",
410
- " <td>59263</td>\n",
411
- " <td>Titan Women Silver Watch</td>\n",
412
- " <td>2</td>\n",
413
- " </tr>\n",
414
- " <tr>\n",
415
- " <th>3</th>\n",
416
- " <td>Men</td>\n",
417
- " <td>Apparel</td>\n",
418
- " <td>Bottomwear</td>\n",
419
- " <td>Track Pants</td>\n",
420
- " <td>Black</td>\n",
421
- " <td>Fall</td>\n",
422
- " <td>Casual</td>\n",
423
- " <td>21379</td>\n",
424
- " <td>Manchester United Men Solid Black Track Pants</td>\n",
425
- " <td>3</td>\n",
426
- " </tr>\n",
427
- " <tr>\n",
428
- " <th>4</th>\n",
429
- " <td>Men</td>\n",
430
- " <td>Apparel</td>\n",
431
- " <td>Topwear</td>\n",
432
- " <td>Tshirts</td>\n",
433
- " <td>Grey</td>\n",
434
- " <td>Summer</td>\n",
435
- " <td>Casual</td>\n",
436
- " <td>53759</td>\n",
437
- " <td>Puma Men Grey T-shirt</td>\n",
438
- " <td>4</td>\n",
439
- " </tr>\n",
440
- " </tbody>\n",
441
- "</table>\n",
442
- "</div>\n",
443
- " <div class=\"colab-df-buttons\">\n",
444
- " \n",
445
- " <div class=\"colab-df-container\">\n",
446
- " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-819ad3e2-5ed3-4458-823f-695ea26d18b3')\"\n",
447
- " title=\"Convert this dataframe to an interactive table.\"\n",
448
- " style=\"display:none;\">\n",
449
- " \n",
450
- " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
451
- " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
452
- " </svg>\n",
453
- " </button>\n",
454
- " \n",
455
- " <style>\n",
456
- " .colab-df-container {\n",
457
- " display:flex;\n",
458
- " gap: 12px;\n",
459
- " }\n",
460
- "\n",
461
- " .colab-df-convert {\n",
462
- " background-color: #E8F0FE;\n",
463
- " border: none;\n",
464
- " border-radius: 50%;\n",
465
- " cursor: pointer;\n",
466
- " display: none;\n",
467
- " fill: #1967D2;\n",
468
- " height: 32px;\n",
469
- " padding: 0 0 0 0;\n",
470
- " width: 32px;\n",
471
- " }\n",
472
- "\n",
473
- " .colab-df-convert:hover {\n",
474
- " background-color: #E2EBFA;\n",
475
- " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
476
- " fill: #174EA6;\n",
477
- " }\n",
478
- "\n",
479
- " .colab-df-buttons div {\n",
480
- " margin-bottom: 4px;\n",
481
- " }\n",
482
- "\n",
483
- " [theme=dark] .colab-df-convert {\n",
484
- " background-color: #3B4455;\n",
485
- " fill: #D2E3FC;\n",
486
- " }\n",
487
- "\n",
488
- " [theme=dark] .colab-df-convert:hover {\n",
489
- " background-color: #434B5C;\n",
490
- " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
491
- " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
492
- " fill: #FFFFFF;\n",
493
- " }\n",
494
- " </style>\n",
495
- "\n",
496
- " <script>\n",
497
- " const buttonEl =\n",
498
- " document.querySelector('#df-819ad3e2-5ed3-4458-823f-695ea26d18b3 button.colab-df-convert');\n",
499
- " buttonEl.style.display =\n",
500
- " google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
501
- "\n",
502
- " async function convertToInteractive(key) {\n",
503
- " const element = document.querySelector('#df-819ad3e2-5ed3-4458-823f-695ea26d18b3');\n",
504
- " const dataTable =\n",
505
- " await google.colab.kernel.invokeFunction('convertToInteractive',\n",
506
- " [key], {});\n",
507
- " if (!dataTable) return;\n",
508
- "\n",
509
- " const docLinkHtml = 'Like what you see? Visit the ' +\n",
510
- " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
511
- " + ' to learn more about interactive tables.';\n",
512
- " element.innerHTML = '';\n",
513
- " dataTable['output_type'] = 'display_data';\n",
514
- " await google.colab.output.renderOutput(dataTable, element);\n",
515
- " const docLink = document.createElement('div');\n",
516
- " docLink.innerHTML = docLinkHtml;\n",
517
- " element.appendChild(docLink);\n",
518
- " }\n",
519
- " </script>\n",
520
- " </div>\n",
521
- " \n",
522
- " </div>\n",
523
- " </div>\n",
524
- " "
525
- ],
526
- "text/plain": [
527
- " gender masterCategory subCategory articleType baseColour season usage \\\n",
528
- "0 Men Apparel Topwear Shirts Navy Blue Fall Casual \n",
529
- "1 Men Apparel Bottomwear Jeans Blue Summer Casual \n",
530
- "2 Women Accessories Watches Watches Silver Winter Casual \n",
531
- "3 Men Apparel Bottomwear Track Pants Black Fall Casual \n",
532
- "4 Men Apparel Topwear Tshirts Grey Summer Casual \n",
533
- "\n",
534
- " id productDisplayName dataset_idx \n",
535
- "0 15970 Turtle Check Men Navy Blue Shirt 0 \n",
536
- "1 39386 Peter England Men Party Blue Jeans 1 \n",
537
- "2 59263 Titan Women Silver Watch 2 \n",
538
- "3 21379 Manchester United Men Solid Black Track Pants 3 \n",
539
- "4 53759 Puma Men Grey T-shirt 4 "
540
- ]
541
- },
542
- "execution_count": 9,
543
- "metadata": {},
544
- "output_type": "execute_result"
545
- }
546
- ],
547
- "source": [
548
- "metadata = {}\n",
549
- "for_col = []\n",
550
- "extra_columns = [\"id\", \"productDisplayName\"]\n",
551
- "\n",
552
- "for task in TASKS:\n",
553
- " metadata[task] = [str(value).strip() for value in clean_dataset[task]]\n",
554
- "\n",
555
- "\n",
556
- "for col in extra_columns:\n",
557
- " if col in clean_dataset.column_names:\n",
558
- " metadata[col] = clean_dataset[col]\n",
559
- " for_col.append(col)\n",
560
- "\n",
561
- "df = pd.DataFrame(metadata)\n",
562
- "df[\"dataset_idx\"] = np.arange(len(clean_dataset))\n",
563
- "\n",
564
- "df.head()"
565
- ]
566
- },
567
- {
568
- "cell_type": "markdown",
569
- "id": "0d55ac03",
570
- "metadata": {},
571
- "source": [
572
- "#### Label Distribution"
573
- ]
574
- },
575
- {
576
- "cell_type": "code",
577
- "execution_count": 10,
578
- "id": "5fe58be4",
579
- "metadata": {},
580
- "outputs": [],
581
- "source": [
582
- "label_distribution = {}\n",
583
- "\n",
584
- "for task in TASKS:\n",
585
- " counts = df[task].value_counts().to_dict()\n",
586
- " label_distribution[task] = counts\n",
587
- " \n",
588
- "with open(EVAL_DIR / \"label_distribution.json\", \"w\", encoding=\"utf-8\") as f:\n",
589
- " json.dump(label_distribution, f, indent=2, ensure_ascii=False)"
590
- ]
591
- },
592
- {
593
- "cell_type": "code",
594
- "execution_count": 11,
595
- "id": "f77e1f0d",
596
- "metadata": {},
597
- "outputs": [
598
- {
599
- "data": {
600
- "text/plain": [
601
- "{'dataset_name': 'ashraq/fashion-product-images-small',\n",
602
- " 'total_clean_samples': 44072,\n",
603
- " 'tasks': ['gender',\n",
604
- " 'masterCategory',\n",
605
- " 'subCategory',\n",
606
- " 'articleType',\n",
607
- " 'baseColour',\n",
608
- " 'season',\n",
609
- " 'usage'],\n",
610
- " 'num_classes': {'gender': 5,\n",
611
- " 'masterCategory': 7,\n",
612
- " 'subCategory': 45,\n",
613
- " 'articleType': 141,\n",
614
- " 'baseColour': 46,\n",
615
- " 'season': 4,\n",
616
- " 'usage': 8}}"
617
- ]
618
- },
619
- "execution_count": 11,
620
- "metadata": {},
621
- "output_type": "execute_result"
622
- }
623
- ],
624
- "source": [
625
- "summary = {\n",
626
- " \"dataset_name\": DATASET_NAME,\n",
627
- " \"total_clean_samples\": len(df),\n",
628
- " \"tasks\": TASKS,\n",
629
- " \"num_classes\": {\n",
630
- " task: int(df[task].nunique())\n",
631
- " for task in TASKS\n",
632
- " }\n",
633
- "}\n",
634
- "\n",
635
- "with open(EVAL_DIR / \"dataset_summary.json\", \"w\", encoding=\"utf-8\") as f:\n",
636
- " json.dump(summary, f, indent=2, ensure_ascii=False)\n",
637
- "\n",
638
- "summary"
639
- ]
640
- },
641
- {
642
- "cell_type": "markdown",
643
- "id": "b73fb107",
644
- "metadata": {},
645
- "source": [
646
- "#### Train / Validation / Test Split"
647
- ]
648
- },
649
- {
650
- "cell_type": "code",
651
- "execution_count": 12,
652
- "id": "ff328f23",
653
- "metadata": {},
654
- "outputs": [],
655
- "source": [
656
- "def make_safe_stratify_labels(series):\n",
657
- " counts = series.value_counts()\n",
658
- " return series.apply(lambda x: x if counts[x] >= 2 else \"__rare__\")\n",
659
- "\n",
660
- "stratify_labels = make_safe_stratify_labels(df[\"articleType\"])\n",
661
- "all_indices = df.index.to_numpy()"
662
- ]
663
- },
664
- {
665
- "cell_type": "code",
666
- "execution_count": 13,
667
- "id": "c876c207",
668
- "metadata": {},
669
- "outputs": [],
670
- "source": [
671
- "train_idx, temp_idx = train_test_split(\n",
672
- " all_indices,\n",
673
- " test_size=0.30,\n",
674
- " random_state=SEED,\n",
675
- " stratify=stratify_labels\n",
676
- ")\n",
677
- "\n",
678
- "temp_df = df.loc[temp_idx].copy()\n",
679
- "temp_stratify_labels = make_safe_stratify_labels(temp_df[\"articleType\"])"
680
- ]
681
- },
682
- {
683
- "cell_type": "code",
684
- "execution_count": 14,
685
- "id": "0ffaad6a",
686
- "metadata": {},
687
- "outputs": [],
688
- "source": [
689
- "val_idx, test_idx = train_test_split(\n",
690
- " temp_idx,\n",
691
- " test_size=0.50,\n",
692
- " random_state=SEED,\n",
693
- " stratify=temp_stratify_labels\n",
694
- ")\n",
695
- "\n",
696
- "train_df = df.loc[train_idx].copy()\n",
697
- "val_df = df.loc[val_idx].copy()\n",
698
- "test_df = df.loc[test_idx].copy()\n",
699
- "\n",
700
- "train_df[\"split\"] = \"train\"\n",
701
- "val_df[\"split\"] = \"validation\"\n",
702
- "test_df[\"split\"] = \"test\"\n",
703
- "\n",
704
- "train_df.to_csv(PROCESSED_DIR / \"train.csv\", index=False)\n",
705
- "val_df.to_csv(PROCESSED_DIR / \"val.csv\", index=False)\n",
706
- "test_df.to_csv(PROCESSED_DIR / \"test.csv\", index=False)"
707
- ]
708
- },
709
- {
710
- "cell_type": "code",
711
- "execution_count": 15,
712
- "id": "0bd769f2",
713
- "metadata": {},
714
- "outputs": [
715
- {
716
- "name": "stdout",
717
- "output_type": "stream",
718
- "text": [
719
- "Train: 30850 0.7\n",
720
- "Validation: 6611 0.15\n",
721
- "Test: 6611 0.15\n"
722
- ]
723
- }
724
- ],
725
- "source": [
726
- "print(\"Train:\", len(train_df), round(len(train_df) / len(df), 3))\n",
727
- "print(\"Validation:\", len(val_df), round(len(val_df) / len(df), 3))\n",
728
- "print(\"Test:\", len(test_df), round(len(test_df) / len(df), 3))"
729
- ]
730
- },
731
- {
732
- "cell_type": "markdown",
733
- "id": "1b2990da",
734
- "metadata": {},
735
- "source": [
736
- "#### Build HF Split Datasets"
737
- ]
738
- },
739
- {
740
- "cell_type": "code",
741
- "execution_count": 16,
742
- "id": "c42e21e5",
743
- "metadata": {},
744
- "outputs": [
745
- {
746
- "data": {
747
- "text/plain": [
748
- "(30850, 6611, 6611)"
749
- ]
750
- },
751
- "execution_count": 16,
752
- "metadata": {},
753
- "output_type": "execute_result"
754
- }
755
- ],
756
- "source": [
757
- "train_hf_dataset = clean_dataset.select(train_df[\"dataset_idx\"].tolist())\n",
758
- "val_hf_dataset = clean_dataset.select(val_df[\"dataset_idx\"].tolist())\n",
759
- "test_hf_dataset = clean_dataset.select(test_df[\"dataset_idx\"].tolist())\n",
760
- "\n",
761
- "len(train_hf_dataset), len(val_hf_dataset), len(test_hf_dataset)"
762
- ]
763
- }
764
- ],
765
- "metadata": {
766
- "kernelspec": {
767
- "display_name": "Python 3 (ipykernel)",
768
- "language": "python",
769
- "name": "python3"
770
- },
771
- "language_info": {
772
- "codemirror_mode": {
773
- "name": "ipython",
774
- "version": 3
775
- },
776
- "file_extension": ".py",
777
- "mimetype": "text/x-python",
778
- "name": "python",
779
- "nbconvert_exporter": "python",
780
- "pygments_lexer": "ipython3",
781
- "version": "3.12.13"
782
- }
783
- },
784
- "nbformat": 4,
785
- "nbformat_minor": 5
786
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
notebooks/autocatalog-v2.ipynb ADDED
The diff for this file is too large to render. See raw diff