{"id":26370119,"url":"https://github.com/hari31416/torchlight","last_synced_at":"2025-03-16T23:18:02.568Z","repository":{"id":244616130,"uuid":"815763835","full_name":"Hari31416/TorchLight","owner":"Hari31416","description":"A module to visualize features learned by CNNs","archived":false,"fork":false,"pushed_at":"2024-09-16T15:31:26.000Z","size":28602,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-09-16T18:42:50.823Z","etag":null,"topics":["computer-vision","feature-visualization","mechanistic-interpretability"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Hari31416.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-16T04:38:55.000Z","updated_at":"2024-09-16T15:31:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"6e31cf3c-af72-4cf8-b5f3-2b65056f9d81","html_url":"https://github.com/Hari31416/TorchLight","commit_stats":null,"previous_names":["hari31416/machanistic-interpretability","hari31416/mechanistic-interpretability"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hari31416%2FTorchLight","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hari31416%2FTorchLight/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hari31416%2FTorchLight/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hari31416%2FTorchLight/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Hari31416","download_url":"https://codeload.github.com/Hari31416/TorchLight/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243945569,"owners_count":20372897,"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":["computer-vision","feature-visualization","mechanistic-interpretability"],"created_at":"2025-03-16T23:18:02.106Z","updated_at":"2025-03-16T23:18:02.548Z","avatar_url":"https://github.com/Hari31416.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TorchLight\n\n`torchlight` is a suit of optimization codes for visualizing what a layer, channel or neuron learns. The module is a result of my attempt to learn reverse-engineer feature visualization techniques. A large part of the module is either taken or insipired by the [lucent](https://github.com/greentfrapp/lucent/tree/dev) library (which, in turn, is [lucid](https://github.com/tensorflow/lucid), adapted for PyTorch.)\n\n## Usage\n\nThe module is very straightforward to use. Here is an example of how to visualize the 97th channel of the `mixed4a` layer of the InceptionV1 model.\n\n```python\n# import the main class that does the optimization\nfrom torchlight.optimize import FeatureViz\n# import the model\nfrom torchlight.models import InceptionV1\n\n\n# load the model\nmodel = InceptionV1(pretrained=True, redirected_ReLU=True).eval()\n# create an instance of the FeatureViz class with objective as the 97th channel of the mixed4a layer\nfc = FeatureViz(model=model, objective=\"mixed4a:97\")\nimages = fc.visualize(thresholds=[512, 1024, 2028], show_last_image=True,lr=0.05)\n```\n\nThis will create a list of images that show the optimization process at different stages. The `thresholds` parameter is used to specify the number of iterations at which the image is saved. The `show_last_image` parameter is used to specify whether to show the last image or not. The `lr` parameter is used to specify the learning rate of the optimization process. `images` is a list of images that show the optimization process at different stages. The image at last index is the final image and should look like:\n\n![mixed4a, 97th channel](./images/mixed4a:97.png)\n\nIf you want more control over the optimization objective, you can use the `create_objective` function which internally calls two classes `Hook` and `Objective` to create the objective. Suppose you want to optmize a combination of two objectives, you can do the following:\n\n```python\nfrom torchlight.objective import create_objective\n\n# create the objective to visualize the 97th channel of the mixed4a layer\nobjective_1 = create_objective(\"mixed4a:97\")\n# create the objective to visualize the 143rd channel of the mixed5a layer\nobjective_2 = create_objective(\"mixed5a:143\")\n# A number of operators are overloaded to allow for easy addition of objectives\nobjective = objective_1 + objective_2\n# create an instance of the FeatureViz class with the objective\nfc = FeatureViz(model=model, objective=objective)\nimages = fc.visualize(thresholds=[512, 1024, 2028], show_last_image=True,lr=0.05)\n```\n\nHere is the final image:\n\n![mixed4a, 97th channel + mixed5a, 143rd channel](./images/mixed4a:97_mixed5a:143.png)\n\nThe module implemets methods to get parameterize image, combine objectives in an arbitrary ways, apply different transformations to the image, etc. A number of interesting visualizations can be created using this module. A couple of examples and the code to generate them are show below.\n\n### Positive and Negative Optimization\n\n```python\nfrom torchlight.io import show_images\n# create two objectives with the same layer and channel\nobjective_1 = create_objective(\"mixed4b:154\", batch=0)\nobjective_2 = create_objective(\"mixed4b:154\", batch=1)\n# maximizes the activation for the first image\n# and minimizes the activation for the second image\nobjective = objective_1 - objective_2\nfv = FeatureViz(model=model, objective=objective)\n\n# create a parameterize image with two batches\nparam_f = lambda: get_image(w=224, batch=2, decorrelate=True)\n\nimages = fv.visualize(param_f, show_last_image=True, show_progress=True, thresholds=[512, 1024], lr=0.05)\nshow_images(images[-1], [\"Positive\", \"Negative\"])\n```\n\nThis will create an image that maximizes the activation for the first image and minimizes the activation for the second image. The optimized images are:\n\n![mixed4b, 154th channel, batch 0](./images/mixed4b:154_pos_neg.png)\n\n### Interpolate Between two Channels\n\n```python\n# Choose two channels to interpolate between\nneuron1 = \"mixed4a:479\"\nneuron2 = \"mixed4b:360\"\n\n# The image should have three batches\nparam_f = lambda: get_image(w=224, batch=3, decorrelate=True)\n\n# create the objective to interpolate between the two channels\nobjective = (\n    create_objective(neuron1, batch=0) # only neuron1\n    + create_objective(neuron2, batch=1) # only neuron2\n    + create_objective(neuron1, batch=2) # interpolate between neuron1 and neuron2\n    + create_objective(neuron2, batch=2) # interpolate between neuron1 and neuron2\n)\nfv = FeatureViz(model=model, objective=objective)\nimages = fv.visualize(\n    param_f,\n    show_last_image=False,\n    show_progress=True,\n    thresholds=[512, 1024],\n    lr=0.05,\n)\n# show the images with the two neurons and the interpolation\nshow_images(images[-1], [neuron1, neuron2, neuron1+ \" and \"+ neuron2])\n```\n\nHere is the final image:\n\n![mixed4a, 479th channel](./images/interpolate.png)\n\n### Using CPPN Parameterized Image\n\nThe module also supports using a CPPN (Compositional Pattern Producing Network) to parameterize the image. Here is an example of how to use a CPPN to parameterize the image.\n\n```python\nfrom torchlight.image import cppn\nfc = FeatureViz(model=model, objective=\"mixed4b_3x3_pre_relu_conv:77\")\nimages = fc.visualize(param_f = lambda : cppn(224), show_last_image=True, show_progress=True, thresholds=thresholds, lr=0.002)\n```\n\nHere is the final image:\n\n![cppn_mixed4b_pool_reduce_pre_relu_conv_16](./images/cppn_mixed4b_pool_reduce_pre_relu_conv_16.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhari31416%2Ftorchlight","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhari31416%2Ftorchlight","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhari31416%2Ftorchlight/lists"}