Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions torchsummary/torchsummary.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np


def summary(model, input_size, batch_size=-1, device="cuda"):
def summary(model, input_size, batch_size=2, input_initializer=torch.rand, dtype=torch.float64, device="cuda"):

def register_hook(module):

Expand Down Expand Up @@ -47,17 +47,14 @@ def hook(module, input, output):
"cpu",
], "Input device is not valid, please specify 'cuda' or 'cpu'"

if device == "cuda" and torch.cuda.is_available():
dtype = torch.cuda.FloatTensor
else:
dtype = torch.FloatTensor
device = torch.device(device) if torch.cuda.is_available() else torch.device('cpu')

# multiple inputs to the network
if isinstance(input_size, tuple):
input_size = [input_size]

# batch_size of 2 for batchnorm
x = [torch.rand(2, *in_size).type(dtype) for in_size in input_size]
x = [input_initializer((batch_size, *in_size)).type(dtype).to(device) for in_size in input_size]
# print(type(x[0]))

# create properties
Expand Down Expand Up @@ -97,7 +94,7 @@ def hook(module, input, output):
print(line_new)

# assume 4 bytes/number (float on cuda).
total_input_size = abs(np.prod(input_size) * batch_size * 4. / (1024 ** 2.))
total_input_size = abs(np.prod([dimension for tensor_size in input_size for dimension in tensor_size]) * batch_size * 4. / (1024 ** 2.))
total_output_size = abs(2. * total_output * 4. / (1024 ** 2.)) # x2 for gradients
total_params_size = abs(total_params.numpy() * 4. / (1024 ** 2.))
total_size = total_params_size + total_output_size + total_input_size
Expand Down