[Solved] Keras model.summary() object to string

I want to write a *.txt file with the neural network hyperparameters and the model architecture. Is it possible to write the object model.summary() to my output file?

(...)
summary = str(model.summary())
(...)
out = open(filename + 'report.txt','w')
out.write(summary)
out.close

It happens that I’m getting “None” as you can see below.

Hyperparameters
=========================

learning_rate: 0.01
momentum: 0.8
decay: 0.0
batch size: 128
no. epochs: 3
dropout: 0.5
-------------------------

None
val_acc: 0.232323229313
val_loss: 3.88496732712
train_acc: 0.0965207634216
train_loss: 4.07161939425
train/val loss ratio: 1.04804469418

Any idea how to deal with that?

Solution #1:

With my version of Keras (2.0.6) and Python (3.5.0), this works for me:

# Create an empty model
from keras.models import Sequential
model = Sequential()

# Open the file
with open(filename + 'report.txt','w') as fh:
    # Pass the file handle in as a lambda function to make it callable
    model.summary(print_fn=lambda x: fh.write(x + 'n'))

This outputs the following lines to the file:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
Respondent: winni2k

Solution #2:

For me, this worked to just get the model summary as a string:

stringlist = []
model.summary(print_fn=lambda x: stringlist.append(x))
short_model_summary = "n".join(stringlist)
print(short_model_summary)
Respondent: FAnders

Solution #3:

And if you want to write to a log:

import logging
logger = logging.getLogger(__name__)

model.summary(print_fn=logger.info)
Respondent: ajb

Solution #4:

It’s not the best way to do it but one thing you can do is to redirect stdout:

orig_stdout = sys.stdout
f = open('out.txt', 'w')
sys.stdout = f
print(model.summary())
sys.stdout = orig_stdout
f.close()

see “How to redirect ‘print’ output to a file using python?”

Respondent: Maryam Hnr

Solution #5:

I understand the OP has already accepted winni2k’s answer, but since the question title actually implies saving the outputs of model.summary() to a string, not a file, the following code might help others who come to this page looking for that (like I did).

The code below was run using TensorFlow 1.12.0, which comes with Keras 2.1.6-tf on Python 3.6.2.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
import io

# Example model
model = Sequential([
    Dense(32, input_shape=(784,)),
    Activation('relu'),
    Dense(10),
    Activation('softmax'),
])

def get_model_summary(model):
    stream = io.StringIO()
    model.summary(print_fn=lambda x: stream.write(x + 'n'))
    summary_string = stream.getvalue()
    stream.close()
    return summary_string

model_summary_string = get_model_summary(model)

print(model_summary_string)

Which yields (as a string):

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 32)                25120     
_________________________________________________________________
activation (Activation)      (None, 32)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 10)                330       
_________________________________________________________________
activation_1 (Activation)    (None, 10)                0         
=================================================================
Total params: 25,450
Trainable params: 25,450
Non-trainable params: 0
_________________________________________________________________
Respondent: Pasa

Solution #6:

I stumbled upon the same problem as well!
There are two possible workarounds:

Use to_json() method of the model

summary = str(model.to_json())

This is your case above.

Otherwise use the ascii method from keras_diagram

from keras_diagram import ascii
summary = ascii(model)
Respondent: sunil manikani

Solution #7:

One option, although not an exact replacement for model.summary, is to export a model’s configuration using model.get_config(). From the docs:

model.get_config(): returns a dictionary containing the configuration of the model. The model can be reinstantiated from its config via:

config = model.get_config()
model = Model.from_config(config)
# or, for Sequential:
model = Sequential.from_config(config)
Respondent: jkdev

Solution #8:

I had this same problem. @Pasa’s answer was very useful but I thought I would post a more minimal example: it’s a reasonable assumption that you already have a Keras model at this point.

import io

s = io.StringIO()
model.summary(print_fn=lambda x: s.write(x + 'n'))
model_summary = s.getvalue()
s.close()

print("The model summary is:nn{}".format(model_summary))

An example of when having this string is useful: if you have a matplotlib plot. You can then use:

plt.text(0, 0.25, model_summary)

To write the summary of your model to your performance chart, for quick reference:
enter image description here

Respondent: Mark

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.