def maxpool(data, factor, getPos=True): """ Return max pooled data and the pooled pixel positions. Args: ----- data: An N x k x m x n array. factor: Pooling factor. Returns: -------- An N x k x (m/factor) x (n/factor), N x k x m x n arrays. """ _data = np.asarray(data, dtype='float32') x = tn.ftensor4('x') f = thn.function([], max_pool(x, factor, True), givens={x: shared(_data)}) g = thn.function([], max_pool_same(x, factor) / x, givens={x: shared(_data + 0.0000000001)}) pooled = f() if not getPos: return pooled positions = g() positions[np.where(np.isnan(positions))] = 0 return pooled, positions
def maxpool(data, factor, getPos=True): """ Return max pooled data and the pooled pixel positions. Args: ----- data: An N x k x m x n array. factor: Pooling factor. Returns: -------- An N x k x (m/factor) x (n/factor), N x k x m x n arrays. """ _data = np.asarray(data, dtype='float32') x = tn.ftensor4('x') f = thn.function([], max_pool(x, factor, True), givens={x: shared(_data)}) g = thn.function([], max_pool_same(x, factor)/x, givens={x: shared(_data + 0.0000000001)}) pooled = f() if not getPos: return pooled positions = g() positions[np.where(np.isnan(positions))] = 0 return pooled, positions
def feedf(self, data): """ Pool features within a given receptive from the input data. Args: ----- data: An N x k x m1 x n1 array of input plains. Returns: ------- A N x k x m2 x n2 array of output plains. """ if self.type == 'max': _data = np.asarray(data, dtype='float32') x = tn.ftensor4('x') f = thn.function([], max_pool(x, self.factor), givens={x: shared(_data)}) g = thn.function([], max_pool_same(x, self.factor)/x, givens={x: shared(_data + 0.0000000001)}) self.grad = g() self.grad[np.where(np.isnan(self.grad))] = 0 return f() else: return downsample(data, (1, 1, self.factor[0], self.factor[1]))