Beispiel #1
0
def make_viewer(mat, grid_shape=None, patch_shape=None, activation=None, pad=None, is_color = False, rescale = True):
    """ Given filters in rows, guesses dimensions of patches
        and nice dimensions for the PatchViewer and returns a PatchViewer
        containing visualizations of the filters"""

    num_channels = 1
    if is_color:
        num_channels = 3

    if grid_shape is None:
        grid_shape = PatchViewer.pick_shape(mat.shape[0] )
    if patch_shape is None:
        assert mat.shape[1] % num_channels == 0
        patch_shape = PatchViewer.pick_shape(mat.shape[1] / num_channels, exact = True)
        assert patch_shape[0] * patch_shape[1] * num_channels == mat.shape[1]
    rval = PatchViewer(grid_shape, patch_shape, pad=pad, is_color = is_color)
    topo_shape = (patch_shape[0], patch_shape[1], num_channels)
    view_converter = DefaultViewConverter(topo_shape)
    topo_view = view_converter.design_mat_to_topo_view(mat)
    for i in xrange(mat.shape[0]):
        if activation is not None:
            if hasattr(activation[0], '__iter__'):
                act = [a[i] for a in activation]
            else:
                act = activation[i]
        else:
            act = None

        patch = topo_view[i, :]

        rval.add_patch(patch, rescale=rescale,
                       activation=act)
    return rval
Beispiel #2
0
    def _transform_multi_channel_data(self, X, y):
        # Data partitioning
        parted_X, parted_y = self._partition_data(
            X=X, y=y, partition_size=self.window_size)
        transposed_X = np.transpose(parted_X, [0, 2, 1])
        converted_X = np.reshape(transposed_X,
                                 (transposed_X.shape[0], transposed_X.shape[1],
                                  1, transposed_X.shape[2]))

        # Create view converter
        view_converter = DefaultViewConverter(shape=self.sample_shape,
                                              axes=('b', 0, 1, 'c'))

        # Convert data into a design matrix
        view_converted_X = view_converter.topo_view_to_design_mat(converted_X)
        assert np.all(converted_X == view_converter.design_mat_to_topo_view(
            view_converted_X))

        # Format the target into proper format
        sum_y = np.sum(parted_y, axis=1)
        sum_y[sum_y > 0] = 1
        one_hot_formatter = OneHotFormatter(max_labels=self.n_classes)
        hot_y = one_hot_formatter.format(sum_y)

        return view_converted_X, hot_y, view_converter
def plot(w):

    nblocks = int(model.n_g / model.sparse_gmask.bw_g)
    filters_per_block = model.sparse_gmask.bw_g * model.sparse_hmask.bw_h

    block_viewer = PatchViewer((model.sparse_gmask.bw_g, model.sparse_hmask.bw_h),
                               (opts.height, opts.width),
                               is_color = opts.color,
                               pad=(2,2))

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

    main_viewer = PatchViewer(get_dims(nplots),
                              (chan_viewer.image.shape[0],
                               chan_viewer.image.shape[1]),
                              is_color = opts.color,
                              pad=(10,10))

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

    if opts.splitblocks:
        os.makedirs('filters/')

    for chan_i in xrange(nplots):

        viewer_dims = slice(0, None) if opts.color else chan_i

        for bidx in xrange(nblocks):

            for fidx in xrange(filters_per_block):
                fi = bidx * filters_per_block + fidx
                topo_view = view_converter.design_mat_to_topo_view(w[fi:fi+1,:])
                try:
                    block_viewer.add_patch(topo_view[0,:,:,viewer_dims])
                except:
                    import pdb; pdb.set_trace()

            if opts.splitblocks:
                pl.imshow(block_viewer.image, interpolation='nearest')
                pl.axis('off')
                pl.title('Wv - block %i, chan %i' % (bidx, chan_i))
                pl.savefig('filters/filters_chan%i_block%i.png' % (bidx, chan_i))

            chan_viewer.add_patch(block_viewer.image[:,:,viewer_dims] - 0.5)
            block_viewer.clear()

        main_viewer.add_patch(chan_viewer.image[:,:,viewer_dims] - 0.5)
        chan_viewer.clear()

    return copy.copy(main_viewer.image)
Beispiel #4
0
def make_viewer(mat,
                grid_shape=None,
                patch_shape=None,
                activation=None,
                pad=None,
                is_color=False,
                rescale=True):
    """
    .. todo::

        WRITEME properly

    Given filters in rows, guesses dimensions of patches
    and nice dimensions for the PatchViewer and returns a PatchViewer
    containing visualizations of the filters
    """

    num_channels = 1
    if is_color:
        num_channels = 3

    if grid_shape is None:
        grid_shape = PatchViewer.pick_shape(mat.shape[0])
    if mat.ndim > 2:
        patch_shape = mat.shape[1:3]
        topo_view = mat
        num_channels = mat.shape[3]
        is_color = num_channels > 1
    else:
        if patch_shape is None:
            assert mat.shape[1] % num_channels == 0
            patch_shape = PatchViewer.pick_shape(mat.shape[1] / num_channels,
                                                 exact=True)
            assert mat.shape[1] == (patch_shape[0] * patch_shape[1] *
                                    num_channels)
        topo_shape = (patch_shape[0], patch_shape[1], num_channels)
        view_converter = DefaultViewConverter(topo_shape)
        topo_view = view_converter.design_mat_to_topo_view(mat)
    rval = PatchViewer(grid_shape, patch_shape, pad=pad, is_color=is_color)
    for i in xrange(mat.shape[0]):
        if activation is not None:
            if hasattr(activation[0], '__iter__'):
                act = [a[i] for a in activation]
            else:
                act = activation[i]
        else:
            act = None

        patch = topo_view[i, :]

        rval.add_patch(patch, rescale=rescale, activation=act)
    return rval
Beispiel #5
0
    def _transform_multi_channel_data(self, X, y):
        # Data partitioning
        parted_X, parted_y = self._partition_data(X=X, y=y, partition_size=self.window_size)
        transposed_X = np.transpose(parted_X, [0, 2, 1])
        converted_X = np.reshape(transposed_X, (transposed_X.shape[0],
                                                transposed_X.shape[1],
                                                1,
                                                transposed_X.shape[2]))

        # Create view converter
        view_converter = DefaultViewConverter(shape=self.sample_shape,
                                              axes=('b', 0, 1, 'c'))

        # Convert data into a design matrix
        view_converted_X = view_converter.topo_view_to_design_mat(converted_X)
        assert np.all(converted_X == view_converter.design_mat_to_topo_view(view_converted_X))

        # Format the target into proper format
        sum_y = np.sum(parted_y, axis=1)
        sum_y[sum_y > 0] = 1
        one_hot_formatter = OneHotFormatter(max_labels=self.n_classes)
        hot_y = one_hot_formatter.format(sum_y)

        return view_converted_X, hot_y, view_converter
Beispiel #6
0
def make_viewer(mat, grid_shape=None, patch_shape=None,
                activation=None, pad=None, is_color = False, rescale = True):
    """
    Given filters in rows, guesses dimensions of patches
    and nice dimensions for the PatchViewer and returns a PatchViewer
    containing visualizations of the filters.

    Parameters
    ----------
    mat : ndarray
        Values should lie in [-1, 1] if `rescale` is False.
        0. always indicates medium gray, with negative values drawn as
        blacker and positive values drawn as whiter.
        A matrix with each row being a different image patch, OR
        a 4D tensor in ('b', 0, 1, 'c') format.
        If matrix, we assume it was flattened using the same procedure as a
        ('b', 0, 1, 'c') DefaultViewConverter uses.
    grid_shape : tuple, optional
        A tuple of two ints specifying the shape of the grad in the
        PatchViewer, in (rows, cols) format. If not specified, this
        function does its best to choose an aesthetically pleasing
        value.
    patch_shape : tupe, optional
        A tuple of two ints specifying the shape of the patch.
        If `mat` is 4D, this function gets the patch shape from the shape of
        `mat`. If `mat` is 2D and patch_shape is not specified, this function
        assumes the patches are perfectly square.
    activation : iterable
        An iterable collection describing some kind of activation value
        associated with each patch. This is indicated with a border around the
        patch whose color intensity increases with activation value.
        The individual activation values may be single floats to draw one
        border or iterable collections of floats to draw multiple borders with
        differing intensities around the patch.
    pad : int, optional
        The amount of padding to add between patches in the displayed image.
    is_color : int
        If True, assume the images are in color.
        Note needed if `mat` is in ('b', 0, 1, 'c') format since we can just
        look at its shape[-1].
    rescale : bool
        If True, rescale each patch so that its highest magnitude pixel
        reaches a value of either 0 or 1 depending on the sign of that pixel.

    Returns
    -------
    patch_viewer : PatchViewer
        A PatchViewer containing the patches stored in `mat`.
    """

    num_channels = 1
    if is_color:
        num_channels = 3

    if grid_shape is None:
        grid_shape = PatchViewer.pick_shape(mat.shape[0] )
    if mat.ndim > 2:
        patch_shape = mat.shape[1:3]
        topo_view = mat
        num_channels = mat.shape[3]
        is_color = num_channels > 1
    else:
        if patch_shape is None:
            assert mat.shape[1] % num_channels == 0
            patch_shape = PatchViewer.pick_shape(mat.shape[1] / num_channels,
                                                 exact = True)
            assert mat.shape[1] == (patch_shape[0] *
                                    patch_shape[1] *
                                    num_channels)
        topo_shape = (patch_shape[0], patch_shape[1], num_channels)
        view_converter = DefaultViewConverter(topo_shape)
        topo_view = view_converter.design_mat_to_topo_view(mat)
    rval = PatchViewer(grid_shape, patch_shape, pad=pad, is_color = is_color)
    for i in xrange(mat.shape[0]):
        if activation is not None:
            if hasattr(activation[0], '__iter__'):
                act = [a[i] for a in activation]
            else:
                act = activation[i]
        else:
            act = None

        patch = topo_view[i, :]

        rval.add_patch(patch, rescale=rescale,
                       activation=act)
    return rval
Beispiel #7
0
def make_viewer(mat,
                grid_shape=None,
                patch_shape=None,
                activation=None,
                pad=None,
                is_color=False,
                rescale=True):
    """
    Given filters in rows, guesses dimensions of patches
    and nice dimensions for the PatchViewer and returns a PatchViewer
    containing visualizations of the filters.

    Parameters
    ----------
    mat : ndarray
        Values should lie in [-1, 1] if `rescale` is False.
        0. always indicates medium gray, with negative values drawn as
        blacker and positive values drawn as whiter.
        A matrix with each row being a different image patch, OR
        a 4D tensor in ('b', 0, 1, 'c') format.
        If matrix, we assume it was flattened using the same procedure as a
        ('b', 0, 1, 'c') DefaultViewConverter uses.
    grid_shape : tuple, optional
        A tuple of two ints specifying the shape of the grad in the
        PatchViewer, in (rows, cols) format. If not specified, this
        function does its best to choose an aesthetically pleasing
        value.
    patch_shape : tupe, optional
        A tuple of two ints specifying the shape of the patch.
        If `mat` is 4D, this function gets the patch shape from the shape of
        `mat`. If `mat` is 2D and patch_shape is not specified, this function
        assumes the patches are perfectly square.
    activation : iterable
        An iterable collection describing some kind of activation value
        associated with each patch. This is indicated with a border around the
        patch whose color intensity increases with activation value.
        The individual activation values may be single floats to draw one
        border or iterable collections of floats to draw multiple borders with
        differing intensities around the patch.
    pad : int, optional
        The amount of padding to add between patches in the displayed image.
    is_color : int
        If True, assume the images are in color.
        Note needed if `mat` is in ('b', 0, 1, 'c') format since we can just
        look at its shape[-1].
    rescale : bool
        If True, rescale each patch so that its highest magnitude pixel
        reaches a value of either 0 or 1 depending on the sign of that pixel.

    Returns
    -------
    patch_viewer : PatchViewer
        A PatchViewer containing the patches stored in `mat`.
    """

    num_channels = 1
    if is_color:
        num_channels = 3

    if grid_shape is None:
        grid_shape = PatchViewer.pick_shape(mat.shape[0])
    if mat.ndim > 2:
        patch_shape = mat.shape[1:3]
        topo_view = mat
        num_channels = mat.shape[3]
        is_color = num_channels > 1
    else:
        if patch_shape is None:
            assert mat.shape[1] % num_channels == 0
            patch_shape = PatchViewer.pick_shape(mat.shape[1] / num_channels,
                                                 exact=True)
            assert mat.shape[1] == (patch_shape[0] * patch_shape[1] *
                                    num_channels)
        topo_shape = (patch_shape[0], patch_shape[1], num_channels)
        view_converter = DefaultViewConverter(topo_shape)
        topo_view = view_converter.design_mat_to_topo_view(mat)
    rval = PatchViewer(grid_shape, patch_shape, pad=pad, is_color=is_color)
    for i in xrange(mat.shape[0]):
        if activation is not None:
            if hasattr(activation[0], '__iter__'):
                act = [a[i] for a in activation]
            else:
                act = activation[i]
        else:
            act = None

        patch = topo_view[i, :]

        rval.add_patch(patch, rescale=rescale, activation=act)
    return rval
Beispiel #8
0
        else: 
            new_w = w_di
    else:
        new_w = numpy.zeros((len(w_di), opts.height * opts.width)) if di else w_di

    for fi in xrange(len(w_di)):

        if opts.k != -1:
            # build "new_w" as a linear combination of "strongest" filters in layer below
            if di > 0:
                temp.fill(0.)
                idx = numpy.argsort(w_di[fi])[-opts.k:]
                for fi_m1 in idx:
                    new_w[fi:fi+1] += w_di[fi, fi_m1] * prev_w[fi_m1:fi_m1+1,:]
                #for fi_m1 in xrange(len(w_di[fi])):
            else:
                temp = w_di[fi:fi+1,:]

        topo_view = view_converter.design_mat_to_topo_view(new_w[fi:fi+1])
        block_viewer.add_patch(topo_view[0])

    main_viewer.add_patch(block_viewer.image[:,:,0] - 0.5)
    block_viewer.clear()
    
    prev_w = new_w

pl.imshow(main_viewer.image, interpolation=None)
pl.axis('off');
pl.savefig('weights.png')
pl.show()
Beispiel #9
0
def analyze(config):
    output_path = config.get('output_path');
#     model_file = os.path.join(output_path, 'eeg', 'conv3', 'convolutional_network.pkl');
#     model_file = os.path.join(output_path, 'eeg', 'conv10', 'epochs', 'cnn_epoch94.pkl');
    model_file = '../../../debug/debug_run4/debug_network.pkl';
    with log_timing(log, 'loading convnet model from {}'.format(model_file)):
        model = serial.load(model_file);
        
    input_shape =  model.get_input_space().shape;
        
    config = config.eeg;
    hyper_params = {
                'input_length':input_shape[0], #25+151-1+301-1, # this should leave a single value per channel after convolution
                'hop_size':5,               # reduce amount of data by factor 5
                
                'dataset_root': config.get('dataset_root'),
                'dataset_suffix': config.get('dataset_suffix'),
                'save_path': config.get('save_path'),
        }
        
    dataset_yaml = '''
    !obj:deepthought.datasets.rwanda2013rhythms.EEGDataset.EEGDataset {
                                 name : 'testset',
                                 path : %(dataset_root)s, 
                                 suffix : '_channels', # %(dataset_suffix)s,
                                 subjects : [0],
                                 resample : [400, 100],
                                 start_sample : 2500,
                                 stop_sample  : 3200,     # None (empty) = end of sequence
                  # FIXME:                
#                                  n_fft : 24,
#                                  frame_size : 10, # %(input_length)i,                                
                                 frame_size : %(input_length)i,
                                 
                                 hop_size : %(hop_size)i,           
                                 label_mode : 'rhythm_type',
#                                  save_matrix_path: '../../../debug/debug.pkl'
                            }
'''
    dataset_yaml = dataset_yaml  % hyper_params;
    print dataset_yaml;

    with log_timing(log, 'parsing yaml'):    
        testset = yaml_parse.load(dataset_yaml);
        
#     print testset.subject_partitions;
#     print testset.sequence_partitions;
    
    seq_starts = testset.sequence_partitions;
#     return;
    
#     axes=['b', 0, 1, 'c']
#     def dimshuffle(b01c):
#         default = ('b', 0, 1, 'c')
#         return b01c.transpose(*[default.index(axis) for axis in axes])
#     data = dimshuffle(testset.X);
    
#     design_matrix = model.get_design_matrix()

#     view_converter = DefaultViewConverter([475, 1, 1]);
#     data = view_converter.


#     ## get the labels
#     data_specs= (model.get_output_space(), "targets");
#     it = testset.iterator(
#                            mode='sequential', 
#                            batch_size=100,
#                            data_specs=data_specs);
#     labels = np.hstack([np.argmax(minibatch, axis = 1) for minibatch in it])
#     print labels[0:1000]
# 
#     ## get the predictions
#     minibatch = model.get_input_space().make_theano_batch();
#     output_fn = theano.function(inputs=[minibatch], 
#                                 outputs=T.argmax(model.fprop(minibatch), axis = 1));
#     print "function compiled"
# #     data_specs= (CompositeSpace((
# #                                 model.get_input_space(), 
# #                                 model.get_output_space())), 
# #                 ("features", "targets"));
#                 
#     data_specs= (model.get_input_space(), "features");    
#     it = testset.iterator(
#                             mode='sequential', 
#                             batch_size=100,
#                             data_specs=data_specs);
#     print "iterator ready"
#         
#     y_pred = np.hstack([output_fn(minibatch) for minibatch in it])
#     
#     print y_pred[0:1000]
    
    
    minibatch = model.get_input_space().make_theano_batch();
    output_fn = theano.function(inputs=[minibatch], 
                                outputs=T.argmax(model.fprop(minibatch), axis = 1));
    print "function compiled"
    
    data_specs= (CompositeSpace((
                                model.get_input_space(), 
                                model.get_output_space())), 
                ("features", "targets"));
    it = testset.iterator('sequential',
                          batch_size=100,
                          data_specs=data_specs);
    print "iterator ready"
                    
    y_pred = [];
    y_real = [];                
    for minibatch, target in it:
        y_pred.append(output_fn(minibatch));
        y_real.append(np.argmax(target, axis = 1));
    y_pred = np.hstack(y_pred);
    y_real = np.hstack(y_real);   
    
    print y_pred[0:1000]
    
    print classification_report(y_real, y_pred);
    print confusion_matrix(y_real, y_pred);

    misclass = (y_real != y_pred);
    print misclass.mean();
    
    correct = 0;
    s_real = [];
    s_pred = [];
    s_pred_agg = [];
    
    n_channels = 16;
    channel_scores = np.zeros(n_channels, dtype=np.int);
    
    for i in xrange(len(seq_starts)):
        
        start = seq_starts[i];
        if i < len(seq_starts) - 1:
            stop = seq_starts[i+1];
        else:
            stop = None;
        
        s_real.append(y_real[start]);
        
#         print np.bincount(y_pred[start:stop]);
#         print np.argmax(np.bincount(y_pred[start:stop]));

        s_pred.append(np.argmax(np.bincount(y_pred[start:stop])));
        
        s_pred_agg.append(np.mean(y_pred[start:stop])); # works only for binary classification
        
        seq_misclass = misclass[start:stop].mean();
#         print '{} [{}{}]: {}'.format(i, start, stop, seq_misclass);
        
        if seq_misclass < 0.5: # more correct than incorrect
            correct += 1;
            channel_scores[i%n_channels] += 1;
    
    s_real = np.hstack(s_real);
    s_pred = np.hstack(s_pred);  
    
    print s_real;
    print s_pred;       
    print s_pred_agg;
    
    print 'aggregated'
    print classification_report(s_real, s_pred);
    print confusion_matrix(s_real, s_pred);
    
    s_misclass = (s_real != s_pred);
    print s_misclass.mean();
    
    print channel_scores;
    
    return;
    
    
    
    
    
    
    
    

    input_shape =  model.get_input_space().shape;
    
    print input_shape
    
    view_converter = DefaultViewConverter((input_shape[0], input_shape[1], 1));
    
    data = view_converter.design_mat_to_topo_view(testset.X);
    print data.shape;
                
    X = model.get_input_space().make_theano_batch()
    Y = model.fprop( X )
    Y = T.argmax( Y, axis = 1 ) # needed - otherwise not single value
    output_fn = theano.function( [X], Y );
    


    
#     y_pred = output_fn( data );

    batch_size = 1000;
    y_pred = [];
    batch_start = 0;
    while batch_start < data.shape[0]:
        batch_stop = min(data.shape[0], batch_start + batch_size);
        y_pred.append(output_fn( data[batch_start:batch_stop] ));
#         if batch_start == 0: print y_pred;
        batch_start = batch_stop;
    y_pred = np.hstack(y_pred);

    print testset.labels[0:1000]
    print y_pred[0:1000]

    print classification_report(testset.labels, y_pred);
    print confusion_matrix(testset.labels, y_pred);

    labels = np.argmax(testset.y, axis=1)
    print classification_report(labels, y_pred);
    print confusion_matrix(labels, y_pred);
    
    labels = np.argmax(testset.y, axis=1)
    print classification_report(labels, y_pred);
    print confusion_matrix(labels, y_pred);

    misclass = (labels != y_pred).mean()
    print misclass
    
#     # alternative version from KeepBestParams
#     minibatch = T.matrix('minibatch')
#     output_fn = theano.function(inputs=[minibatch],outputs=T.argmax( model.fprop(minibatch), axis = 1 ));
#     it = testset.iterator('sequential', batch_size=batch_size, targets=False);
#     y_pred = [output_fn(mbatch) for mbatch in it];

#             y_hat = T.argmax(state, axis=1)
#             y = T.argmax(target, axis=1)
#             misclass = T.neq(y, y_hat).mean()
#             misclass = T.cast(misclass, config.floatX)
#             rval['misclass'] = misclass
#             rval['nll'] = self.cost(Y_hat=state, Y=target)
        
    

    log.debug('done');
Beispiel #10
0
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()
pl.axis('off')
pl.subplot(1, 2, 2)
pl.imshow(view2.image[:, :, viewdims])
    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]

wv_viewer = 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])
    wv_viewer.add_patch(topo_wvi[0])
if opts.wv_only:
    wv_viewer.show()
    os.sys.exit()

wg = model.Wg.get_value()
wh = model.Wh.get_value()
wg_viewer2 = PatchViewer((opts.top, opts.top), (opts.height, opts.width),
                         is_color = opts.color, pad=(2,2))
wg_viewer1 = PatchViewer(get_dims(len(wg)/opts.top),
                         (wg_viewer2.image.shape[0], wg_viewer2.image.shape[1]),
                         is_color = opts.color, pad=(2,2))
for i in xrange(0, len(wg), opts.top):
    for j in xrange(i, i + opts.top):
        idx = numpy.argsort(wg[j])[-opts.top:][::-1]
Beispiel #12
0
if not opts.local:
    samples = samples / numpy.abs(samples).max()

##############
# PLOT FILTERS
##############

import pdb; pdb.set_trace()
viewer = PatchViewer(get_dims(model.batch_size),
                     (opts.height, opts.width),
                     is_color = opts.color,
                     pad=(2,2))

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

for chan_i in xrange(nplots):

    topo_chan = topo_view if opts.color else topo_view[..., chan_i:chan_i+1]

    for bi in xrange(model.batch_size):
        viewer.add_patch(topo_chan[bi])

    #pl.subplot(1, nplots, chan_i+1)
    #pl.imshow(viewer.image, interpolation=None)
    #pl.axis('off'); pl.title('samples (channel %i)' % chan_i)
    viewer.show()


pl.savefig('weights.png')
Beispiel #13
0
class myDenseDesignMatrix(dense_design_matrix.DenseDesignMatrix):

    _default_seed = (17, 2, 946)

    def __init__(self, X=None, topo_view=None, y=None, latent = None,
                 view_converter=None, axes=('b', 0, 1, 'c'),
                 rng=_default_seed, preprocessor=None, fit_preprocessor=False,
                 X_labels=None, y_labels=None):

        self.latent = latent
        self.X = X
        self.y = y
        self.view_converter = view_converter
        self.X_labels = X_labels
        self.y_labels = y_labels

        self._check_labels()

        if topo_view is not None:
            assert view_converter is None
            self.set_topological_view(topo_view, axes)
        else:
            assert X is not None, ("DenseDesignMatrix needs to be provided "
                                   "with either topo_view, or X")
            if view_converter is not None:

                # Get the topo_space (usually Conv2DSpace) from the
                # view_converter
                if not hasattr(view_converter, 'topo_space'):
                    raise NotImplementedError("Not able to get a topo_space "
                                              "from this converter: %s"
                                              % view_converter)

                # self.X_topo_space stores a "default" topological space that
                # will be used only when self.iterator is called without a
                # data_specs, and with "topo=True", which is deprecated.
                self.X_topo_space = view_converter.topo_space
            else:
                self.X_topo_space = None

            # Update data specs, if not done in set_topological_view
            X_source = 'features'
            if X_labels is None:
                X_space = VectorSpace(dim=X.shape[1])
            else:
                if X.ndim == 1:
                    dim = 1
                else:
                    dim = X.shape[-1]
                X_space = IndexSpace(dim=dim, max_labels=X_labels)

            if y is None:
                space = X_space
                source = X_source
            else:
                if y.ndim == 1:
                    dim = 1
                else:
                    dim = y.shape[-1]
                if y_labels is not None:
                    y_space = IndexSpace(dim=dim, max_labels=y_labels)
                else:
                    y_space = VectorSpace(dim=dim)
                y_source = 'targets'

                Latent_space = VectorSpace(dim=latent.shape[-1])
                Latent_source = 'latents'
                space = CompositeSpace((X_space, y_space, Latent_space))
                source = (X_source, y_source, Latent_source)

            self.data_specs = (space, source)
            self.X_space = X_space

        self.compress = False
        self.design_loc = None
        self.rng = make_np_rng(rng, which_method="random_integers")
        # Defaults for iterators
        self._iter_mode = resolve_iterator_class('sequential')
        self._iter_topo = False
        self._iter_targets = False
        self._iter_data_specs = (self.X_space, 'features')

        if preprocessor:
            preprocessor.apply(self, can_fit=fit_preprocessor)
        self.preprocessor = preprocessor



    def get_data(self):
        """
        Returns all the data, as it is internally stored.
        The definition and format of these data are described in
        `self.get_data_specs()`.

        Returns
        -------
        data : numpy matrix or 2-tuple of matrices
            The data
        """
        if self.y is None:
            return self.X
        else:
            return (self.X, self.y, self.latent)



    def set_topological_view(self, V, axes=('b', 0, 1, 'c')):
        """
        Sets the dataset to represent V, where V is a batch
        of topological views of examples.

        .. todo::

            Why is this parameter named 'V'?

        Parameters
        ----------
        V : ndarray
            An array containing a design matrix representation of
            training examples.
        axes : WRITEME
        """
        assert not contains_nan(V)
        rows = V.shape[axes.index(0)]
        cols = V.shape[axes.index(1)]
        channels = V.shape[axes.index('c')]
        self.view_converter = DefaultViewConverter([rows, cols, channels],
                                                   axes=axes)
        self.X = self.view_converter.topo_view_to_design_mat(V)
        # self.X_topo_space stores a "default" topological space that
        # will be used only when self.iterator is called without a
        # data_specs, and with "topo=True", which is deprecated.
        self.X_topo_space = self.view_converter.topo_space
        assert not contains_nan(self.X)

        # Update data specs
        X_space = VectorSpace(dim=self.X.shape[1])
        X_source = 'features'
        if self.y is None:
            space = X_space
            source = X_source
        else:
            if self.y.ndim == 1:
                dim = 1
            else:
                dim = self.y.shape[-1]
            # This is to support old pickled models
            if getattr(self, 'y_labels', None) is not None:
                y_space = IndexSpace(dim=dim, max_labels=self.y_labels)
            elif getattr(self, 'max_labels', None) is not None:
                y_space = IndexSpace(dim=dim, max_labels=self.max_labels)
            else:
                y_space = VectorSpace(dim=dim)
            y_source = 'targets'

            Latent_space = VectorSpace(dim=self.latent.shape[-1])
            Latent_source = 'latents'

            space = CompositeSpace((X_space, y_space,Latent_space))
            source = (X_source, y_source,Latent_source)

        self.data_specs = (space, source)
        self.X_space = X_space
        self._iter_data_specs = (X_space, X_source)

    def get_targets(self):
        """
        .. todo::

            WRITEME
        """
        return self.y
    def get_latents(self):
        """
        .. todo::

            WRITEME
        """
        return self.latent

    def get_batch_design(self, batch_size, include_labels=False):

        try:
            idx = self.rng.randint(self.X.shape[0] - batch_size + 1)
        except ValueError:
            if batch_size > self.X.shape[0]:
                reraise_as(ValueError("Requested %d examples from a dataset "
                                      "containing only %d." %
                                      (batch_size, self.X.shape[0])))
            raise
        rx = self.X[idx:idx + batch_size, :]
        if include_labels:
            if self.y is None:
                return rx, None
            ry = self.y[idx:idx + batch_size]
            rlatent = self.latent[idx:idx + batch_size]
            return rx, ry,rlatent
        rx = np.cast[config.floatX](rx)
        return rx

    def get_batch_topo(self, batch_size, include_labels=False):
        """
        .. todo::

            WRITEME

        Parameters
        ----------
        batch_size : int
            WRITEME
        include_labels : bool
            WRITEME
        """

        if include_labels:
            batch_design, labels, latents= self.get_batch_design(batch_size, True)
        else:
            batch_design = self.get_batch_design(batch_size)

        rval = self.view_converter.design_mat_to_topo_view(batch_design)

        if include_labels:
            return rval, labels, latents

        return rval
Beispiel #14
0
    def __init__(self,
                 patient_id,
                 which_set,
                 leave_out_seizure_idx_valid,
                 leave_out_seizure_idx_test,
                 data_dir,
                 preprocessor_dir,
                 batch_size=None,
                 balance_class=True,
                 decompose_subbands=False,
                 axes=('b', 0, 1, 'c'),
                 default_seed=0):

        self.balance_class = balance_class
        self.batch_size = batch_size

        EpilepsiaeEEGLoader.__init__(
            self,
            patient_id=patient_id,
            which_set=which_set,
            leave_out_seizure_idx_valid=leave_out_seizure_idx_valid,
            leave_out_seizure_idx_test=leave_out_seizure_idx_test,
            data_dir=data_dir)

        print 'Load signal ...'
        t = time.time()
        # (# of segments, # of samples, # of channels)
        raw_X, y = self.load_data()
        elapsed = time.time() - t
        print(' Elapsed time: ' + str(elapsed) + ' seconds')

        # Preprocessing
        print 'Scaling signal ...'
        t = time.time()
        if which_set == 'train':
            # Reshape the data back to (number of samples x number of channels) for pre-processing
            unrolled_X = np.reshape(raw_X,
                                    (-1, self.scalp_channel_labels.size))

            scaler = preprocessing.StandardScaler()
            # scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1))
            scaler = scaler.fit(unrolled_X)

            with open(
                    os.path.join(
                        preprocessor_dir, self.patient_id + '_scaler_eeg_' +
                        str(self.leave_out_seizure_idx_valid) + '_' +
                        str(self.leave_out_seizure_idx_test) + '.pkl'),
                    'w') as f:
                pickle.dump(scaler, f)

            scaled_X = raw_X.copy()
            for seg_idx in range(scaled_X.shape[0]):
                scaled_X[seg_idx, :, :] = scaler.transform(
                    scaled_X[seg_idx, :, :])
        else:
            with open(
                    os.path.join(
                        preprocessor_dir, self.patient_id + '_scaler_eeg_' +
                        str(self.leave_out_seizure_idx_valid) + '_' +
                        str(self.leave_out_seizure_idx_test) + '.pkl')) as f:
                scaler = pickle.load(f)

            scaled_X = raw_X.copy()
            for seg_idx in range(scaled_X.shape[0]):
                scaled_X[seg_idx, :, :] = scaler.transform(
                    scaled_X[seg_idx, :, :])
        elapsed = time.time() - t
        print(' Elapsed time: ' + str(elapsed) + ' seconds')

        raw_X = None

        if decompose_subbands:

            def bandpass_fir(data,
                             lowcut_f,
                             highcut_f,
                             sampling_rate,
                             window='hamming'):
                '''
                Bandpass filtering using a FIR filter.

                Parameters
                ----------
                data: numpy array
                    Input data with shape [n_samples, n_channels].
                :param lowcut_f:
                :param highcut_f:
                :param sampling_rate:
                :param window:
                :return:
                '''

                nyq_f = sampling_rate * 0.5
                n_taps = max(3 * (sampling_rate / (lowcut_f * 1.0)),
                             3 * nyq_f)  # Filter length

                # The filter length must be even if a passband includes the Nyquist frequency.
                if n_taps % 2 == 1:
                    n_taps = n_taps + 1

                taps = firwin(n_taps, [lowcut_f, highcut_f],
                              nyq=nyq_f,
                              pass_zero=False,
                              window=window,
                              scale=False)

                # If the data is too short, zero-padding
                extra = (3 * taps.size) - data.shape[0]
                half_extra = int(np.ceil(extra / 2.0)) + 1
                if half_extra > 0:
                    padded_data = np.lib.pad(data, ((half_extra, half_extra),
                                                    (0, 0)),
                                             'constant',
                                             constant_values=0)
                else:
                    padded_data = data

                filtered_data = filtfilt(taps, 1.0, padded_data, axis=0)

                if half_extra > 0:
                    return filtered_data[half_extra:-half_extra, :]
                else:
                    return filtered_data

            print 'Decompose EEG signals into 5 sub-bands ...'

            # Decompose EEG data in each segment in to 5 sub-bands
            preprocessed_X = np.zeros((
                scaled_X.shape[0],  # Number of segments
                scaled_X.shape[1],  # Segment samples
                5,  # Number of sub-bands
                scaled_X.shape[2]))  # Number of channels

            t = time.time()
            for seg_idx in range(preprocessed_X.shape[0]):
                delta_X = bandpass_fir(scaled_X[seg_idx], 0.5, 4,
                                       self.sampling_rate)  # Delta 0.5-4 Hz
                theta_X = bandpass_fir(scaled_X[seg_idx], 4, 8,
                                       self.sampling_rate)  # Theta 4-8 Hz
                alpha_X = bandpass_fir(scaled_X[seg_idx], 8, 15,
                                       self.sampling_rate)  # Alpha 8-15 Hz
                beta_X = bandpass_fir(scaled_X[seg_idx], 15, 30,
                                      self.sampling_rate)  # Beta 15-30 Hz
                gamma_X = bandpass_fir(
                    scaled_X[seg_idx], 30, (self.sampling_rate * 0.5) - 0.1,
                    self.sampling_rate)  # Gamma 30-Nyquist Hz

                for ch_idx in range(preprocessed_X.shape[3]):
                    preprocessed_X[seg_idx][:, 0, ch_idx] = delta_X[:, ch_idx]
                    preprocessed_X[seg_idx][:, 1, ch_idx] = theta_X[:, ch_idx]
                    preprocessed_X[seg_idx][:, 2, ch_idx] = alpha_X[:, ch_idx]
                    preprocessed_X[seg_idx][:, 3, ch_idx] = beta_X[:, ch_idx]
                    preprocessed_X[seg_idx][:, 4, ch_idx] = gamma_X[:, ch_idx]

                if seg_idx % 20 == 0 or seg_idx == preprocessed_X.shape[0] - 1:
                    print ' {0} segments {1} seconds ...'.format(
                        seg_idx + 1,
                        time.time() - t)

            elapsed = time.time() - t
            print ' Elapsed time: ' + str(elapsed) + ' seconds'

        else:
            # Reshape the preprocessed EEG data into a compatible format for CNN in pylearn2
            preprocessed_X = np.reshape(
                scaled_X,
                (
                    scaled_X.shape[0],  # Number of segments
                    scaled_X.shape[1],  # Segment samples
                    1,  # EEG data are time-series data (i.e., 1 dimension)
                    scaled_X.shape[2]))  # Number of channels

        scaled_X = None

        # Print shape of input data
        print '------------------------------'
        print 'Dataset: {0}'.format(self.which_set)
        print 'Number of samples: {0}'.format(preprocessed_X.shape[0])
        print ' Preictal samples: {0}'.format(self.preictal_samples)
        print ' Nonictal samples: {0}'.format(self.nonictal_samples)
        print 'Shape of each sample: ({0}, {1})'.format(
            preprocessed_X.shape[1], preprocessed_X.shape[2])
        print 'Number of channels: {0}'.format(preprocessed_X.shape[3])
        print '------------------------------'

        # Create a view converter
        view_converter = DefaultViewConverter(
            shape=[
                preprocessed_X.shape[1],  # Segment samples
                preprocessed_X.shape[2],  # Number of sub-bands
                preprocessed_X.shape[3]
            ],  # Number of channels
            axes=('b', 0, 1, 'c'))

        # Sanity check
        view_converted_X = view_converter.topo_view_to_design_mat(
            preprocessed_X)
        assert np.all(preprocessed_X == view_converter.design_mat_to_topo_view(
            view_converted_X))

        preprocessed_X = None

        if self.balance_class and (self.which_set == 'train'
                                   or self.which_set == 'valid_train'):
            self.X_full = view_converted_X
            self.y_full = y

            (X, y) = self.get_data()
        else:
            # Zero-padding (if necessary)
            if not (self.batch_size is None):
                view_converted_X, y = self.zero_pad(view_converted_X, y,
                                                    self.batch_size)

            X = view_converted_X

        # Initialize DenseDesignMatrix
        DenseDesignMatrix.__init__(self,
                                   X=X,
                                   y=y,
                                   view_converter=view_converter,
                                   axes=axes)
Beispiel #15
0
    assert opts.chans == 3
    nplots = 1

def get_dims(nf):
    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(); pl.axis('off')
pl.subplot(1,2,2); pl.imshow(view2.image[:,:,viewdims]); pl.gray(); pl.axis('off')
pl.show()
(opts, args) = parser.parse_args()

nplots = opts.chans
if opts.color:
    assert opts.chans == 3
    nplots = 1

def get_dims(nf):
    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)
if isinstance(model, TemperedDBN):
    rbm = model.rbms[opts.layer]
else:
    rbm = model

wv = rbm.Wv.get_value().T
wv_viewer = 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])
    wv_viewer.add_patch(topo_wvi[0])

wv_viewer.show()