def gen_data(fm_shape, w_shape, pad, stride, dilation, strided=-1): IN, IC, IH, IW = fm_shape C0 = 16 IC = ((IC + C0 - 1) // C0) * C0 WN, WC, WH, WW = w_shape WN = ((WN + C0 - 1) // C0) * C0 WC = ((WC + C0 - 1) // C0) * C0 ON = IN OC = WN WHD = (WH - 1) * dilation + 1 WWD = (WW - 1) * dilation + 1 OH = (IH + 2 * pad - WHD) // stride + 1 OW = (IW + 2 * pad - WWD) // stride + 1 if (strided <= 1): x = random_gaussian((IN, IC, IH, IW), miu=1, sigma=0.1).astype(np.float16) else: x_tmp = random_gaussian( (IN, IC, (IH // strided + 1), (IW // strided + 1)), miu=1, sigma=0.1).astype(np.float16) x = np.full((IN, IC, IH, IW), 0, dtype=np.float16) for i0 in range(x_tmp.shape[0]): for i1 in range(x_tmp.shape[1]): for i2 in range(x_tmp.shape[2]): for i3 in range(x_tmp.shape[3]): x[i0, i1, i2 * strided, i3 * strided] = x_tmp[i0, i1, i2, i3] w = random_gaussian((WN, WC, WH, WW), miu=0.5, sigma=0.01).astype(np.float16) conv_param = {'stride': stride, 'pad': pad, 'dilation': dilation} out = conv_forward_naive(x, w, None, conv_param) # transpose to 5D - NC1HWC0 feature = x.reshape(IN, IC // C0, C0, IH, IW).transpose(0, 1, 3, 4, 2).copy() # transpose to 5D - C1HWNC0 filter = w.reshape(WN, WC // C0, C0, WH, WW).transpose(1, 3, 4, 0, 2).copy() # transpose to 5D - NC1HWC0 output = out.reshape(ON, OC // C0, C0, OH, OW).transpose(0, 1, 3, 4, 2).copy() return feature, filter, output
def gen_data(fm_shape, w_shape, pad, stride, dilation, bias): IN, IC, IH, IW = fm_shape C0 = 16 IC = ((IC + C0 - 1) // C0) * C0 WN, WC, WH, WW = w_shape WN = ((WN + C0 - 1) // C0) * C0 WC = ((WC + C0 - 1) // C0) * C0 ON = IN OC = WN WHD = (WH - 1) * dilation + 1 WWD = (WW - 1) * dilation + 1 OH = (IH + 2 * pad - WHD) // stride + 1 OW = (IW + 2 * pad - WWD) // stride + 1 x = random_gaussian((IN, IC, IH, IW), miu=1, sigma=0.1).astype(np.float16) w = random_gaussian((WN, WC, WH, WW), miu=0.5, sigma=0.01).astype(np.float16) x_add = x + 1.0 if bias: b = np.random.rand(WN).astype(np.float16, copy=False) else: b = (np.array(np.zeros(WN))).astype(np.float16, copy=False) conv_param = {'stride': stride, 'pad': pad, 'dilation': dilation} out = conv_forward_naive(x_add, w, b, conv_param) ''' transpose to 5D - NC1HWC0 ''' feature = x.reshape(IN, IC // C0, C0, IH, IW).transpose(0, 1, 3, 4, 2).copy() ''' transpose to 5D - C1HWNC0 ''' filter = w.reshape(WN, WC // C0, C0, WH, WW).transpose(1, 3, 4, 0, 2).copy() ''' transpose to 5D - NC1HWC0 ''' output = out.reshape(ON, OC // C0, C0, OH, OW).transpose(0, 1, 3, 4, 2).copy() return feature, filter, b, output
def gen_data(fm_shape, w_shape, pad, stride, dilation, bias): if isinstance(stride, int): stride = [stride] * 2 elif isinstance(stride, (list, tuple)) and 1 == len(stride): stride = list(stride) * 2 elif isinstance(stride, (list, tuple)) and 2 == len(stride): pass else: raise RuntimeError('stride para illegal !!!') if isinstance(pad, int): pad = [pad] * 4 elif isinstance(pad, (list, tuple)) and 1 == len(pad): pad = list(pad) * 4 elif isinstance(pad, (list, tuple)) and 4 == len(pad): pass else: raise RuntimeError('pad para illegal !!!') if isinstance(dilation, int): dilation = [dilation] * 2 elif isinstance(dilation, (list, tuple)) and 1 == len(dilation): dilation = list(dilation) * 2 elif isinstance(dilation, (list, tuple)) and 2 == len(dilation): pass else: raise RuntimeError('dilation para illegal !!!') S_h, S_w = stride P_top, P_bottom, P_left, P_right = pad D_h, D_w = dilation IN, IC, IH, IW = fm_shape C0 = 16 IC = ((IC + C0 - 1) // C0) * C0 WN, WC, WH, WW = w_shape WN = ((WN + C0 - 1) // C0) * C0 WC = ((WC + C0 - 1) // C0) * C0 ON = IN OC = WN WHD = (WH - 1) * D_h + 1 WWD = (WW - 1) * D_w + 1 OH = (IH + P_top + P_bottom - WHD) // S_h + 1 OW = (IW + P_left + P_right - WWD) // S_w + 1 x1 = random_gaussian((IN, IC, IH, IW), miu=1, sigma=0.1).astype(np.float16) x2 = random_gaussian((IN, IC, IH, IW), miu=1, sigma=0.1).astype(np.float16) x = fmap_data = np.multiply(x1, x2) w = random_gaussian((WN, WC, WH, WW), miu=0.5, sigma=0.01).astype(np.float16) if bias: b = np.random.rand(WN).astype(np.float16, copy=False) else: b = (np.array(np.zeros(WN))).astype(np.float16, copy=False) conv_param = {'stride': stride, 'pad': pad, 'dilation': dilation} out = conv_forward_naive(x, w, b, conv_param) ''' transpose to 5D - NC1HWC0 ''' feature1 = x1.reshape(IN, IC // C0, C0, IH, IW).transpose(0, 1, 3, 4, 2).copy() feature2 = x2.reshape(IN, IC // C0, C0, IH, IW).transpose(0, 1, 3, 4, 2).copy() ''' transpose to 5D - C1HWNC0 ''' filter = w.reshape(WN, WC // C0, C0, WH, WW).transpose(1, 3, 4, 0, 2).copy() filter = filter.reshape(WC // C0 * WH * WW, WN // 16, 16, C0) bb = b.reshape(1, WN // 16, 1, 1, 16) ''' transpose to 5D - NC1HWC0 ''' output = out.reshape(ON, OC // C0, C0, OH, OW).transpose(0, 1, 3, 4, 2).copy() return feature1, feature2, filter, bb, output
def gen_data(dtype, shape): x = random_gaussian(shape).astype(dtype) expect = np.divide(x, np.abs(x) + 1).astype(dtype) output = np.full(shape, np.nan, dtype) return expect, x, output