示例#1
0
    def fprop(self, state_below):

        d = state_below
        if self.pool_type is not None:
            # Format the input to be supported by max pooling
            assert self.pool_type in ['max', 'mean'], ("pool_type should be"
                                                      "either max or mean"
                                                      "pooling.")

            if self.pool_type == 'max':
                p = downsample.max_pool_2d(d,
                                           self.pool_shape,
                                           ignore_border=False)

                #p = max_pool(bc01=d, pool_shape=self.pool_shape,
                #        pool_stride=self.pool_stride,
                #        image_shape=self.detector_space.shape)
            elif self.pool_type == 'mean':
                p = mean_pool(bc01=d, pool_shape=self.pool_shape,
                        pool_stride=self.pool_stride,
                        image_shape=self.detector_space.shape)

            self.output_space.validate(p)
        else:
            p = d

        if not hasattr(self, 'output_normalization'):
           self.output_normalization = None

        if self.output_normalization:
           p = self.output_normalization(p)
        return p
示例#2
0
    def fprop(self, state_below):
        #self.input_space.validate(state_below)

        z = self.transformer.lmul(state_below)
        if not hasattr(self, 'tied_b'):
            self.tied_b = False

        print self.layer_name
        #assert self.tied_b
        if self.tied_b:
            b = self.b.dimshuffle('x', 0, 'x', 'x')
        else:
            b = self.b.dimshuffle('x', 0, 1, 2)

        z = z + b
        d = self.nonlin.apply(z)

        if self.layer_name is not None:
            d.name = self.layer_name + '_z'
            self.detector_space.validate(d)



        if self.pool_type is not None:
            # Format the input to be supported by max pooling


            if not hasattr(self, 'detector_normalization'):
                self.detector_normalization = None

            if self.detector_normalization:
                d = self.detector_normalization(d)

            assert self.pool_type in ['max', 'mean'], ("pool_type should be"
                                                      "either max or mean"
                                                      "pooling.")

            if self.pool_type == 'max':
                p = downsample.max_pool_2d(d, self.pool_shape,
                                                ignore_border=False)

                #p = max_pool(bc01=d, pool_shape=self.pool_shape,
                #        pool_stride=self.pool_stride,
                #        image_shape=self.detector_space.shape)
            elif self.pool_type == 'mean':
                p = mean_pool(bc01=d, pool_shape=self.pool_shape,
                        pool_stride=self.pool_stride,
                        image_shape=self.detector_space.shape)

                #self.output_space.validate(p)
        else:
            p = d

        if not hasattr(self, 'output_normalization'):
           self.output_normalization = None

        if self.output_normalization:
           p = self.output_normalization(p)

        return p
示例#3
0
def test_mean_pool():
    X_sym = tensor.tensor4('X')
    pool_it = mean_pool(X_sym,
                        pool_shape=(2, 2),
                        pool_stride=(2, 2),
                        image_shape=(6, 4))

    f = theano.function(inputs=[X_sym], outputs=pool_it)

    t = np.array([[1, 1, 3, 3], [1, 1, 3, 3], [5, 5, 7, 7], [5, 5, 7, 7],
                  [9, 9, 11, 11], [9, 9, 11, 11]],
                 dtype=theano.config.floatX)

    X = np.zeros((3, t.shape[0], t.shape[1]), dtype=theano.config.floatX)
    X[:] = t
    X = X[np.newaxis]
    expected = np.array([[1, 3], [5, 7], [9, 11]], dtype=theano.config.floatX)
    actual = f(X)
    assert np.allclose(expected, actual)

    # With different values in pools
    t = np.array([[0, 1, 3, 2], [1, 2, 4, 3], [4, 6, 7, 7], [5, 5, 6, 8],
                  [8, 10, 11, 11], [9, 9, 10, 12]],
                 dtype=theano.config.floatX)

    X = np.zeros((3, t.shape[0], t.shape[1]), dtype=theano.config.floatX)
    X[:] = t
    X = X[np.newaxis]
    expected = np.array([[1, 3], [5, 7], [9, 11]], dtype=theano.config.floatX)
    actual = f(X)
    assert np.allclose(expected, actual)
示例#4
0
    def fprop(self, state_below):
        #self.input_space.validate(state_below)

        z = self.transformer.lmul(state_below)
        if not hasattr(self, 'tied_b'):
            self.tied_b = False

        print self.layer_name
        #assert self.tied_b
        if self.tied_b:
            b = self.b.dimshuffle('x', 0, 'x', 'x')
        else:
            b = self.b.dimshuffle('x', 0, 1, 2)

        z = z + b
        d = self.nonlin.apply(z)

        if self.layer_name is not None:
            d.name = self.layer_name + '_z'
            self.detector_space.validate(d)

        if self.pool_type is not None:
            # Format the input to be supported by max pooling

            if not hasattr(self, 'detector_normalization'):
                self.detector_normalization = None

            if self.detector_normalization:
                d = self.detector_normalization(d)

            assert self.pool_type in ['max', 'mean'], ("pool_type should be"
                                                       "either max or mean"
                                                       "pooling.")

            if self.pool_type == 'max':
                p = downsample.max_pool_2d(d,
                                           self.pool_shape,
                                           ignore_border=False)

                #p = max_pool(bc01=d, pool_shape=self.pool_shape,
                #        pool_stride=self.pool_stride,
                #        image_shape=self.detector_space.shape)
            elif self.pool_type == 'mean':
                p = mean_pool(bc01=d,
                              pool_shape=self.pool_shape,
                              pool_stride=self.pool_stride,
                              image_shape=self.detector_space.shape)

                #self.output_space.validate(p)
        else:
            p = d

        if not hasattr(self, 'output_normalization'):
            self.output_normalization = None

        if self.output_normalization:
            p = self.output_normalization(p)

        return p
示例#5
0
def test_pooling_with_anon_variable():
    """
    Ensure that pooling works with anonymous
    variables.
    """
    X_sym = tensor.ftensor4()
    shp = (3, 3)
    strd = (1, 1)
    im_shp = (6, 6)
    pool_0 = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                      image_shape=im_shp, try_dnn=False)
    pool_1 = mean_pool(X_sym, pool_shape=shp, pool_stride=strd,
                       image_shape=im_shp)
示例#6
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
        dummy_detector =\
                sharedX(self.detector_space.get_origin_batch(dummy_batch_size))

        # Redefine num channels of the outut space
        if isinstance(self.nonlin, MaxoutBC01):
            num_channels = self.output_channels / self.nonlin.num_pieces
        else:
            num_channels = self.output_channels

        if self.pool_type is not None:
            assert self.pool_type in ['max', 'mean']
            if self.pool_type == 'max':
                dummy_p = max_pool(bc01=dummy_detector,
                                   pool_shape=self.pool_shape,
                                   pool_stride=self.pool_stride,
                                   image_shape=self.detector_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.detector_space.shape)
            dummy_p = dummy_p.eval()
            print dummy_p.shape
            self.output_space = Conv2DSpace(shape=[dummy_p.shape[2],
                                                   dummy_p.shape[3]],
                                            num_channels=num_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=num_channels,
                                            axes=('b', 'c', 0, 1))


	print "Input:", self.layer_name, self.input_space.shape, self.input_space.num_channels
	print "Detector:", self.layer_name, self.detector_space.shape, self.detector_space.num_channels
        print "Output:", self.layer_name, self.output_space.shape, self.output_space.num_channels
示例#7
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
        dummy_detector =\
                sharedX(self.detector_space.get_origin_batch(dummy_batch_size))

        # Redefine num channels of the outut space
        if isinstance(self.nonlin, MaxoutBC01):
            num_channels = self.output_channels / self.nonlin.num_pieces
        else:
            num_channels = self.output_channels

        if self.pool_type is not None:
            assert self.pool_type in ['max', 'mean']
            if self.pool_type == 'max':
                dummy_p = max_pool(bc01=dummy_detector,
                                   pool_shape=self.pool_shape,
                                   pool_stride=self.pool_stride,
                                   image_shape=self.detector_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.detector_space.shape)
            dummy_p = dummy_p.eval()
            print dummy_p.shape
            self.output_space = Conv2DSpace(
                shape=[dummy_p.shape[2], dummy_p.shape[3]],
                num_channels=num_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=num_channels,
                axes=('b', 'c', 0, 1))

        print "Input:", self.layer_name, self.input_space.shape, self.input_space.num_channels
        print "Detector:", self.layer_name, self.detector_space.shape, self.detector_space.num_channels
        print "Output:", self.layer_name, self.output_space.shape, self.output_space.num_channels
示例#8
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
示例#9
0
def test_pooling_with_anon_variable():
    """
    Ensure that pooling works with anonymous
    variables.
    """
    X_sym = tensor.ftensor4()
    shp = (3, 3)
    strd = (1, 1)
    im_shp = (6, 6)
    pool_0 = max_pool(X_sym,
                      pool_shape=shp,
                      pool_stride=strd,
                      image_shape=im_shp,
                      try_dnn=False)
    pool_1 = mean_pool(X_sym,
                       pool_shape=shp,
                       pool_stride=strd,
                       image_shape=im_shp)
示例#10
0
def test_mean_pool():
    X_sym = tensor.tensor4('X')
    pool_it = mean_pool(X_sym, pool_shape=(2, 2), pool_stride=(2, 2),
                        image_shape=(6, 4))

    f = theano.function(inputs=[X_sym], outputs=pool_it)

    t = np.array([[1, 1, 3, 3],
                  [1, 1, 3, 3],
                  [5, 5, 7, 7],
                  [5, 5, 7, 7],
                  [9, 9, 11, 11],
                  [9, 9, 11, 11]], dtype=theano.config.floatX)

    X = np.zeros((3, t.shape[0], t.shape[1]), dtype=theano.config.floatX)
    X[:] = t
    X = X[np.newaxis]
    expected = np.array([[1, 3],
                         [5, 7],
                         [9, 11]], dtype=theano.config.floatX)
    actual = f(X)
    assert np.allclose(expected, actual)