Dataset Deduplication Tutorial

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

  1. A python envornment with following packages installed:

    • dataeval or dataeval[all]

  2. 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 dataeval.detectors.linters import Duplicates
from dataeval.utils.dataset.datasets import MNIST

Loading in the data

Let’s start by loading in torchvision’s MNIST dataset, then we will examine it

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/", train=False, download=True, unit_interval=True)
test_data = testing_dataset.data
labels = testing_dataset.targets
Files already downloaded and verified

Because the MNIST dataset does not contain any exact duplicates we are going to adjust the dataset to include some.

# Creating some duplicates
print("Exact duplicates")
duplicates = {}
for i in [1, 2, 5, 9]:
    matching_indices = np.where(labels == i)[0]
    test_data[matching_indices[78]] = test_data[matching_indices[23]]
    print(f"\t{i} - ({matching_indices[23]}, {matching_indices[78]})")
    duplicates[i] = (matching_indices[23], matching_indices[78], matching_indices[2])
Exact duplicates
	1 - (231, 781)
	2 - (232, 782)
	5 - (235, 785)
	9 - (239, 789)
print("Number of samples: ", len(test_data))
Number of samples:  8920

Finding the Duplicates

Now we are asking our Duplicates class to find the needle in the haystack. There are only 4 exact duplicates and then there are 3 shifted exact duplicates.

# Initialize the Duplicates class to begin to identify duplicate images. 
identifyDuplicates = Duplicates()

# Evaluate the data
results = identifyDuplicates.evaluate(test_data)

The results can be returned as a dictionary with exact and near as the keys. So we will extract those to view the results.

for category, images in results.dict().items():
    print(f"{category} - {len(images)}")
    print(f"\t{images}")
exact - 4
	[[231, 781], [232, 782], [235, 785], [239, 789]]
near - 69
	[[1, 1571, 6631], [31, 7221], [141, 4771, 4851, 5881, 6191, 7791], [147, 3987], [151, 5131], [161, 8141], [171, 2251], [241, 7161], [270, 2910], [281, 751, 951, 2721, 2801], [291, 701, 3911], [311, 5281], [341, 1741], [451, 8591], [711, 3341], [821, 5221], [911, 5421], [921, 4951], [961, 8021], [1019, 4629], [1021, 2931, 8481], [1051, 4101], [1104, 3414], [1281, 2781], [1415, 5635], [1511, 3861], [1531, 1731, 1821, 5811, 6261, 8031], [1670, 2710], [1687, 5827], [1791, 3761], [1831, 5021], [1891, 3791], [2021, 2141, 3661, 8051], [2051, 4471, 4871, 5031, 8191], [2057, 6947], [2101, 4781], [2191, 4741], [2211, 7601, 8421], [2377, 6657], [2407, 3417], [2481, 3541, 7501, 8521, 8571], [2631, 4261], [2791, 5561, 7371, 7841, 8261], [2911, 7551], [3061, 3551], [3151, 6551, 8631], [3177, 6887], [3249, 3367], [3301, 6021, 8361], [3361, 3711, 4051, 4831, 7381, 8641], [3441, 8811], [3571, 5371, 5651, 5761, 6891, 7061, 8231, 8401, 8621], [3577, 4687], [3649, 4329], [3851, 4691], [4241, 6421], [4407, 7487], [4501, 4991], [4581, 6881], [4641, 7361], [4821, 6341, 8471, 8801], [5041, 5401], [5081, 6471], [5197, 7977], [5361, 5661], [5461, 7041], [5511, 7071], [6379, 8429], [7021, 7981]]

If we recall from above, our exact duplicates were:

(231, 781), (232, 782), (235, 785), (239, 789)

Which exactly matches what the Duplicates class was able to find.

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.