-
Notifications
You must be signed in to change notification settings - Fork 0
/
dimensions.py
96 lines (82 loc) · 3.13 KB
/
dimensions.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
def calculate_output_dims(layer_type, input_dims, hyperparameters):
"""Calculates the output dimensions of a given layer
Keyword Arguments:
layer_type -- string specifying layer type
input_dims -- list containing the input dimensions, e.g. [W_in, H_in, D_in]
hyperparameters -- list containing the hyperparameters such as kernel number
"""
if layer_type == 'Conv':
return conv_output_dims(input_dims, hyperparameters)
elif layer_type == 'Pool':
return pool_output_dims(input_dims, hyperparameters)
else:
print('Unrecognized layer type')
def conv_output_dims(input_dims, hyperparameters):
"""Calculates the output dimensions of a given convolutional layer
Keyword Arguments:
input_dims -- list in format [w_in, h_in, d_in]
respectively corresponding to input width, height and depth
hyperparameters -- list in format [k, f, s, p]
respectively corresponding to number of kernels, kernel size, stride
and padding
"""
# parse input dimensions
w_in, h_in, d_in = input_dims
# parse hyperparameters
k, f, s, p = hyperparameters
# calculate output width
w_out = int((w_in - f + 2 * p) / s + 1)
# calculate output height
h_out = int((h_in - f + 2 * p) / s + 1)
# calculate output depth
d_out = int(k)
# return list of output dimensions
return [w_out, h_out, d_out]
def pool_output_dims(input_dims, hyperparameters):
"""Calculates the output dimensions of a given pooling layer
Keyword Arguments:
input_dims -- list in format [w_in, h_in, d_in]
respectively corresponding to input width, height and depth
hyperparameters -- list in format [f, s]
respectively corresponding to pooling extent and stride
"""
# parse input dimensions
w_in, h_in, d_in = input_dims
# parse hyperparameters
f, s = hyperparameters
# calculate output width
w_out = int((w_in - f) / s + 1)
# calculate output height
h_out = int((h_in - f) / s + 1)
# calculate output depth
d_out = int(d_in)
# return list of output dimensions
return [w_out, h_out, d_out]
while True:
print("Welcome to the NN layer dimensionality analyzer")
# get layer type
while True:
requested_type = input(
"Which layer type are you interested in? [Conv/Pool]: "
)
if requested_type in ['Conv', 'Pool']:
break
else:
print("Invalid layer type")
# get inputs dimensions
requested_input = input(
"Please enter the input dimensions separated by a space: "
).split()
# convert to integers
requested_input = list(map(int, requested_input))
# get hyperparameters
requested_hyperparameters = input(
"Please enter the hyperparameters separated by a space: "
).split()
# convert to integers
requested_hyperparameters = list(map(int, requested_hyperparameters))
# calculate output dimensions
output_dims = calculate_output_dims(
requested_type, requested_input, requested_hyperparameters
)
print("The output dimensions for your requested layer are {}.".format(output_dims))