Search code examples
pythonkerasdashboardplotly-dash

Displaying model summary on dash


I have a trained Keras model and stored it in variable 'model'. When I print model.summary() I get something like a table with column headers

Layer (type)        Output Shape         Param#       Connected to

How do I display this table in dash? Do I need to turn it into some sort of dataframe first? Or can I call the summary and the output printed on the dashboard?

I managed to use this code to turn it into a string. My code:

strlist = []
model.summary(print_fn=lambda x: strlist.append(x))
str_sum = "\n".join(strlist)

app = dash.Dash()

app.layout = html.Div(children=[
            html.Div(str_sum)
     ])

if __name__ == '__main__':
  app.run_server() 

But what I get on my dashboard looks all disjointed and not in the right order It looks something like:

Model: "model"_________________________________Layer (type) Output Shape Param #
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =   
input_1 (InputLayer) [None, 256, 256, 3) 0 __________________________________
block1_conv1 (Conv2D) (None, 127, 127, 32) 864 input_1[0][0]

Appreciate some input!


Solution

  • Dash renders output as html so you need a bit of extra care with linebreaks. Try replacing your app.layout line with the following

    app.layout = html.Div(html.P(
        children=str_sum, style={'whiteSpace': 'pre-wrap'}))
    

    Works for me:

    dash model summary