File size: 23,404 Bytes
c254ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "aa923c26-81c8-4565-9277-1cb686e3702e",
      "metadata": {
        "id": "aa923c26-81c8-4565-9277-1cb686e3702e"
      },
      "source": [
        "# VOC Exploration Example\n",
        "<div align=\"center\">\n",
        "\n",
        "  <a href=\"https://ultralytics.com/yolov8\" target=\"_blank\">\n",
        "    <img width=\"1024\", src=\"https://raw.githubusercontent.com/ultralytics/assets/main/yolov8/banner-yolov8.png\"></a>\n",
        "\n",
        "  [中文](https://docs.ultralytics.com/zh/) | [한국어](https://docs.ultralytics.com/ko/) | [日本語](https://docs.ultralytics.com/ja/) | [Русский](https://docs.ultralytics.com/ru/) | [Deutsch](https://docs.ultralytics.com/de/) | [Français](https://docs.ultralytics.com/fr/) | [Español](https://docs.ultralytics.com/es/) | [Português](https://docs.ultralytics.com/pt/) | [हिन्दी](https://docs.ultralytics.com/hi/) | [العربية](https://docs.ultralytics.com/ar/)\n",
        "\n",
        "  <a href=\"https://console.paperspace.com/github/ultralytics/ultralytics\"><img src=\"https://assets.paperspace.io/img/gradient-badge.svg\" alt=\"Run on Gradient\"/></a>\n",
        "  <a href=\"https://colab.research.google.com/github/ultralytics/ultralytics/blob/main/examples/tutorial.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
        "  <a href=\"https://www.kaggle.com/ultralytics/yolov8\"><img src=\"https://kaggle.com/static/images/open-in-kaggle.svg\" alt=\"Open In Kaggle\"></a>\n",
        "\n",
        "Welcome to the Ultralytics Explorer API notebook!  This notebook serves as the starting point for exploring the various resources available to help you get started with using Ultralytics to explore your datasets using with the power of semantic search. You can utilities out of the box that allow you to examine specific types of labels using vector search or even SQL queries.\n",
        "\n",
        "We hope that the resources in this notebook will help you get the most out of Ultralytics. Please browse the Explorer <a href=\"https://docs.ultralytics.com/\">Docs</a> for details, raise an issue on <a href=\"https://github.com/ultralytics/ultralytics\">GitHub</a> for support, and join our <a href=\"https://ultralytics.com/discord\">Discord</a> community for questions and discussions!\n",
        "\n",
        "Try `yolo explorer` powered by Exlorer API\n",
        "\n",
        "Simply `pip install ultralytics` and run `yolo explorer` in your terminal to run custom queries and semantic search on your datasets right inside your browser!\n",
        "\n",
        "</div>"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "2454d9ba-9db4-4b37-98e8-201ba285c92f",
      "metadata": {
        "id": "2454d9ba-9db4-4b37-98e8-201ba285c92f"
      },
      "source": [
        "## Setup\n",
        "Pip install `ultralytics` and [dependencies](https://github.com/ultralytics/ultralytics/blob/main/pyproject.toml) and check software and hardware."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "433f3a4d-a914-42cb-b0b6-be84a84e5e41",
      "metadata": {
        "id": "433f3a4d-a914-42cb-b0b6-be84a84e5e41"
      },
      "outputs": [],
      "source": [
        "%pip install ultralytics[explorer] openai\n",
        "import ultralytics\n",
        "ultralytics.checks()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ae602549-3419-4909-9f82-35cba515483f",
      "metadata": {
        "id": "ae602549-3419-4909-9f82-35cba515483f"
      },
      "outputs": [],
      "source": [
        "from ultralytics import Explorer"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "d8c06350-be8e-45cf-b3a6-b5017bbd943c",
      "metadata": {
        "id": "d8c06350-be8e-45cf-b3a6-b5017bbd943c"
      },
      "source": [
        "## Similarity search\n",
        "Utilize the power of vector similarity search to find the similar data points in your dataset along with their distance in the embedding space. Simply create an embeddings table for the given dataset-model pair. It is only needed once and it is reused automatically.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "334619da-6deb-4b32-9fe0-74e0a79cee20",
      "metadata": {
        "id": "334619da-6deb-4b32-9fe0-74e0a79cee20"
      },
      "outputs": [],
      "source": [
        "exp = Explorer(\"VOC.yaml\", model=\"yolov8n.pt\")\n",
        "exp.create_embeddings_table()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "b6c5e42d-bc7e-4b4c-bde0-643072a2165d",
      "metadata": {
        "id": "b6c5e42d-bc7e-4b4c-bde0-643072a2165d"
      },
      "source": [
        "One the embeddings table is built, you can get run semantic search in any of the following ways:\n",
        "- On a given index / list of indices in the dataset like - `exp.get_similar(idx=[1,10], limit=10)`\n",
        "- On any image/ list of images not in the dataset  - `exp.get_similar(img=[\"path/to/img1\", \"path/to/img2\"], limit=10)`\n",
        "In case of multiple inputs, the aggregade of their embeddings is used.\n",
        "\n",
        "You get a pandas dataframe with the `limit` number of most similar data points to the input, along with their distance in the embedding space. You can use this dataset to perform further filtering\n",
        "<img width=\"1120\" alt=\"Screenshot 2024-01-06 at 9 45 42 PM\" src=\"https://github.com/AyushExel/assets/assets/15766192/7742ac57-e22a-4cea-a0f9-2b2a257483c5\">\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b485f05b-d92d-42bc-8da7-5e361667b341",
      "metadata": {
        "id": "b485f05b-d92d-42bc-8da7-5e361667b341"
      },
      "outputs": [],
      "source": [
        "similar = exp.get_similar(idx=1, limit=10)\n",
        "similar.head()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "acf4b489-2161-4176-a1fe-d1d067d8083d",
      "metadata": {
        "id": "acf4b489-2161-4176-a1fe-d1d067d8083d"
      },
      "source": [
        "You can use the also plot the similar samples directly using the `plot_similar` util\n",
        "<p>\n",
        "\n",
        " <img src=\"https://github.com/AyushExel/assets/assets/15766192/a3c9247b-9271-47df-aaa5-36d96c5034b1\" />\n",
        "</p>\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9dbfe7d0-8613-4529-adb6-6e0632d7cce7",
      "metadata": {
        "id": "9dbfe7d0-8613-4529-adb6-6e0632d7cce7"
      },
      "outputs": [],
      "source": [
        "exp.plot_similar(idx=6500, limit=20)\n",
        "#exp.plot_similar(idx=[100,101], limit=10) # Can also pass list of idxs or imgs\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "260e09bf-4960-4089-a676-cb0e76ff3c0d",
      "metadata": {
        "id": "260e09bf-4960-4089-a676-cb0e76ff3c0d"
      },
      "outputs": [],
      "source": [
        "exp.plot_similar(img=\"https://ultralytics.com/images/bus.jpg\", limit=10, labels=False) # Can also pass any external images\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "faa0b7a7-6318-40e4-b0f4-45a8113bdc3a",
      "metadata": {
        "id": "faa0b7a7-6318-40e4-b0f4-45a8113bdc3a"
      },
      "source": [
        "<p>\n",
        "<img  src=\"https://github.com/AyushExel/assets/assets/15766192/8e011195-b0da-43ef-b3cd-5fb6f383037e\">\n",
        "\n",
        "</p>"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0cea63f1-71f1-46da-af2b-b1b7d8f73553",
      "metadata": {
        "id": "0cea63f1-71f1-46da-af2b-b1b7d8f73553"
      },
      "source": [
        "## 2. Ask AI: Search or filter with Natural Language\n",
        "You can prompt the Explorer object with the kind of data points you want to see and it'll try to return a dataframe with those. Because it is powered by LLMs, it doesn't always get it right. In that case, it'll return None.\n",
        "<p>\n",
        "<img width=\"1131\" alt=\"Screenshot 2024-01-07 at 2 34 53 PM\" src=\"https://github.com/AyushExel/assets/assets/15766192/c4a69fd9-e54f-4d6a-aba5-dc9cfae1bc04\">\n",
        "\n",
        "</p>\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "92fb92ac-7f76-465a-a9ba-ea7492498d9c",
      "metadata": {
        "id": "92fb92ac-7f76-465a-a9ba-ea7492498d9c"
      },
      "outputs": [],
      "source": [
        "df = exp.ask_ai(\"show me images containing more than 10 objects with at least 2 persons\")\n",
        "df.head(5)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f2a7d26e-0ce5-4578-ad1a-b1253805280f",
      "metadata": {
        "id": "f2a7d26e-0ce5-4578-ad1a-b1253805280f"
      },
      "source": [
        "for plotting these results you can use `plot_query_result` util\n",
        "Example:\n",
        "```\n",
        "plt = plot_query_result(exp.ask_ai(\"show me 10 images containing exactly 2 persons\"))\n",
        "Image.fromarray(plt)\n",
        "```\n",
        "<p>\n",
        "    <img src=\"https://github.com/AyushExel/assets/assets/15766192/2cb780de-d05b-4412-a526-7f7f0f10e669\">\n",
        "\n",
        "</p>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b1cfab84-9835-4da0-8e9a-42b30cf84511",
      "metadata": {
        "id": "b1cfab84-9835-4da0-8e9a-42b30cf84511"
      },
      "outputs": [],
      "source": [
        "# plot\n",
        "from ultralytics.data.explorer import plot_query_result\n",
        "from PIL import Image\n",
        "\n",
        "plt = plot_query_result(exp.ask_ai(\"show me 10 images containing exactly 2 persons\"))\n",
        "Image.fromarray(plt)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "35315ae6-d827-40e4-8813-279f97a83b34",
      "metadata": {
        "id": "35315ae6-d827-40e4-8813-279f97a83b34"
      },
      "source": [
        "## 3. Run SQL queries on your Dataset!\n",
        "Sometimes you might want to investigate a certain type of entries in your dataset. For this Explorer allows you to execute SQL queries.\n",
        "It accepts either of the formats:\n",
        "- Queries beginning with \"WHERE\" will automatically select all columns. This can be thought of as a short-hand query\n",
        "- You can also write full queries where you can specify which columns to select\n",
        "\n",
        "This can be used to investigate model performance and specific data points. For example:\n",
        "- let's say your model struggles on images that have humans and dogs. You can write a query like this to select the points that have at least 2 humans AND at least one dog.\n",
        "\n",
        "You can combine SQL query and semantic search to filter down to specific type of results\n",
        "<img width=\"994\" alt=\"Screenshot 2024-01-06 at 9 47 30 PM\" src=\"https://github.com/AyushExel/assets/assets/15766192/92bc3178-c151-4cd5-8007-c76178deb113\">\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8cd1072f-3100-4331-a0e3-4e2f6b1005bf",
      "metadata": {
        "id": "8cd1072f-3100-4331-a0e3-4e2f6b1005bf"
      },
      "outputs": [],
      "source": [
        "table = exp.sql_query(\"WHERE labels LIKE '%person, person%' AND labels LIKE '%dog%' LIMIT 10\")\n",
        "table"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "debf8a00-c9f6-448b-bd3b-454cf62f39ab",
      "metadata": {
        "id": "debf8a00-c9f6-448b-bd3b-454cf62f39ab"
      },
      "source": [
        "Just like similarity search, you also get a util to directly plot the sql queries using `exp.plot_sql_query`\n",
        "<img src=\"https://github.com/AyushExel/assets/assets/15766192/f8b66629-8dd0-419e-8f44-9837969ba678\">\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "18b977e7-d048-4b22-b8c4-084a03b04f23",
      "metadata": {
        "id": "18b977e7-d048-4b22-b8c4-084a03b04f23"
      },
      "outputs": [],
      "source": [
        "exp.plot_sql_query(\"WHERE labels LIKE '%person, person%' AND labels LIKE '%dog%' LIMIT 10\", labels=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f26804c5-840b-4fd1-987f-e362f29e3e06",
      "metadata": {
        "id": "f26804c5-840b-4fd1-987f-e362f29e3e06"
      },
      "source": [
        "## 3. Working with embeddings Table (Advanced)\n",
        "Explorer works on [LanceDB](https://lancedb.github.io/lancedb/) tables internally. You can access this table directly, using `Explorer.table` object and run raw queries, push down pre and post filters, etc."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ea69260a-3407-40c9-9f42-8b34a6e6af7a",
      "metadata": {
        "id": "ea69260a-3407-40c9-9f42-8b34a6e6af7a"
      },
      "outputs": [],
      "source": [
        "table = exp.table\n",
        "table.schema"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "238db292-8610-40b3-9af7-dfd6be174892",
      "metadata": {
        "id": "238db292-8610-40b3-9af7-dfd6be174892"
      },
      "source": [
        "### Run raw queries\n",
        "Vector Search finds the nearest vectors from the database. In a recommendation system or search engine, you can find similar products from the one you searched. In LLM and other AI applications, each data point can be presented by the embeddings generated from some models, it returns the most relevant features.\n",
        "\n",
        "A search in high-dimensional vector space, is to find K-Nearest-Neighbors (KNN) of the query vector.\n",
        "\n",
        "Metric\n",
        "In LanceDB, a Metric is the way to describe the distance between a pair of vectors. Currently, it supports the following metrics:\n",
        "- L2\n",
        "- Cosine\n",
        "- Dot\n",
        "Explorer's similarity search uses L2 by default. You can run queries on tables directly, or use the lance format to build custom utilities to manage datasets. More details on available LanceDB table ops in the [docs](https://lancedb.github.io/lancedb/)\n",
        "\n",
        "<img width=\"1015\" alt=\"Screenshot 2024-01-06 at 9 48 35 PM\" src=\"https://github.com/AyushExel/assets/assets/15766192/a2ccdaf3-8877-4f70-bf47-8a9bd2bb20c0\">\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d74430fe-5aee-45a1-8863-3f2c31338792",
      "metadata": {
        "id": "d74430fe-5aee-45a1-8863-3f2c31338792"
      },
      "outputs": [],
      "source": [
        "dummy_img_embedding = [i for i in range(256)]\n",
        "table.search(dummy_img_embedding).limit(5).to_pandas()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "587486b4-0d19-4214-b994-f032fb2e8eb5",
      "metadata": {
        "id": "587486b4-0d19-4214-b994-f032fb2e8eb5"
      },
      "source": [
        "### Inter-conversion to popular data formats"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "bb2876ea-999b-4eba-96bc-c196ba02c41c",
      "metadata": {
        "id": "bb2876ea-999b-4eba-96bc-c196ba02c41c"
      },
      "outputs": [],
      "source": [
        "df = table.to_pandas()\n",
        "pa_table = table.to_arrow()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "42659d63-ad76-49d6-8dfc-78d77278db72",
      "metadata": {
        "id": "42659d63-ad76-49d6-8dfc-78d77278db72"
      },
      "source": [
        "### Work with Embeddings\n",
        "You can access the raw embedding from lancedb Table and analyse it. The image embeddings are stored in column `vector`"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "66d69e9b-046e-41c8-80d7-c0ee40be3bca",
      "metadata": {
        "id": "66d69e9b-046e-41c8-80d7-c0ee40be3bca"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "embeddings = table.to_pandas()[\"vector\"].tolist()\n",
        "embeddings = np.array(embeddings)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e8df0a49-9596-4399-954b-b8ae1fd7a602",
      "metadata": {
        "id": "e8df0a49-9596-4399-954b-b8ae1fd7a602"
      },
      "source": [
        "### Scatterplot\n",
        "One of the preliminary steps in analysing embeddings is by plotting them in 2D space via dimensionality reduction. Let's try an example\n",
        "\n",
        "<img width=\"646\" alt=\"Screenshot 2024-01-06 at 9 48 58 PM\" src=\"https://github.com/AyushExel/assets/assets/15766192/9e1da25c-face-4426-abc0-2f64a4e4952c\">\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d9a150e8-8092-41b3-82f8-2247f8187fc8",
      "metadata": {
        "id": "d9a150e8-8092-41b3-82f8-2247f8187fc8"
      },
      "outputs": [],
      "source": [
        "!pip install scikit-learn --q"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "196079c3-45a9-4325-81ab-af79a881e37a",
      "metadata": {
        "id": "196079c3-45a9-4325-81ab-af79a881e37a"
      },
      "outputs": [],
      "source": [
        "%matplotlib inline\n",
        "import numpy as np\n",
        "from sklearn.decomposition import PCA\n",
        "import matplotlib.pyplot as plt\n",
        "from mpl_toolkits.mplot3d import Axes3D\n",
        "\n",
        "# Reduce dimensions using PCA to 3 components for visualization in 3D\n",
        "pca = PCA(n_components=3)\n",
        "reduced_data = pca.fit_transform(embeddings)\n",
        "\n",
        "# Create a 3D scatter plot using Matplotlib's Axes3D\n",
        "fig = plt.figure(figsize=(8, 6))\n",
        "ax = fig.add_subplot(111, projection='3d')\n",
        "\n",
        "# Scatter plot\n",
        "ax.scatter(reduced_data[:, 0], reduced_data[:, 1], reduced_data[:, 2], alpha=0.5)\n",
        "ax.set_title('3D Scatter Plot of Reduced 256-Dimensional Data (PCA)')\n",
        "ax.set_xlabel('Component 1')\n",
        "ax.set_ylabel('Component 2')\n",
        "ax.set_zlabel('Component 3')\n",
        "\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "1c843c23-e3f2-490e-8d6c-212fa038a149",
      "metadata": {
        "id": "1c843c23-e3f2-490e-8d6c-212fa038a149"
      },
      "source": [
        "## 4. Similarity Index\n",
        "Here's a simple example of an operation powered by the embeddings table. Explorer comes with a `similarity_index` operation-\n",
        "* It tries to estimate how similar each data point is with the rest of the dataset.\n",
        "*  It does that by counting how many image embeddings lie closer than `max_dist` to the current image in the generated embedding space, considering `top_k` similar images at a time.\n",
        "\n",
        "For a given dataset, model, `max_dist` & `top_k` the similarity index once generated will be reused. In case, your dataset has changed, or you simply need to regenerate the similarity index, you can pass `force=True`.\n",
        "Similar to vector and SQL search, this also comes with a util to directly plot it. Let's look at the plot first\n",
        "<img width=\"633\" alt=\"Screenshot 2024-01-06 at 9 49 36 PM\" src=\"https://github.com/AyushExel/assets/assets/15766192/96a9d984-4a72-4784-ace1-428676ee2bdd\">\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "953c2a5f-1b61-4acf-a8e4-ed08547dbafc",
      "metadata": {
        "id": "953c2a5f-1b61-4acf-a8e4-ed08547dbafc"
      },
      "outputs": [],
      "source": [
        "exp.plot_similarity_index(max_dist=0.2, top_k=0.01)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "28228a9a-b727-45b5-8ca7-8db662c0b937",
      "metadata": {
        "id": "28228a9a-b727-45b5-8ca7-8db662c0b937"
      },
      "source": [
        "Now let's look at the output of the operation"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f4161aaa-20e6-4df0-8e87-d2293ee0530a",
      "metadata": {
        "id": "f4161aaa-20e6-4df0-8e87-d2293ee0530a"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "sim_idx = exp.similarity_index(max_dist=0.2, top_k=0.01, force=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b01d5b1a-9adb-4c3c-a873-217c71527c8d",
      "metadata": {
        "id": "b01d5b1a-9adb-4c3c-a873-217c71527c8d"
      },
      "outputs": [],
      "source": [
        "sim_idx"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "22b28e54-4fbb-400e-ad8c-7068cbba11c4",
      "metadata": {
        "id": "22b28e54-4fbb-400e-ad8c-7068cbba11c4"
      },
      "source": [
        "Let's create a query to see what data points have similarity count of more than 30 and plot images similar to them."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "58d2557b-d401-43cf-937d-4f554c7bc808",
      "metadata": {
        "id": "58d2557b-d401-43cf-937d-4f554c7bc808"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "sim_count = np.array(sim_idx[\"count\"])\n",
        "sim_idx['im_file'][sim_count > 30]"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a5ec8d76-271a-41ab-ac74-cf8c0084ba5e",
      "metadata": {
        "id": "a5ec8d76-271a-41ab-ac74-cf8c0084ba5e"
      },
      "source": [
        "You should see something like this\n",
        "<img src=\"https://github.com/AyushExel/assets/assets/15766192/649bc366-ca2d-46ea-bfd9-3097cf575584\">\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "3a7b2ee3-9f35-48a2-9c38-38379516f4d2",
      "metadata": {
        "id": "3a7b2ee3-9f35-48a2-9c38-38379516f4d2"
      },
      "outputs": [],
      "source": [
        "exp.plot_similar(idx=[7146, 14035]) # Using avg embeddings of 2 images"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "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.6"
    },
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}