# Similar Image Leakage#

This notebook provides an overview for using and understanding the “Similar Image Leakage” check.

Structure:

## What is the purpose of the check?#

The check helps identify if the training dataset contains any images that are similar to any images in the test dataset. Such a situation is nearly always a case of leakage, because we can expect that the model will have an easier time getting correct predictions on an image that is similar to an image in the training set, compared to it’s “real world” performance. This may mean that the metrics we’re seeing for the test data are too optimistic, and we should remove those similar images from the test set.

### How is Similarity calculated?#

The similarity is calculated using an image hash known as Average Hash. This hash compresses the image using the following algorithm:

1. Resize the image to a very compact form (the check default is 8X8).

2. Compute the average of the image pixels.

3. For each pixel, replace value by the boolean result of pixel_value >= image_average.

Now we end up with a representation of the image that is 8 bytes long, but still contains some real information about the image content.

We then proceed to check for similar images by searching for test images whose hash is close to a hash of a training image, when distance is defined by the Hamming distance between the binary vectors that are the hashed images.

Similarity between images depends on the purpose of the dataset. This is because sometimes we’re training a model to find large differences between images (e.g. people vs dogs) and sometimes we’re training to find small differences (e.g. different types of trees). Moreover, sometimes our images are taken from real-world datasets, where they were taken by different people, in different locations. In some use-cases though the images are “cleaner”, such as ones taken under microscope or from the same security camera with the same background.

The check’s default parameters are set to match a real-world rgb photos and their differences. If your dataset has more delicate differences in it, it is advised to use the hash_size and similarity_threshold parameters of this check. The hash_size parameter controls the size of the hashed image. A larger hash_size will enable to find finer differences between images (and results in less similarity). The similarity_threshold parameter controls the ratio of pixels that need to be different in order for 2 images to be considered “different”. A lower similarity_threshold will define less images as “similar”.

# # Run the check # ==============

from deepchecks.vision.checks import SimilarImageLeakage


check = SimilarImageLeakage()
check.run(train_ds, test_ds)


Out:

Validating Input:   0%| | 0/1 [00:00<?, ? /s]

Ingesting Batches - Train Dataset:   0%|  | 0/2 [00:00<?, ? Batch/s]
Ingesting Batches - Train Dataset:  50%|# | 1/2 [00:00<00:00,  3.07 Batch/s]
Ingesting Batches - Train Dataset: 100%|##| 2/2 [00:00<00:00,  2.97 Batch/s]

Ingesting Batches - Test Dataset:   0%|  | 0/2 [00:00<?, ? Batch/s]
Ingesting Batches - Test Dataset:  50%|# | 1/2 [00:00<00:00,  3.02 Batch/s]
Ingesting Batches - Test Dataset: 100%|##| 2/2 [00:00<00:00,  3.18 Batch/s]

Computing Check:   0%| | 0/1 [00:00<?, ? Check/s]


#### Similar Image Leakage

Check for images in training that are similar to images in test.

Nothing to display

As we can see, no similar images were found.

### Insert training images into test#

Let’s now see what happens when we insert some of the training images into the test set. We’ll insert them with some changes to brightness to see what happens.

from copy import copy

import numpy as np
from PIL import Image

test_ds_modified = copy(test_ds)

def get_modification_func():

def mod_func(orig_dataset, idx):
if idx in range(5):
# Run only on the first 5 images
data, label = other_dataset[idx]
return Image.fromarray(np.clip(np.array(data, dtype=np.uint16) + 50, 0, 255).astype(np.uint8)), label
else:
return orig_dataset[idx]

return mod_func



### Re-run after introducing the similar images#

check = SimilarImageLeakage()
check.run(train_ds, test_ds_modified)


Out:

Validating Input:   0%| | 0/1 [00:00<?, ? /s]

Ingesting Batches - Train Dataset:   0%|  | 0/2 [00:00<?, ? Batch/s]
Ingesting Batches - Train Dataset:  50%|# | 1/2 [00:00<00:00,  3.13 Batch/s]
Ingesting Batches - Train Dataset: 100%|##| 2/2 [00:00<00:00,  3.02 Batch/s]

Ingesting Batches - Test Dataset:   0%|  | 0/2 [00:00<?, ? Batch/s]
Ingesting Batches - Test Dataset: |  | 10/? [00:00<00:00, 89.65 Batch/s]
Ingesting Batches - Test Dataset: |  | 21/? [00:00<00:00, 98.60 Batch/s]
Ingesting Batches - Test Dataset: |  | 32/? [00:00<00:00, 103.42 Batch/s]
Ingesting Batches - Test Dataset: |  | 44/? [00:00<00:00, 107.70 Batch/s]
Ingesting Batches - Test Dataset: |  | 56/? [00:00<00:00, 110.94 Batch/s]

Computing Check:   0%| | 0/1 [00:00<?, ? Check/s]
Computing Check: 100%|#| 1/1 [00:00<00:00,  5.27 Check/s]


#### Similar Image Leakage

Check for images in training that are similar to images in test.

### Similar Images

Total number of test samples with similar images in train: 5

#### Samples

##### Test

We can see that the check detected the five images from the training set we introduced to the test set.

## Define a condition#

We can define on our check a condition that will validate no similar images where found. The default is that no similar images are allowed at all, but this can be modified as shown here.

check = SimilarImageLeakage().add_condition_similar_images_not_more_than(3)
result = check.run(train_dataset=train_ds, test_dataset=test_ds_modified)


Out:

Validating Input:   0%| | 0/1 [00:00<?, ? /s]

Ingesting Batches - Train Dataset:   0%|  | 0/2 [00:00<?, ? Batch/s]
Ingesting Batches - Train Dataset:  50%|# | 1/2 [00:00<00:00,  3.57 Batch/s]
Ingesting Batches - Train Dataset: 100%|##| 2/2 [00:00<00:00,  3.46 Batch/s]

Ingesting Batches - Test Dataset:   0%|  | 0/2 [00:00<?, ? Batch/s]
Ingesting Batches - Test Dataset: |  | 11/? [00:00<00:00, 107.92 Batch/s]
Ingesting Batches - Test Dataset: |  | 23/? [00:00<00:00, 110.52 Batch/s]
Ingesting Batches - Test Dataset: |  | 36/? [00:00<00:00, 115.55 Batch/s]
Ingesting Batches - Test Dataset: |  | 50/? [00:00<00:00, 121.73 Batch/s]
Ingesting Batches - Test Dataset: |  | 63/? [00:00<00:00, 122.88 Batch/s]

Computing Check:   0%| | 0/1 [00:00<?, ? Check/s]
Computing Check: 100%|#| 1/1 [00:00<00:00,  5.32 Check/s]

Similar Image Leakage

Total running time of the script: ( 0 minutes 4.628 seconds)

Gallery generated by Sphinx-Gallery