Note

This page was generated from a Jupyter notebook. The original can be downloaded from here.

Inactive Unit Analysis: Example

This notebook demonstrates how to run IUA on a simple dataset. For more general information about how to use DeepView and about each of these steps, it’s suggested to start with the How-To guides in the docs (starting with how to load a model), and then checking out the Familiarity Notebook for Rare Data and Data Errors.

1. Use DeepView to run inference

Let us start by importing everything needed to run on this notebook.

[1]:
from deepview.base import pipeline, ResponseInfo
from deepview.introspectors import IUA
from deepview.samples import StubImageDataset
from deepview.processors import FieldRenamer, Transposer
from deepview.exceptions import enable_deprecation_warnings

enable_deprecation_warnings(error=True)  # treat DeepView deprecation warnings as errors

from deepview_tensorflow import load_tf_model_from_path
2025-03-10 15:40:55.759270: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2025-03-10 15:40:55.795557: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2025-03-10 15:40:55.796846: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2025-03-10 15:40:56.548910: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT

Download a model, MobileNet, and store it locally

[2]:
from deepview_tensorflow import TFModelExamples

mobilenet = TFModelExamples.MobileNet()
model = mobilenet.model
WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.
/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/keras/src/engine/training.py:3000: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
  saving_api.save_model(

Create a Producer to generate data

[3]:
data_producer = StubImageDataset(
    dataset_size=32,
    image_width=224,
    image_height=224,
    channel_count=3
)

Find Convolutional Layers

[4]:
conv2d_responses = [
    info.name
    for info in model.response_infos.values()
    if info.layer.kind is ResponseInfo.LayerKind.CONV_2D
]

Set up processing pipeline

[5]:
response_producer = pipeline(
    data_producer,
    FieldRenamer({"images": "input_1:0"}),
    model(conv2d_responses),
    Transposer(dim=(0, 3, 1, 2))
)

2. Execute IUA introspector

Which can be done with just a single line of code!

[6]:
iua = IUA.introspect(response_producer)

Show table of results

IUA.show(iua) will show, by default, a table of layers and the discovered inactive units.

[7]:
print(IUA.show(iua).head())
     response  mean inactive  std inactive
0       conv1   100352.40625      0.654760
1   conv_dw_1    87841.87500      7.030069
2  conv_dw_10    23129.34375    339.003743
3  conv_dw_11    25587.81250    505.221204
4  conv_dw_12    10791.50000    129.931232

Plot results

IUA.show can also be used to view charts, by setting vis_type to IUA.VisType.CHART.

One layer’s chart can be viewed in this manner, e.g. conv_pw_9

[8]:
IUA.show(iua, vis_type=IUA.VisType.CHART, response_names=['conv_pw_9'])
[8]:
<Axes: title={'center': 'conv_pw_9 Inactive Unit Proportions '}>
../../_images/notebooks_model_introspection_inactive_unit_analysis_16_1.png

… or view all responses’ charts (omitting the response_names parameter):

[9]:
IUA.show(iua, vis_type=IUA.VisType.CHART)
[9]:
array([<Axes: title={'center': 'conv1 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_1 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_10 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_11 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_12 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_13 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_2 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_3 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_4 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_5 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_6 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_7 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_8 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_dw_9 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_pad_12 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_pad_2 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_pad_4 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_pad_6 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_preds Inactive Unit Proportions - no inactive units'}>,
       <Axes: title={'center': 'conv_pw_1 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_pw_10 Inactive Unit Proportions - no inactive units'}>,
       <Axes: title={'center': 'conv_pw_11 Inactive Unit Proportions - no inactive units'}>,
       <Axes: title={'center': 'conv_pw_12 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_pw_13 Inactive Unit Proportions - no inactive units'}>,
       <Axes: title={'center': 'conv_pw_2 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_pw_3 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_pw_4 Inactive Unit Proportions - no inactive units'}>,
       <Axes: title={'center': 'conv_pw_5 Inactive Unit Proportions - no inactive units'}>,
       <Axes: title={'center': 'conv_pw_6 Inactive Unit Proportions - no inactive units'}>,
       <Axes: title={'center': 'conv_pw_7 Inactive Unit Proportions - no inactive units'}>,
       <Axes: title={'center': 'conv_pw_8 Inactive Unit Proportions '}>,
       <Axes: title={'center': 'conv_pw_9 Inactive Unit Proportions '}>],
      dtype=object)
../../_images/notebooks_model_introspection_inactive_unit_analysis_18_1.png
[ ]: