Пример #1
0
def test_np_format_as_conv2d_vector_conv2d():
    conv2d_space1 = Conv2DSpace(shape=(8, 8),
                                num_channels=3,
                                axes=('c', 'b', 1, 0))
    vector_space = VectorSpace(dim=8 * 8 * 3, sparse=False)
    conv2d_space0 = Conv2DSpace(shape=(8, 8),
                                num_channels=3,
                                axes=('b', 'c', 0, 1))
    data = np.arange(5 * 8 * 8 * 3).reshape(5, 3, 8, 8)

    vecval = conv2d_space0.np_format_as(data, vector_space)
    rval1 = vector_space.np_format_as(vecval, conv2d_space1)
    rval2 = conv2d_space0.np_format_as(data, conv2d_space1)
    assert np.allclose(rval1, rval2)

    nval = data.transpose(1, 0, 3, 2)
    assert np.allclose(nval, rval1)
Пример #2
0
 def get_weights_topo(self):
     if not isinstance(self.input_space, Conv2DSpace):
         raise NotImplementedError()
     desired = self.W.get_value().T
     ipt = self.desired_space.format_as(desired, self.input_space)
     rval = Conv2DSpace.convert_numpy(ipt, self.input_space.axes,
                                      ('b', 0, 1, 'c'))
     return rval
Пример #3
0
    def initialize_output_space(self):
        """
        Initializes the output space of the ConvElemwise layer by taking
        pooling operator and the hyperparameters of the convolutional layer
        into consideration as well.
        """
        dummy_batch_size = self.mlp.batch_size
        if dummy_batch_size is None:
            dummy_batch_size = 2

        self.output_channels = self.input_space.num_channels

        dummy_detector = \
            sharedX(self.input_space.get_origin_batch(dummy_batch_size))
        if self.pool_type is not None:
            assert self.pool_type in ['max', 'mean']
            if self.pool_type == 'max':
                dummy_p = downsample.max_pool_2d(dummy_detector,
                                                 self.pool_shape,
                                                 ignore_border=False)
                # dummy_p = max_pool(bc01=dummy_detector,
                #                    pool_shape=self.pool_shape,
                #                    pool_stride=self.pool_stride,
                #                    image_shape=self.input_space.shape)
            elif self.pool_type == 'mean':
                dummy_p = mean_pool(bc01=dummy_detector,
                                    pool_shape=self.pool_shape,
                                    pool_stride=self.pool_stride,
                                    image_shape=self.input_space.shape)


            dummy_p = dummy_p.eval()
            print dummy_detector.eval().shape, dummy_p.shape
            self.output_space = Conv2DSpace(shape=[dummy_p.shape[2],
                                                   dummy_p.shape[3]],
                                            num_channels=
                                                self.output_channels,
                                            axes=('b', 'c', 0, 1))
        else:
            dummy_detector = dummy_detector.eval()
            print dummy_detector.shape
            self.output_space = Conv2DSpace(shape=[dummy_detector.shape[2],
                                            dummy_detector.shape[3]],
                                            num_channels=self.output_channels,
                                            axes=('b', 'c', 0, 1))
        print "Output shape", self.output_space.shape, self.output_space.num_channels
Пример #4
0
    def get_weights_topo(self):
        """
        Returns a topological view of the weights, the first half
        corresponds to wxf and the second half to wyf.

        Returns
        -------
        weights : ndarray
            Same as the return value of `get_weights` but formatted as a 4D
            tensor with the axes being (hidden/factor units, rows, columns,
            channels).The the number of channels is either 1 or 3
            (because they will be visualized as grayscale or RGB color).
            At the moment the function only supports factors whose sqrt
            is exact.
        """
        if (not isinstance(self.input_space.components[0], Conv2DSpace) or
                not isinstance(self.input_space.components[1], Conv2DSpace)):
            raise NotImplementedError()
        wxf = self.wxf.get_value(borrow=False).T
        wyf = self.wyf.get_value(borrow=False).T
        convx = self.input_space.components[0]
        convy = self.input_space.components[1]
        vecx = VectorSpace(self.nvisx)
        vecy = VectorSpace(self.nvisy)
        wxf_view = vecx.np_format_as(
            wxf,
            Conv2DSpace(convx.shape,
                        num_channels=convx.num_channels,
                        axes=('b', 0, 1, 'c')))
        wyf_view = vecy.np_format_as(
            wyf,
            Conv2DSpace(convy.shape,
                        num_channels=convy.num_channels,
                        axes=('b', 0, 1, 'c')))
        h = int(numpy.ceil(numpy.sqrt(self.nfac)))
        new_weights = numpy.zeros((wxf_view.shape[0] * 2, wxf_view.shape[1],
                                   wxf_view.shape[2], wxf_view.shape[3]),
                                  dtype=wxf_view.dtype)
        t = 0
        while t < (self.nfac // h):
            filter_pair = numpy.concatenate(
                (wxf_view[h * t:h * (t + 1), ...], wyf_view[h * t:h *
                                                            (t + 1), ...]), 0)
            new_weights[h * 2 * t:h * 2 * (t + 1), ...] = filter_pair
            t += 1
        return new_weights
Пример #5
0
 def _get_desired_space(self, space):
     if isinstance(space, Conv2DSpace):
         return Conv2DSpace(shape=space.shape,
                            num_channels=space.num_channels,
                            axes=('b', 'c', 0, 1))
     elif isinstance(space, VectorSpace):
         return space
     else:
         raise ValueError('Space not supported: {}'.format(space))
Пример #6
0
    def test_make_sparse_random_conv2D(self):
        """
        Make random sparse filters, count whether the number of
        non-zero elements is sensible
        """
        axes = ('c', 0, 1, 'b')
        input_space = Conv2DSpace((3, 3), 16, axes=axes)
        output_space = Conv2DSpace((3, 3), 16, axes=axes)
        num_nonzero = 2
        kernel_shape = (2, 2)

        conv2d = make_sparse_random_conv2D(num_nonzero, input_space,
                                           output_space, kernel_shape)
        f = theano.function([self.image_tensor],
                            conv2d.lmul(self.image_tensor))
        assert f(self.image).shape == (16, 2, 2, 1)
        assert conv2d.output_axes == axes
        assert numpy.count_nonzero(conv2d._filters.get_value()) >= 32
Пример #7
0
    def __init__(self, shape, rings):
        self.shape = shape
        self.rings = rings

        rows, cols, channels = shape

        self.topo_space = Conv2DSpace(shape=[rows, cols], num_channels=channels)

        self.decoder = RetinaDecodingBlock(shape, rings)
        self.encoder = RetinaEncodingBlock(rings)
Пример #8
0
 def new_model(self, model_params, dataset):
     layers = construct_layers(model_params, dataset.y.shape[1],
                               self.out_nonlin)
     space = Conv2DSpace(shape=dataset.X_topo_space.shape,
                         num_channels=1,
                         axes=['c', 0, 1, 'b'])
     model = MLP(batch_size=model_params['batch_size'],
                 layers=layers,
                 input_space=space)
     return model
Пример #9
0
    def make_composite_space(dtype0, dtype1, use_conv2d):
        if use_conv2d:
            second_space = Conv2DSpace(shape=shape[:2],
                                       dtype=dtype1,
                                       num_channels=shape[2])
        else:
            second_space = VectorSpace(dim=np.prod(shape), dtype=dtype1)

        return CompositeSpace((VectorSpace(dim=shape.prod(),
                                           dtype=dtype0), second_space))
Пример #10
0
 def make_conv2d_space(shape, axes):
     shape_axes = list(axes)
     shape_axes.remove('b')
     image_shape = tuple(shape[shape_axes.index(axis)]
                         for axis in (0, 1))
     conv2d_axes = list(axes)
     conv2d_axes.remove('s')
     return Conv2DSpace(shape=image_shape,
                        num_channels=shape[shape_axes.index('c')],
                        axes=conv2d_axes)
Пример #11
0
    def set_input_space(self, space):
        """ Note: this resets parameters! """

        self.input_space = space
        rng = self.mlp.rng

        if self.border_mode == 'valid':
            output_shape = [
                (self.input_space.shape[0] - self.kernel_shape[0]) /
                self.kernel_stride[0] + 1,
                (self.input_space.shape[1] - self.kernel_shape[1]) /
                self.kernel_stride[1] + 1
            ]
        elif self.border_mode == 'full':
            output_shape = [
                (self.input_space.shape[0] + self.kernel_shape[0]) /
                self.kernel_stride[0] - 1,
                (self.input_space.shape[1] + self.kernel_shape[1]) /
                self.kernel_stride_stride[1] - 1
            ]

        self.output_space = self.detector_space = Conv2DSpace(
            shape=output_shape,
            num_channels=self.output_channels,
            axes=('b', 'c', 0, 1))

        if self.irange is not None:
            assert self.sparse_init is None
            self.transformer = conv2d.make_random_conv2D(
                irange=self.irange,
                input_space=self.input_space,
                output_space=self.detector_space,
                kernel_shape=self.kernel_shape,
                batch_size=self.mlp.batch_size,
                subsample=self.kernel_stride,
                border_mode=self.border_mode,
                rng=rng)
        elif self.sparse_init is not None:
            self.transformer = conv2d.make_sparse_random_conv2D(
                num_nonzero=self.sparse_init,
                input_space=self.input_space,
                output_space=self.detector_space,
                kernel_shape=self.kernel_shape,
                batch_size=self.mlp.batch_size,
                subsample=self.kernel_stride,
                border_mode=self.border_mode,
                rng=rng)
        W, = self.transformer.get_params()
        W.name = 'W'

        self.b = sharedX(self.detector_space.get_origin() + self.init_bias)
        self.b.name = 'b'

        print 'Input shape: ', self.input_space.shape
        print 'Output space: ', self.output_space.shape
Пример #12
0
def convert_to_axes(reference_result, axes):
    # assuming we have b c 0 1 (2) for reference
    if (reference_result.ndim == 4):
        return Conv2DSpace.convert_numpy(reference_result, ('b', 'c', 0, 1),
                                         axes)
    elif (reference_result.ndim == 5):
        return Conv3DSpace.convert_numpy(reference_result, ('b', 'c', 0, 1, 2),
                                         axes)
    else:
        raise ValueError(("Expect result to have 4 or 5 dims, "
                          "has {:d} dims".format(reference_result.ndim)))
Пример #13
0
def test_np_format_as_conv2d2conv2d():
    conv2d_space_initial = Conv2DSpace(shape=(8, 8),
                                       num_channels=3,
                                       axes=('b', 'c', 0, 1))
    conv2d_space_final = Conv2DSpace(shape=(8, 8),
                                     num_channels=3,
                                     axes=('b', 'c', 0, 1))
    data = np.arange(5 * 8 * 8 * 3).reshape(5, 3, 8, 8)
    rval = conv2d_space_initial.np_format_as(data, conv2d_space_final)
    assert np.all(rval == data)

    conv2d_space1 = Conv2DSpace(shape=(8, 8),
                                num_channels=3,
                                axes=('c', 'b', 1, 0))
    conv2d_space0 = Conv2DSpace(shape=(8, 8),
                                num_channels=3,
                                axes=('b', 'c', 0, 1))
    data = np.arange(5 * 8 * 8 * 3).reshape(5, 3, 8, 8)
    rval = conv2d_space0.np_format_as(data, conv2d_space1)
    nval = data.transpose(1, 0, 3, 2)
    assert np.all(rval == nval)
Пример #14
0
 def test_channels(self):
     """
     Go from 2 to 3 channels and see whether the shape is correct.
     """
     input_space = Conv2DSpace((3, 3), num_channels=3)
     filters_values = np.ones((2, 3, 2, 2), dtype=theano.config.floatX)
     filters = sharedX(filters_values)
     image = np.random.rand(1, 3, 3, 3).astype(theano.config.floatX)
     cudnn2d = Cudnn2D(filters, 1, input_space)
     f = theano.function([self.image_tensor],
                         cudnn2d.lmul(self.image_tensor))
     assert f(image).shape == (1, 2, 2, 2)
Пример #15
0
 def _create_matrix_input(self, X, y):
     if self.is_convolution:
         # Using `b01c` arrangement of data, see this for details:
         #   http://benanne.github.io/2014/04/03/faster-convolutions-in-theano.html
         # input: (batch size, channels, rows, columns)
         # filters: (number of filters, channels, rows, columns)
         input_space = Conv2DSpace(shape=X.shape[1:3],
                                   num_channels=X.shape[-1])
         view = input_space.get_origin_batch(X.shape[0])
         return DenseDesignMatrix(topo_view=view, y=y), input_space
     else:
         return DenseDesignMatrix(X=X, y=y), None
Пример #16
0
 def setUp(self):
     """
     Set up a test image and filter to re-use
     """
     self.image = numpy.random.rand(1, 3, 3, 1).astype(theano.config.floatX)
     self.image_tensor = tensor.tensor4()
     self.input_space = Conv2DSpace((3, 3), 1)
     self.filters_values = numpy.ones(
         (1, 1, 2, 2), dtype=theano.config.floatX
     )
     self.filters = sharedX(self.filters_values, name='filters')
     self.conv2d = Conv2D(self.filters, 1, self.input_space)
Пример #17
0
    def __init__(self, which_set,
                 one_hot=False,
                 shuffle_rng=None, 
                 size=(48,48), num_channels=1,
                 splitRatio = 0.7,
                 selectFraction = 1.0,
                 path=None):

        if path is None:
            #path = '/data/lisa/data/faces/EmotiW/preproc/arranged_data'
            path = '/Tmp/aggarwal/all'

        self.x = np.memmap(path + '_x.npy', mode='r', dtype='float32')
        self.y = np.memmap(path + '_y.npy', mode='r', dtype='uint8')
        self.y = self.y.view()
        self.y.shape = (len(self.y), 1)

        self.x = self.x.view()
        self.x.shape = (len(self.y), size[0], size[1], num_channels)
        
        numSamples = int(self.x.shape[0]*selectFraction)
        
        if which_set =='train':
            start = 0
            stop = int(numSamples*splitRatio)
            
        elif which_set == 'val':
            start = int(numSamples*splitRatio)
            stop = numSamples
            
        print 'start, stop', stop-start

            
        self.x = self.x[start:stop]
        self.y =self.y[start:stop]
        
        if shuffle_rng is None:
            shuffle_rng = np.random.RandomState((2013, 06, 11))
        elif not isinstance(shuffle_rng, np.random.RandomState):
            shuffle_rng = np.random.RandomState(shuffle_rng)

        self.permutation = shuffle_rng.permutation(len(self.y))
        self.one_hot = one_hot

        self.space = CompositeSpace(
            (Conv2DSpace(shape=size,
                          num_channels=num_channels,
                          axes=('b', 0, 1, 'c')),
            VectorSpace(dim=(self.one_hot and 7 or 1))))
        self.source = ('features', 'targets')
        self.data_specs = (self.space, self.source)

        self.n_samples = len(self.y)
Пример #18
0
def test_np_format_as_conv2D2vector():
    vector_space = VectorSpace(dim=8 * 8 * 3, sparse=False)
    conv2d_space = Conv2DSpace(shape=(8, 8),
                               num_channels=3,
                               axes=('b', 'c', 0, 1))
    data = np.arange(5 * 8 * 8 * 3).reshape(5, 3, 8, 8)
    rval = conv2d_space.np_format_as(data, vector_space)
    nval = data.transpose(
        *[conv2d_space.axes.index(ax) for ax in conv2d_space.default_axes])
    nval = nval.reshape(5, 3 * 8 * 8)
    assert np.all(rval == nval)

    vector_space = VectorSpace(dim=8 * 8 * 3, sparse=False)
    conv2d_space = Conv2DSpace(shape=(8, 8),
                               num_channels=3,
                               axes=('c', 'b', 0, 1))
    data = np.arange(5 * 8 * 8 * 3).reshape(3, 5, 8, 8)
    rval = conv2d_space.np_format_as(data, vector_space)
    nval = data.transpose(
        *[conv2d_space.axes.index(ax) for ax in conv2d_space.default_axes])
    nval = nval.reshape(5, 3 * 8 * 8)
    assert np.all(rval == nval)
Пример #19
0
 def test_make_random_conv2D(self):
     """
     Create a random convolution and check whether the shape, axes and
     input space are all what we expect
     """
     output_space = Conv2DSpace((2, 2), 1)
     conv2d = make_random_conv2D(1, self.input_space, output_space,
                                 (2, 2), 1)
     f = theano.function([self.image_tensor],
                         conv2d.lmul(self.image_tensor))
     assert f(self.image).shape == (1, 2, 2, 1)
     assert conv2d.input_space == self.input_space
     assert conv2d.output_axes == output_space.axes
Пример #20
0
    def set_input_space(self, space):
        """ Note: this function will reset the parameters! """

        self.input_space = space

        if not isinstance(space, Conv2DSpace):
            raise BadInputSpaceError(self.__class__.__name__ +
                                     ".set_input_space "
                                     "expected a Conv2DSpace, got " +
                                     str(space) + " of type " +
                                     str(type(space)))

        rng = self.get_mlp().rng

        if self.border_mode == 'valid':
            output_shape = [
                (self.input_space.shape[0] - self.kernel_shape[0]) /
                self.kernel_stride[0] + 1,
                (self.input_space.shape[1] - self.kernel_shape[1]) /
                self.kernel_stride[1] + 1
            ]
        elif self.border_mode == 'full':
            output_shape = [
                (self.input_space.shape[0] + self.kernel_shape[0]) /
                self.kernel_stride[0] - 1,
                (self.input_space.shape[1] + self.kernel_shape[1]) /
                self.kernel_stride[1] - 1
            ]

        print self.layer_name
        self.detector_space = Conv2DSpace(shape=output_shape,
                                          num_channels=self.output_channels,
                                          axes=('b', 'c', 0, 1))

        self.initialize_transformer(rng)

        W, = self.transformer.get_params()
        W.name = self.layer_name + '_W'

        if self.tied_b:
            self.b = sharedX(
                np.zeros((self.detector_space.num_channels)) + self.init_bias)
        else:
            self.b = sharedX(self.detector_space.get_origin() + self.init_bias)

        self.b.name = self.layer_name + '_b'

        logger.info('Input shape: {0}'.format(self.input_space.shape))
        logger.info('Detector space: {0}'.format(self.detector_space.shape))

        self.initialize_output_space()
Пример #21
0
    def set_input_space(self, space):
        self.input_space = space
        rng = self.mlp.rng

        # Make sure number of channels is supported by cuda-convnet
        # (multiple of 4 or <= 3)
        # If not supported, pad the input with dummy channels
        ch = self.input_space.num_channels
        assert ch <= 3 or ch % 4 == 0

        if self.border_mode == 'valid':
            output_shape = [
                (self.input_space.shape[0] - self.kernel_shape[0]) /
                self.kernel_stride[0] + 1,
                (self.input_space.shape[1] - self.kernel_shape[1]) /
                self.kernel_stride[1] + 1
            ]
        elif self.border_mode == 'full':
            output_shape = [
                (self.input_space.shape[0] + self.kernel_shape[0]) /
                self.kernel_stride[0] - 1,
                (self.input_space.shape[1] + self.kernel_shape[1]) /
                self.kernel_stride_stride[1] - 1
            ]

        self.output_space = Conv2DSpace(shape=output_shape,
                                        num_channels=self.output_channels,
                                        axes=('c', 0, 1, 'b'))

        rng = self.mlp.rng
        W = sharedX( rng.uniform(-self.irange,self.irange,(self.input_space.num_channels, \
               self.kernel_shape[0], self.kernel_shape[1], self.output_channels)))

        self.transformer = Conv2D(filters=W,
                                  input_axes=self.input_space.axes,
                                  output_axes=self.output_space.axes,
                                  kernel_stride=self.kernel_stride,
                                  pad=self.pad,
                                  message="",
                                  partial_sum=self.partial_sum)

        W, = self.transformer.get_params()
        W.name = self.layer_name + '_W'

        self.b = sharedX(
            np.zeros((self.output_space.num_channels)) + self.init_bias)
        self.b.name = self.layer_name + '_b'

        print 'Input shape: ', self.input_space.shape
        print 'Output space: ', self.output_space.shape
Пример #22
0
def get_conv2D(dim_input, batch_size=200):
    config = {
        'batch_size':
        batch_size,
        'input_space':
        Conv2DSpace(shape=dim_input[:2], num_channels=dim_input[2]),
        'layers': [
            ConvRectifiedLinear(layer_name='h0',
                                output_channels=20,
                                irange=.04,
                                init_bias=0.,
                                max_kernel_norm=1.9365,
                                kernel_shape=[5, 5],
                                border_mode='full',
                                pool_shape=[8, 4],
                                pool_stride=[3, 2],
                                W_lr_scale=0.64),
            ConvRectifiedLinear(layer_name='h1',
                                output_channels=40,
                                irange=.04,
                                init_bias=0.,
                                max_kernel_norm=1.9365,
                                kernel_shape=[3, 3],
                                border_mode='valid',
                                pool_shape=[4, 4],
                                pool_stride=[2, 2],
                                W_lr_scale=0.64),
            ConvRectifiedLinear(layer_name='h2',
                                output_channels=60,
                                irange=.04,
                                init_bias=0.,
                                max_kernel_norm=1.9365,
                                kernel_shape=[3, 3],
                                border_mode='valid',
                                pool_shape=[2, 2],
                                pool_stride=[1, 1],
                                W_lr_scale=0.64),
            ConvRectifiedLinear(layer_name='h3',
                                output_channels=80,
                                irange=.04,
                                init_bias=0.,
                                max_kernel_norm=1.9365,
                                kernel_shape=[3, 3],
                                pool_shape=[2, 2],
                                pool_stride=[2, 2],
                                W_lr_scale=0.64),
            Softmax(layer_name='y', n_classes=2, istdev=.025, W_lr_scale=0.25)
        ]
    }
    return MLP(**config)
Пример #23
0
def test_np_format_as_composite_composite():
    def make_composite_space(image_space):
        return CompositeSpace((CompositeSpace(
            (image_space, ) * 2), VectorSpace(dim=1)))

    shape = np.array([8, 11])
    channels = 3
    datum_size = channels * shape.prod()

    composite_topo = make_composite_space(
        Conv2DSpace(shape=shape, num_channels=channels))
    composite_flat = make_composite_space(VectorSpace(dim=datum_size))

    def make_flat_data(batch_size, space):
        if isinstance(space, CompositeSpace):
            return tuple(
                make_flat_data(batch_size, subspace)
                for subspace in space.components)
        else:
            assert isinstance(space, VectorSpace)
            return np.random.rand(batch_size, space.dim)

    batch_size = 5
    flat_data = make_flat_data(batch_size, composite_flat)
    composite_flat.np_validate(flat_data)

    topo_data = composite_flat.np_format_as(flat_data, composite_topo)
    composite_topo.np_validate(topo_data)
    new_flat_data = composite_topo.np_format_as(topo_data, composite_flat)

    def get_shape(batch):
        if isinstance(batch, np.ndarray):
            return batch.shape
        else:
            return tuple(get_shape(b) for b in batch)

    def batch_equals(batch_0, batch_1):
        assert type(batch_0) == type(batch_1)
        if isinstance(batch_0, tuple):
            if len(batch_0) != len(batch_1):
                return False

            return np.all(
                tuple(
                    batch_equals(b0, b1) for b0, b1 in zip(batch_0, batch_1)))
        else:
            assert isinstance(batch_0, np.ndarray)
            return np.all(batch_0 == batch_1)

    assert batch_equals(new_flat_data, flat_data)
Пример #24
0
 def get_space_conv2dspace(self, space_id):
     row = self.db.executeSQL(
         """
     SELECT num_row, num_column, num_channel, axes
     FROM hps3.space_conv2DSpace
     WHERE space_id = %s
     """, (space_id, ), self.db.FETCH_ONE)
     if not row or row is None:
         raise HPSData("No conv2DSpace for space_id=" + str(space_id))
     (num_row, num_column, num_channels, axes_char) = row
     axes = self.get_axes(axes_char)
     return Conv2DSpace(shape=(num_row, num_column),
                        num_channels=num_channels,
                        axes=axes)
Пример #25
0
    def set_input_space(self, space):
        """ Note: this function will reset the parameters! """

        self.input_space = space

        if not isinstance(space, CompositeSpace):
            raise BadInputSpaceError(self.__class__.__name__ +
                                     ".set_input_space "
                                     "expected a CompositeSpace, got " +
                                     str(space) + " of type " +
                                     str(type(space)))

        rng = self.mlp.rng

        output_shape = [(self.input_space.components[0].shape[0] + self.kernel_shape[0])
                            / self.kernel_stride[0] - 1,
                            (self.input_space.components[0].shape[1] + self.kernel_shape[1])
                            / self.kernel_stride[1] - 1]

        self.detector_space = Conv2DSpace(shape=output_shape,
                                          num_channels=self.output_channels,
                                          axes=('b', 'c', 0, 1))

        self.initialize_x_space(rng)
        self.initialize_transformer(rng)

        W, = self.transformer.get_params()
        W.name = self.layer_name + '_W'

        W1 = rng.normal(0, .001, size=(self.dim_x, self.dim))
        self.W1 = sharedX(W1, name=self.layer_name + '_W1')

        if self.tied_b:
            self.b = sharedX(np.zeros((self.detector_space.num_channels)) +
                             self.init_bias)
        else:
            self.b = sharedX(self.detector_space.get_origin() + self.init_bias)

        self.b.name = self.layer_name + '_b'

        logger.info('Input 0 shape: {0}'.format(self.input_space.components[0].shape))
        logger.info('Input 1 shape: {0}'.format(self.input_space.components[1].shape))
        logger.info('Detector space: {0}'.format(self.detector_space.shape))

        self.initialize_output_space()

        cost = self.get_local_cost()
        self.opt = top.Optimizer(self.X, cost, method='rmsprop', 
                                 learning_rate=self.lr, momentum=.9)
Пример #26
0
    def get_weights_topo(self):

        if not isinstance(self.input_space, Conv2DSpace):
            raise NotImplementedError()

        W, = self.transformer.get_params()

        W = W.T

        W = W.reshape((self.detector_layer_dim, self.input_space.shape[0],
                       self.input_space.shape[1], self.input_space.nchannels))

        W = Conv2DSpace.convert(W, self.input_space.axes, ('b', 0, 1, 'c'))

        return function([], W)()
Пример #27
0
 def test_setup_detector_layer_c01b(self):
     """
     Very basic test to see whether a detector layer can be set up
     without error. Not checking much for the actual output.
     """
     axes = ('c', 0, 1, 'b')
     layer = MaxoutConvC01B(16, 2, (2, 2), (2, 2),
                            (1, 1), 'maxout', irange=1.)
     input_space = Conv2DSpace((3, 3), 16, axes=axes)
     MLP(layers=[layer], input_space=input_space)
     layer.set_input_space(input_space)
     assert isinstance(layer.input_space, Conv2DSpace)
     input = theano.tensor.tensor4()
     f = theano.function([input], layer.fprop(input))
     f(numpy.random.rand(16, 3, 3, 1).astype(theano.config.floatX))
def gen_fcn(batch_size,
            img_shape,
            kernel_size,
            data_type='float32',
            threshold=1e-4):
    '''
    generate theano function for doing lecun lcn of a given setting
    modified from lecun_lcn in pylearn2.datasets.preprocessing 

    currently data_type can only be float32
    if not, will report error saying input and kernel should be the same type
    and kernel type is float32
    '''

    X = tensor.matrix(dtype=data_type)
    X = X.reshape((batch_size, img_shape[0], img_shape[1], 1))

    filter_shape = (1, 1, kernel_size, kernel_size)
    filters = sharedX(gaussian_filter(kernel_size).reshape(filter_shape))

    input_space = Conv2DSpace(shape=img_shape, num_channels=1)
    transformer = Conv2D(filters=filters,
                         batch_size=batch_size,
                         input_space=input_space,
                         border_mode='full')
    convout = transformer.lmul(X)

    # For each pixel, remove mean of 9x9 neighborhood
    mid = int(np.floor(kernel_size / 2.))
    centered_X = X - convout[:, mid:-mid, mid:-mid, :]

    # Scale down norm of 9x9 patch if norm is bigger than 1
    transformer = Conv2D(filters=filters,
                         batch_size=batch_size,
                         input_space=input_space,
                         border_mode='full')
    sum_sqr_XX = transformer.lmul(X**2)

    denom = tensor.sqrt(sum_sqr_XX[:, mid:-mid, mid:-mid, :])
    per_img_mean = denom.mean(axis=[1, 2])
    divisor = tensor.largest(per_img_mean.dimshuffle(0, 'x', 'x', 1), denom)
    divisor = tensor.maximum(divisor, threshold)

    new_X = centered_X / divisor
    new_X = tensor.flatten(new_X, outdim=3)

    f = function([X], new_X)
    return f
Пример #29
0
def test_np_format_as_vector2conv2D():
    vector_space = VectorSpace(dim=8 * 8 * 3, sparse=False)
    conv2d_space = Conv2DSpace(shape=(8, 8),
                               num_channels=3,
                               axes=('b', 'c', 0, 1))
    data = np.arange(5 * 8 * 8 * 3).reshape(5, 8 * 8 * 3)
    rval = vector_space.np_format_as(data, conv2d_space)

    # Get data in a Conv2DSpace with default axes
    new_axes = conv2d_space.default_axes
    axis_to_shape = {'b': 5, 'c': 3, 0: 8, 1: 8}
    new_shape = tuple([axis_to_shape[ax] for ax in new_axes])
    nval = data.reshape(new_shape)
    # Then transpose
    nval = nval.transpose(*[new_axes.index(ax) for ax in conv2d_space.axes])
    assert np.all(rval == nval)
Пример #30
0
    def get_weights_topo(self):
        warnings.warn("BoltzmannIsingHidden.get_weights_topo returns the BOLTZMANN weights, is that what we want?")

        if not isinstance(self.input_space, Conv2DSpace):
            raise NotImplementedError()

        W = self.W

        W = W.T

        W = W.reshape((self.detector_layer_dim, self.input_space.shape[0],
            self.input_space.shape[1], self.input_space.nchannels))

        W = Conv2DSpace.convert(W, self.input_space.axes, ('b', 0, 1, 'c'))

        return function([], W)()