rmayormartins commited on
Commit
eaf12d1
Β·
1 Parent(s): 44f92e0

Subindo arquivos

Browse files
Files changed (4) hide show
  1. README.md +38 -3
  2. app.py +89 -0
  3. mobilenetv2_035.pth +3 -0
  4. requirements.txt +3 -0
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
- title: Pth Scope
3
- emoji: πŸ“š
4
  colorFrom: pink
5
  colorTo: gray
6
  sdk: gradio
@@ -10,4 +10,39 @@ pinned: false
10
  license: ecl-2.0
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Pth-Scope
3
+ emoji: πŸ”Ž
4
  colorFrom: pink
5
  colorTo: gray
6
  sdk: gradio
 
10
  license: ecl-2.0
11
  ---
12
 
13
+ ## PTH-Scope
14
+
15
+ Analysis of PyTorch `.pth` files used in model training with PyTorch, FastAI, and TIMM.
16
+
17
+ ## Developer
18
+
19
+ ## Developer
20
+
21
+ Developed by Ramon Mayor Martins (2023)
22
+
23
+ - E-mail: [rmayormartins@gmail.com](mailto:rmayormartins@gmail.com)
24
+ - Homepage: [https://rmayormartins.github.io/](https://rmayormartins.github.io/)
25
+ - Twitter: [@rmayormartins](https://twitter.com/rmayormartins)
26
+ - GitHub: [https://github.com/rmayormartins](https://github.com/rmayormartins)
27
+
28
+ ## Description
29
+
30
+ **PTH-Scope** is an tool designed to provide a detailed analysis of `.pth` files, which are model files typically used in PyTorch for neural network training. This tool helps users to understand the architecture and parameters of neural networks, including layer count, types of layers, total parameters, file size, and inferred architecture.
31
+
32
+ ## How to Use
33
+
34
+ 1. **Starting the Application:** Launch PTH-Scope by running the script.
35
+ 2. **Uploading Files:** Click on the 'Upload .PTH File' button to select the `.pth` file you wish to analyze.
36
+ 3. **Analyzing the File:** After selecting the file, the tool will process it and display information such as the layer count, total parameters, file size, main layers, and shapes of the first and last layers.
37
+ 4. **Viewing the Results:** The results will be displayed in a structured text format, providing insights into the neural network's architecture and complexity.
38
+ 5. **Interpreting the Data:** The results are presented in an easy-to-understand manner. Review the data to gain insights into the neural network's design and parameters.
39
+ 6. **Error Handling:** In case of any errors, such as incompatible file formats or corrupt files, the tool will display an error message, guiding you to troubleshoot.
40
+
41
+ Feel free to upload different `.pth` files to explore the capabilities of PTH-Scope and deepen your understanding of neural network architectures.
42
+
43
+ Contact me if you have any questions or suggestions!
44
+
45
+ ## Other
46
+
47
+ Check out the configuration reference at [Hugging Face Spaces Config Reference](https://huggingface.co/docs/hub/spaces-config-reference).
48
+
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import os
4
+ import json
5
+ import pandas as pd
6
+
7
+ #
8
+ def count_layers(model_state):
9
+ layer_types = {'weight': 0, 'bias': 0, 'running_mean': 0, 'running_var': 0}
10
+ total_parameters = 0
11
+ for key, tensor in model_state.items():
12
+ if torch.is_tensor(tensor):
13
+ total_parameters += torch.numel(tensor)
14
+ if 'weight' in key:
15
+ layer_types['weight'] += 1
16
+ elif 'bias' in key:
17
+ layer_types['bias'] += 1
18
+ elif 'running_mean' in key:
19
+ layer_types['running_mean'] += 1
20
+ elif 'running_var' in key:
21
+ layer_types['running_var'] += 1
22
+ return layer_types, total_parameters
23
+
24
+ #
25
+ def infer_architecture(layer_names):
26
+ if any("res" in name for name in layer_names):
27
+ return "ResNet"
28
+ elif any("dw" in name for name in layer_names):
29
+ return "MobileNet"
30
+ elif any("efficient" in name for name in layer_names):
31
+ return "EfficientNet"
32
+ else:
33
+ return "Unknown or other"
34
+
35
+ #
36
+ def process_pth(file):
37
+ df = pd.DataFrame(columns=['Pth File', 'Information', 'Layer Counts', 'Total Parameters', 'File Size', 'Inferred Architecture'])
38
+ try:
39
+ model_state = torch.load(file.name, map_location='cpu')
40
+ if 'model' in model_state:
41
+ model_state = model_state['model']
42
+
43
+ #
44
+ layer_counts, total_parameters = count_layers(model_state)
45
+
46
+ #
47
+ inferred_architecture = infer_architecture(model_state.keys())
48
+
49
+ #
50
+ main_layers = [k for k in model_state.keys() if not any(sub in k for sub in ['bias', 'running_mean', 'running_var'])]
51
+ total_main_layers = len(main_layers)
52
+
53
+ #
54
+ first_tensor_key = list(model_state.keys())[0]
55
+ last_tensor_key = list(model_state.keys())[-1]
56
+ first_tensor = model_state[first_tensor_key]
57
+ last_tensor = model_state[last_tensor_key]
58
+
59
+ first_layer_shape = list(first_tensor.shape) if torch.is_tensor(first_tensor) else "Unknown"
60
+ last_layer_shape = list(last_tensor.shape) if torch.is_tensor(last_tensor) else "Unknown"
61
+
62
+ #
63
+ info = {
64
+ 'Layer Count': layer_counts,
65
+ 'Total Parameters': total_parameters,
66
+ 'File Size (KB)': os.path.getsize(file.name) // 1024,
67
+ 'Total Main Layers': total_main_layers,
68
+ 'First Layer Shape': first_layer_shape,
69
+ 'Last Layer Shape': last_layer_shape,
70
+ 'Inferred Architecture': inferred_architecture
71
+ }
72
+
73
+ return json.dumps(info, indent=4)
74
+ except Exception as e:
75
+ return f"Failed to process the file: {e}"
76
+
77
+ #Gradio
78
+ iface = gr.Interface(
79
+ fn=process_pth,
80
+ inputs=gr.File(label="Upload .PTH File"),
81
+ outputs="text",
82
+ title="PTH-Scope",
83
+ description="Upload a .PTH file to analyze its structure and parameters. A .PTH file is typically a PyTorch model file, which contains the state of a neural network trained using libraries like PyTorch. These files encapsulate the learned weights and biases of the model after training. ",
84
+ examples=[["mobilenetv2_035.pth"]]
85
+ )
86
+
87
+
88
+ #
89
+ iface.launch(debug=True)
mobilenetv2_035.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:23f89acc8b610bdf0d1c8d8257761173b2b9455ae5e91238c6dc67e289820ccd
3
+ size 5076821
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ torch
3
+ pandas