提交 b7b10608 authored 作者: Li's avatar Li

tentative push

上级 125cca78
...@@ -202,11 +202,12 @@ class DownsampleFactorMax(Op): ...@@ -202,11 +202,12 @@ class DownsampleFactorMax(Op):
if len(x.shape) != 4: if len(x.shape) != 4:
raise NotImplementedError( raise NotImplementedError(
'DownsampleFactorMax requires 4D input for now') 'DownsampleFactorMax requires 4D input for now')
z_shape = self.out_shape(x.shape, self.ds, self.ignore_border, self.st,self.padding) z_shape = self.out_shape(x.shape, self.ds, self.ignore_border, self.st,
self.padding)
if (z[0] is None) or (z[0].shape != z_shape): if (z[0] is None) or (z[0].shape != z_shape):
z[0] = numpy.empty(self.out_shape(x.shape, self.ds, z[0] = numpy.empty(self.out_shape(x.shape, self.ds,
self.ignore_border, self.st), self.ignore_border, self.st, self.padding),
dtype=x.dtype) dtype=x.dtype)
zz = z[0] zz = z[0]
#number of pooling output rows #number of pooling output rows
...@@ -219,37 +220,19 @@ class DownsampleFactorMax(Op): ...@@ -219,37 +220,19 @@ class DownsampleFactorMax(Op):
img_cols = x.shape[-1] + 2 * self.padding[1] img_cols = x.shape[-1] + 2 * self.padding[1]
pad_h = self.padding[0] pad_h = self.padding[0]
pad_w = self.padding[1] pad_w = self.padding[1]
def get_valid_corners(x): def pad_img(x):
# x (m,c,h,w) w = x.shape[3]
img_h,img_w = x.shape[-2:] h = x.shape[2]
row_st_valid = pad_h fill = x.min()-1
row_end_valid = img_h + pad_h - 1 t = numpy.ones((x.shape[0],x.shape[1],1,1))
col_st_valid = pad_w ud_bar = (numpy.zeros((pad_h, w)) + fill)[
col_end_valid = img_w + pad_w - 1 numpy.newaxis, numpy.newaxis,:,:] * t
return row_st_valid, row_end_valid, col_st_valid, col_end_valid lr_bar = (numpy.zeros((pad_h * 2 + h, pad_w)) + fill)[
row_st_valid, row_end_valid, col_st_valid, col_end_valid = get_valid_corners(x) numpy.newaxis, numpy.newaxis,:,:] * t
def change_coordinate(row_st, row_end, col_st, col_end): y = numpy.concatenate([ud_bar, x, ud_bar], axis=2)
new_row_st = None y = numpy.concatenate([lr_bar, y, lr_bar], axis=3)
new_row_end = None return y
new_col_st = None y = pad_img(x)
new_col_end = None
if row_st <= row_st_valid:
new_row_st = row_st_valid
if row_end >= row_end_valid:
new_row_end = row_end_valid
if col_st <= col_st_valid:
new_col_st = col_st_valid
if col_end >= col_end_valid:
new_col_end = col_end_valid
if new_row_st is None:
new_row_st = row_st - pad_h
if new_row_end is None:
new_row_end = row_end - pad_h
if new_col_st is None:
new_col_st = col_st - pad_w
if new_col_end is None:
new_col_end = col_end - pad_w
return new_row_st, new_row_end, new_col_st, new_col_end
for n in xrange(x.shape[0]): for n in xrange(x.shape[0]):
for k in xrange(x.shape[1]): for k in xrange(x.shape[1]):
for r in xrange(pr): for r in xrange(pr):
...@@ -258,9 +241,7 @@ class DownsampleFactorMax(Op): ...@@ -258,9 +241,7 @@ class DownsampleFactorMax(Op):
for c in xrange(pc): for c in xrange(pc):
col_st = c * st1 col_st = c * st1
col_end = __builtin__.min(col_st + ds1, img_cols) col_end = __builtin__.min(col_st + ds1, img_cols)
row_st, row_end, col_st, col_end = change_coordinate( zz[n, k, r, c] = y[
row_st, row_end, col_st, col_end)
zz[n, k, r, c] = x[
n, k, row_st:row_end, col_st:col_end].max() n, k, row_st:row_end, col_st:col_end].max()
def infer_shape(self, node, in_shapes): def infer_shape(self, node, in_shapes):
...@@ -385,8 +366,7 @@ class DownsampleFactorMaxGrad(Op): ...@@ -385,8 +366,7 @@ class DownsampleFactorMaxGrad(Op):
def perform(self, node, inp, out): def perform(self, node, inp, out):
x, maxout, gz = inp x, maxout, gz = inp
gx_stg, = out gx_stg, = out
gx = numpy.zeros_like(x)
#number of pooling output rows #number of pooling output rows
pr = maxout.shape[-2] pr = maxout.shape[-2]
#number of pooling output cols #number of pooling output cols
...@@ -397,37 +377,28 @@ class DownsampleFactorMaxGrad(Op): ...@@ -397,37 +377,28 @@ class DownsampleFactorMaxGrad(Op):
img_cols = x.shape[-1] + 2 * self.padding[1] img_cols = x.shape[-1] + 2 * self.padding[1]
pad_h = self.padding[0] pad_h = self.padding[0]
pad_w = self.padding[1] pad_w = self.padding[1]
def get_valid_corners(x): def pad_img(x):
# x (m,c,h,w) w = x.shape[3]
img_h,img_w = x.shape[-2:] h = x.shape[2]
row_st_valid = pad_h fill = x.min()-1
row_end_valid = img_h + pad_h - 1 t = numpy.ones((x.shape[0],x.shape[1],1,1))
col_st_valid = pad_w ud_bar = (numpy.zeros((pad_h, w)) + fill)[
col_end_valid = img_w + pad_w - 1 numpy.newaxis, numpy.newaxis,:,:] * t
return row_st_valid, row_end_valid, col_st_valid, col_end_valid lr_bar = (numpy.zeros((pad_h * 2 + h, pad_w)) + fill)[
row_st_valid, row_end_valid, col_st_valid, col_end_valid = get_valid_corners(x) numpy.newaxis, numpy.newaxis,:,:] * t
def change_coordinate(row_st, row_end, col_st, col_end): y = numpy.concatenate([ud_bar, x, ud_bar], axis=2)
new_row_st = None y = numpy.concatenate([lr_bar, y, lr_bar], axis=3)
new_row_end = None return y
new_col_st = None def unpad(g):
new_col_end = None w = x.shape[3]
if row_st <= row_st_valid: h = x.shape[2]
new_row_st = row_st_valid r_st = pad_h
if row_end >= row_end_valid: r_end = h + pad_h
new_row_end = row_end_valid c_st = pad_w
if col_st <= col_st_valid: c_end = w + pad_w
new_col_st = col_st_valid return g[:,:,r_st:r_end,c_st:c_end]
if col_end >= col_end_valid: y = pad_img(x)
new_col_end = col_end_valid gx = numpy.zeros_like(y)
if new_row_st is None:
new_row_st = row_st - pad_h
if new_row_end is None:
new_row_end = row_end - pad_h
if new_col_st is None:
new_col_st = col_st - pad_w
if new_col_end is None:
new_col_end = col_end - pad_w
return new_row_st, new_row_end, new_col_st, new_col_end
for n in xrange(x.shape[0]): for n in xrange(x.shape[0]):
for k in xrange(x.shape[1]): for k in xrange(x.shape[1]):
for r in xrange(pr): for r in xrange(pr):
...@@ -436,12 +407,12 @@ class DownsampleFactorMaxGrad(Op): ...@@ -436,12 +407,12 @@ class DownsampleFactorMaxGrad(Op):
for c in xrange(pc): for c in xrange(pc):
col_st = c * st1 col_st = c * st1
col_end = __builtin__.min(col_st + ds1, img_cols) col_end = __builtin__.min(col_st + ds1, img_cols)
row_st, row_end, col_st, col_end = change_coordinate(
row_st, row_end, col_st, col_end)
for row_ind in xrange(row_st, row_end): for row_ind in xrange(row_st, row_end):
for col_ind in xrange(col_st, col_end): for col_ind in xrange(col_st, col_end):
if (maxout[n, k, r, c] == x[n, k, row_ind, col_ind]): if (maxout[n, k, r, c] == y[n, k, row_ind, col_ind]):
gx[n, k, row_ind, col_ind] += gz[n, k, r, c] gx[n, k, row_ind, col_ind] += gz[n, k, r, c]
import ipdb; ipdb.set_trace()
gx = unpad(gx)
gx_stg[0] = gx gx_stg[0] = gx
def infer_shape(self, node, in_shapes): def infer_shape(self, node, in_shapes):
......
...@@ -40,43 +40,34 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -40,43 +40,34 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
@staticmethod @staticmethod
def numpy_max_pool_2d_stride_padding( def numpy_max_pool_2d_stride_padding(
x, ds, ignore_border=True, st=None, padding=None): x, ds, ignore_border=True, st=None, padding=None):
img_rows = x.shape[-2] + 2 * padding[0] pad_h = padding[0]
img_cols = x.shape[-1] + 2 * padding[1] pad_w = padding[1]
h = x.shape[-2]
w = x.shape[-1]
assert ds[0] > pad_h
assert ds[1] > pad_w
def pad_img(x):
fill = x.min()-1
t = numpy.ones((x.shape[0],x.shape[1],1,1))
ud_bar = (numpy.zeros((pad_h, w)) + fill)[
numpy.newaxis, numpy.newaxis,:,:] * t
lr_bar = (numpy.zeros((pad_h * 2 + h, pad_w)) + fill)[
numpy.newaxis, numpy.newaxis,:,:] * t
y = numpy.concatenate([ud_bar, x, ud_bar], axis=2)
y = numpy.concatenate([lr_bar, y, lr_bar], axis=3)
return y
img_rows = h + 2 * pad_h
img_cols = w + 2 * pad_w
out_r = (img_rows - ds[0]) // st[0] + 1 out_r = (img_rows - ds[0]) // st[0] + 1
out_c = (img_cols - ds[1]) // st[1] + 1 out_c = (img_cols - ds[1]) // st[1] + 1
out_shp = list(x.shape[:-2]) out_shp = list(x.shape[:-2])
out_shp.append(out_r) out_shp.append(out_r)
out_shp.append(out_c) out_shp.append(out_c)
ds0, ds1 = ds ds0, ds1 = ds
st0, st1 = st st0, st1 = st
pad_h = padding[0]
pad_w = padding[1]
output_val = numpy.zeros(out_shp) output_val = numpy.zeros(out_shp)
def get_valid_corners(x):
# x (m,c,h,w)
img_h,img_w = x.shape[-2:]
row_st_valid = pad_h
row_end_valid = img_h + pad_h -1
col_st_valid = pad_w
col_end_valid = img_w + pad_w -1
return row_st_valid, row_end_valid, col_st_valid, col_end_valid
row_st_valid, row_end_valid, col_st_valid, col_end_valid = get_valid_corners(x)
def change_coordinate(row_st, row_end, col_st, col_end):
if row_st <= row_st_valid:
row_st = row_st_valid
if row_end >= row_end_valid:
row_end = row_end_valid
if col_st <= col_st_valid:
col_st = col_st_valid
if col_end >= col_end_valid:
col_end = col_end_valid
new_row_st = row_st - pad_h
new_row_end = row_end - pad_h
new_col_st = col_st - pad_w
new_col_end = col_end - pad_w
return new_row_st, new_row_end, new_col_st, new_col_end
tt = [] tt = []
y = pad_img(x)
for k in numpy.ndindex(*x.shape[:-2]): for k in numpy.ndindex(*x.shape[:-2]):
for i in range(output_val.shape[-2]): for i in range(output_val.shape[-2]):
ii_st = i * st[0] ii_st = i * st[0]
...@@ -85,10 +76,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -85,10 +76,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
try: try:
jj_st = j * st[1] jj_st = j * st[1]
jj_end = __builtin__.min(jj_st + ds[1], img_cols) jj_end = __builtin__.min(jj_st + ds[1], img_cols)
ii_st, ii_end, jj_st, jj_end = change_coordinate( patch = y[k][ii_st:ii_end, jj_st:jj_end]
ii_st, ii_end, jj_st, jj_end)
tt.append([ii_st, ii_end, jj_st, jj_end])
patch = x[k][ii_st:ii_end, jj_st:jj_end]
output_val[k][i, j] = numpy.max(patch) output_val[k][i, j] = numpy.max(patch)
except Exception,e: except Exception,e:
import ipdb; ipdb.set_trace() import ipdb; ipdb.set_trace()
...@@ -256,37 +244,22 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -256,37 +244,22 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def test_DownsampleFactorMaxPaddingStride(self): def test_DownsampleFactorMaxPaddingStride(self):
ignore_border = True # padding does not support ignore_border=False ignore_border = True # padding does not support ignore_border=False
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
maxpoolsizes = [(5, 3)] maxpoolsizes = [(3, 3)]
stridesizes = [(3, 2)] stridesizes = [(2, 2)]
paddingsizes = [(2, 2)] paddingsizes = [(2, 2)]
imgsizes = [(10, 10)] imgsizes = [(5, 5)]
m = 4 # minibatch
def decide_out_shape(imgsize, maxpoolsize, stridesize, paddingsize): c = 10 # channel size
img_h, img_w = imgsize
p_h, p_w = maxpoolsize
st_h, st_w = stridesize
pad_h, pad_w = paddingsize
r = img_h
c = img_w
r += pad_h * 2
c += pad_w * 2
out_r = (r - p_h) // st_h + 1
out_c = (c - p_w) // st_w + 1
nr = numpy.maximum(out_r, 0)
nc = numpy.maximum(out_c, 0)
images = tensor.dtensor4() images = tensor.dtensor4()
for indx in numpy.arange(len(maxpoolsizes)): for indx in numpy.arange(len(maxpoolsizes)):
imgsize = imgsizes[indx] imgsize = imgsizes[indx]
imval = rng.rand(4, 10, imgsize[0], imgsize[1]) imval = rng.rand(m, c, imgsize[0], imgsize[1])
stridesize = stridesizes[indx] stridesize = stridesizes[indx]
maxpoolsize = maxpoolsizes[indx] maxpoolsize = maxpoolsizes[indx]
paddingsize = paddingsizes[indx] paddingsize = paddingsizes[indx]
outputsize = decide_out_shape(imgsize,maxpoolsize,stridesize,paddingsize)
numpy_output_val = self.numpy_max_pool_2d_stride_padding( numpy_output_val = self.numpy_max_pool_2d_stride_padding(
imval, maxpoolsize,ignore_border, stridesize, paddingsize) imval, maxpoolsize,ignore_border, stridesize, paddingsize)
assert numpy_output_val.shape == outputsize, (
"outshape is %s, calculated shape is %s"
% (outputsize, numpy_output_val.shape))
maxpool_op = DownsampleFactorMax(maxpoolsize, maxpool_op = DownsampleFactorMax(maxpoolsize,
ignore_border=ignore_border, ignore_border=ignore_border,
st=stridesize,padding=paddingsize)(images) st=stridesize,padding=paddingsize)(images)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论