-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
137 lines (108 loc) · 4.06 KB
/
Copy pathutils.py
File metadata and controls
137 lines (108 loc) · 4.06 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import torch
import torchvision
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
import numpy as np
from prettytable import PrettyTable
class UnNormalize(object):
""" Un-normalize a tensor image. Useful to visualize the original image."""
def __init__(self, mean: tuple, std: tuple):
""" Initialize the UnNormalize object with a tuple of mean and standard deviation values.
The length of each tuple must be equal to the number of channels in the image to un-normalize.
Parameters
----------
mean : tuple
The mean value for each channel
std : tuple
The standard deviation for each channel
"""
self.mean = mean
self.std = std
def __call__(self, tensor: torch.Tensor) -> torch.Tensor:
"""
Parameters
----------
tensor : torch.Tensor
Image of size (C, H, W) to be un-normalized.
Returns
-------
torch.Tensor: Normalized image.
"""
for t, m, s in zip(tensor, self.mean, self.std):
t.mul_(s).add_(m)
# The normalize code -> t.sub_(m).div_(s)
return tensor
def get_device():
""" Get the device (CPU or GPU) that is available """
if torch.cuda.is_available():
return torch.device("cuda")
elif torch.backends.mps.is_available():
return torch.device("mps")
return torch.device("cpu")
def get_conv_out_dim(input_size, kernel_size, padding, stride):
""" Compute the dimension of a convolutional layer, assuming the image is squared
If several layers, the parameters must be lists of the same length. The code assumes that the
parameters are ordered from the first layer to the last.
Parameters
----------
input_size : int
Width (or height) of the square image
kernel_size : int or list
padding : int or list
stride : int or list
Returns
-------
The output width of the convolutional layer
Examples
--------
>>> get_conv_out_dim(32, 3, 1, 1) # output size of a 3x3 convolution with padding of 1 and stride of 1
32
>>> get_conv_out_dim(32, [5, 3], [1, 0], [1, 1]) # 5x5 convolution followed by 3x3 convolution
28
"""
if isinstance(kernel_size, tuple) or isinstance(kernel_size, list):
if len(kernel_size) != len(padding) or len(kernel_size) != len(stride) or len(kernel_size) < 1:
raise ValueError("kernel_size, padding and stride must have the same length")
for k, p, s in zip(kernel_size, padding, stride):
if s < 1:
raise ValueError("stride must be greater than 0")
# recursive call for sake of simplicity
input_size = get_conv_out_dim(input_size, k, p, s)
return input_size
# at this point stride should be a list
if stride < 1:
raise ValueError("stride must be greater than 0")
return int((input_size - kernel_size + 2 * padding) / stride + 1)
def get_input_size(loader):
""" Get the size of the input images from a torchvision DataLoader """
images, _ = next(iter(loader))
input_size = images.shape
return input_size
def count_parameters(model, verbose=True):
""" Count the number of parameters in a model
@see: https://stackoverflow.com/questions/49201236/check-the-total-number-of-parameters-in-a-pytorch-model
Parameters
----------
model : torch.nn.Module
The model to count the parameters from
verbose : bool
Whether to print the parameters or not
Returns
-------
PrettyTable:
A table with the model parameters
int:
The total number of parameters
"""
table = PrettyTable(["Modules", "Parameters"])
total_params = 0
for name, parameter in model.named_parameters():
if not parameter.requires_grad:
continue
params = parameter.numel()
table.add_row([name, params])
total_params += params
if verbose:
print(table)
print(f"Total Trainable Params: {total_params}")
return table, total_params