0

I'm trying to print my model.summary() to pdf.

I originally tried following the question asked here: How to save model.summary() to file in Keras?

def myprint(s):
    with open('/content/drive/My Drive/xxx/yyy/zzz.pdf','w') as f:
        print(s, file=f)

model.summary(print_fn=myprint)

But the file was unreadable.

I asked here, and Bhargav gave me the following solution:

with open('modelsummary.pdf', 'w') as f:

    model.summary(print_fn=lambda x: f.write(x + '\n'))

which, when opened with google docs seems fine but when opened/downloaded as a PDF just gives errors, as if the file was corrupted.

Since I have several CNN models to include in my document I need them to automatically be in the style and format that I want them in.

Getting a LaTeX-friendly table would also be ok, but I need someone to give me a MWE because I don't know how to do that.

Beth Long
  • 375
  • 3
  • 24
  • No, you are taking the completely wrong approach, that is not how you make a PDF. If you want to include in latex, it is much easier to use the verbatim environment since the summary is just text. – Dr. Snoopy Mar 16 '21 at 23:23
  • The verbatim environment will only print the code, I don't want to print the code I want to print the summary as a table so that it's nicely formatted and so that I can then also use it in presentations etc. – Beth Long Mar 17 '21 at 08:58
  • None of that happens automatically if you somehow convert to PDF, again as I said, you have the wrong concept – Dr. Snoopy Mar 17 '21 at 11:29
  • Note that any kind of nice formatting needs to be done by yourself, you could just rewrite the summary as a latex table, but that needs to be done manually. Keras also has some functionality to print the network graph which might be what you actually need to present in a scientific paper. – Dr. Snoopy Mar 17 '21 at 13:06
  • I don't want to present the graph, it's too long and not clear. The table that is printed when doing model.summary() is perfect but I don't want to screenshot it every time. You're saying there's no way to convert this table into anything other than a .txt? – Beth Long Mar 17 '21 at 13:34
  • The summary table is plain text, any conversion that you want to do needs to be done **manually**, it will not magically happen if you convert text to PDF. – Dr. Snoopy Mar 17 '21 at 13:35
  • I don't want to do anything other than save the plain text in the table exactly as it appears into a format that I can then import directly into LaTeX without having to copy and paste it manually for each of the 12 networks I talk about in the document. – Beth Long Mar 17 '21 at 13:44
  • Doing that is exactly the same as putting the summary as text in the verbatim environment. So you might just save the summary as text and do that. – Dr. Snoopy Mar 17 '21 at 13:46
  • Note that you can include files as verbatim input: https://tex.stackexchange.com/questions/85200/include-data-from-a-txt-verbatim so a txt output would work fine in your use case. – Dr. Snoopy Mar 18 '21 at 13:04

1 Answers1

0

Do you need to save the summary as a PDF? If so, you need to use a wrapper library like fpdf or PyPDF2 in order to generate the required PDF metadata so that the file is properly formatted.

Otherwise, you can write to a .txt file using your approach:

def myprint(s):
    with open('/content/drive/My Drive/xxx/yyy/zzz.txt','w') as f:
        print(s, file=f)

model.summary(print_fn=myprint)
cosentiyes
  • 364
  • 2
  • 16
  • Yes ideally I'd like to save it as a pdf so that I can then import it directly into LaTeX – Beth Long Mar 16 '21 at 19:14
  • Can you please give me a MWE of how to use one of the pdf packages you mentioned? – Beth Long Mar 17 '21 at 08:58
  • Hi, nearly two years on, please can you provide a MWE of how you would go about using these packages to achieve the result want? – Beth Long Feb 27 '23 at 16:45
  • There are a number of MWEs in the documentation for both projects that I linked to! You just need to plug in your desired text to those demos. – cosentiyes Feb 28 '23 at 19:08
  • fpdf produces a pdf but changes the formatting terribly, i haven't yet found a mwe for pypdf that takes a txt file and produces a pdf – Beth Long Mar 01 '23 at 13:21