본문 바로가기
AI | 딥러닝/Coding

[Python] 3x3 depthwise convolution 코드

by 고뭉나무 2023. 9. 9.

 

 

 

 

# loading the layer file
layer_name = 'intro.conv1.convs.2'
inp = np.load(f'npy/{layer_name}_inp.npy')
wgt = np.load(f'npy/{layer_name}_wgt.npy')
try:
	bias = np.load(f'npy/{layer_name}_bias.npy')
except:
	bias_flag = False
else:
	bias_flag = True
out = np.load(f'npy/{layer_name}_out.npy')

# mapping fixed point
inp_fxp = inp.astype(np.int64)
wgt_fxp = wgt.astype(np.int64)
psum_fxp = np.zeros_like(out).astype(np.int64)
out_ref_fxp = out.astype(np.int64)

# zero-padding
inp_fxp_ = np.pad(inp_fxp, ((0,0), (0,0), (1,1), (1,1)), 'constant', constant_values=0)

HGT = inp_fxp_.shape[2]
WID = inp_fxp_.shape[3]

# 3x3 depthwise convolution
for h in range(HGT-2):
  for w in range(WID-2):
    for co in range(C_OUT):		
  	  psum_fxp[0, co, h, w] = np.sum( inp_fxp_[0, co, h:h+3, w:w+3] * wgt_fxp[co, 0, 0:3, 0:3] )
# scaling
psum_fxp_scale = psum_fxp * S_y_fxp
반응형

댓글