-
Notifications
You must be signed in to change notification settings - Fork 4
/
pytorch_summary.py
108 lines (88 loc) · 2.87 KB
/
pytorch_summary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'''
Generates a summary of a model's layers and dimensionality
'''
import torch
from torch import nn
from torch.autograd import Variable
import numpy as np
import pandas as pd
class Summary(object):
def __init__(self, model, input_size=(1,1,256,256)):
'''
Generates summaries of model layers and dimensions.
'''
self.model = model
self.input_size = input_size
self.summarize()
print(self.summary)
def get_variable_sizes(self):
'''Run sample input through each layer to get output sizes'''
input_ = Variable(torch.FloatTensor(*self.input_size), volatile=True)
mods = list(self.model.modules())
in_sizes = []
out_sizes = []
for i in range(1, len(mods)):
m = mods[i]
out = m(input_)
in_sizes.append(np.array(input_.size()))
out_sizes.append(np.array(out.size()))
input_ = out
self.in_sizes = in_sizes
self.out_sizes = out_sizes
return
def get_layer_names(self):
'''Collect Layer Names'''
mods = list(self.model.named_modules())
names = []
layers = []
for m in mods[1:]:
names += [m[0]]
layers += [str(m[1].__class__)]
layer_types = [x.split('.')[-1][:-2] for x in layers]
self.layer_names = names
self.layer_types = layer_types
return
def get_parameter_sizes(self):
'''Get sizes of all parameters in `model`'''
mods = list(self.model.modules())
sizes = []
for i in range(1,len(mods)):
m = mods[i]
p = list(m.parameters())
modsz = []
for j in range(len(p)):
modsz.append(np.array(p[j].size()))
sizes.append(modsz)
self.param_sizes = sizes
return
def get_parameter_nums(self):
'''Get number of parameters in each layer'''
param_nums = []
for mod in self.param_sizes:
all_params = 0
for p in mod:
all_params += np.prod(p)
param_nums.append(all_params)
self.param_nums = param_nums
return
def summary(self):
'''
Makes a summary listing with:
Layer Name, Layer Type, Input Size, Output Size, Number of Parameters
'''
df = pd.DataFrame( np.zeros( (len(self.layer_names), 5) ) )
df.columns = ['Name', 'Type', 'InSz', 'OutSz', 'Params']
df['Name'] = self.layer_names
df['Type'] = self.layer_types
df['InSz'] = self.in_sizes
df['OutSz'] = self.out_sizes
df['Params'] = self.param_nums
self.summary = df
return
def summarize(self):
self.get_variable_sizes()
self.get_layer_names()
self.get_parameter_sizes()
self.get_parameter_nums()
self.summary()
return