Exemple #1
0
    def on_monitor(self, *args, **kwargs):

        if not hasattr(self, 'record'):
            self.record = {}
            self.size = {}
            for dataset in self.datasets:
                assert tuple(dataset.view_converter.axes) == ('c', 0, 1, 'b')
                self.record[dataset] = dataset.get_topological_view().copy()
                self.size[dataset] = dataset.X.shape[0]
        else:
            for i, dataset in enumerate(self.datasets):
                size = self.size[dataset]
                assert dataset.X.shape[0] == size
                self.record[dataset] = np.concatenate(
                    (self.record[dataset],
                     dataset.get_topological_view().copy()),
                    axis=-1)
                record_view = self.record[dataset].copy()
                record_view /= np.abs(record_view).max()
                pv = PatchViewer(grid_shape=(record_view.shape[3] / size,
                                             size),
                                 patch_shape=record_view.shape[1:3],
                                 is_color=record_view.shape[0] == 3)
                for j in xrange(record_view.shape[3]):
                    pv.add_patch(np.transpose(record_view[:, :, :, j],
                                              (1, 2, 0)),
                                 rescale=False)
                print 'Dataset %d: ' % i
                pv.show()
                x = raw_input()
Exemple #2
0
def show_samples(generator,Noise_Dim,data_obj,filename):
    if data_obj.pitch_scale:
        pitch_max = 1.0
    else:
        pitch_max = 108.0
    rows = 4
    sample_cols = 5
    input_noise = np.random.uniform(-1.0,1.0,(rows*sample_cols, Noise_Dim))
    samples = generator.predict(input_noise)
    topo_samples = samples.reshape(samples.shape[0],4,samples.shape[-1]/4)
    #get topological_view

    pv = PatchViewer(grid_shape=(rows,sample_cols + 1),patch_shape=(4,samples.shape[-1]/4), \
                     is_color=False)
    X = np.concatenate((data_obj.X_train,data_obj.X_val,data_obj.X_test),axis = 0)
    topo_X = X
    print('Shape of dataset is {}').format(X.shape)
    X = X.reshape(X.shape[0],X.shape[1]*X.shape[2])

    for i in xrange(topo_samples.shape[0]):
        topo_sample = patch_quantize_01(patch_thresholding(topo_samples[i,:]/pitch_max))
        pv.add_patch(topo_sample * 2. -1.,rescale=False)
        if(i + 1) % sample_cols ==0:
            sample = samples[i,:]
            dists = np.square(X - sample).sum(axis = 1)
            j = np.argmin(dists)
            match = patch_quantize_01(patch_thresholding(topo_X[j,:]/pitch_max))
            pv.add_patch(match*2-1,rescale=False,activation = 1)
    print "Saving %s ..."%filename
    pv.save(filename)
    def __init__(self, generator, save_prefix, batch_size=20, grid_shape=(5, 4)):
        assert isinstance(generator, Generator)

        self.batch_sym = T.matrix('generate_batch')
        self.generate_f = theano.function([self.batch_sym],
                                          generator.dropout_fprop(self.batch_sym)[0])

        self.batch = generator.get_noise(batch_size).eval()
        self.save_prefix = save_prefix
        self.patch_viewer = PatchViewer(grid_shape=grid_shape, patch_shape=(32, 32),
                                        is_color=True)
Exemple #4
0
def create_connect_viewer(N, N1, imgs, count, W2):
    """
    Create the patch to show connections between layers.

    Parameters
    ----------
    N: int
        Number of rows.
    N1: int
        Number of elements in the first layer.
    imgs: ndarray
        Images of weights from the first layer.
    count: int
        Number of elements to show.
    W2: list
        Second hidden layer.
    """
    pv = PatchViewer((N, count), imgs.shape[1:3], is_color=imgs.shape[3] == 3)

    for i in xrange(N):
        w = W2[:, i]

        wneg = w[w < 0.]
        wpos = w[w > 0.]

        w /= np.abs(w).max()

        wa = np.abs(w)

        to_sort = zip(wa, range(N1), w)

        s = sorted(to_sort)

        for j in xrange(count):

            idx = s[N1-j-1][1]
            mag = s[N1-j-1][2]

            if mag > 0:
                act = (mag, 0)
            else:
                act = (0, -mag)

            pv.add_patch(imgs[idx, ...], rescale=True, activation=act)

    return pv
def init_viewer(dataset, rows, cols, vis_batch):
    """
    Initialisation of the PatchViewer with given rows and columns.

    Parameters
    ----------
    dataset: pylearn2 dataset
    rows: int
    cols: int
    vis_batch: numpy array
    """
    m = rows * cols
    _, patch_rows, patch_cols, channels = vis_batch.shape

    assert _ == m

    mapback = hasattr(dataset, 'mapback_for_viewer')
    actual_cols = 2 * cols * (1 + mapback) * (1 + (channels == 2))
    pv = PatchViewer((rows, actual_cols), (patch_rows, patch_cols),
                     is_color=(channels == 3))
    return pv
def create_patch_viewer(grid_shape, vis_chains, m):
    """
    Add the patches to show.

    Parameters
    ----------
    grid_shape: tuple
        The shape of the grid to show.
    vis_chains: numpy array
        Visibles chains.
    m: int
        Number of visible chains.
    """
    pv = PatchViewer(grid_shape,
                     vis_chains.shape[1:3],
                     is_color=vis_chains.shape[-1] == 3)

    for i in xrange(m):
        pv.add_patch(vis_chains[i, :], rescale=False)

    return pv
Exemple #7
0
def init_viewer(dataset, rows, cols):
    """
    Initialisation of the PatchViewer with given rows and columns.

    Parameters
    ----------
    dataset: pylearn2 dataset
    rows: int
    cols: int
    """
    m = rows * cols
    vis_batch = dataset.get_batch_topo(m)

    _, patch_rows, patch_cols, channels = vis_batch.shape

    assert _ == m

    mapback = hasattr(dataset, 'mapback_for_viewer')

    pv = PatchViewer((rows, cols * (1 + mapback)), (patch_rows, patch_cols),
                     is_color=(channels == 3))
    return pv
Exemple #8
0
for layer_idx in xrange(num_layers):
    print 'Making viewer for layer',layer_idx
    X = dataset.get_batch_topo(1)
    _, rows, cols, channels = X.shape
    assert _ == 1

    n = num_filters[layer_idx]

    c = int(np.sqrt(n))
    if c % 2 != 0:
        c += 1
    r =  n / c
    if r * c <  n:
        r += 1

    pv = PatchViewer((r,c), (rows, cols), is_color = channels == 3)

    for filter_idx in xrange(num_filters[layer_idx]):
        print '\t\tComputing response image for filter',filter_idx
        filter_act_record = act_record[layer_idx][:, filter_idx]
        assert filter_act_record.min() >= 0.0
        filter_act_record = [ (val, idx) for idx, val in enumerate(filter_act_record) ]
        filter_act_record.sort()
        num_top_filters = 1
        peak = filter_act_record[-1][0]
        thresh = perc * peak
        while num_top_filters < len(filter_act_record) and filter_act_record[-num_top_filters][0] > thresh:
            num_top_filters += 1
        top = filter_act_record[-num_top_filters:]
        print '\t\t\tUsing %d top examples (activation value %f - %f)' % \
                (num_top_filters, top[0][0], peak)
Exemple #9
0
print 'average needed filters', ave

count = max_count

print 'It takes', count, 'of', N1, 'elements to account for ', (
    thresh * 100.), '\% of the weight in at least one filter'

lim = 10
if count > lim:
    count = lim
    print 'Only displaying ', count, ' elements though.'

if count > N1:
    count = N1

pv = PatchViewer((N, count), imgs.shape[1:3], is_color=imgs.shape[3] == 3)

for i in xrange(N):
    w = W2[:, i]

    wneg = w[w < 0.]
    wpos = w[w > 0.]

    w /= np.abs(w).max()

    wa = np.abs(w)

    to_sort = zip(wa, range(N1), w)

    s = sorted(to_sort)
Exemple #10
0
ns = int(N.sqrt(n))

rows = 10
examplesPerRow = 5

if 'dataset_desc' not in dir(model):
    dataset = yaml_parse.load(model.dataset_yaml_src)

if dataset.view_shape()[2] == 3:
    print 'n=' + str(n)
    grey_dim = n / 3
    print 'grey_dim = ' + str(grey_dim)
    ns = int(N.sqrt(grey_dim))
    assert ns * ns == grey_dim

p = PatchViewer((rows, examplesPerRow * 5), (ns, ns),
                is_color=dataset.view_shape()[2] == 3)


def reshape(x):
    if dataset.view_shape()[2] == 3:
        fuckyou = []
        for i in xrange(3):
            #print 'x shape :'+str(x.shape)
            #print (i*grey_dim,(i+1)*grey_dim)
            channel = x[:, i * grey_dim:(i + 1) * grey_dim]
            #print 'channel shape :'+str(channel.shape)
            fuckyou.append(channel.reshape(ns, ns, 1))
        return N.concatenate(fuckyou, axis=2)
    else:
        return x.reshape((ns, ns))
print 'sorting'
indexed = zip(ranges, xrange(ranges.shape[0]))
indexed = sorted(indexed)

print 'assembling viewer'
if hasattr(model, 's3c'):
    W = model.s3c.W.get_value()
else:
    W = model.W.get_value()
topo_view = dataset.get_weights_view(W.T)

m, r, c, ch = topo_view.shape
assert ch in [1, 3]

n = int(np.sqrt(m))
if n**2 < m:
    n += 1

pv = PatchViewer((n, n), (r, c), is_color=(ch == 3))

for rng, idx in indexed:

    if norms != None:
        act = (rng, norms[idx])
    else:
        act = rng

    pv.add_patch(topo_view[idx, :, :, :], rescale=True, activation=act)

pv.show()
Exemple #12
0
to_sort = []

for i in xrange(Wv.shape[1] - 1):
    for j in xrange(i + 1, Wv.shape[1]):
        dot = abs(np.dot(Wv[:, i], Wv[:, j]))
        to_sort.append((-dot, (i, j)))

to_sort = sorted(to_sort)[0:100]

print - to_sort[0][0]
print - to_sort[99][0]

from pylearn2.config import yaml_parse

dataset = yaml_parse.load(model.dataset_yaml_src)

weights_view = dataset.get_weights_view(Wv.T)

from pylearn2.gui.patch_viewer import PatchViewer

pv = PatchViewer((100, 2), (28, 28), is_color=False)

for i in xrange(100):
    l, r = to_sort[i][1]
    l = weights_view[l]
    r = weights_view[r]
    pv.add_patch(l, rescale=True)
    pv.add_patch(r, rescale=True)

pv.show()
Exemple #13
0
    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())

    W = model.W.get_value()

    weights_view = dataset.get_weights_view(W.T)
Exemple #14
0
##############
# PLOT FILTERS
##############

def get_dims(n):
    num_rows = numpy.floor(numpy.sqrt(n))
    return (numpy.int(num_rows),
            numpy.int(numpy.ceil(n / num_rows)))

nblocks = model.depth - 1
W = [model.W[i].get_value().T for i in xrange(1, model.depth)]
max_filters = max([len(Wi) for Wi in W])
print 'max_filters = ', max_filters

block_viewer = PatchViewer(get_dims(max_filters),
                           (opts.height, opts.width), 
                           is_color = opts.color,
                           pad=(2,2))

main_viewer = PatchViewer(get_dims(nblocks), 
                          (block_viewer.image.shape[0],
                           block_viewer.image.shape[1]),
                          is_color = opts.color, 
                          pad=(5,5))

topo_shape = [opts.height, opts.width, opts.chans]
view_converter = DefaultViewConverter(topo_shape)

for di, w_di in enumerate(W):

    if opts.k == -1:
        # build "new_w" as linear combination of all previous filters
from pylearn2.gui.patch_viewer import PatchViewer
from pylearn2.utils import get_choice

print('Use test set?')
choices = {'y': 'test', 'n': 'train'}
which_set = choices[get_choice(choices)]

dataset = FoveatedNORB(which_set=which_set, center=True)

topo = dataset.get_topological_view()

b, r, c, ch = topo.shape

assert ch == 2

pv = PatchViewer((1, 2), (r, c), is_color=False)

i = 0
while True:
    patch = topo[i, :, :, :]
    patch = patch / np.abs(patch).max()

    pv.add_patch(patch[:, :, 1], rescale=False)
    pv.add_patch(patch[:, :, 0], rescale=False)

    pv.show()

    print(dataset.y[i])

    choices = {'g': 'goto image', 'q': 'quit'}
Exemple #16
0
    num_rows = numpy.floor(numpy.sqrt(nf))
    return (int(num_rows), int(numpy.ceil(nf / num_rows)))


topo_shape = [opts.height, opts.width, opts.chans]
viewconv = DefaultViewConverter(topo_shape)
viewdims = slice(0, None) if opts.color else 0

# load model and retrieve parameters
model = serial.load(opts.path)
wv = model.Wv.get_value().T
if opts.mu:
    wv = wv * model.mu.get_value()[:, None]

view1 = PatchViewer(get_dims(len(wv)), (opts.height, opts.width),
                    is_color=opts.color,
                    pad=(2, 2))
for i in xrange(len(wv)):
    topo_wvi = viewconv.design_mat_to_topo_view(wv[i:i + 1, :48 * 48])
    view1.add_patch(topo_wvi[0])

view2 = PatchViewer(get_dims(len(wv)), (opts.height, opts.width),
                    is_color=opts.color,
                    pad=(2, 2))
for i in xrange(len(wv)):
    topo_wvi = viewconv.design_mat_to_topo_view(wv[i:i + 1, 48 * 48:])
    view2.add_patch(topo_wvi[0])

pl.subplot(1, 2, 1)
pl.imshow(view1.image[:, :, viewdims])
pl.gray()
Exemple #17
0
# Begin modifying axes
base_conditional_data = args.conditional_sampler(
    generator, n, 1, embedding_file=args.embedding_file)
print 'Mean for each axis:'
pprint.pprint(zip(args.axes, base_conditional_data[:, args.axes].mean(axis=1)))
base_conditional_data[:, args.axes] -= 0.5 * shift

mod_conditional_data = base_conditional_data.copy()

# Build up a flat array of modified conditional data
mod_conditional_steps = []
for axis in args.axes:
    mod_conditional_data[:, axis] += shift
    mod_conditional_steps.extend(mod_conditional_data.copy())

mod_conditional_steps = np.array(mod_conditional_steps)

samples_orig = topo_sample_f(noise_data, base_conditional_data).swapaxes(0, 3)
samples_mod = topo_sample_f(np.tile(noise_data, (m, 1)),
                            mod_conditional_steps).swapaxes(0, 3)

pv = PatchViewer(grid_shape=(m + 1, n), patch_shape=(32, 32), is_color=True)

for sample_orig in samples_orig:
    pv.add_patch(sample_orig, activation=1)

for sample_mod in samples_mod:
    pv.add_patch(sample_mod)

pv.show()
Exemple #18
0
dataset = yaml_parse.load(model.dataset_yaml_src)

rows = 10
cols = 3
m = rows * cols

X = dataset.get_batch_design(100)

drop_mask, X_hat = f(X)

X, drop_mask, X_hat = [ dataset.get_topological_view(mat)
        for mat in [X, drop_mask, X_hat] ]
X = dataset.adjust_for_viewer(X)
X_hat = dataset.adjust_for_viewer(X_hat)

pv = PatchViewer( (rows, cols*3), (X.shape[1], X.shape[2]), is_color = True)

for i in xrange(m):
    #add original patch
    patch = X[i,:,:,:]
    if patch.shape[-1] != 3:
        patch = np.concatenate( (patch,patch,patch), axis=2)
    pv.add_patch(patch, rescale = False)

    #mark the masked areas as red
    mask_patch = drop_mask[i,:,:,0]
    red_channel = patch[:,:,0]
    green_channel = patch[:,:,1]
    blue_channel = patch[:,:,2]
    red_channel[mask_patch == 1] = 1.
    green_channel[mask_patch == 1] = -1.
Exemple #19
0
        ]
    X = dataset.adjust_to_be_viewed_with(Xt, Xt, per_example=True)
    if X_sequence[0].ndim == 2:
        X_sequence = [dataset.get_topological_view(mat) for mat in X_sequence]
    X_sequence = [
        dataset.adjust_to_be_viewed_with(mat, Xt, per_example=True)
        for mat in X_sequence
    ]

    if cost.supervised:
        rows += m

        Y_sequence = outputs[end_X_outputs:]

    pv = PatchViewer((rows, cols), (X.shape[1], X.shape[2]),
                     is_color=True,
                     pad=(8, 8))

    for i in xrange(m):
        """
        #add original patch
        patch = X[i,:,:,:]
        if patch.shape[-1] != 3:
            patch = np.concatenate( (patch,patch,patch), axis=2)
        pv.add_patch(patch, rescale = False, activation = (1,0,0))
        orig_patch = patch

        #mark the masked areas as red
        mask_patch = drop_mask[i,:,:,0]
        if drop_mask.shape[-1] > 1 and mask_gen.sync_channels and mask_gen.n_channels > 1:
            assert np.all(mask_patch == drop_mask[i,:,:,1])
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()


Exemple #21
0
# Setup for training and display
dataset_yaml_src = model.dataset_yaml_src
train_set = yaml_parse.load(dataset_yaml_src)
test_set = MNIST(which_set='test')

dataset = train_set
num_samples = n_examples

vis_batch = dataset.get_batch_topo(num_samples)
rval = tuple(vis_batch.shape[dataset.X_topo_space.axes.index(axis)]
             for axis in ('b', 0, 1, 'c'))
_, patch_rows, patch_cols, channels = rval
mapback = hasattr(dataset, 'mapback_for_viewer')
pv = PatchViewer((rows, cols*(1+mapback)),
                 (patch_rows, patch_cols),
                 is_color=(channels == 3))

# Get examples and masks
x_val = test_set.get_batch_design(num_samples)
m_val = np.ones((n_examples, input_dim))
m_val[:10, :392] = 0
m_val[10:20, 392:] = 0
m_val[20:30, ::2] = 0
m_val[30:40, 1::2] = 0
for i in xrange(28):
    m_val[40:50, (i*28):((2*i+1)*14)] = 0
for i in xrange(28):
    m_val[50:60, ((2*i+1)*14):((i+1)*28)] = 0
m_val[60:70, 196:588] = 0
for i in xrange(28):
Exemple #22
0
                    default=False,
                    action='store_true')
parser.add_argument('model_path')
args = parser.parse_args()

m, n = 4, 5
topo_samples, _ = sampler.get_conditional_topo_samples(
    args.model_path,
    m,
    n,
    args.conditional_sampler,
    embedding_file=(args.embedding_file if args.embedding_file is not None else
                    sampler.DEFAULT_EMBEDDING_FILE))

pv = PatchViewer(grid_shape=(m, (n + 1 if args.show_nearest_training else n)),
                 patch_shape=(32, 32),
                 is_color=True)

# Optionally load dataset for --show-nearest-training
dataset = None
if args.show_nearest_training:
    model = serial.load(args.model_path)

    # Shape: b * (0 * 1 * c)
    # (topo view)
    dataset = yaml_parse.load(model.dataset_yaml_src)

for i in xrange(topo_samples.shape[0]):
    topo_sample = topo_samples[i, :, :, :]
    pv.add_patch(topo_sample)
Exemple #23
0
import sys
from pylearn2.utils import serial
from pylearn2.datasets.preprocessing import ZCA
from pylearn2.datasets.dense_design_matrix import DenseDesignMatrix, DefaultViewConverter
from pylearn2.gui.patch_viewer import PatchViewer
import numpy as np

path = sys.argv[1]

prepro = serial.load(path)

zca = prepro.items[-1]

assert isinstance(zca, ZCA)

W = zca.P_

assert W.shape[1] % 3 == 0
n = int(np.sqrt(W.shape[1] / 3))

d = DenseDesignMatrix(X=W, view_converter=DefaultViewConverter((n, n, 3)))

W = d.get_weights_view(W)

pv = PatchViewer(grid_shape=(n * 3, n), patch_shape=(n, n), is_color=True)

for i in xrange(n * n * 3):
    pv.add_patch(W[i, ...], rescale=True)

pv.show()
Exemple #24
0
from pylearn2.datasets.cifar10 import CIFAR10
from pylearn2.gui.patch_viewer import PatchViewer

dataset = CIFAR10(which_set='test')

pv = PatchViewer((10, 1), (32, 32), is_color=True)

T, y = dataset.get_batch_topo(10, include_labels=True)

for i in xrange(10):
    print dataset.label_names[y[i]]
    pv.add_patch(dataset.adjust_for_viewer(T[i, :, :, :]), rescale=False)

pv.show()
from pylearn2.gui.patch_viewer import PatchViewer
import numpy as np

dataset = yaml_parse.load(model.dataset_yaml_src)

grid_shape = None

rows = 4
sample_cols = 5

# For some reason format_as from VectorSpace is not working right
samples = model.generator.sample(rows * sample_cols).eval()
topo_samples = dataset.get_topological_view(samples)

pv = PatchViewer(grid_shape=(rows, sample_cols + 1),
                 patch_shape=(48, 48),
                 is_color=False)

X = dataset.X
topo = dataset.get_topological_view()
index = 0
for i in xrange(samples.shape[0]):
    topo_sample = topo_samples[i, :, :, :]
    pv.add_patch(topo_sample * 2. - 1., rescale=False)

    if (i + 1) % sample_cols == 0:
        sample = samples[i, :]
        dists = np.square(X - sample).sum(axis=1)
        j = np.argmin(dists)
        match = topo[j, :]
        pv.add_patch(match * 2 - 1, rescale=False, activation=1)
from pylearn2.gui.patch_viewer import PatchViewer
import numpy as np

dataset = yaml_parse.load(model.dataset_yaml_src)

grid_shape = None

rows = 4
sample_cols = 5

# For some reason format_as from VectorSpace is not working right
samples = model.generator.sample(rows * sample_cols).eval()
topo_samples = dataset.get_topological_view(samples)

pv = PatchViewer(grid_shape=(rows, sample_cols + 1),
                 patch_shape=(32, 32),
                 is_color=True)
scale = np.abs(samples).max()

X = dataset.X
topo = dataset.get_topological_view()
index = 0
for i in xrange(samples.shape[0]):
    topo_sample = topo_samples[i, :, :, :]
    print topo_sample.min(), topo_sample.max()
    pv.add_patch(topo_sample / scale, rescale=False)

    if (i + 1) % sample_cols == 0:
        sample = samples[i, :]
        dists = np.square(X - sample).sum(axis=1)
        j = np.argmin(dists)
Exemple #27
0
    c = rng.randint(0, 8)
    cropped = X[:, r:r + 32, c:c + 32, i]
    other_cropped = other_X[:, r:r + 32, c:c + 32, i]
    if rng.randint(2):
        cropped = cropped[:, :, ::-1]
        other_cropped = other_cropped[:, :, ::-1]
    new_X[:, :, :, i] = cropped.copy()
    other_new_X[:, :, :, i] = other_cropped.copy()
X = new_X
other_X = other_new_X

b01c = f(X)

max_abs = max(np.abs(b01c).max(), np.abs(other_X).max())

b01c = b01c / max_abs
other_X = other_X / max_abs

other_X = np.transpose(other_X, (3, 1, 2, 0))

print "Formatting"
from pylearn2.gui.patch_viewer import PatchViewer

pv = PatchViewer(grid_shape=(rows, cols), patch_shape=(32, 32), is_color=True)

for i in xrange(m):
    pv.add_patch(b01c[i, :, :, :], rescale=False)
    pv.add_patch(other_X[i, :, :, :], rescale=False)
print "Showing"
pv.save('/u/goodfeli/vis.png')
Exemple #28
0
    normalizer = gdpdf_normalizer
elif t == SCORE:
    df = grad_func(- dataset.free_energy(X))
    mf = grad_func( - model.free_energy(X))

    normalizer = gpdf_normalizer
elif t == SCORED:
    df = grad_dir_func(- dataset.free_energy(X))
    mf = grad_dir_func( - model.free_energy(X))

    normalizer = gdpdf_normalizer
else:
    assert False


g.components.append(HeatMap(f = df, normalizer = normalizer))
dimg = g.render()

g.components.pop()
g.components.append(HeatMap(f = mf, normalizer = normalizer))

mimg = g.render()

from pylearn2.gui.patch_viewer import PatchViewer

pv = PatchViewer((2,1),dimg.shape[0:2], is_color = dimg.shape[2] == 3)
pv.add_patch(dimg,rescale = False)
pv.add_patch(mimg, rescale = False)
pv.show()

dataset_yaml_src = model.dataset_yaml_src

print 'Loading data...'
dataset = yaml_parse.load(dataset_yaml_src)


vis_batch = dataset.get_batch_topo(m)

_, patch_rows, patch_cols, channels = vis_batch.shape

assert _ == m

mapback = hasattr(dataset, 'mapback_for_viewer')

actual_cols = 2 * cols * (1 + mapback) * (1 + (channels == 2))
pv = PatchViewer((rows, actual_cols), (patch_rows, patch_cols), is_color=(channels == 3))


batch = model.visible_layer.space.make_theano_batch()
topo = batch.ndim > 2
reconstruction = model.reconstruct(batch)
recons_func = function([batch], reconstruction)

def show():
    ipt = vis_batch.copy()
    if not topo:
        ipt = dataset.get_design_matrix(ipt)
    recons_batch = recons_func(ipt.astype(batch.dtype))
    if not topo:
        recons_batch = dataset.get_topological_view(recons_batch)
    if mapback:
control.push_load_data(False)

dataset = yaml_parse.load(model.dataset_yaml_src)

try:
    layer_to_chains = model.layer_to_chains
except AttributeError:
    print "This model doesn't have negative chains."
    quit(-1)

vis_chains = layer_to_chains[model.visible_layer]
vis_chains = vis_chains.get_value()
if vis_chains.ndim == 2:
    vis_chains = dataset.get_topological_view(vis_chains)
vis_chains = dataset.adjust_for_viewer(vis_chains)

m = vis_chains.shape[0]
r = int(np.sqrt(m))
c = m // r
while r * c < m:
    c += 1

pv = PatchViewer((r, c),
                 vis_chains.shape[1:3],
                 is_color=vis_chains.shape[-1] == 3)

for i in xrange(m):
    pv.add_patch(vis_chains[i, :], rescale=False)

pv.show()