mert-kurttutan / torchview

torchview: visualize pytorch models
https://torchview.dev
MIT License
822 stars 37 forks source link

saving to disk as SVG #91

Open matthijsvk opened 1 year ago

matthijsvk commented 1 year ago

Is your feature request related to a problem? Please describe. If saving to disk, the default output format is PNG which is inefficient and low-resolution. I couldn't find a way to save as SVG instead, though I'm sure graphviz supports this.

Describe the solution you'd like add option "format=[svg|png]" that allows choosing the file format. Alternatively, derive it from the filename, i.e. filename="model.svg" sets format=svg automatically.

Describe alternatives you've considered This works:

model_graph = draw_graph(model, 
                        input_data=dummy_input, 
                        save_graph=True, 
                        directory="figures/")
model_graph.visual_graph.render(format='svg')

but this first saves a PNG and then the SVG as well, it would be cleaner if we had an option in the draw_graph() function itself

mert-kurttutan commented 1 year ago

Sorry for the late reply, What you could is to turn off save graph option and then save it in svg format manually. Since this could be done with just one line of code, I am not really willing to put it. But if you or someone else provide nice PR, I would accept it.

saifkhichi96 commented 1 year ago

@matthijsvk You can save the graph as SVG (or any other format supported by graphviz) like this:

graph = draw_graph(model, ...).visual_graph
graph_svg = graph.pipe(format='svg').decode('utf-8') # convert to binary data
with open(save_path + '.' + format, 'wb') as f:
    f.write(graph_svg)
naik-amey commented 11 months ago

How can I save as png?

xdevfaheem commented 11 months ago

s, u can @naik-amey. just set format arg to "png"