How to identify duplicates¶
Problem statement¶
One of the first steps in Exploratory Data Analysis (EDA) is to check for duplicates. Duplicates add no new information and can distort model training by over-emphasizing features that in appear in the duplicates.
DataEval provides a Duplicates class to assist you in removing duplicates so you can start training your models on high quality data.
When to use¶
The Duplicates class should be used if you need to find duplicate images in your dataset.
What you will need¶
A python envornment with following packages installed:
dataeval
maite-datasets
A dataset to analyze
Getting started¶
Let’s import the required libraries needed to set up a minimal working example
import numpy as np
from IPython.display import display
from maite_datasets.image_classification import MNIST
from dataeval import Metadata
from dataeval.config import set_max_processes
from dataeval.quality import Duplicates
from dataeval.selection import Indices, Select
set_max_processes(4)
Loading in the data¶
Load the MNIST data and create the dataset.
The MNIST dataset contains 70,000 images - 60,000 in the train set and 10,000 in the test set. For the purposes of this demonstration, we are just going to use the test set.
# Load in the mnist dataset
testing_dataset = MNIST(root="./data/", image_set="test", download=True)
# Get the labels
labels = Metadata(testing_dataset).class_labels
Because the MNIST dataset does not contain any exact duplicates we are going to adjust the dataset to include some.
# Creating some indices to duplicate
print("Exact duplicates")
duplicates = {}
for i in [1, 2, 5, 9]:
matching_indices = np.where(labels == i)[0]
print(f"\t{i} - ({matching_indices[23]}, {matching_indices[78]})")
duplicates[int(matching_indices[78])] = int(matching_indices[23])
Exact duplicates
1 - (180, 663)
2 - (249, 728)
5 - (219, 866)
9 - (212, 773)
# Create a subset with the identified duplicate indices swapped
indices_with_duplicates = [duplicates.get(i, i) for i in range(len(testing_dataset))]
duplicates_ds = Select(testing_dataset, Indices(indices_with_duplicates))
Finding the duplicates¶
Now we are asking our Duplicates class to find the needle in the haystack. There are only 4 exact duplicates.
# Initialize the Duplicates class to begin to identify duplicate images.
identifyDuplicates = Duplicates()
# Evaluate the data
results = identifyDuplicates.evaluate(duplicates_ds)
The results can be viewd as a DataFrame with exact and near groups enumerated.
display(results)
shape: (133, 5)
┌──────────┬───────┬──────────┬──────────────┬────────────┐
│ group_id ┆ level ┆ dup_type ┆ item_indices ┆ methods │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str ┆ list[i64] ┆ list[str] │
╞══════════╪═══════╪══════════╪══════════════╪════════════╡
│ 0 ┆ item ┆ exact ┆ [180, 663] ┆ ["xxhash"] │
│ 1 ┆ item ┆ exact ┆ [212, 773] ┆ ["xxhash"] │
│ 2 ┆ item ┆ exact ┆ [219, 866] ┆ ["xxhash"] │
│ 3 ┆ item ┆ exact ┆ [249, 728] ┆ ["xxhash"] │
│ 4 ┆ item ┆ near ┆ [14, 8575] ┆ ["dhash"] │
│ … ┆ … ┆ … ┆ … ┆ … │
│ 128 ┆ item ┆ near ┆ [8938, 9969] ┆ ["dhash"] │
│ 129 ┆ item ┆ near ┆ [8979, 9222] ┆ ["dhash"] │
│ 130 ┆ item ┆ near ┆ [8986, 9725] ┆ ["dhash"] │
│ 131 ┆ item ┆ near ┆ [9689, 9819] ┆ ["dhash"] │
│ 132 ┆ item ┆ near ┆ [9772, 9796] ┆ ["phash"] │
└──────────┴───────┴──────────┴──────────────┴────────────┘
The Duplicates class was able to find all 4 exact duplicates out of the 10,000 samples.
It also found several sets of images that are very closely related to each other, and since we are using hand written digits we would expect it to find some images that were nearly identical.