def process(self, image, out=None): # 0.25 is the default value used in Ng's paper alpha = self.specs.get('alpha', 0.25) # check if we would like to do two-side thresholding. Default yes. if self.specs.get('twoside', True): # concatenate, and make sure the output is C_CONTIGUOUS # for the temporary product, we check if we can utilize the # buffer to save allocation time product = mathutil.dot_image(image, self.dictionary.T) imshape = product.shape[:-1] N = product.shape[-1] product.resize((np.prod(imshape), N)) if out is None: out = np.empty((np.prod(imshape), N*2)) else: out.resize((np.prod(imshape), N*2)) out[:,:N] = product out[:,N:] = -product out.resize(imshape + (N*2,)) elif self.specs['twoside'] == 'abs': out = mathutil.dot_image(image, self.dictionary.T, out=out) np.abs(out, out=out) else: out = mathutil.dot_image(image, self.dictionary.T, out=out) # do threshold out -= alpha np.clip(out, 0., np.inf, out=out) return out
def testdot_image(self): A = np.random.rand(2,3,4) B = np.random.rand(4,5) result = mathutil.dot_image(A, B) self.assertTrue(result.flags['C_CONTIGUOUS']) self.assertEqual(result.shape[:-1], A.shape[:-1]) self.assertEqual(result.shape[-1], B.shape[-1]) B = np.array(B, order='f') result = mathutil.dot_image(A, B) self.assertTrue(result.flags['C_CONTIGUOUS']) self.assertEqual(result.shape[:-1], A.shape[:-1]) self.assertEqual(result.shape[-1], B.shape[-1])
def testdot_image(self): A = np.random.rand(2, 3, 4) B = np.random.rand(4, 5) result = mathutil.dot_image(A, B) self.assertTrue(result.flags['C_CONTIGUOUS']) self.assertEqual(result.shape[:-1], A.shape[:-1]) self.assertEqual(result.shape[-1], B.shape[-1]) B = np.array(B, order='f') result = mathutil.dot_image(A, B) self.assertTrue(result.flags['C_CONTIGUOUS']) self.assertEqual(result.shape[:-1], A.shape[:-1]) self.assertEqual(result.shape[-1], B.shape[-1])
def process(self, image, out = None): W, b = self.dictionary # we create the offset in-place: this might introduce some numerical # differences but should be fine most of the time image += b out = mathutil.dot_image(image, W, out=out) image -= b return out
def process(self, image): # 0.25 is the default value used in Ng's paper alpha = self.specs.get('alpha', 0.25) output = mathutil.dot_image(image, self.dictionary.T) # check if we would like to do two-side thresholding. Default yes. if self.specs.get('twoside', True): # concatenate, and make sure to be C_CONTIGUOUS imshape = output.shape[:-1] N = output.shape[-1] output.resize((np.prod(imshape), N)) temp = np.empty((np.prod(imshape), N*2)) temp[:,:N] = output temp[:,N:] = -output output = temp.reshape(imshape + (N*2,)) else: # otherwise, we will take the absolute value output = np.abs(output) output -= alpha np.clip(output, 0., np.inf, out=output) return output
def process(self, image, out = None): return mathutil.dot_image(image, self.dictionary.T, out=out)
def process(self, image, out=None): W, b = self.dictionary out = mathutil.dot_image(image, W, out=out) out += b return out
def process(self, image): return mathutil.dot_image(image, self.dictionary.T)
def process(self, image): W, b = self.dictionary output = mathutil.dot_image(image, W) output += b return output