{"id":13454838,"url":"https://github.com/InFoCusp/tf_cnnvis","last_synced_at":"2025-03-24T07:32:10.179Z","repository":{"id":69527678,"uuid":"85802370","full_name":"infocusp/tf_cnnvis","owner":"infocusp","description":"CNN visualization tool in TensorFlow","archived":false,"fork":false,"pushed_at":"2019-03-16T07:05:13.000Z","size":3827,"stargazers_count":780,"open_issues_count":23,"forks_count":208,"subscribers_count":42,"default_branch":"master","last_synced_at":"2024-12-12T19:03:11.594Z","etag":null,"topics":["cnn","convolutional-networks","convolutional-neural-networks","deepdream","tensorboard","tensorflow","visualization"],"latest_commit_sha":null,"homepage":"","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/infocusp.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,"governance":null}},"created_at":"2017-03-22T08:17:52.000Z","updated_at":"2024-11-07T13:16:22.000Z","dependencies_parsed_at":"2023-09-16T04:50:28.823Z","dependency_job_id":null,"html_url":"https://github.com/infocusp/tf_cnnvis","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infocusp%2Ftf_cnnvis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infocusp%2Ftf_cnnvis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infocusp%2Ftf_cnnvis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infocusp%2Ftf_cnnvis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/infocusp","download_url":"https://codeload.github.com/infocusp/tf_cnnvis/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245227538,"owners_count":20580895,"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":["cnn","convolutional-networks","convolutional-neural-networks","deepdream","tensorboard","tensorflow","visualization"],"created_at":"2024-07-31T08:00:58.387Z","updated_at":"2025-03-24T07:32:09.718Z","avatar_url":"https://github.com/infocusp.png","language":"Python","funding_links":[],"categories":["Models/Projects","Python","模型项目"],"sub_categories":["微信群"],"readme":"# tf_cnnvis\n[![DOI](https://zenodo.org/badge/85802370.svg)](https://zenodo.org/badge/latestdoi/85802370)\n\nA blog post describing the library: https://medium.com/@falaktheoptimist/want-to-look-inside-your-cnn-we-have-just-the-right-tool-for-you-ad1e25b30d90 \n\ntf_cnnvis is a CNN visualization library which you can use to better understand your own CNNs. We use the [TensorFlow](https://www.tensorflow.org/) library at the backend and the generated images are displayed in [TensorBoard](https://www.tensorflow.org/get_started/summaries_and_tensorboard). We have implemented 2 CNN visualization techniques so far:\n\n1) Based on the paper [Visualizing and Understanding Convolutional Networks](https://www.cs.nyu.edu/~fergus/papers/zeilerECCV2014.pdf) by Matthew D. Zeiler and Rob Fergus. The goal here is to reconstruct the input image from the information contained in any given layers of the convolutional neural network. Here are a few examples\n\n|   |   |   |   |\n| :-----------: | :-----------: | :-----------: | :-----------: |\n| \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/1.jpg\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/2.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/3.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/4.png\" width=\"196\" height=\"196\"\u003e |\n| \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/5.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/6.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/7.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/8.png\" width=\"196\" height=\"196\"\u003e |\n| \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/9.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/10.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/11.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/12.png\" width=\"196\" height=\"196\"\u003e |\n\nFigure 1: Original image and the reconstructed versions from maxpool layer 1,2 and 3 of Alexnet generated using tf_cnnvis. \n\n2) CNN visualization based on [Deep dream](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/deepdream/deepdream.ipynb) by Google. Here's the relevant [blog post](https://research.googleblog.com/2015/06/inceptionism-going-deeper-into-neural.html) explaining the technique. In essence, it attempts to construct an input image that maximizes the activation for a given output. We present some samples below:  \n\n|   |   |   |   |\n| :-----------: | :-----------: | :-----------: | :-----------: |\n| \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/Carbonara.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/Ibex.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/Elephant.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/Ostrich.png\" width=\"196\" height=\"196\"\u003e |\n| Carbonara | Ibex | Elephant | Ostrich |\n| \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/Cheese burger.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/Tennis ball.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/Fountain pen.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/Clock tower.png\" width=\"196\" height=\"196\"\u003e |\n| Cheese burger | Tennis ball | Fountain pen | Clock tower |\n| \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/Cauliflower.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/Baby Milk bottle.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/Sea lion.png\" width=\"196\" height=\"196\"\u003e | \u003cimg src=\"https://github.com/InFoCusp/ui_resources/blob/master/cnnvis_images/Dolphin.png\" width=\"196\" height=\"196\"\u003e |\n| Cauliflower | Baby Milk bottle | Sea lion | Dolphin |\n\n## Requirements:\n* Tensorflow (\u003e= 1.8)\n* numpy\n* scipy\n* h5py\n* wget\n* Pillow\n* six\n* scikit-image\n\nIf you are using pip you can install these with\n\n```pip install tensorflow numpy scipy h5py wget Pillow six scikit-image```\n\n## Setup script\nClone the repository\n\n```\n#!bash\n\ngit clone https://github.com/InFoCusp/tf_cnnvis.git\n```\n\nAnd run \n\n```\n#!bash\nsudo pip install setuptools\nsudo pip install six\nsudo python setup.py install\nsudo python setup.py clean\n```\n\n### Citation\nIf you use this library in your work, please cite \n```\n  @misc{tf_cnnvis,\n    author = {Bhagyesh Vikani, Falak Shah},\n    title = {CNN Visualization},\n    year = {2017},\n    howpublished = {\\url{https://github.com/InFoCusp/tf_cnnvis/}},\n    doi = {10.5281/zenodo.2594491}\n  }\n```\n\n## API\n**tf_cnnvis.activation_visualization(graph_or_path, value_feed_dict, input_tensor=None, layers='r', path_logdir='./Log', path_outdir='./Output')** \n\nThe function to generate the activation visualizations of the input image at the given layer.\n#### Parameters\n* graph_or_path (tf.Graph object or String) – TF graph or [Path-to-saved-graph] as String containing the CNN.\n* value_feed_dict (dict) – Values of placeholders to feed while evaluating the graph\n    * dict : {placeholder1 : value1, ...}\n\n* input_tensor (tf.tensor object (Default = None)) – tf.tensor (input tensor to the model - where images enter into the models) Note: This is not a standalone tensor/placeholder separate from the model\n* layers (list or String (Default = 'r')) – \n    * layerName : Reconstruction from a layer specified by name \n    * ‘r’ : Reconstruction from all the relu layers \n    * ‘p’ : Reconstruction from all the pooling layers \n    * ‘c’ : Reconstruction from all the convolutional layers\n* path_outdir (String (Default = \"./Output\")) – [path-to-dir] to save results into disk as images\n* path_logdir (String (Default = \"./Log\")) – [path-to-log-dir] to make log file for TensorBoard visualization\n\n#### Returns\n* is_success (boolean) – True if the function ran successfully. False otherwise\n\n**tf_cnnvis.deconv_visualization(graph_or_path, value_feed_dict, input_tensor=None, layers='r', path_logdir='./Log', path_outdir='./Output')** \n\nThe function to generate the visualizations of the input image reconstructed from the feature maps of a given layer.\n#### Parameters\n* graph_or_path (tf.Graph object or String) – TF graph or [Path-to-saved-graph] as String containing the CNN.\n* value_feed_dict (dict) – Values of placeholders to feed while evaluating the graph\n    * dict : {placeholder1 : value1, ...}\n\n* input_tensor (tf.tensor object (Default = None)) – tf.tensor (input tensor to the model - where images enter into the models) Note: This is not a standalone tensor/placeholder separate from the model\n* layers (list or String (Default = 'r')) – \n    * layerName : Reconstruction from a layer specified by name \n    * ‘r’ : Reconstruction from all the relu layers \n    * ‘p’ : Reconstruction from all the pooling layers \n    * ‘c’ : Reconstruction from all the convolutional layers\n* path_outdir (String (Default = \"./Output\")) – [path-to-dir] to save results into disk as images\n* path_logdir (String (Default = \"./Log\")) – [path-to-log-dir] to make log file for TensorBoard visualization\n\n#### Returns\n* is_success (boolean) – True if the function ran successfully. False otherwise\n\n**tf_cnnvis.deepdream_visualization(graph_or_path, value_feed_dict, layer, classes, input_tensor=None, path_logdir='./Log', path_outdir='./Output')** \n\nThe function to generate the visualizations of the input image reconstructed from the feature maps of a given layer.\n#### Parameters\n* graph_or_path (tf.Graph object or String) – TF graph or [Path-to-saved-graph] as String containing the CNN.\n* value_feed_dict (dict) – Values of placeholders to feed while evaluating the graph\n    * dict : {placeholder1 : value1, ...}\n\n* layer (String) - name of a layer in TF graph\n* classes (List) - list featuremap index for the class classification layer\n* input_tensor (tf.tensor object (Default = None)) – tf.tensor (input tensor to the model - where images enter into the models) Note: This is not a standalone tensor/placeholder separate from the model\n* path_outdir (String (Default = \"./Output\")) – [path-to-dir] to save results into disk as images\n* path_logdir (String (Default = \"./Log\")) – [path-to-log-dir] to make log file for TensorBoard visualization\n\n#### Returns\n* is_success (boolean) – True if the function ran successfully. False otherwise\n\n## To visualize in TensorBoard\nTo start Tensorflow, run the following command on the console\n\n```\n#!bash\n\ntensorboard --logdir=./Log\n```\n\nand on the TensorBoard homepage look under the *Images* tab\n\n## Additional helper functions\n### tf_cnnvis.utils.image_normalization(image, ubound=255.0, epsilon=1e-07)\nPerforms Min-Max image normalization. Transforms the pixel intensity values to range [0, ubound]\n#### Parameters\n* image (3-D numpy array) – A numpy array to normalize\n* ubound (float (Default = 255.0)) – upperbound for a image pixel value\n\n#### Returns\n* norm_image (3-D numpy array) – The normalized image\n\n### tf_cnnvis.utils.convert_into_grid(Xs, padding=1, ubound=255.0)\nConvert 4-D numpy array into a grid of images for display\n#### Parameters\n* Xs (4-D numpy array (first axis contations an image)) – The 4D array of images to put onto grid\n* padding (int (Default = 1)) – Spacing between grid cells\n* ubound (float (Default = 255.0)) – upperbound for a image pixel value\n\n#### Returns\n* (3-D numpy array) – A grid of input images\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FInFoCusp%2Ftf_cnnvis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FInFoCusp%2Ftf_cnnvis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FInFoCusp%2Ftf_cnnvis/lists"}