标签:mic 不用 soft nump pat pad 三维 random rem
import sys,os
path = os.getcwd()+"\\sourcecode"
sys.path.append(path)
from common.util import im2col
import numpy as np
x1 = np.random.rand(1,3,7,7)#批大小为1,通道为3的7x7的数据
col1 = im2col(x1,5,5,stride=1,pad=0)#滤波器通道为3,大小为5x5
print(col1.shape)
x2 = np.random.rand(10,3,7,7)
col2 = im2col(x2,5,5,stride=1,pad=0)
print(col2.shape)
(9, 75)
(90, 75)
class Convolution:
def __init__(self,W,b,stride=1,pad=0):
self.W=W
self.b=b
self.stride=stride
self.pad = pad
def forward(self,x):
FN,C,FH,FW = self.W.shape
N,C,H,W = x.shape
out_h = int(1+(H+2*self.pad-FH)/self.stride)
out_w = int(1+(W+2*self.pad-FW)/self.stride)
col = im2col(x,FH,FW,self.stride,self.pad)
col_W = self.W.reshape(FN,-1).T
out = np.dot(col,col_W)+self.b
out = out.reshape(N,out_h,out_w,-1).tranpose(0,3,1,2)
return out
class Pooling:
def __init__(self,pool_h,pool_w,stride=1,pad=0):
self.pool_h = pool_h
self.pool_w = pool_w
self.stride = stride
self.pad = pad
def forward(self,x):
N,C,H,W = x.shape
out_h = int(1+(H-self.pool_h)/self.stride)
out_w = int(1+(W-self.pool_w)/self.stride)
#按照通道单独展开
col = im2col(x,self.pool_h,self.pool_w,self.stride,self.pad)
col = col.reshape(-1,self.pool_h*self.pool_w)
out = np.max(col,axis=1)
out = out.reshape(N,out_h,out_w,C).tranpose(0,3,1,2)
return out
标签:mic 不用 soft nump pat pad 三维 random rem
原文地址:https://www.cnblogs.com/suqinghang/p/12264507.html