{
  "id": 267288,
  "title": "[Info] Keras : layer_range to plot model",
  "url": "/competitions/rsna-miccai-brain-tumor-radiogenomic-classification/discussion/267288",
  "author_name": "Innat",
  "post_date": "2021-08-22T15:48:06.865000",
  "votes": 7,
  "comment_count": 0,
  "views": 0,
  "content": "<p><strong>Sorry, it's off-topic but we thought it would be useful to others in the modeling approach.</strong></p>\n<hr>\n<p>Many times plotting big models is so messy as they're too big to visually inspect. To remedy this, <strong><code>tf.keras.utils.plot_model</code></strong>  has now <a href=\"https://keras.io/api/utils/model_plotting_utils/\" target=\"_blank\"><code>layer_range</code></a> parameter to <strong>sub-plot</strong> the <strong>internal part only</strong> of deep neural network. It would be very convenient to debug the model (shape, layer flow, etc), especially if we like to integrate any <strong>custom layers</strong> inside a big network. </p>\n<pre><code>import tensorflow as tf \nmodel = tf.keras.applications.EfficientNetB1(\n    include_top=True,\n    weights=\"imagenet\",\n    input_tensor=None,\n    input_shape=None,\n    pooling=None,\n    classes=1000,\n    classifier_activation=\"softmax\"\n)\n</code></pre>\n<p>Now, let's plot only layer from <code>block5a_expand_conv</code> to <code>block5a_project_bn</code>,</p>\n<pre><code>tf.keras.utils.plot_model(\n    model,\n    show_shapes=True,\n    show_dtype=True,\n    show_layer_names=True,\n    expand_nested=True,\n    layer_range=['block5a_expand_conv', 'block5a_project_bn'],\n)\n</code></pre>\n<p><img src=\"https://user-images.githubusercontent.com/17668390/130361327-59e498b1-30a1-41c5-a2a8-810ead6fa889.png\" alt=\"download\"></p>\n<blockquote>\n  <p><strong>layer_range</strong>: input of list containing two str items, which is the starting layer name and ending layer name (<strong>both inclusive</strong>) indicating the range of layers for which the pydot.Dot will be generated. It also accepts <strong>regex patterns</strong> instead of the exact names. In such a case, start predicate will be the first element it matches to <strong>layer_range[0]</strong> and the end predicate will be the last element it matches to layer_range[1]. By default None which considers all layers of the model. <strong>Note that you must pass range such that the resultant subgraph must be complete.</strong></p>\n</blockquote>\n<p>It's a small feature but I found it very effective to <strong>debug the customized model</strong>. Thanks to the <a href=\"https://github.com/ashutosh1919\" target=\"_blank\">Ashutosh Hathidara</a> for quick implementation, <a href=\"https://github.com/tensorflow/tensorflow/pull/49860\" target=\"_blank\">PR discussion</a>. </p>",
  "messages": [
    {
      "id": 1486032,
      "postDate": "2021-08-22T15:48:06.867Z",
      "content": "<p><strong>Sorry, it's off-topic but we thought it would be useful to others in the modeling approach.</strong></p>\n<hr>\n<p>Many times plotting big models is so messy as they're too big to visually inspect. To remedy this, <strong><code>tf.keras.utils.plot_model</code></strong>  has now <a href=\"https://keras.io/api/utils/model_plotting_utils/\" target=\"_blank\"><code>layer_range</code></a> parameter to <strong>sub-plot</strong> the <strong>internal part only</strong> of deep neural network. It would be very convenient to debug the model (shape, layer flow, etc), especially if we like to integrate any <strong>custom layers</strong> inside a big network. </p>\n<pre><code>import tensorflow as tf \nmodel = tf.keras.applications.EfficientNetB1(\n    include_top=True,\n    weights=\"imagenet\",\n    input_tensor=None,\n    input_shape=None,\n    pooling=None,\n    classes=1000,\n    classifier_activation=\"softmax\"\n)\n</code></pre>\n<p>Now, let's plot only layer from <code>block5a_expand_conv</code> to <code>block5a_project_bn</code>,</p>\n<pre><code>tf.keras.utils.plot_model(\n    model,\n    show_shapes=True,\n    show_dtype=True,\n    show_layer_names=True,\n    expand_nested=True,\n    layer_range=['block5a_expand_conv', 'block5a_project_bn'],\n)\n</code></pre>\n<p><img src=\"https://user-images.githubusercontent.com/17668390/130361327-59e498b1-30a1-41c5-a2a8-810ead6fa889.png\" alt=\"download\"></p>\n<blockquote>\n  <p><strong>layer_range</strong>: input of list containing two str items, which is the starting layer name and ending layer name (<strong>both inclusive</strong>) indicating the range of layers for which the pydot.Dot will be generated. It also accepts <strong>regex patterns</strong> instead of the exact names. In such a case, start predicate will be the first element it matches to <strong>layer_range[0]</strong> and the end predicate will be the last element it matches to layer_range[1]. By default None which considers all layers of the model. <strong>Note that you must pass range such that the resultant subgraph must be complete.</strong></p>\n</blockquote>\n<p>It's a small feature but I found it very effective to <strong>debug the customized model</strong>. Thanks to the <a href=\"https://github.com/ashutosh1919\" target=\"_blank\">Ashutosh Hathidara</a> for quick implementation, <a href=\"https://github.com/tensorflow/tensorflow/pull/49860\" target=\"_blank\">PR discussion</a>. </p>",
      "rawMarkdown": "**Sorry, it's off-topic but we thought it would be useful to others in the modeling approach.**\n\n---\n\nMany times plotting big models is so messy as they're too big to visually inspect. To remedy this, **`tf.keras.utils.plot_model`**  has now [`layer_range`](https://keras.io/api/utils/model_plotting_utils/) parameter to **sub-plot** the **internal part only** of deep neural network. It would be very convenient to debug the model (shape, layer flow, etc), especially if we like to integrate any **custom layers** inside a big network. \n\n```\nimport tensorflow as tf \nmodel = tf.keras.applications.EfficientNetB1(\n    include_top=True,\n    weights=\"imagenet\",\n    input_tensor=None,\n    input_shape=None,\n    pooling=None,\n    classes=1000,\n    classifier_activation=\"softmax\"\n)\n```\n\nNow, let's plot only layer from `block5a_expand_conv` to `block5a_project_bn`,\n\n```\ntf.keras.utils.plot_model(\n    model,\n    show_shapes=True,\n    show_dtype=True,\n    show_layer_names=True,\n    expand_nested=True,\n    layer_range=['block5a_expand_conv', 'block5a_project_bn'],\n)\n```\n\n![download](https://user-images.githubusercontent.com/17668390/130361327-59e498b1-30a1-41c5-a2a8-810ead6fa889.png)\n\n\n> **layer_range**: input of list containing two str items, which is the starting layer name and ending layer name (**both inclusive**) indicating the range of layers for which the pydot.Dot will be generated. It also accepts **regex patterns** instead of the exact names. In such a case, start predicate will be the first element it matches to **layer_range[0]** and the end predicate will be the last element it matches to layer_range[1]. By default None which considers all layers of the model. **Note that you must pass range such that the resultant subgraph must be complete.**\n\nIt's a small feature but I found it very effective to **debug the customized model**. Thanks to the [Ashutosh Hathidara](https://github.com/ashutosh1919) for quick implementation, [PR discussion](https://github.com/tensorflow/tensorflow/pull/49860). ",
      "votes": 7
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "1486032": "**Sorry, it's off-topic but we thought it would be useful to others in the modeling approach.**\n\n---\n\nMany times plotting big models is so messy as they're too big to visually inspect. To remedy this, **`tf.keras.utils.plot_model`**  has now [`layer_range`](https://keras.io/api/utils/model_plotting_utils/) parameter to **sub-plot** the **internal part only** of deep neural network. It would be very convenient to debug the model (shape, layer flow, etc), especially if we like to integrate any **custom layers** inside a big network. \n\n```\nimport tensorflow as tf \nmodel = tf.keras.applications.EfficientNetB1(\n    include_top=True,\n    weights=\"imagenet\",\n    input_tensor=None,\n    input_shape=None,\n    pooling=None,\n    classes=1000,\n    classifier_activation=\"softmax\"\n)\n```\n\nNow, let's plot only layer from `block5a_expand_conv` to `block5a_project_bn`,\n\n```\ntf.keras.utils.plot_model(\n    model,\n    show_shapes=True,\n    show_dtype=True,\n    show_layer_names=True,\n    expand_nested=True,\n    layer_range=['block5a_expand_conv', 'block5a_project_bn'],\n)\n```\n\n![download](https://user-images.githubusercontent.com/17668390/130361327-59e498b1-30a1-41c5-a2a8-810ead6fa889.png)\n\n\n> **layer_range**: input of list containing two str items, which is the starting layer name and ending layer name (**both inclusive**) indicating the range of layers for which the pydot.Dot will be generated. It also accepts **regex patterns** instead of the exact names. In such a case, start predicate will be the first element it matches to **layer_range[0]** and the end predicate will be the last element it matches to layer_range[1]. By default None which considers all layers of the model. **Note that you must pass range such that the resultant subgraph must be complete.**\n\nIt's a small feature but I found it very effective to **debug the customized model**. Thanks to the [Ashutosh Hathidara](https://github.com/ashutosh1919) for quick implementation, [PR discussion](https://github.com/tensorflow/tensorflow/pull/49860). "
  }
}