-
Notifications
You must be signed in to change notification settings - Fork 7
ANN-visualization-test [Do not merge] #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| {% load static %} | ||
|
|
||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Model Visualization - {{ model.id }}</title> | ||
| <script src="https://d3js.org/d3.v7.min.js"></script> | ||
|
|
||
| </head> | ||
| <script> | ||
| document.addEventListener("DOMContentLoaded", function() { | ||
| console.log("D3.js is loaded:", d3); | ||
| }); | ||
| </script> | ||
|
|
||
|
|
||
| <body> | ||
| <h1>Model Architecture Visualization (Model ID: {{ model.id }})</h1> | ||
|
|
||
| <div id="svg-wrapper" style="width: 100%; overflow-x: auto;"> | ||
| <svg width="1600" height="600"></svg> | ||
| </div> | ||
|
|
||
| <!-- Floating info box for layer metadata --> | ||
| <div id="layer-info-box" | ||
| style="display:none; position:absolute; z-index:1000; background:white; border:1px solid #ccc; | ||
| padding:10px; border-radius:8px; box-shadow:0 4px 8px rgba(0,0,0,0.2); font-size:14px;"> | ||
| </div> | ||
|
|
||
|
|
||
| {{ visualization_metadata|json_script:"layers-json" }} | ||
|
|
||
|
|
||
|
|
||
|
|
||
| <script src="{% static 'benchmarks/js/draw_layers.js' %}"></script> | ||
|
|
||
|
|
||
|
|
||
| </body> | ||
| </html> | ||
|
|
||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| # import json | ||
| # import os | ||
| # import torch | ||
| # from torchsummary import summary | ||
| # from io import StringIO | ||
| # from contextlib import redirect_stdout | ||
| # | ||
| # # Directory where model JSON files will be stored | ||
| # ARCHITECTURE_JSON_DIR = "static/model_architecture_json/" | ||
| # | ||
| # | ||
| # def extract_trainable_layers(model, model_id): | ||
| # """ | ||
| # Extracts trainable layers from a PyTorch model and saves them as a JSON file. | ||
| # - Trainable layers are those where `Param # != 0` | ||
| # - Uses `torchsummary.summary()` to get layer details. | ||
| # """ | ||
| # os.makedirs(ARCHITECTURE_JSON_DIR, exist_ok=True) | ||
| # | ||
| # # Redirect torchsummary output to a string buffer | ||
| # buffer = StringIO() | ||
| # with redirect_stdout(buffer): | ||
| # summary(model, input_size=(3, 224, 224)) # Assuming an image model with 3x224x224 input | ||
| # | ||
| # output = buffer.getvalue() | ||
| # lines = output.split("\n") # Convert output to lines | ||
| # | ||
| # model_params = { | ||
| # "Visualization-Layer-Parameters": {} | ||
| # } | ||
| # | ||
| # # Process each line and extract Layer Type, Output Shape, and Param # | ||
| # for line in lines[3:]: # Skip the header | ||
| # parts = line.split() | ||
| # if len(parts) < 4: # Ignore invalid lines | ||
| # continue | ||
| # | ||
| # layer_name = parts[0] # First column is Layer Type | ||
| # param_count = parts[-1] # Last column is Param # | ||
| # | ||
| # try: | ||
| # param_count = int(param_count) # Convert to integer | ||
| # except ValueError: | ||
| # continue # Skip if it's not a number | ||
| # | ||
| # if param_count > 0: # **Only keep trainable layers** | ||
| # model_params["Visualization-Layer-Parameters"][layer_name] = [param_count, param_count + 5] # Example range | ||
| # | ||
| # # Define JSON file path | ||
| # json_file_path = os.path.join(ARCHITECTURE_JSON_DIR, f"model_{model_id}.json") | ||
| # | ||
| # # Save JSON data | ||
| # with open(json_file_path, "w") as json_file: | ||
| # json.dump(model_params, json_file, indent=4) | ||
| # | ||
| # print(f"Model architecture saved at: {json_file_path}") | ||
| # | ||
| # | ||
| # if __name__ == "__main__": | ||
| # # Example: Use a pre-trained ResNet model for testing | ||
| # model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet50', pretrained=True) | ||
| # extract_trainable_layers(model, model_id="resnet50") | ||
|
|
||
| import json | ||
| import os | ||
| import torch | ||
| from torchsummary import summary | ||
| from io import StringIO | ||
| from contextlib import redirect_stdout | ||
|
|
||
| ARCHITECTURE_JSON_DIR = "static/model_architecture_json/" | ||
|
|
||
| import json | ||
| import os | ||
| import torch | ||
| from torchsummary import summary | ||
| from io import StringIO | ||
| from contextlib import redirect_stdout | ||
|
|
||
| ARCHITECTURE_JSON_DIR = "static/model_architecture_json/" | ||
|
|
||
|
|
||
| def extract_model_parameters(model, model_id): | ||
| """ | ||
| Extracts model layer details (Layer Name, Output Shape, Param #) and saves as JSON. | ||
| """ | ||
| os.makedirs(ARCHITECTURE_JSON_DIR, exist_ok=True) | ||
|
|
||
| model_params = {"layers": []} | ||
|
|
||
| buffer = StringIO() | ||
| with redirect_stdout(buffer): | ||
| summary(model, input_size=(3, 224, 224)) | ||
|
|
||
| output = buffer.getvalue() | ||
| lines = output.split("\n") | ||
|
|
||
| for line in lines[3:]: | ||
| parts = line.split() | ||
| if len(parts) < 4: | ||
| continue | ||
|
|
||
| print(parts) | ||
| layer_name = parts[0] | ||
| output_shape = parts[1] | ||
| param_count = parts[-1] | ||
|
|
||
| try: | ||
| param_count = int(param_count) | ||
| except ValueError: | ||
| continue | ||
|
|
||
| model_params["layers"].append({ | ||
| "layer_name": layer_name, | ||
| "output_shape": output_shape, | ||
| "param_count": param_count | ||
| }) | ||
|
|
||
|
|
||
| json_file_path = os.path.join(ARCHITECTURE_JSON_DIR, f"model_{model_id}.json") | ||
|
|
||
| with open(json_file_path, "w") as json_file: | ||
| json.dump(model_params, json_file, indent=4) | ||
|
|
||
| print(f"✅ Model architecture saved at: {json_file_path}") | ||
| print(json.dumps(model_params, indent=4)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet50', pretrained=True) | ||
| extract_model_parameters(model, model_id="1226") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| asgiref==3.7.2 | ||
| backports.zoneinfo==0.2.1 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This appears to be related to the use of Python 3.11 vs 3.8. Related to #346 |
||
| boto3==1.28.15 | ||
| botocore==1.31.15 | ||
| certifi==2023.7.22 | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.