3

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!

solo55
  • 71
  • 1
  • 6
  • Is your problem capturing the output of `model.summary()` in a string? As by default it just prints it on stdout. If so, this may help: https://stackoverflow.com/questions/41665799/keras-model-summary-object-to-string Once you capture it in a string or a file, you can display it in your dash application or wherever, but to help further you need to tell us a bit more what you are trying to do. – piterbarg Dec 06 '20 at 10:08
  • Thanks @piterbarg! Yes I am trying to capture the output of `model.summary()` on Dash. I used some of the code from that question which helped turn it into a string. But now there is an issue with my output, the spacing on dash comes out quite odd, like the 2nd line of the summary tables starts on the 1st and the same issue with subsequent lines – solo55 Dec 12 '20 at 00:56
  • How to get the output in dict format: https://stackoverflow.com/a/68128858/10375049 – Marco Cerliani Jun 25 '21 at 09:59

1 Answers1

1

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

piterbarg
  • 8,089
  • 2
  • 6
  • 22