{"id":13738639,"url":"https://github.com/yu4u/convnet-drawer","last_synced_at":"2025-04-05T04:12:46.754Z","repository":{"id":41092774,"uuid":"115941735","full_name":"yu4u/convnet-drawer","owner":"yu4u","description":"Python script for illustrating Convolutional Neural Networks (CNN) using Keras-like model definitions","archived":false,"fork":false,"pushed_at":"2020-04-30T18:30:49.000Z","size":949,"stargazers_count":600,"open_issues_count":11,"forks_count":98,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-03-29T03:08:43.268Z","etag":null,"topics":["convolutional-neural-networks","deep-neural-networks","keras","python-pptx","visualization"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yu4u.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-01T19:25:50.000Z","updated_at":"2025-03-21T16:07:01.000Z","dependencies_parsed_at":"2022-07-14T08:17:12.713Z","dependency_job_id":null,"html_url":"https://github.com/yu4u/convnet-drawer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yu4u%2Fconvnet-drawer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yu4u%2Fconvnet-drawer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yu4u%2Fconvnet-drawer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yu4u%2Fconvnet-drawer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yu4u","download_url":"https://codeload.github.com/yu4u/convnet-drawer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247284951,"owners_count":20913704,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["convolutional-neural-networks","deep-neural-networks","keras","python-pptx","visualization"],"created_at":"2024-08-03T03:02:30.179Z","updated_at":"2025-04-05T04:12:46.734Z","avatar_url":"https://github.com/yu4u.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# ConvNet Drawer\n\nPython script for illustrating Convolutional Neural Networks (CNN).\nInspired by the draw_convnet project [1].\n\nModels can be visualized via Keras-like ([Sequential](https://keras.io/models/sequential/)) model definitions.\nThe result can be saved as SVG file or pptx file!\n\n## Requirements\npython-pptx (if you want to save models as pptx)\n\n```sh\npip install python-pptx\n```\n\nKeras (if you want to convert Keras sequential model)\n\n```sh\npip install keras\n```\n\nmatplotlib (if you want to save models via matplotlib)\n\n```bash\npip install matplotlib\n```\n\n## Usage\nWrite a script to define and save a model. An example of visualizing AlexNet [2] is as follows.\n\n### Write and save convnet_drawer.Model\n\n```python\nfrom convnet_drawer import Model, Conv2D, MaxPooling2D, Flatten, Dense\nfrom pptx_util import save_model_to_pptx\nfrom matplotlib_util import save_model_to_file\n\nmodel = Model(input_shape=(227, 227, 3))\nmodel.add(Conv2D(96, (11, 11), (4, 4)))\nmodel.add(MaxPooling2D((3, 3), strides=(2, 2)))\nmodel.add(Conv2D(256, (5, 5), padding=\"same\"))\nmodel.add(MaxPooling2D((3, 3), strides=(2, 2)))\nmodel.add(Conv2D(384, (3, 3), padding=\"same\"))\nmodel.add(Conv2D(384, (3, 3), padding=\"same\"))\nmodel.add(Conv2D(256, (3, 3), padding=\"same\"))\nmodel.add(MaxPooling2D((3, 3), strides=(2, 2)))\nmodel.add(Flatten())\nmodel.add(Dense(4096))\nmodel.add(Dense(4096))\nmodel.add(Dense(1000))\n\n# save as svg file\nmodel.save_fig(\"example.svg\")\n\n# save as pptx file\nsave_model_to_pptx(model, \"example.pptx\")\n\n# save via matplotlib\nsave_model_to_file(model, \"example.pdf\")\n```\n\nResult:\n\n\u003cimg src=\"examples/AlexNet.svg\"\u003e\n\nThe other examples can be found [here](examples).\n\n### Convert Keras sequential model\nKeras sequential model can be converted to `convnet_drawer.Model` (thanks to @wakamezake).\nOnly Conv2D, MaxPooling2D, GlobalAveragePooling2D, Flatten, Dense layers are supported for this conversion.\n\n```python\nfrom keras_util import convert_drawer_model\nfrom keras_models import AlexNet\nfrom pptx_util import save_model_to_pptx\nfrom matplotlib_util import save_model_to_file\n\n# get Keras sequential model\nkeras_sequential_model = AlexNet.get_model()\nmodel = convert_drawer_model(keras_sequential_model)\n\n# save as svg file\nmodel.save_fig(\"example.svg\")\n\n# save as pptx file\nsave_model_to_pptx(model, \"example.pptx\")\n\n# save via matplotlib\nsave_model_to_file(model, \"example.pdf\")\n```\n\n### Supported Layers\n\n- *Conv2D*\n  - ```Conv2D(filters=None, kernel_size=None, strides=(1, 1), padding=\"valid\")```\n  - e.g. `Conv2D(96, (11, 11), (4, 4)))`\n- *Deconv2D*\n  - ```Deconv2D(filters=None, kernel_size=None, strides=(1, 1), padding=\"valid\")```\n  - e.g. `Deconv2D(256, (3, 3), (2, 2)))`\n- *MaxPooling2D*, *AveragePooling2D*\n  - ```MaxPooling2D(pool_size=(2, 2), strides=None, padding=\"valid\")```\n  - e.g. `MaxPooling2D((3, 3), strides=(2, 2))`\n  - If `strides = None`, stride is set to be `pool_size`.\n- *GlobalAveragePooling2D*\n  - ```GlobalAveragePooling2D()```\n- *Flatten*\n  - ```Flatten()```\n- *Dense*\n  - ```Dense(units)```\n  - e.g. `Dense(4096)`\n\n### Visualization Parameters\nVisualization Parameters can be found in [config.py](config.py).\nPlease adjust these parameters before model definition (see [LeNet.py](examples/LeNet.py)).\nThe most important parameter is `channel_scale = 3 / 5`.\nThis parameter rescale actual channel size `c` to `c_` for visualization as:\n\n```c_ = math.pow(c, channel_scale)```\n\nIf the maximum channel size is small (e.g. 512), please increase `channel_scale`.\n\nCheck how the other parameters works:\n\n\u003cimg src=\"parameters.png\"\u003e\n\n#### Default Values\n\n```python\ntheta = - math.pi / 6\nratio = 0.7\nbounding_box_margin = 10\ninter_layer_margin = 50\ntext_margin = 10\nchannel_scale = 3 / 5\ntext_size = 14\none_dim_width = 4\nline_color_feature_map = (0, 0, 0)\nline_color_layer = (0, 0, 255)\ntext_color_feature_map = (0, 0, 0)\ntext_color_layer = (0, 0, 0)\n```\n\n\n## TODOs\n- [x] Implement padding option for Conv2D and Pooling layers.\n- [x] Add some effects to Dense layer (and Flatten / GlobalAveragePooling2D).\n- [ ] Automatically calibrate the scale of feature maps for better visibility.\n- [x] Move hard-coded parameters to a config file or options.\n- [x] Refactor Layer classes.\n- [x] ~~Draw with matplotlib? for other formats.~~ The model is now directly saved as a pptx file.\n\n## Results\nLeNet\n\n\u003cimg src=\"examples/LeNet.svg\"\u003e\n\nAlexNet\n\n\u003cimg src=\"examples/AlexNet.svg\"\u003e\n\nZFNet\n\n\u003cimg src=\"examples/ZFNet.svg\"\u003e\n\nVGG16\n\n\u003cimg src=\"examples/VGG16.svg\"\u003e\n\nAutoEncoder\n\n\u003cimg src=\"examples/AutoEncoder.svg\"\u003e\n\nAlexNet saved by matplotlib with plt.xkcd()\n\n\u003cimg src=\"examples/AlexNet_xkcd.png\"\u003e\n\n## References\n[1] https://github.com/gwding/draw_convnet\n\n[2] A. Krizhevsky, I. Sutskever, and G. E. Hinton, \"ImageNet Classification with Deep Convolutional Neural Networks,\" in Proc. of NIPS, 2012.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyu4u%2Fconvnet-drawer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyu4u%2Fconvnet-drawer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyu4u%2Fconvnet-drawer/lists"}