.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "checks_gallery/vision/train_test_validation/plot_train_test_label_drift.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_checks_gallery_vision_train_test_validation_plot_train_test_label_drift.py: .. _plot_vision_train_test_label_drift: Train Test Label Drift ********************** This notebooks provides an overview for using and understanding label drift check. **Structure:** * `What Is Label Drift? <#what-is-label-drift>`__ * `Which Label Properties Are Used? <#which-label-properties-are-used>`__ * `Run check on a Classification task <#run-the-check-on-a-classification-task-mnist>`__ * `Run check on an Object Detection task <#run-the-check-on-an-object-detection-task-coco>`__ What Is Label 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. Label drift is when drift occurs in the label itself. For more information on drift, please visit our :doc:`drift guide `. How Deepchecks Detects Label Drift ------------------------------------ This check detects label drift by using :ref:`univariate measures ` on the label properties. Using Label Properties to Detect Label Drift -------------------------------------------- In computer vision specifically, our labels may be complex, and measuring their drift is not a straightforward task. Therefore, we calculate drift on different :doc:`properties of the label`, on which we can directly measure drift. Which Label Properties Are Used? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ================ =================================== ========== Task Type Property name What is it ================ =================================== ========== Classification Samples Per Class Number of images per class Object Detection Samples Per Class Number of bounding boxes per class Object Detection Bounding Box Area Area of bounding box (height * width) Object Detection Number of Bounding Boxes Per Image Number of bounding box objects in each image ================ =================================== ========== Run the check on a Classification task (MNIST) ============================================== Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. code-block:: default from deepchecks.vision.checks import TrainTestLabelDrift from deepchecks.vision.datasets.classification.mnist import load_dataset .. GENERATED FROM PYTHON SOURCE LINES 62-64 Loading Data ------------ .. GENERATED FROM PYTHON SOURCE LINES 64-69 .. code-block:: default train_ds = load_dataset(train=True, batch_size=64, object_type='VisionData') test_ds = load_dataset(train=False, batch_size=1000, object_type='VisionData') .. GENERATED FROM PYTHON SOURCE LINES 70-72 Running TrainTestLabelDrift on classification --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 72-77 .. code-block:: default check = TrainTestLabelDrift() result = check.run(train_ds, test_ds) result .. rst-class:: sphx-glr-script-out .. code-block:: none Validating Input: | | 0/1 [Time: 00:00] Validating Input: |#####| 1/1 [Time: 00:00] Ingesting Batches - Train Dataset: | | 0/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################## | 30/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################################################ | 60/157 [Time: 00:00] Ingesting Batches - Train Dataset: |########################################################################################## | 90/157 [Time: 00:00] Ingesting Batches - Train Dataset: |######################################################################################################################## | 120/157 [Time: 00:00] Ingesting Batches - Train Dataset: |###################################################################################################################################################### | 150/157 [Time: 00:00] Ingesting Batches - Train Dataset: |#############################################################################################################################################################| 157/157 [Time: 00:00] Ingesting Batches - Test Dataset: | | 0/10 [Time: 00:00] Ingesting Batches - Test Dataset: |## | 2/10 [Time: 00:00] Ingesting Batches - Test Dataset: |#### | 4/10 [Time: 00:00] Ingesting Batches - Test Dataset: |###### | 6/10 [Time: 00:00] Ingesting Batches - Test Dataset: |######## | 8/10 [Time: 00:00] Ingesting Batches - Test Dataset: |##########| 10/10 [Time: 00:00] Ingesting Batches - Test Dataset: |##########| 10/10 [Time: 00:00] Computing Check: | | 0/1 [Time: 00:00] Computing Check: |#####| 1/1 [Time: 00:00] .. raw:: html
Train Test Label Drift


.. GENERATED FROM PYTHON SOURCE LINES 78-83 Understanding the results ------------------------- We can see there is almost no drift between the train & test labels. This means the split to train and test was good (as it is balanced and random). Let's check the performance of a simple model trained on MNIST. .. GENERATED FROM PYTHON SOURCE LINES 83-91 .. code-block:: default from deepchecks.vision.checks import ClassPerformance from deepchecks.vision.datasets.classification.mnist import \ load_model as load_mnist_model mnist_model = load_mnist_model(pretrained=True) ClassPerformance().run(train_ds, test_ds, mnist_model) .. rst-class:: sphx-glr-script-out .. code-block:: none Validating Input: | | 0/1 [Time: 00:00] Validating Input: |#####| 1/1 [Time: 00:00] Validating Input: |#####| 1/1 [Time: 00:00] Ingesting Batches - Train Dataset: | | 0/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############### | 15/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################### | 31/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################################### | 47/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################################################### | 63/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################################################################### | 79/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################################################################################### | 95/157 [Time: 00:00] Ingesting Batches - Train Dataset: |##############################################################################################################9 | 111/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################################################################################################################### | 127/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################################################################################################################################### | 143/157 [Time: 00:00] Ingesting Batches - Train Dataset: |#############################################################################################################################################################| 157/157 [Time: 00:01] Ingesting Batches - Test Dataset: | | 0/10 [Time: 00:00] Ingesting Batches - Test Dataset: |## | 2/10 [Time: 00:00] Ingesting Batches - Test Dataset: |#### | 4/10 [Time: 00:00] Ingesting Batches - Test Dataset: |###### | 6/10 [Time: 00:00] Ingesting Batches - Test Dataset: |######## | 8/10 [Time: 00:00] Ingesting Batches - Test Dataset: |##########| 10/10 [Time: 00:00] Ingesting Batches - Test Dataset: |##########| 10/10 [Time: 00:00] Computing Check: | | 0/1 [Time: 00:00] Computing Check: |#####| 1/1 [Time: 00:00] .. raw:: html
Class Performance


.. GENERATED FROM PYTHON SOURCE LINES 92-93 To display the results in an IDE like PyCharm, you can use the following code: .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: default # ClassPerformance().run(train_ds, test_ds, mnist_model).show_in_window() .. GENERATED FROM PYTHON SOURCE LINES 96-97 The result will be displayed in a new window. .. GENERATED FROM PYTHON SOURCE LINES 99-105 MNIST with label drift ====================== Now, let's try to separate the MNIST dataset in a different manner that will result in a label drift, and see how it affects the performance. We are going to create a custom `collate_fn`` in the test dataset, that will select samples with class 0 in a 1/10 chances. .. GENERATED FROM PYTHON SOURCE LINES 105-112 .. code-block:: default import torch mnist_dataloader_train = load_dataset(train=True, batch_size=64, object_type='DataLoader') mnist_dataloader_test = load_dataset(train=False, batch_size=1000, object_type='DataLoader') full_mnist = torch.utils.data.ConcatDataset([mnist_dataloader_train.dataset, mnist_dataloader_test.dataset]) .. GENERATED FROM PYTHON SOURCE LINES 113-115 .. code-block:: default train_dataset, test_dataset = torch.utils.data.random_split(full_mnist, [60000,10000], generator=torch.Generator().manual_seed(42)) .. GENERATED FROM PYTHON SOURCE LINES 116-118 Inserting drift to the test set ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 118-143 .. code-block:: default import numpy as np from torch.utils.data._utils.collate import default_collate np.random.seed(42) def collate_test(batch): modified_batch = [] for item in batch: image, label = item if label == 0: if np.random.randint(5) == 0: modified_batch.append(item) else: modified_batch.append((image, 1)) else: modified_batch.append(item) return default_collate(modified_batch) mod_train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64) mod_test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, collate_fn=collate_test) .. GENERATED FROM PYTHON SOURCE LINES 144-150 .. code-block:: default from deepchecks.vision.datasets.classification.mnist import MNISTData mod_train_ds = MNISTData(mod_train_loader) mod_test_ds = MNISTData(mod_test_loader) .. GENERATED FROM PYTHON SOURCE LINES 151-153 Run the check ============= .. GENERATED FROM PYTHON SOURCE LINES 153-157 .. code-block:: default check = TrainTestLabelDrift() check.run(mod_train_ds, mod_test_ds) .. rst-class:: sphx-glr-script-out .. code-block:: none Validating Input: | | 0/1 [Time: 00:00] Validating Input: |#####| 1/1 [Time: 00:00] Ingesting Batches - Train Dataset: | | 0/157 [Time: 00:00] Ingesting Batches - Train Dataset: |###########################9 | 28/157 [Time: 00:00] Ingesting Batches - Train Dataset: |#######################################################9 | 56/157 [Time: 00:00] Ingesting Batches - Train Dataset: |#################################################################################### | 84/157 [Time: 00:00] Ingesting Batches - Train Dataset: |###############################################################################################################9 | 112/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################################################################################################################################ | 140/157 [Time: 00:00] Ingesting Batches - Train Dataset: |#############################################################################################################################################################| 157/157 [Time: 00:00] Ingesting Batches - Test Dataset: | | 0/157 [Time: 00:00] Ingesting Batches - Test Dataset: |###########################9 | 28/157 [Time: 00:00] Ingesting Batches - Test Dataset: |#######################################################9 | 56/157 [Time: 00:00] Ingesting Batches - Test Dataset: |#################################################################################### | 84/157 [Time: 00:00] Ingesting Batches - Test Dataset: |###############################################################################################################9 | 112/157 [Time: 00:00] Ingesting Batches - Test Dataset: |############################################################################################################################################ | 140/157 [Time: 00:00] Ingesting Batches - Test Dataset: |#############################################################################################################################################################| 157/157 [Time: 00:00] Computing Check: | | 0/1 [Time: 00:00] Computing Check: |#####| 1/1 [Time: 00:00] .. raw:: html
Train Test Label Drift


.. GENERATED FROM PYTHON SOURCE LINES 158-162 Add a condition --------------- We could also add a condition to the check to alert us to changes in the label distribution, such as the one that occurred here. .. GENERATED FROM PYTHON SOURCE LINES 162-168 .. code-block:: default check = TrainTestLabelDrift().add_condition_drift_score_less_than() check.run(mod_train_ds, mod_test_ds) # As we can see, the condition alerts us to the present of drift in the label. .. rst-class:: sphx-glr-script-out .. code-block:: none Validating Input: | | 0/1 [Time: 00:00] Validating Input: |#####| 1/1 [Time: 00:00] Ingesting Batches - Train Dataset: | | 0/157 [Time: 00:00] Ingesting Batches - Train Dataset: |###########################9 | 28/157 [Time: 00:00] Ingesting Batches - Train Dataset: |#######################################################9 | 56/157 [Time: 00:00] Ingesting Batches - Train Dataset: |#################################################################################### | 84/157 [Time: 00:00] Ingesting Batches - Train Dataset: |###############################################################################################################9 | 112/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################################################################################################################################ | 140/157 [Time: 00:00] Ingesting Batches - Train Dataset: |#############################################################################################################################################################| 157/157 [Time: 00:00] Ingesting Batches - Test Dataset: | | 0/157 [Time: 00:00] Ingesting Batches - Test Dataset: |###########################9 | 28/157 [Time: 00:00] Ingesting Batches - Test Dataset: |#######################################################9 | 56/157 [Time: 00:00] Ingesting Batches - Test Dataset: |#################################################################################### | 84/157 [Time: 00:00] Ingesting Batches - Test Dataset: |###############################################################################################################9 | 112/157 [Time: 00:00] Ingesting Batches - Test Dataset: |############################################################################################################################################ | 140/157 [Time: 00:00] Ingesting Batches - Test Dataset: |#############################################################################################################################################################| 157/157 [Time: 00:00] Computing Check: | | 0/1 [Time: 00:00] Computing Check: |#####| 1/1 [Time: 00:00] .. raw:: html
Train Test Label Drift


.. GENERATED FROM PYTHON SOURCE LINES 169-173 Results ------- We can see the check successfully detects the (expected) drift in class 0 distribution between the train and test sets .. GENERATED FROM PYTHON SOURCE LINES 175-177 But how does this affect the performance of the model? ------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 177-180 .. code-block:: default ClassPerformance().run(mod_train_ds, mod_test_ds, mnist_model) .. rst-class:: sphx-glr-script-out .. code-block:: none Validating Input: | | 0/1 [Time: 00:00] Validating Input: |#####| 1/1 [Time: 00:00] Ingesting Batches - Train Dataset: | | 0/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############### | 15/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################## | 30/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################################# | 45/157 [Time: 00:00] Ingesting Batches - Train Dataset: |############################################################ | 60/157 [Time: 00:00] Ingesting Batches - Train Dataset: |########################################################################### | 75/157 [Time: 00:00] Ingesting Batches - Train Dataset: |########################################################################################## | 90/157 [Time: 00:00] Ingesting Batches - Train Dataset: |######################################################################################################### | 105/157 [Time: 00:00] Ingesting Batches - Train Dataset: |######################################################################################################################## | 120/157 [Time: 00:00] Ingesting Batches - Train Dataset: |####################################################################################################################################### | 135/157 [Time: 00:00] Ingesting Batches - Train Dataset: |###################################################################################################################################################### | 150/157 [Time: 00:01] Ingesting Batches - Train Dataset: |#############################################################################################################################################################| 157/157 [Time: 00:01] Ingesting Batches - Test Dataset: | | 0/157 [Time: 00:00] Ingesting Batches - Test Dataset: |############### | 15/157 [Time: 00:00] Ingesting Batches - Test Dataset: |############################## | 30/157 [Time: 00:00] Ingesting Batches - Test Dataset: |############################################# | 45/157 [Time: 00:00] Ingesting Batches - Test Dataset: |############################################################ | 60/157 [Time: 00:00] Ingesting Batches - Test Dataset: |########################################################################### | 75/157 [Time: 00:00] Ingesting Batches - Test Dataset: |########################################################################################## | 90/157 [Time: 00:00] Ingesting Batches - Test Dataset: |######################################################################################################### | 105/157 [Time: 00:00] Ingesting Batches - Test Dataset: |######################################################################################################################## | 120/157 [Time: 00:00] Ingesting Batches - Test Dataset: |####################################################################################################################################### | 135/157 [Time: 00:00] Ingesting Batches - Test Dataset: |###################################################################################################################################################### | 150/157 [Time: 00:01] Ingesting Batches - Test Dataset: |#############################################################################################################################################################| 157/157 [Time: 00:01] Computing Check: | | 0/1 [Time: 00:00] Computing Check: |#####| 1/1 [Time: 00:00] .. raw:: html
Class Performance


.. GENERATED FROM PYTHON SOURCE LINES 181-184 Inferring the results --------------------- We can see the drop in the precision of class 0, which was caused by the class imbalance indicated earlier by the label drift check. .. GENERATED FROM PYTHON SOURCE LINES 186-188 Run the check on an Object Detection task (COCO) ================================================ .. GENERATED FROM PYTHON SOURCE LINES 188-194 .. code-block:: default from deepchecks.vision.datasets.detection.coco import load_dataset train_ds = load_dataset(train=True, object_type='VisionData') test_ds = load_dataset(train=False, object_type='VisionData') .. GENERATED FROM PYTHON SOURCE LINES 195-199 .. code-block:: default check = TrainTestLabelDrift() check.run(train_ds, test_ds) .. rst-class:: sphx-glr-script-out .. code-block:: none Validating Input: | | 0/1 [Time: 00:00] Validating Input: |#####| 1/1 [Time: 00:00] Ingesting Batches - Train Dataset: | | 0/2 [Time: 00:00] Ingesting Batches - Train Dataset: |##5 | 1/2 [Time: 00:00] Ingesting Batches - Train Dataset: |#####| 2/2 [Time: 00:00] Ingesting Batches - Train Dataset: |#####| 2/2 [Time: 00:00] Ingesting Batches - Test Dataset: | | 0/2 [Time: 00:00] Ingesting Batches - Test Dataset: |##5 | 1/2 [Time: 00:00] Ingesting Batches - Test Dataset: |#####| 2/2 [Time: 00:00] Ingesting Batches - Test Dataset: |#####| 2/2 [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
Train Test Label Drift


.. GENERATED FROM PYTHON SOURCE LINES 200-206 Label drift is detected! ------------------------ We can see that the COCO128 contains a drift in the out of the box dataset. In addition to the label count per class, the label drift check for object detection tasks include drift calculation on certain measurements, like the bounding box area and the number of bboxes per image. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 9.756 seconds) .. _sphx_glr_download_checks_gallery_vision_train_test_validation_plot_train_test_label_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_train_test_label_drift.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_train_test_label_drift.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_