.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/visualkeras/plot_dl_cnn_custom_vgg16.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 or to run this example in your browser via JupyterLite or Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_visualkeras_plot_dl_cnn_custom_vgg16.py: visualkeras: custom vgg16 example ========================================== .. currentmodule:: scikitplot.visualkeras An example showing the :py:func:`~scikitplot.visualkeras` function used by a :py:class:`~tensorflow.keras.Model` model. .. GENERATED FROM PYTHON SOURCE LINES 10-14 .. code-block:: Python # Authors: The scikit-plots developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 15-33 .. code-block:: Python # visualkeras Need aggdraw tensorflow # !pip install scikitplot[core, cpu] # or # !pip install aggdraw # !pip install tensorflow # python -c "import tensorflow as tf, google.protobuf as pb; print('tf', tf.__version__); print('protobuf', pb.__version__)" # python -m pip check # If Needed # pip install -U "protobuf<6" # pip install protobuf==5.29.4 import tensorflow as tf # Clear any session to reset the state of TensorFlow/Keras tf.keras.backend.clear_session() from scikitplot import visualkeras .. GENERATED FROM PYTHON SOURCE LINES 34-35 create VGG16 .. GENERATED FROM PYTHON SOURCE LINES 35-89 .. code-block:: Python image_size = 224 model = tf.keras.models.Sequential() model.add(tf.keras.layers.InputLayer(shape=(image_size, image_size, 3))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(64, activation="relu", kernel_size=(3, 3))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(64, activation="relu", kernel_size=(3, 3))) model.add(visualkeras.SpacingDummyLayer()) model.add(tf.keras.layers.MaxPooling2D((2, 2), strides=(2, 2))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(128, activation="relu", kernel_size=(3, 3))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(128, activation="relu", kernel_size=(3, 3))) model.add(visualkeras.SpacingDummyLayer()) model.add(tf.keras.layers.MaxPooling2D((2, 2), strides=(2, 2))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(256, activation="relu", kernel_size=(3, 3))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(256, activation="relu", kernel_size=(3, 3))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(256, activation="relu", kernel_size=(3, 3))) model.add(visualkeras.SpacingDummyLayer()) model.add(tf.keras.layers.MaxPooling2D((2, 2), strides=(2, 2))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(512, activation="relu", kernel_size=(3, 3))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(512, activation="relu", kernel_size=(3, 3))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(512, activation="relu", kernel_size=(3, 3))) model.add(visualkeras.SpacingDummyLayer()) model.add(tf.keras.layers.MaxPooling2D((2, 2), strides=(2, 2))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(512, activation="relu", kernel_size=(3, 3))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(512, activation="relu", kernel_size=(3, 3))) model.add(tf.keras.layers.ZeroPadding2D((1, 1))) model.add(tf.keras.layers.Conv2D(512, activation="relu", kernel_size=(3, 3))) model.add(tf.keras.layers.MaxPooling2D()) model.add(visualkeras.SpacingDummyLayer()) model.add(tf.keras.layers.Flatten()) model.add(tf.keras.layers.Dense(4096, activation="relu")) model.add(tf.keras.layers.Dropout(0.5)) model.add(tf.keras.layers.Dense(4096, activation="relu")) model.add(tf.keras.layers.Dropout(0.5)) model.add(tf.keras.layers.Dense(1000, activation="softmax")) # model.summary() .. GENERATED FROM PYTHON SOURCE LINES 90-91 Now visualize the model! .. GENERATED FROM PYTHON SOURCE LINES 91-101 .. code-block:: Python from collections import defaultdict color_map = defaultdict(dict) color_map[tf.keras.layers.Conv2D]["fill"] = "orange" color_map[tf.keras.layers.ZeroPadding2D]["fill"] = "gray" color_map[tf.keras.layers.Dropout]["fill"] = "pink" color_map[tf.keras.layers.MaxPooling2D]["fill"] = "red" color_map[tf.keras.layers.Dense]["fill"] = "green" color_map[tf.keras.layers.Flatten]["fill"] = "teal" .. GENERATED FROM PYTHON SOURCE LINES 102-106 .. code-block:: Python from PIL import ImageFont ImageFont.load_default() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 107-115 .. code-block:: Python img_vgg16 = visualkeras.layered_view( model, type_ignore=[visualkeras.SpacingDummyLayer], # to_file="result_images/vgg16.png", save_fig=True, save_fig_filename="vgg16.png", ) .. image-sg:: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_001.png :alt: plot dl cnn custom vgg16 :srcset: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-02-01 06:39:41.115763: W scikitplot 140704519912320 _pil.py:204:load_font] Error loading system font: cannot open resource 2026-02-01 06:39:41.115895: W scikitplot 140704519912320 _pil.py:206:load_font] Falling back to PIL default font. 2026-02-01 06:39:41.116001: W scikitplot 140704519912320 _layered.py:216:layered_view] The legend_text_spacing_offset parameter is deprecated andwill be removed in a future release. .. GENERATED FROM PYTHON SOURCE LINES 116-131 .. code-block:: Python img_vgg16_legend = visualkeras.layered_view( model, type_ignore=[visualkeras.SpacingDummyLayer], legend=True, font={ "font_size": 61, # 'use_default_font': False, # 'font_path': '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf' }, # to_file="result_images/vgg16_legend.png", save_fig=True, save_fig_filename="vgg16_legend.png", ) img_vgg16_legend .. image-sg:: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_002.png :alt: plot dl cnn custom vgg16 :srcset: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-02-01 06:39:42.546549: W scikitplot 140704519912320 _pil.py:204:load_font] Error loading system font: cannot open resource 2026-02-01 06:39:42.546700: W scikitplot 140704519912320 _pil.py:206:load_font] Falling back to PIL default font. 2026-02-01 06:39:42.546809: W scikitplot 140704519912320 _layered.py:216:layered_view] The legend_text_spacing_offset parameter is deprecated andwill be removed in a future release. .. GENERATED FROM PYTHON SOURCE LINES 132-142 .. code-block:: Python img_vgg16_spacing_layers = visualkeras.layered_view( model, type_ignore=[], spacing=0, # to_file="result_images/vgg16_spacing_layers.png", save_fig=True, save_fig_filename="vgg16_spacing_layers.png", ) img_vgg16_spacing_layers .. image-sg:: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_003.png :alt: plot dl cnn custom vgg16 :srcset: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-02-01 06:39:43.549249: W scikitplot 140704519912320 _pil.py:204:load_font] Error loading system font: cannot open resource 2026-02-01 06:39:43.549376: W scikitplot 140704519912320 _pil.py:206:load_font] Falling back to PIL default font. 2026-02-01 06:39:43.549481: W scikitplot 140704519912320 _layered.py:216:layered_view] The legend_text_spacing_offset parameter is deprecated andwill be removed in a future release. .. GENERATED FROM PYTHON SOURCE LINES 143-157 .. code-block:: Python img_vgg16_type_ignore = visualkeras.layered_view( model, type_ignore=[ tf.keras.layers.ZeroPadding2D, tf.keras.layers.Dropout, tf.keras.layers.Flatten, visualkeras.SpacingDummyLayer, ], # to_file="result_images/vgg16_type_ignore.png", save_fig=True, save_fig_filename="vgg16_type_ignore.png", ) img_vgg16_type_ignore .. image-sg:: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_004.png :alt: plot dl cnn custom vgg16 :srcset: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-02-01 06:39:44.372650: W scikitplot 140704519912320 _pil.py:204:load_font] Error loading system font: cannot open resource 2026-02-01 06:39:44.372744: W scikitplot 140704519912320 _pil.py:206:load_font] Falling back to PIL default font. 2026-02-01 06:39:44.372839: W scikitplot 140704519912320 _layered.py:216:layered_view] The legend_text_spacing_offset parameter is deprecated andwill be removed in a future release. .. GENERATED FROM PYTHON SOURCE LINES 158-168 .. code-block:: Python img_vgg16_color_map = visualkeras.layered_view( model, type_ignore=[visualkeras.SpacingDummyLayer], color_map=color_map, # to_file="result_images/vgg16_color_map.png", save_fig=True, save_fig_filename="vgg16_color_map.png", ) img_vgg16_color_map .. image-sg:: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_005.png :alt: plot dl cnn custom vgg16 :srcset: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-02-01 06:39:44.947032: W scikitplot 140704519912320 _pil.py:204:load_font] Error loading system font: cannot open resource 2026-02-01 06:39:44.947128: W scikitplot 140704519912320 _pil.py:206:load_font] Falling back to PIL default font. 2026-02-01 06:39:44.947219: W scikitplot 140704519912320 _layered.py:216:layered_view] The legend_text_spacing_offset parameter is deprecated andwill be removed in a future release. .. GENERATED FROM PYTHON SOURCE LINES 169-179 .. code-block:: Python img_vgg16_flat = visualkeras.layered_view( model, type_ignore=[visualkeras.SpacingDummyLayer], draw_volume=False, # to_file="result_images/vgg16_flat.png", save_fig=True, save_fig_filename="vgg16_flat.png", ) img_vgg16_flat .. image-sg:: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_006.png :alt: plot dl cnn custom vgg16 :srcset: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_006.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-02-01 06:39:45.829832: W scikitplot 140704519912320 _pil.py:204:load_font] Error loading system font: cannot open resource 2026-02-01 06:39:45.829925: W scikitplot 140704519912320 _pil.py:206:load_font] Falling back to PIL default font. 2026-02-01 06:39:45.830015: W scikitplot 140704519912320 _layered.py:216:layered_view] The legend_text_spacing_offset parameter is deprecated andwill be removed in a future release. .. GENERATED FROM PYTHON SOURCE LINES 180-197 .. code-block:: Python img_vgg16_scaling = visualkeras.layered_view( model, type_ignore=[visualkeras.SpacingDummyLayer], # legend=True, min_z=1, min_xy=1, max_z=4096, max_xy=4096, scale_z=0.5, scale_xy=11, # font={'font_size': 99}, # to_file="result_images/vgg16_scaling.png", save_fig=True, save_fig_filename="vgg16_scaling.png", ) img_vgg16_scaling .. image-sg:: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_007.png :alt: plot dl cnn custom vgg16 :srcset: /auto_examples/visualkeras/images/sphx_glr_plot_dl_cnn_custom_vgg16_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-02-01 06:39:46.466833: W scikitplot 140704519912320 _pil.py:204:load_font] Error loading system font: cannot open resource 2026-02-01 06:39:46.466940: W scikitplot 140704519912320 _pil.py:206:load_font] Falling back to PIL default font. 2026-02-01 06:39:46.467034: W scikitplot 140704519912320 _layered.py:216:layered_view] The legend_text_spacing_offset parameter is deprecated andwill be removed in a future release. .. GENERATED FROM PYTHON SOURCE LINES 198-206 .. tags:: model-type: classification model-workflow: model building plot-type: visualkeras domain: neural network level: intermediate purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 15.599 seconds) .. _sphx_glr_download_auto_examples_visualkeras_plot_dl_cnn_custom_vgg16.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-plots/scikit-plots/main?urlpath=lab/tree/notebooks/auto_examples/visualkeras/plot_dl_cnn_custom_vgg16.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/visualkeras/plot_dl_cnn_custom_vgg16.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_dl_cnn_custom_vgg16.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_dl_cnn_custom_vgg16.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_dl_cnn_custom_vgg16.zip ` .. include:: plot_dl_cnn_custom_vgg16.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_