import math
def count_conv(operation, input_size, k, d, s, p):
input_size =[int(x1)for x1 in input_size]
H_out, W_out = None, None
iflen(input_size)!=2:print("The height and width of the data is wrong!")return None
k =[int(x2)for x2 in k]iflen(k)==1:
k =2* k
else:print("The kernel size is wrong!")return None
d =[int(x3)for x3 in d]iflen(d)==1:
d =2* d
else:print("The dilatation is wrong!")return None
s =[int(x4)for x4 in s]iflen(s)==1:
s =2* s
else:print("The stride is wrong!")return None
if p[0]=='same':if operation =='conv':
H_out = math.floor(input_size[0]/ s[0])
W_out = math.floor(input_size[1]/ s[1])
elif operation =='maxpool':
H_out = math.ceil(input_size[0]/ s[0])
W_out = math.ceil(input_size[1]/ s[1])return[H_out, W_out]
elif p[0]=='valid':
p =[0,0]else:
p =[int(x5)for x5 in p]ifisinstance(p[0], int):iflen(p)==1:
p =2* p
else:print("The padding is wrong!")return None
if operation =='conv':
H_out = math.floor((input_size[0]+2* p[0]- d[0]*(k[0]-1)-1)/ s[0]+1)
W_out = math.floor((input_size[1]+2* p[1]- d[1]*(k[1]-1)-1)/ s[1]+1)
elif operation =='maxpool':
H_out = math.ceil((input_size[0]+2* p[0]- d[0]*(k[0]-1)-1)/ s[0]+1)
W_out = math.ceil((input_size[1]+2* p[1]- d[1]*(k[1]-1)-1)/ s[1]+1)return[H_out, W_out]if __name__ =="__main__":
operation =input("DL operation is: (input format: 'conv' or 'maxpool') ")if operation =='conv':
input_size =input("The height and width of the data: (input format: H W) ").split()
k =input("The kernel size of conv: ").split()
d =input("The dilatation of kernel: (Default: 1) ").split()
s =input("The stride of conv: (Default: 1) ").split()
p =input("The padding of conv: (Default: 0) ").split()
out_size =count_conv(operation, input_size, k, d, s, p)print("The height and width of the output data is ([H_out, W_out]) ", out_size)
elif operation =='maxpool':
input_size =input("The height and width of the data: (input format: H W) ").split()
k =input("The kernel size of conv: ").split()
d =input("The dilatation of kernel: (Default: 1) ").split()
s =input("The stride of conv: (Default: 1) ").split()
p =input("The padding of conv: (Default: 0) ").split()
out_size =count_conv(operation, input_size, k, d, s, p)print("The height and width of the output data is ([H_out, W_out]) ", out_size)