def im2col(image, ksize, stride):
# image is a 4d tensor([ channel,height ,width])
image_col = []
for i in range(0, image.shape[1] - ksize + 1, stride):
for j in range(0, image.shape[2] - ksize + 1, stride):
col = image[:, i:i + ksize, j:j + ksize].reshape([-1])
image_col.append(col)
image_col = np.array(image_col)
return image_col
def init_bias(bias,img,chout,strides):
Hin = img.shape[1] // strides
Win = img.shape[2] // strides
out=[]
for i in range(chout):
a=np.repeat(bias[i],Hin*Win)
out.append(a)
out=np.array(out) #chout,hin*win
return out
def conv(img,weights,bias,ksize,strides,chout,relu):
# yy=[]
out_bias=init_bias(bias, img,chout,strides)
padding=(ksize-1)//2
img1 = np.pad(img, ((0, 0), (padding, padding), (padding, padding)), 'constant', constant_values=(0, 0))
a=im2col(img1, ksize, strides)
weights = np.reshape(weights, [chout, -1])
a = np.transpose(a, [1, 0])
yy = np.matmul(weights, a)
cyj1 = np.add(out_bias, yy)
if(relu):
cyj1 = np.maximum(cyj1, 0.1 * cyj1)
out = np.reshape(cyj1, [chout, (img.shape[1] // strides), (img.shape[2] // strides)])
return out
def resblock_body(img,chout,weights,bias,ksize):
# conv(input,conv1,bias1,ksize,strides,chout,leakyrelu)
x = conv(img, weights[0], bias[0], ksize, 1, chout, 1)
route = x
x = x[(chout // 2)::]
x = conv(x, weights[1], bias[1], ksize, 1, (chout // 2), 1)
route_1 = x
x = conv(x, weights[2], bias[2], ksize, 1, (chout // 2), 1)
x = np.concatenate((x, route_1), axis=0)
x = conv(x, weights[3], bias[3], 1, 1, chout , 1)
feat = x
x = np.concatenate((route, x), axis=0)
x = max_pool(x)
return x, feat
yolov4_tiny
o = conv(Image_data1, conv1, bias1, 3, 2, 32, 1)
o1 = conv(o, conv2, bias2, 3, 2, 64, 1)
o2, _ = resblock_body(o1, 64, weights1, bete1, 3)
o3, _ = resblock_body(o2, 128, weights2, bete2, 3)
o4, feat1 = resblock_body(o3, 256, weights3, bete3, 3)
x = conv(o4, conv15, bias15, 3, 1, 512, 1)
feat2 = x
o5 = conv( feat2, conv16, bias16, 1, 1, 256, 1)
o6 = conv( o5, conv17, bias17, 3, 1, 512, 1)
y1 = conv( o6, conv18, bias18, 1, 1, 27, 0)
o7 = conv(o5, conv19, bias19, 1, 1, 128, 1)
o8 = up_sample(o7)
o9 = np.concatenate((feat1, o8), axis=0)
o10 = conv(o9, conv20, bias20, 3, 1, 256, 1)
y2 = conv(o10, conv21, bias21, 1, 1, 27, 0)