def test_unit_norm(self): """ Test that using std_bias = 0.0 and use_norm = True results in vectors having unit norm """ tol = 1e-5 num_examples = 5 num_features = 10 rng = np.random.RandomState([1, 2, 3]) X = as_floatX(rng.randn(num_examples, num_features)) dataset = DenseDesignMatrix(X=X) # the setting of subtract_mean is not relevant to the test # the test only applies when std_bias = 0.0 and use_std = False preprocessor = GlobalContrastNormalization(subtract_mean=False, sqrt_bias=0.0, use_std=False) dataset.apply_preprocessor(preprocessor) result = dataset.get_design_matrix() norms = np.sqrt(np.square(result).sum(axis=1)) max_norm_error = np.abs(norms - 1.).max() tol = 3e-5 assert max_norm_error < tol
def test_extract_reassemble(): """ Tests that ExtractGridPatches and ReassembleGridPatches are inverse of each other """ rng = np.random.RandomState([1, 3, 7]) topo = rng.randn(4, 3 * 5, 3 * 7, 2) dataset = DenseDesignMatrix(topo_view=topo) patch_shape = (3, 7) extractor = ExtractGridPatches(patch_shape, patch_shape) reassemblor = ReassembleGridPatches(patch_shape=patch_shape, orig_shape=topo.shape[1:3]) dataset.apply_preprocessor(extractor) dataset.apply_preprocessor(reassemblor) new_topo = dataset.get_topological_view() assert new_topo.shape == topo.shape if not np.all(new_topo == topo): assert False
def test_extract_reassemble(): """ Tests that ExtractGridPatches and ReassembleGridPatches are inverse of each other """ rng = np.random.RandomState([1, 3, 7]) topo = rng.randn(4, 3 * 5, 3 * 7, 2) dataset = DenseDesignMatrix(topo_view=topo) patch_shape = (3, 7) extractor = ExtractGridPatches(patch_shape, patch_shape) reassemblor = ReassembleGridPatches(patch_shape=patch_shape, orig_shape=topo.shape[1:3]) dataset.apply_preprocessor(extractor) dataset.apply_preprocessor(reassemblor) new_topo = dataset.get_topological_view() assert new_topo.shape == topo.shape if not np.all(new_topo == topo): assert False
def test_unit_norm(self): """ Test that using std_bias = 0.0 and use_norm = True results in vectors having unit norm """ tol = 1e-5 num_examples = 5 num_features = 10 rng = np.random.RandomState([1, 2, 3]) X = as_floatX(rng.randn(num_examples, num_features)) dataset = DenseDesignMatrix(X=X) # the setting of subtract_mean is not relevant to the test # the test only applies when std_bias = 0.0 and use_std = False preprocessor = GlobalContrastNormalization(subtract_mean=False, sqrt_bias=0.0, use_std=False) dataset.apply_preprocessor(preprocessor) result = dataset.get_design_matrix() norms = np.sqrt(np.square(result).sum(axis=1)) max_norm_error = np.abs(norms - 1.).max() tol = 3e-5 assert max_norm_error < tol
def test_zero_image(self): """ Test on zero-value image if cause any division by zero """ X = as_floatX(np.zeros((5, 32 * 32 * 3))) axes = ['b', 0, 1, 'c'] view_converter = dense_design_matrix.DefaultViewConverter((32, 32, 3), axes) dataset = DenseDesignMatrix(X=X, view_converter=view_converter) dataset.axes = axes preprocessor = LeCunLCN(img_shape=[32, 32]) dataset.apply_preprocessor(preprocessor) result = dataset.get_design_matrix() assert isfinite(result)
def test_zero_image(self): """ Test on zero-value image if cause any division by zero """ X = as_floatX(np.zeros((5, 32 * 32 * 3))) axes = ['b', 0, 1, 'c'] view_converter = dense_design_matrix.DefaultViewConverter((32, 32, 3), axes) dataset = DenseDesignMatrix(X=X, view_converter=view_converter) dataset.axes = axes preprocessor = LeCunLCN(img_shape=[32, 32]) dataset.apply_preprocessor(preprocessor) result = dataset.get_design_matrix() assert isfinite(result)
def test_channel(self): """ Test if works fine withe different number of channel as argument """ rng = np.random.RandomState([1, 2, 3]) X = as_floatX(rng.randn(5, 32 * 32 * 3)) axes = ['b', 0, 1, 'c'] view_converter = dense_design_matrix.DefaultViewConverter((32, 32, 3), axes) dataset = DenseDesignMatrix(X=X, view_converter=view_converter) dataset.axes = axes preprocessor = LeCunLCN(img_shape=[32, 32], channels=[1, 2]) dataset.apply_preprocessor(preprocessor) result = dataset.get_design_matrix() assert isfinite(result)
def test_channel(self): """ Test if works fine withe different number of channel as argument """ rng = np.random.RandomState([1, 2, 3]) X = as_floatX(rng.randn(5, 32 * 32 * 3)) axes = ['b', 0, 1, 'c'] view_converter = dense_design_matrix.DefaultViewConverter((32, 32, 3), axes) dataset = DenseDesignMatrix(X=X, view_converter=view_converter) dataset.axes = axes preprocessor = LeCunLCN(img_shape=[32, 32], channels=[1, 2]) dataset.apply_preprocessor(preprocessor) result = dataset.get_design_matrix() assert isfinite(result)
def test_zero_vector(self): """ Test that passing in the zero vector does not result in a divide by 0 """ dataset = DenseDesignMatrix(X=as_floatX(np.zeros((1, 1)))) # the settings of subtract_mean and use_norm are not relevant to # the test # std_bias = 0.0 is the only value for which there should be a risk # of failure occurring preprocessor = GlobalContrastNormalization(subtract_mean=True, sqrt_bias=0.0, use_std=True) dataset.apply_preprocessor(preprocessor) result = dataset.get_design_matrix() assert not np.any(np.isnan(result)) assert not np.any(np.isinf(result))
def test_zero_vector(self): """ Test that passing in the zero vector does not result in a divide by 0 """ dataset = DenseDesignMatrix(X=as_floatX(np.zeros((1, 1)))) # the settings of subtract_mean and use_norm are not relevant to # the test # std_bias = 0.0 is the only value for which there should be a risk # of failure occurring preprocessor = GlobalContrastNormalization(subtract_mean=True, sqrt_bias=0.0, use_std=True) dataset.apply_preprocessor(preprocessor) result = dataset.get_design_matrix() assert isfinite(result)
def test_rgb_yuv(): """ Test on a random image if the per-processor loads and works without anyerror and doesn't result in any nan or inf values """ rng = np.random.RandomState([1, 2, 3]) X = as_floatX(rng.randn(5, 32 * 32 * 3)) axes = ['b', 0, 1, 'c'] view_converter = dense_design_matrix.DefaultViewConverter((32, 32, 3), axes) dataset = DenseDesignMatrix(X=X, view_converter=view_converter) dataset.axes = axes preprocessor = RGB_YUV() dataset.apply_preprocessor(preprocessor) result = dataset.get_design_matrix() assert isfinite(result)
def test_rgb_yuv(): """ Test on a random image if the per-processor loads and works without anyerror and doesn't result in any nan or inf values """ rng = np.random.RandomState([1, 2, 3]) X = as_floatX(rng.randn(5, 32 * 32 * 3)) axes = ['b', 0, 1, 'c'] view_converter = dense_design_matrix.DefaultViewConverter((32, 32, 3), axes) dataset = DenseDesignMatrix(X=X, view_converter=view_converter) dataset.axes = axes preprocessor = RGB_YUV() dataset.apply_preprocessor(preprocessor) result = dataset.get_design_matrix() assert isfinite(result)
def test_random_image(self): """ Test on a random image if the per-processor loads and works without anyerror and doesn't result in any nan or inf values """ rng = np.random.RandomState([1, 2, 3]) X = as_floatX(rng.randn(5, 32 * 32 * 3)) axes = ["b", 0, 1, "c"] view_converter = dense_design_matrix.DefaultViewConverter((32, 32, 3), axes) dataset = DenseDesignMatrix(X=X, view_converter=view_converter) dataset.axes = axes preprocessor = LeCunLCN(img_shape=[32, 32]) dataset.apply_preprocessor(preprocessor) result = dataset.get_design_matrix() assert not np.any(np.isnan(result)) assert not np.any(np.isinf(result))
def test_random_image(self): """ Test on a random image if the per-processor loads and works without anyerror and doesn't result in any nan or inf values """ rng = np.random.RandomState([1, 2, 3]) X = as_floatX(rng.randn(5, 32 * 32 * 3)) axes = ['b', 0, 1, 'c'] view_converter = dense_design_matrix.DefaultViewConverter((32, 32, 3), axes) dataset = DenseDesignMatrix(X=X, view_converter=view_converter) dataset.axes = axes preprocessor = LeCunLCN(img_shape=[32, 32]) dataset.apply_preprocessor(preprocessor) result = dataset.get_design_matrix() assert not np.any(np.isnan(result)) assert not np.any(np.isinf(result))
from pylearn2.utils import serial stl10 = serial.load('/data/lisa/data/stl10/stl10_32x32/train.pkl') batch = stl10.X[24:25,:] img = stl10.view_converter.design_mat_to_topo_view(batch)[0,...] / 127.5 from pylearn2.gui.patch_viewer import PatchViewer pv = PatchViewer((27,27),(6,6),pad=(1,1),is_color=True) pipeline = serial.load('/data/lisa/data/stl10/stl10_patches/preprocessor.pkl') del pipeline.items[0] from pylearn2.datasets.dense_design_matrix import DenseDesignMatrix, DefaultViewConverter for row in xrange(27): for col in xrange(27): patch = img[row:row+6,col:col+6] d = DenseDesignMatrix( topo_view = patch.reshape(1,6,6,3), view_converter = DefaultViewConverter((6,6,3)) ) d.apply_preprocessor(pipeline) pv.add_patch(d.get_topological_view()[0,...], rescale = True) pv.show()
rng = np.random.RandomState([1,2,3]) for i, img_path in enumerate(ImageIterator(path, suffix=".npy")): img = np.load(img_path) if img.shape[2] == 1: img = np.concatenate((img,img,img),axis=2) img = img.reshape(1,img.shape[0],img.shape[1],img.shape[2]) d = DenseDesignMatrix( topo_view = img, view_converter = DefaultViewConverter(img.shape[1:]) ) random_rng = np.random.RandomState([ rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)]) p = ExtractPatches( patch_shape = patch_shape, num_patches = k , rng = random_rng) d.apply_preprocessor(p) X[i*3:(i+1)*3,:] = d.X d.X = X base = '/data/lisatmp/goodfeli/darpa_imagenet_patch_%dx%d_train.' % (patch_shape[0], patch_shape[1]) d.use_design_loc(base+'npy') serial.save(base+'pkl',d)
def __call__(self, full_X): feature_type = self.feature_type pooling_region_counts = self.pooling_region_counts model = self.model size = self.size nan = 0 full_X = full_X.reshape(1,full_X.shape[0],full_X.shape[1],full_X.shape[2]) if full_X.shape[3] == 1: full_X = np.concatenate( (full_X, full_X, full_X), axis=3) print 'full_X.shape: '+str(full_X.shape) num_examples = full_X.shape[0] assert num_examples == 1 pipeline = self.preprocessor def average_pool( stride ): def point( p ): return p * ns / stride rval = np.zeros( (topo_feat.shape[0], stride, stride, topo_feat.shape[3] ) , dtype = 'float32') for i in xrange(stride): for j in xrange(stride): rval[:,i,j,:] = self.region_features( topo_feat[:,point(i):point(i+1), point(j):point(j+1),:] ) return rval outputs = [ np.zeros((num_examples,count,count,model.nhid),dtype='float32') for count in pooling_region_counts ] assert len(outputs) > 0 fd = DenseDesignMatrix(X = np.zeros((1,1),dtype='float32'), view_converter = DefaultViewConverter([1, 1, model.nhid] ) ) ns = 32 - size + 1 depatchifier = ReassembleGridPatches( orig_shape = (ns, ns), patch_shape=(1,1) ) batch_size = 1 for i in xrange(0,num_examples-batch_size+1,batch_size): print i t1 = time.time() d = DenseDesignMatrix( topo_view = np.cast['float32'](full_X[i:i+batch_size,:]), view_converter = DefaultViewConverter((32,32,3))) t2 = time.time() #print '\tapplying preprocessor' d.apply_preprocessor(pipeline, can_fit = False) X2 = d.get_design_matrix() t3 = time.time() #print '\trunning theano function' feat = self.f(X2) t4 = time.time() assert feat.dtype == 'float32' feat_dataset = copy.copy(fd) if np.any(np.isnan(feat)): nan += np.isnan(feat).sum() feat[np.isnan(feat)] = 0 feat_dataset.set_design_matrix(feat) #print '\treassembling features' feat_dataset.apply_preprocessor(depatchifier) #print '\tmaking topological view' topo_feat = feat_dataset.get_topological_view() assert topo_feat.shape[0] == batch_size t5 = time.time() #average pooling for output, count in zip(outputs, pooling_region_counts): output[i:i+batch_size,...] = average_pool(count) t6 = time.time() print (t6-t1, t2-t1, t3-t2, t4-t3, t5-t4, t6-t5) return outputs[0]
def __call__(self, full_X): feature_type = self.feature_type pooling_region_counts = self.pooling_region_counts model = self.model size = self.size nan = 0 full_X = full_X.reshape(1, full_X.shape[0], full_X.shape[1], full_X.shape[2]) if full_X.shape[3] == 1: full_X = np.concatenate((full_X, full_X, full_X), axis=3) print 'full_X.shape: ' + str(full_X.shape) num_examples = full_X.shape[0] assert num_examples == 1 pipeline = self.preprocessor def average_pool(stride): def point(p): return p * ns / stride rval = np.zeros( (topo_feat.shape[0], stride, stride, topo_feat.shape[3]), dtype='float32') for i in xrange(stride): for j in xrange(stride): rval[:, i, j, :] = self.region_features( topo_feat[:, point(i):point(i + 1), point(j):point(j + 1), :]) return rval outputs = [ np.zeros((num_examples, count, count, model.nhid), dtype='float32') for count in pooling_region_counts ] assert len(outputs) > 0 fd = DenseDesignMatrix(X=np.zeros((1, 1), dtype='float32'), view_converter=DefaultViewConverter( [1, 1, model.nhid])) ns = 32 - size + 1 depatchifier = ReassembleGridPatches(orig_shape=(ns, ns), patch_shape=(1, 1)) batch_size = 1 for i in xrange(0, num_examples - batch_size + 1, batch_size): print i t1 = time.time() d = DenseDesignMatrix( topo_view=np.cast['float32'](full_X[i:i + batch_size, :]), view_converter=DefaultViewConverter((32, 32, 3))) t2 = time.time() #print '\tapplying preprocessor' d.apply_preprocessor(pipeline, can_fit=False) X2 = d.get_design_matrix() t3 = time.time() #print '\trunning theano function' feat = self.f(X2) t4 = time.time() assert feat.dtype == 'float32' feat_dataset = copy.copy(fd) if np.any(np.isnan(feat)): nan += np.isnan(feat).sum() feat[np.isnan(feat)] = 0 feat_dataset.set_design_matrix(feat) #print '\treassembling features' feat_dataset.apply_preprocessor(depatchifier) #print '\tmaking topological view' topo_feat = feat_dataset.get_topological_view() assert topo_feat.shape[0] == batch_size t5 = time.time() #average pooling for output, count in zip(outputs, pooling_region_counts): output[i:i + batch_size, ...] = average_pool(count) t6 = time.time() print(t6 - t1, t2 - t1, t3 - t2, t4 - t3, t5 - t4, t6 - t5) return outputs[0]
feat = ( H > 0.5) * Mu1 else: assert False print 'compiling theano function' f = function([V],feat) print 'running theano function' feat = f(X2) feat_dataset = DenseDesignMatrix(X = feat, view_converter = DefaultViewConverter([1, 1, feat.shape[1]] ) ) print 'reassembling features' ns = 32 - size + 1 depatchifier = ReassembleGridPatches( orig_shape = (ns, ns), patch_shape=(1,1) ) feat_dataset.apply_preprocessor(depatchifier) print 'making topological view' topo_feat = feat_dataset.get_topological_view() assert topo_feat.shape[0] == X.shape[0] print 'assembling visualizer' n = np.ceil(np.sqrt(model.nhid)) pv3 = PatchViewer(grid_shape = (X.shape[0], num_filters), patch_shape=(ns,ns), is_color= False) pv4 = PatchViewer(grid_shape = (n,n), patch_shape = (size,size), is_color = True, pad = (7,7)) pv5 = PatchViewer(grid_shape = (1,num_filters), patch_shape = (size,size), is_color = True, pad = (7,7)) idx = sorted(range(model.nhid), key = lambda l : -topo_feat[:,:,:,l].std() )
print 'compiling theano function' f = function([V], feat) print 'running theano function' feat = f(X2) feat_dataset = DenseDesignMatrix(X=feat, view_converter=DefaultViewConverter( [1, 1, feat.shape[1]])) print 'reassembling features' ns = 32 - size + 1 depatchifier = ReassembleGridPatches(orig_shape=(ns, ns), patch_shape=(1, 1)) feat_dataset.apply_preprocessor(depatchifier) print 'making topological view' topo_feat = feat_dataset.get_topological_view() assert topo_feat.shape[0] == X.shape[0] print 'assembling visualizer' n = np.ceil(np.sqrt(model.nhid)) pv3 = PatchViewer(grid_shape=(X.shape[0], num_filters), patch_shape=(ns, ns), is_color=False) pv4 = PatchViewer(grid_shape=(n, n), patch_shape=(size, size), is_color=True,
for i, img_path in enumerate(ImageIterator(path, suffix=".npy")): img = np.load(img_path) if img.shape[2] == 1: img = np.concatenate((img, img, img), axis=2) img = img.reshape(1, img.shape[0], img.shape[1], img.shape[2]) d = DenseDesignMatrix(topo_view=img, view_converter=DefaultViewConverter(img.shape[1:])) random_rng = np.random.RandomState( [rng.randint(0, 256), rng.randint(0, 256), rng.randint(0, 256)]) p = ExtractPatches(patch_shape=patch_shape, num_patches=k, rng=random_rng) d.apply_preprocessor(p) X[i * 3:(i + 1) * 3, :] = d.X d.X = X base = '/data/lisatmp/goodfeli/darpa_imagenet_patch_%dx%d_train.' % ( patch_shape[0], patch_shape[1]) d.use_design_loc(base + 'npy') serial.save(base + 'pkl', d)