pytorchsummary


Namepytorchsummary JSON
Version 1.3.0 PyPI version JSON
download
home_pagehttps://github.com/GSAUC3/pytorch-model-details
SummarySummary of PyTorch Models just like `model.summary() in Keras
upload_time2022-08-10 11:09:55
maintainer
docs_urlNone
authorRajarshi Banerjee
requires_python
licenseMIT
keywords python pytorch pytorch model summary pytorch parameter summary
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PyTorch Model Parameters Summary

#### Install using pip
```
pip install pytorchsummary
```
## Example 1

```python
from torch import nn
from pytorchsummary import summary

class CNNET(nn.Module):
    def __init__(self):
        super(CNNET,self).__init__()

        self.layer = nn.Sequential(
            nn.Conv2d(3,16,5), # 28-5+1
            nn.ReLU(), #24
            nn.MaxPool2d(2,2), # 12

            nn.Conv2d(16,32,3), # 12+1-3
            nn.ReLU(), # 10
            nn.MaxPool2d(2,2), # 5
            

            nn.Conv2d(32,64,5), # 11-3+1
            nn.ReLU(),

            nn.Conv2d(64,10,1)   
        )
    
    def forward(self,x):
        x = self.layer(x)
        return x

m = CNNET()
summary((3,128,128),m) 
```

### Output
```               Layer	Output Shape        	    Kernal Shape    	#params             	#(weights + bias)   	requires_grad
------------------------------------------------------------------------------------------------------------------------------------------------------
            Conv2d-1	[1, 16, 124, 124]   	   [16, 3, 5, 5]    	1216                	(1200 + 16)         	True True 
              ReLU-2	[1, 16, 124, 124]   	                    	                    	                    	          
         MaxPool2d-3	[1, 16, 62, 62]     	                    	                    	                    	          
            Conv2d-4	[1, 32, 60, 60]     	   [32, 16, 3, 3]   	4640                	(4608 + 32)         	True True 
              ReLU-5	[1, 32, 60, 60]     	                    	                    	                    	          
         MaxPool2d-6	[1, 32, 30, 30]     	                    	                    	                    	          
            Conv2d-7	[1, 64, 26, 26]     	   [64, 32, 5, 5]   	51264               	(51200 + 64)        	True True 
              ReLU-8	[1, 64, 26, 26]     	                    	                    	                    	          
            Conv2d-9	[1, 10, 26, 26]     	   [10, 64, 1, 1]   	650                 	(640 + 10)          	True True 
______________________________________________________________________________________________________________________________________________________

Total parameters 57,770
Total Non-Trainable parameters 0
Total Trainable parameters 57,770
(57770, 57770, 0)
```

```python
for i,j in enumerate(m.parameters()):
    if i==2:
        break
    j.requires_grad=False 
summary((3,128,128),m,border=True) 

```
```
              Layer	Output Shape        	    Kernal Shape    	#params             	#(weights + bias)   	requires_grad
------------------------------------------------------------------------------------------------------------------------------------------------------
            Conv2d-1	[1, 16, 124, 124]   	   [16, 3, 5, 5]    	1216                	(1200 + 16)         	False False
______________________________________________________________________________________________________________________________________________________
              ReLU-2	[1, 16, 124, 124]   	                    	                    	                    	          
______________________________________________________________________________________________________________________________________________________
         MaxPool2d-3	[1, 16, 62, 62]     	                    	                    	                    	          
______________________________________________________________________________________________________________________________________________________
            Conv2d-4	[1, 32, 60, 60]     	   [32, 16, 3, 3]   	4640                	(4608 + 32)         	True True 
______________________________________________________________________________________________________________________________________________________
              ReLU-5	[1, 32, 60, 60]     	                    	                    	                    	          
______________________________________________________________________________________________________________________________________________________
         MaxPool2d-6	[1, 32, 30, 30]     	                    	                    	                    	          
______________________________________________________________________________________________________________________________________________________
            Conv2d-7	[1, 64, 26, 26]     	   [64, 32, 5, 5]   	51264               	(51200 + 64)        	True True 
______________________________________________________________________________________________________________________________________________________
              ReLU-8	[1, 64, 26, 26]     	                    	                    	                    	          
______________________________________________________________________________________________________________________________________________________
            Conv2d-9	[1, 10, 26, 26]     	   [10, 64, 1, 1]   	650                 	(640 + 10)          	True True 
______________________________________________________________________________________________________________________________________________________
______________________________________________________________________________________________________________________________________________________

Total parameters 57,770
Total Non-Trainable parameters 1,216
Total Trainable parameters 56,554
(56554, 57770, 1216)
```



## Example 2
```python
from torchvision import models
from pytorchsummary import summary

m = models.alexnet(False)
summary((3,224,224),m)
# this function returns the total number of 
# parameters (int) in a model
```
### ouput
```
               Layer	Output Shape        	    Kernal Shape    	#params             	#(weights + bias)   	requires_grad
------------------------------------------------------------------------------------------------------------------------------------------------------
            Conv2d-1	[1, 64, 55, 55]     	  [64, 3, 11, 11]   	23296               	(23232 + 64)        	True True 
              ReLU-2	[1, 64, 55, 55]     	                    	                    	                    	          
         MaxPool2d-3	[1, 64, 27, 27]     	                    	                    	                    	          
            Conv2d-4	[1, 192, 27, 27]    	  [192, 64, 5, 5]   	307392              	(307200 + 192)      	True True 
              ReLU-5	[1, 192, 27, 27]    	                    	                    	                    	          
         MaxPool2d-6	[1, 192, 13, 13]    	                    	                    	                    	          
            Conv2d-7	[1, 384, 13, 13]    	  [384, 192, 3, 3]  	663936              	(663552 + 384)      	True True 
              ReLU-8	[1, 384, 13, 13]    	                    	                    	                    	          
            Conv2d-9	[1, 256, 13, 13]    	  [256, 384, 3, 3]  	884992              	(884736 + 256)      	True True 
             ReLU-10	[1, 256, 13, 13]    	                    	                    	                    	          
           Conv2d-11	[1, 256, 13, 13]    	  [256, 256, 3, 3]  	590080              	(589824 + 256)      	True True 
             ReLU-12	[1, 256, 13, 13]    	                    	                    	                    	          
        MaxPool2d-13	[1, 256, 6, 6]      	                    	                    	                    	          
AdaptiveAvgPool2d-14	[1, 256, 6, 6]      	                    	                    	                    	          
          Dropout-15	[1, 9216]           	                    	                    	                    	          
           Linear-16	[1, 4096]           	    [4096, 9216]    	37752832            	(37748736 + 4096)   	True True 
             ReLU-17	[1, 4096]           	                    	                    	                    	          
          Dropout-18	[1, 4096]           	                    	                    	                    	          
           Linear-19	[1, 4096]           	    [4096, 4096]    	16781312            	(16777216 + 4096)   	True True 
             ReLU-20	[1, 4096]           	                    	                    	                    	          
           Linear-21	[1, 1000]           	    [1000, 4096]    	4097000             	(4096000 + 1000)    	True True 
______________________________________________________________________________________________________________________________________________________

Total parameters 61,100,840
Total Non-Trainable parameters 0
Total Trainable parameters 61,100,840
(61100840, 61100840, 0)
```

### Calculating the number of specific layer, or layer frequencies
```python
from pytorchsummary import get_num_layers
print(get_num_layers(m)) # alexnet model 
```
Output:
```
{'Conv2d': 5,
 'ReLU': 7,
 'MaxPool2d': 3,
 'AdaptiveAvgPool2d': 1,
 'Dropout': 2,
 'Linear': 3}
 ```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/GSAUC3/pytorch-model-details",
    "name": "pytorchsummary",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "python,PyTorch,Pytorch model summary,Pytorch parameter summary",
    "author": "Rajarshi Banerjee",
    "author_email": "raju.banerjee.720@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/ba/14/488e8a6489c802c5544580faf0a3cc6a572ed8462c64deca4e382ede4120/pytorchsummary-1.3.0.tar.gz",
    "platform": null,
    "description": "# PyTorch Model Parameters Summary\n\n#### Install using pip\n```\npip install pytorchsummary\n```\n## Example 1\n\n```python\nfrom torch import nn\nfrom pytorchsummary import summary\n\nclass CNNET(nn.Module):\n    def __init__(self):\n        super(CNNET,self).__init__()\n\n        self.layer = nn.Sequential(\n            nn.Conv2d(3,16,5), # 28-5+1\n            nn.ReLU(), #24\n            nn.MaxPool2d(2,2), # 12\n\n            nn.Conv2d(16,32,3), # 12+1-3\n            nn.ReLU(), # 10\n            nn.MaxPool2d(2,2), # 5\n            \n\n            nn.Conv2d(32,64,5), # 11-3+1\n            nn.ReLU(),\n\n            nn.Conv2d(64,10,1)   \n        )\n    \n    def forward(self,x):\n        x = self.layer(x)\n        return x\n\nm = CNNET()\nsummary((3,128,128),m) \n```\n\n### Output\n```               Layer\tOutput Shape        \t    Kernal Shape    \t#params             \t#(weights + bias)   \trequires_grad\n------------------------------------------------------------------------------------------------------------------------------------------------------\n            Conv2d-1\t[1, 16, 124, 124]   \t   [16, 3, 5, 5]    \t1216                \t(1200 + 16)         \tTrue True \n              ReLU-2\t[1, 16, 124, 124]   \t                    \t                    \t                    \t          \n         MaxPool2d-3\t[1, 16, 62, 62]     \t                    \t                    \t                    \t          \n            Conv2d-4\t[1, 32, 60, 60]     \t   [32, 16, 3, 3]   \t4640                \t(4608 + 32)         \tTrue True \n              ReLU-5\t[1, 32, 60, 60]     \t                    \t                    \t                    \t          \n         MaxPool2d-6\t[1, 32, 30, 30]     \t                    \t                    \t                    \t          \n            Conv2d-7\t[1, 64, 26, 26]     \t   [64, 32, 5, 5]   \t51264               \t(51200 + 64)        \tTrue True \n              ReLU-8\t[1, 64, 26, 26]     \t                    \t                    \t                    \t          \n            Conv2d-9\t[1, 10, 26, 26]     \t   [10, 64, 1, 1]   \t650                 \t(640 + 10)          \tTrue True \n______________________________________________________________________________________________________________________________________________________\n\nTotal parameters 57,770\nTotal Non-Trainable parameters 0\nTotal Trainable parameters 57,770\n(57770, 57770, 0)\n```\n\n```python\nfor i,j in enumerate(m.parameters()):\n    if i==2:\n        break\n    j.requires_grad=False \nsummary((3,128,128),m,border=True) \n\n```\n```\n              Layer\tOutput Shape        \t    Kernal Shape    \t#params             \t#(weights + bias)   \trequires_grad\n------------------------------------------------------------------------------------------------------------------------------------------------------\n            Conv2d-1\t[1, 16, 124, 124]   \t   [16, 3, 5, 5]    \t1216                \t(1200 + 16)         \tFalse False\n______________________________________________________________________________________________________________________________________________________\n              ReLU-2\t[1, 16, 124, 124]   \t                    \t                    \t                    \t          \n______________________________________________________________________________________________________________________________________________________\n         MaxPool2d-3\t[1, 16, 62, 62]     \t                    \t                    \t                    \t          \n______________________________________________________________________________________________________________________________________________________\n            Conv2d-4\t[1, 32, 60, 60]     \t   [32, 16, 3, 3]   \t4640                \t(4608 + 32)         \tTrue True \n______________________________________________________________________________________________________________________________________________________\n              ReLU-5\t[1, 32, 60, 60]     \t                    \t                    \t                    \t          \n______________________________________________________________________________________________________________________________________________________\n         MaxPool2d-6\t[1, 32, 30, 30]     \t                    \t                    \t                    \t          \n______________________________________________________________________________________________________________________________________________________\n            Conv2d-7\t[1, 64, 26, 26]     \t   [64, 32, 5, 5]   \t51264               \t(51200 + 64)        \tTrue True \n______________________________________________________________________________________________________________________________________________________\n              ReLU-8\t[1, 64, 26, 26]     \t                    \t                    \t                    \t          \n______________________________________________________________________________________________________________________________________________________\n            Conv2d-9\t[1, 10, 26, 26]     \t   [10, 64, 1, 1]   \t650                 \t(640 + 10)          \tTrue True \n______________________________________________________________________________________________________________________________________________________\n______________________________________________________________________________________________________________________________________________________\n\nTotal parameters 57,770\nTotal Non-Trainable parameters 1,216\nTotal Trainable parameters 56,554\n(56554, 57770, 1216)\n```\n\n\n\n## Example 2\n```python\nfrom torchvision import models\nfrom pytorchsummary import summary\n\nm = models.alexnet(False)\nsummary((3,224,224),m)\n# this function returns the total number of \n# parameters (int) in a model\n```\n### ouput\n```\n               Layer\tOutput Shape        \t    Kernal Shape    \t#params             \t#(weights + bias)   \trequires_grad\n------------------------------------------------------------------------------------------------------------------------------------------------------\n            Conv2d-1\t[1, 64, 55, 55]     \t  [64, 3, 11, 11]   \t23296               \t(23232 + 64)        \tTrue True \n              ReLU-2\t[1, 64, 55, 55]     \t                    \t                    \t                    \t          \n         MaxPool2d-3\t[1, 64, 27, 27]     \t                    \t                    \t                    \t          \n            Conv2d-4\t[1, 192, 27, 27]    \t  [192, 64, 5, 5]   \t307392              \t(307200 + 192)      \tTrue True \n              ReLU-5\t[1, 192, 27, 27]    \t                    \t                    \t                    \t          \n         MaxPool2d-6\t[1, 192, 13, 13]    \t                    \t                    \t                    \t          \n            Conv2d-7\t[1, 384, 13, 13]    \t  [384, 192, 3, 3]  \t663936              \t(663552 + 384)      \tTrue True \n              ReLU-8\t[1, 384, 13, 13]    \t                    \t                    \t                    \t          \n            Conv2d-9\t[1, 256, 13, 13]    \t  [256, 384, 3, 3]  \t884992              \t(884736 + 256)      \tTrue True \n             ReLU-10\t[1, 256, 13, 13]    \t                    \t                    \t                    \t          \n           Conv2d-11\t[1, 256, 13, 13]    \t  [256, 256, 3, 3]  \t590080              \t(589824 + 256)      \tTrue True \n             ReLU-12\t[1, 256, 13, 13]    \t                    \t                    \t                    \t          \n        MaxPool2d-13\t[1, 256, 6, 6]      \t                    \t                    \t                    \t          \nAdaptiveAvgPool2d-14\t[1, 256, 6, 6]      \t                    \t                    \t                    \t          \n          Dropout-15\t[1, 9216]           \t                    \t                    \t                    \t          \n           Linear-16\t[1, 4096]           \t    [4096, 9216]    \t37752832            \t(37748736 + 4096)   \tTrue True \n             ReLU-17\t[1, 4096]           \t                    \t                    \t                    \t          \n          Dropout-18\t[1, 4096]           \t                    \t                    \t                    \t          \n           Linear-19\t[1, 4096]           \t    [4096, 4096]    \t16781312            \t(16777216 + 4096)   \tTrue True \n             ReLU-20\t[1, 4096]           \t                    \t                    \t                    \t          \n           Linear-21\t[1, 1000]           \t    [1000, 4096]    \t4097000             \t(4096000 + 1000)    \tTrue True \n______________________________________________________________________________________________________________________________________________________\n\nTotal parameters 61,100,840\nTotal Non-Trainable parameters 0\nTotal Trainable parameters 61,100,840\n(61100840, 61100840, 0)\n```\n\n### Calculating the number of specific layer, or layer frequencies\n```python\nfrom pytorchsummary import get_num_layers\nprint(get_num_layers(m)) # alexnet model \n```\nOutput:\n```\n{'Conv2d': 5,\n 'ReLU': 7,\n 'MaxPool2d': 3,\n 'AdaptiveAvgPool2d': 1,\n 'Dropout': 2,\n 'Linear': 3}\n ```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Summary of PyTorch Models just like `model.summary() in Keras",
    "version": "1.3.0",
    "split_keywords": [
        "python",
        "pytorch",
        "pytorch model summary",
        "pytorch parameter summary"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "8038266d54fbdb6f60dac2068fecbe66",
                "sha256": "0463ee021c92a5144e956f5edabb74e44d796cad627454d4829488b77fc64b9b"
            },
            "downloads": -1,
            "filename": "pytorchsummary-1.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8038266d54fbdb6f60dac2068fecbe66",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5010,
            "upload_time": "2022-08-10T11:09:53",
            "upload_time_iso_8601": "2022-08-10T11:09:53.142951Z",
            "url": "https://files.pythonhosted.org/packages/d9/4c/03701317ba3a23deb99b3ce8c8581103c19c0d83ba4bea6a9613f6717798/pytorchsummary-1.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "178bf240b5c876e8056d7dc0df0abeae",
                "sha256": "60139081ede1db84178507059572482bed47ff67cc686a0173ddde09a81a3025"
            },
            "downloads": -1,
            "filename": "pytorchsummary-1.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "178bf240b5c876e8056d7dc0df0abeae",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4567,
            "upload_time": "2022-08-10T11:09:55",
            "upload_time_iso_8601": "2022-08-10T11:09:55.162860Z",
            "url": "https://files.pythonhosted.org/packages/ba/14/488e8a6489c802c5544580faf0a3cc6a572ed8462c64deca4e382ede4120/pytorchsummary-1.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-08-10 11:09:55",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "GSAUC3",
    "github_project": "pytorch-model-details",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pytorchsummary"
}
        
Elapsed time: 0.03506s