Elffuss BraidHub

kikocis/lancedb public

main · 8.4 KiBRenderedRawPermalink
1# FiftyOne
2
3FiftyOne is an open source toolkit that enables users to curate better data and build better models. It includes tools for data exploration, visualization, and management, as well as features for collaboration and sharing.
4
5Any developers, data scientists, and researchers who work with computer vision and machine learning can use FiftyOne to improve the quality of their datasets and deliver insights about their models.
6
7
8![example](../assets/voxel.gif)
9
10**FiftyOne** provides an API to create LanceDB tables and run similarity queries, both **programmatically in Python** and via **point-and-click in the App**.
11
12Let's get started and see how to use **LanceDB** to create a **similarity index** on your FiftyOne datasets.
13
14## Overview
15
16**[Embeddings](../embeddings/understanding_embeddings.md)** are foundational to all of the **vector search** features. In FiftyOne, embeddings are managed by the [**FiftyOne Brain**](https://docs.voxel51.com/user_guide/brain.html) that provides powerful machine learning techniques designed to transform how you curate your data from an art into a measurable science.
17
18!!!question "Have you ever wanted to find the images most similar to an image in your dataset?"
19 The **FiftyOne Brain** makes computing **visual similarity** really easy. You can compute the similarity of samples in your dataset using an embedding model and store the results in the **brain key**.
20
21 You can then sort your samples by similarity or use this information to find potential duplicate images.
22
23Here we will be doing the following :
24
251. **Create Index** - In order to run similarity queries against our media, we need to **index** the data. We can do this via the `compute_similarity()` function.
26
27 - In the function, specify the **model** you want to use to generate the embedding vectors, and what **vector search engine** you want to use on the **backend** (here LanceDB).
28
29 !!!tip
30 You can also give the similarity index a name(`brain_key`), which is useful if you want to run vector searches against multiple indexes.
31
322. **Query** - Once you have generated your similarity index, you can query your dataset with `sort_by_similarity()`. The query can be any of the following:
33
34 - An ID (sample or patch)
35 - A query vector of same dimension as the index
36 - A list of IDs (samples or patches)
37 - A text prompt (search semantically)
38
39## Prerequisites: install necessary dependencies
40
411. **Create and activate a virtual environment**
42
43 Install virtualenv package and run the following command in your project directory.
44 ```python
45 python -m venv fiftyone_
46 ```
47 From inside the project directory run the following to activate the virtual environment.
48 === "Windows"
49
50 ```python
51 fiftyone_/Scripts/activate
52 ```
53
54 === "macOS/Linux"
55
56 ```python
57 source fiftyone_/Scripts/activate
58 ```
59
602. **Install the following packages in the virtual environment**
61
62 To install FiftyOne, ensure you have activated any virtual environment that you are using, then run
63 ```python
64 pip install fiftyone
65 ```
66
67
68## Understand basic workflow
69
70The basic workflow shown below uses LanceDB to create a similarity index on your FiftyOne datasets:
71
721. Load a dataset into FiftyOne.
73
742. Compute embedding vectors for samples or patches in your dataset, or select a model to use to generate embeddings.
75
763. Use the `compute_similarity()` method to generate a LanceDB table for the samples or object patches embeddings in a dataset by setting the parameter `backend="lancedb"` and specifying a `brain_key` of your choice.
77
784. Use this LanceDB table to query your data with `sort_by_similarity()`.
79
805. If desired, delete the table.
81
82## Quick Example
83
84Let's jump on a quick example that demonstrates this workflow.
85
86
87```python
88
89import fiftyone as fo
90import fiftyone.brain as fob
91import fiftyone.zoo as foz
92
93# Step 1: Load your data into FiftyOne
94dataset = foz.load_zoo_dataset("quickstart")
95```
96Make sure you install torch ([guide here](https://pytorch.org/get-started/locally/)) before proceeding.
97
98```python
99# Steps 2 and 3: Compute embeddings and create a similarity index
100lancedb_index = fob.compute_similarity(
101 dataset,
102 model="clip-vit-base32-torch",
103 brain_key="lancedb_index",
104 backend="lancedb",
105)
106```
107
108!!! note
109 Running the code above will download the clip model (2.6Gb)
110
111Once the similarity index has been generated, we can query our data in FiftyOne by specifying the `brain_key`:
112
113```python
114# Step 4: Query your data
115query = dataset.first().id # query by sample ID
116view = dataset.sort_by_similarity(
117 query,
118 brain_key="lancedb_index",
119 k=10, # limit to 10 most similar samples
120)
121```
122The returned result are of type - `DatasetView`.
123
124!!! note
125 `DatasetView` does not hold its contents in-memory. Views simply store the rule(s) that are applied to extract the content of interest from the underlying Dataset when the view is iterated/aggregated on.
126
127 This means, for example, that the contents of a `DatasetView` may change as the underlying Dataset is modified.
128
129??? question "Can you query a view instead of dataset?"
130 Yes, you can also query a view.
131
132 Performing a similarity search on a `DatasetView` will only return results from the view; if the view contains samples that were not included in the index, they will never be included in the result.
133
134 This means that you can index an entire Dataset once and then perform searches on subsets of the dataset by constructing views that contain the images of interest.
135
136```python
137# Step 5 (optional): Cleanup
138
139# Delete the LanceDB table
140lancedb_index.cleanup()
141
142# Delete run record from FiftyOne
143dataset.delete_brain_run("lancedb_index")
144```
145
146
147## Using LanceDB backend
148By default, calling `compute_similarity()` or `sort_by_similarity()` will use an sklearn backend.
149
150To use the LanceDB backend, simply set the optional `backend` parameter of `compute_similarity()` to `"lancedb"`:
151
152```python
153import fiftyone.brain as fob
154#... rest of the code
155fob.compute_similarity(..., backend="lancedb", ...)
156```
157
158Alternatively, you can configure FiftyOne to use the LanceDB backend by setting the following environment variable.
159
160In your terminal, set the environment variable using:
161=== "Windows"
162
163 ```python
164 $Env:FIFTYONE_BRAIN_DEFAULT_SIMILARITY_BACKEND="lancedb" //powershell
165
166 set FIFTYONE_BRAIN_DEFAULT_SIMILARITY_BACKEND=lancedb //cmd
167 ```
168
169=== "macOS/Linux"
170
171 ```python
172 export FIFTYONE_BRAIN_DEFAULT_SIMILARITY_BACKEND=lancedb
173 ```
174
175!!! note
176 This will only run during the terminal session. Once terminal is closed, environment variable is deleted.
177
178Alternatively, you can **permanently** configure FiftyOne to use the LanceDB backend creating a `brain_config.json` at `~/.fiftyone/brain_config.json`. The JSON file may contain any desired subset of config fields that you wish to customize.
179
180```json
181{
182 "default_similarity_backend": "lancedb"
183}
184```
185This will override the default `brain_config` and will set it according to your customization. You can check the configuration by running the following code :
186
187```python
188import fiftyone.brain as fob
189# Print your current brain config
190print(fob.brain_config)
191```
192
193## LanceDB config parameters
194
195The LanceDB backend supports query parameters that can be used to customize your similarity queries. These parameters include:
196
197| Name| Purpose | Default |
198|:----|:--------|:--------|
199|**table_name**|The name of the LanceDB table to use. If none is provided, a new table will be created|`None`|
200|**metric**|The embedding distance metric to use when creating a new table. The supported values are ("cosine", "euclidean")|`"cosine"`|
201|**uri**| The database URI to use. In this Database URI, tables will be created. |`"/tmp/lancedb"`|
202
203There are two ways to specify/customize the parameters:
204
2051. **Using `brain_config.json` file**
206
207 ```json
208 {
209 "similarity_backends": {
210 "lancedb": {
211 "table_name": "your-table",
212 "metric": "euclidean",
213 "uri": "/tmp/lancedb"
214 }
215 }
216 }
217 ```
218
2192. **Directly passing to `compute_similarity()` to configure a specific new index** :
220
221 ```python
222 lancedb_index = fob.compute_similarity(
223 ...
224 backend="lancedb",
225 brain_key="lancedb_index",
226 table_name="your-table",
227 metric="euclidean",
228 uri="/tmp/lancedb",
229 )
230 ```
231
232For a much more in depth walkthrough of the integration, visit the LanceDB x Voxel51 [docs page](https://docs.voxel51.com/integrations/lancedb.html).