Weak Segments Performance#

This notebook provides an overview for using and understanding the weak segment performance check.

Structure:

What is the purpose of the check?#

The check is designed to easily identify the model’s weakest segments. The segments are characterized by the image properties such as contrast and aspect ratio.

Automatically detecting weak segments#

The check performs several steps:

  1. We calculate the image properties for each sample. The properties to calculate can be passed explicitly or resort to the default image properties.

  2. We calculate loss for each sample in the dataset using the provided model or predictions, the loss function can be passed explicitly or set to a default based on the task type.

  3. We train multiple simple tree based models, each one is trained using two properties to predict the per sample error calculated before.

  4. We convert each of the leafs in each of the trees into a segment and calculate the segment’s performance. For the weakest segments detected we also calculate the model’s performance on the data segments surrounding them.

Generate Dataset#

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 import coco_tensorflow as coco
from deepchecks.vision.checks import WeakSegmentsPerformance
from deepchecks.vision.datasets.detection import coco_torch as coco

coco_data = coco.load_dataset(train=False, object_type='VisionData')

Run the check#

check = WeakSegmentsPerformance()
result = check.run(coco_data)
result
Processing Batches:
|     | 0/1 [Time: 00:00]
Processing Batches:
|#####| 1/1 [Time: 00:00]
Processing Batches:
|#####| 1/1 [Time: 00:00]

Computing Check:
|     | 0/1 [Time: 00:00]

Computing Check:
|#####| 1/1 [Time: 00:05]

Computing Check:
|#####| 1/1 [Time: 00:05]
Weak Segments Performance


To display the results in an IDE like PyCharm, you can use the following code:

#  result.show_in_window()

The result will be displayed in a new window.

Observe the check’s output#

We see in the results that the check indeed found several segments on which the model performance is below average. In the heatmap display we can see the model’s performance on the weakest segments and their environment with respect to the two segmentation features. In order to get the full list of weak segments found we can look at the result.value attribute.

result.value['weak_segments_list']
mean IoU score Feature1 Feature1 range Feature2 Feature2 range % of data
0 0.419188 Aspect Ratio (0.9723437428474426, 1.2355396747589111) None 7.8125
10 0.455363 Area (248490.0, inf) Mean Blue Relative Intensity (0.3016064614057541, 0.31798185408115387) 7.8125
17 0.491290 RMS Contrast (66.42585372924805, 69.78135681152344) Mean Blue Relative Intensity (0.27554573118686676, inf) 7.8125
5 0.500318 Mean Blue Relative Intensity (0.3016064614057541, 0.31798185408115387) None 9.3750
11 0.510713 Brightness (125.55977249145508, inf) RMS Contrast (-inf, 54.26441764831543) 7.8125
3 0.518545 Aspect Ratio (0.66796875, inf) Mean Red Relative Intensity (0.32986657321453094, 0.34265194833278656) 12.5000
12 0.526350 Mean Red Relative Intensity (0.33034907281398773, 0.33868667483329773) None 9.3750
7 0.533640 Area (242000.0, inf) RMS Contrast (64.05989265441895, 69.63874816894531) 12.5000
6 0.536544 Area (177000.0, 242000.0) None 10.9375
18 0.564342 Mean Red Relative Intensity (0.33270610868930817, 0.3437449038028717) Mean Green Relative Intensity (-inf, 0.3315943628549576) 7.8125
14 0.578763 Brightness (94.21614074707031, 100.21716690063477) None 7.8125
8 0.591613 Area (-inf, 329280.0) Mean Red Relative Intensity (0.33266347646713257, 0.3419434726238251) 9.3750
16 0.596756 RMS Contrast (-inf, 48.475643157958984) Mean Green Relative Intensity (-inf, 0.34254515171051025) 14.0625
13 0.599026 Brightness (123.49419784545898, 131.38275909423828) None 10.9375


Now we will run a check with properties and minimum segment size ratio (the minimal fraction of the data to be considered as a segment) different from the defaults.

from deepchecks.vision.utils.image_properties import brightness, texture_level
properties = [{'name': 'brightness', 'method': brightness, 'output_type': 'numerical'},
              {'name': ' texture', 'method': texture_level, 'output_type': 'numerical'}]
check = WeakSegmentsPerformance(segment_minimum_size_ratio=0.03, image_properties=properties)
result = check.run(coco_data)
result.show()
Processing Batches:
|     | 0/1 [Time: 00:00]
Processing Batches:
|#####| 1/1 [Time: 00:00]
Processing 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]
Weak Segments Performance


Define a condition#

We can define on our check a condition that will validate that the ratio of the model performance on the weakest segment to the average model performance is less than a specified ratio.

# Let's add a condition and re-run the check:

check.add_condition_segments_relative_performance_greater_than(0.1)
result = check.run(coco_data)
result.show(show_additional_outputs=False)
Processing Batches:
|     | 0/1 [Time: 00:00]
Processing Batches:
|#####| 1/1 [Time: 00:00]
Processing 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]
Weak Segments Performance


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

Gallery generated by Sphinx-Gallery