voxel51 / fiftyone-brain

Open source AI/ML capabilities for the FiftyOne ecosystem
https://fiftyone.ai/brain.html
Apache License 2.0
128 stars 3 forks source link

Handling empty view in sort_by_similarity() #108

Closed brimoor closed 2 years ago

brimoor commented 2 years ago

Handles a previously uncaught edge case where view.sort_by_similarity() would raise an error if view is empty.

import fiftyone as fo
import fiftyone.brain as fob
import fiftyone.zoo as foz

dataset = foz.load_zoo_dataset("quickstart", max_samples=5).clone()
results = fob.compute_similarity(dataset, brain_key="image_sim")

# Previously would raise an error; now succeeds
view = dataset.limit(0).sort_by_similarity(dataset.first().id)

Note that other methods like find_duplicates() and find_unique() will still raise an error on empty views, but I'm not going to do anything about that right now because:

results.use_view(dataset.limit(0))

results.find_duplicates(thresh=0.1)
ValueError: Found array with 0 sample(s) (shape=(0, 0)) while a minimum of 1 is required by NearestNeighbors.