.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "vision/auto_checks/train_test_validation/plot_image_dataset_drift.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_vision_auto_checks_train_test_validation_plot_image_dataset_drift.py: .. _vision__image_dataset_drift: Image Dataset Drift ******************* This notebooks provides an overview for using and understanding the image dataset drift check, used to detect drift in simple image properties between train and test datasets. **Structure:** * `What Is Image Dataset Drift? <#what-is-image-dataset-drift>`__ * `Which Image Properties Are Used? <#which-image-properties-are-used>`__ * `Loading The Data <#loading-the-data>`__ * `Run The Check <#run-the-check>`__ * `Define a Condition <#define-a-condition>`__ What Is Image Dataset Drift? ---------------------------- Drift is simply a change in the distribution of data over time, and it is also one of the top reasons why machine learning model's performance degrades over time. Image dataset drift is a drift that occurs in more than one image property at a time, and may even affect the relationships between those properties, which are undetectable by univariate drift methods. For more information on drift, please visit our :ref:`Drift Guide `. How Deepchecks Detects Dataset Drift ------------------------------------ This check detects multivariate drift by using :ref:`a domain classifier `. Other methods to detect drift include :ref:`univariate measures ` which is used in other checks, such as :ref:`vision__image_property_drift` check. Using Properties to Detect Image Drift -------------------------------------- In computer vision specifically, we can't measure drift on the images directly, as the individual pixel has little value when estimating drift. Therefore, we calculate drift on different :ref:`properties of the image`, on which we can directly measure drift. Which Image Properties Are Used? -------------------------------- ============================== ========== Property name What is it ============================== ========== Aspect Ratio Ratio between height and width of image (height / width) Area Area of image in pixels (height * width) Brightness Average intensity of image pixels. Color channels have different weights according to RGB-to-Grayscale formula RMS Contrast Contrast of image, calculated by standard deviation of pixels Mean Red Relative Intensity Mean over all pixels of the red channel, scaled to their relative intensity in comparison to the other channels [r / (r + g + b)]. Mean Green Relative Intensity Mean over all pixels of the green channel, scaled to their relative intensity in comparison to the other channels [g / (r + g + b)]. Mean Blue Relative Intensity Mean over all pixels of the blue channel, scaled to their relative intensity in comparison to the other channels [b / (r + g + b)]. ============================== ========== .. GENERATED FROM PYTHON SOURCE LINES 68-76 Imports ------- .. note:: In this example, we use the pytorch version of the coco dataset and model. In order to run this example using tensorflow, please change the import statements to:: from deepchecks.vision.datasets.detection.coco_tensorflow import load_dataset .. GENERATED FROM PYTHON SOURCE LINES 76-82 .. code-block:: default import numpy as np from deepchecks.vision.checks import ImageDatasetDrift from deepchecks.vision.datasets.detection.coco_torch import load_dataset .. GENERATED FROM PYTHON SOURCE LINES 83-85 Loading the data ---------------- .. GENERATED FROM PYTHON SOURCE LINES 85-90 .. code-block:: default train_ds = load_dataset(train=True, object_type='VisionData') test_ds = load_dataset(train=False, object_type='VisionData') .. GENERATED FROM PYTHON SOURCE LINES 91-95 Run the check ------------- without drift ^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 95-100 .. code-block:: default check = ImageDatasetDrift() result = check.run(train_dataset=train_ds, test_dataset=test_ds) result .. rst-class:: sphx-glr-script-out .. code-block:: none Processing Train Batches: | | 0/1 [Time: 00:00] Processing Train Batches: |█████| 1/1 [Time: 00:00] Processing Train Batches: |█████| 1/1 [Time: 00:00] Processing Test Batches: | | 0/1 [Time: 00:00] Processing Test Batches: |█████| 1/1 [Time: 00:00] Processing Test Batches: |█████| 1/1 [Time: 00:00] Computing Check: | | 0/1 [Time: 00:00] Computing Check: |█████| 1/1 [Time: 00:00] Computing Check: |█████| 1/1 [Time: 00:00] .. raw:: html
Image Dataset Drift


.. GENERATED FROM PYTHON SOURCE LINES 101-102 To display the results in an IDE like PyCharm, you can use the following code: .. GENERATED FROM PYTHON SOURCE LINES 102-104 .. code-block:: default # result.show_in_window() .. GENERATED FROM PYTHON SOURCE LINES 105-106 The result will be displayed in a new window. .. GENERATED FROM PYTHON SOURCE LINES 108-112 Insert drift ^^^^^^^^^^^^ Now, we will define a custom collate function that will insert a drift to the training set. .. GENERATED FROM PYTHON SOURCE LINES 112-120 .. code-block:: default def add_brightness(img): reverse = 255 - img addition_of_brightness = (reverse * 0.2).astype(int) return img + addition_of_brightness .. GENERATED FROM PYTHON SOURCE LINES 121-134 .. code-block:: default drifted_train_ds = load_dataset(train=True, object_type='VisionData') def created_drifted_collate_function(collate_fn): def drifted_collate_function(batch): data_dict = collate_fn(batch) data_dict['images'] = [add_brightness(np.array(img)) for img in data_dict['images']] return data_dict return drifted_collate_function drifted_train_ds._batch_loader.collate_fn = created_drifted_collate_function(drifted_train_ds._batch_loader.collate_fn) .. GENERATED FROM PYTHON SOURCE LINES 135-137 Run the check again ^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 137-141 .. code-block:: default check = ImageDatasetDrift() result = check.run(train_dataset=drifted_train_ds, test_dataset=test_ds) result .. rst-class:: sphx-glr-script-out .. code-block:: none Processing Train Batches: | | 0/1 [Time: 00:00] Processing Train Batches: |█████| 1/1 [Time: 00:00] Processing Train Batches: |█████| 1/1 [Time: 00:00] Processing Test Batches: | | 0/1 [Time: 00:00] Processing Test Batches: |█████| 1/1 [Time: 00:00] Processing Test Batches: |█████| 1/1 [Time: 00:00] Computing Check: | | 0/1 [Time: 00:00] Computing Check: |█████| 1/1 [Time: 00:00] Computing Check: |█████| 1/1 [Time: 00:00] .. raw:: html
Image Dataset Drift


.. GENERATED FROM PYTHON SOURCE LINES 142-147 Define a Condition ------------------ Now, we will define a condition that the maximum drift score is less than a certain threshold. In this example we will set the threshold at 0.2. In order to demonstrate the condition, we will use again the original (not drifted) train dataset. .. GENERATED FROM PYTHON SOURCE LINES 147-151 .. code-block:: default check = ImageDatasetDrift().add_condition_drift_score_less_than(0.2) result = check.run(train_dataset=train_ds, test_dataset=test_ds).show(show_additional_outputs=False) result .. rst-class:: sphx-glr-script-out .. code-block:: none Processing Train Batches: | | 0/1 [Time: 00:00] Processing Train Batches: |█████| 1/1 [Time: 00:00] Processing Train Batches: |█████| 1/1 [Time: 00:00] Processing Test Batches: | | 0/1 [Time: 00:00] Processing Test Batches: |█████| 1/1 [Time: 00:00] Processing Test Batches: |█████| 1/1 [Time: 00:00] Computing Check: | | 0/1 [Time: 00:00] Computing Check: |█████| 1/1 [Time: 00:00] Computing Check: |█████| 1/1 [Time: 00:00] .. raw:: html
Image Dataset Drift


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.911 seconds) .. _sphx_glr_download_vision_auto_checks_train_test_validation_plot_image_dataset_drift.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_image_dataset_drift.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_image_dataset_drift.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_