Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to execute plot_model in keras

i'm trying to visualise my model, but when i'm using plot_model function of keras it's giving me error saying "'InputLayer' object is not iterable" i'm attaching my code as well as error. please help

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(96, (5, 5), activation='relu', input_shape=(28, 28, 3), padding = 'same'),
    tf.keras.layers.Conv2D(96, (5, 5), activation='relu', padding = 'same'),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(256, (5, 5), activation='relu', padding = 'same'),
    tf.keras.layers.Conv2D(256, (5, 5), activation='relu', padding = 'same'),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(384, (3, 3), activation='relu', padding = 'same'),
    tf.keras.layers.Conv2D(384, (3, 3), activation='relu', padding = 'same'),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(256, (3, 3), activation='relu', padding = 'same'),
    tf.keras.layers.Conv2D(256, (3, 3), activation='relu', padding = 'same'),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(2304, activation='relu'),
    tf.keras.layers.Dense(2304, activation='relu'),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer=Adam(lr=0.001), loss='sparse_categorical_crossentropy', metrics=['acc'])

plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-92-2aa57a1383be> in <module>()
----> 1 plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)

1 frames
/usr/local/lib/python3.6/dist-packages/keras/utils/vis_utils.py in plot_model(model, to_file, show_shapes, show_layer_names, rankdir)
    130             'LR' creates a horizontal plot.
    131     """
--> 132     dot = model_to_dot(model, show_shapes, show_layer_names, rankdir)
    133     _, extension = os.path.splitext(to_file)
    134     if not extension:

/usr/local/lib/python3.6/dist-packages/keras/utils/vis_utils.py in model_to_dot(model, show_shapes, show_layer_names, rankdir)
    107             node_key = layer.name + '_ib-' + str(i)
    108             if node_key in model._network_nodes:
--> 109                 for inbound_layer in node.inbound_layers:
    110                     inbound_layer_id = str(id(inbound_layer))
    111                     dot.add_edge(pydot.Edge(inbound_layer_id, layer_id))

TypeError: 'InputLayer' object is not iterable
like image 753
Quamer Nasim Avatar asked Aug 27 '19 20:08

Quamer Nasim


1 Answers

Try importing from tensorflow directly:

from tensorflow.keras.utils import plot_model

like image 161
scottlittle Avatar answered Nov 09 '22 07:11

scottlittle