Exemple #1
0
 def test_neibs_half_step_by_valid(self):
     neib_shapes = ((3, 3), (3, 5), (5, 3))
     for shp_idx, (shape, neib_step) in enumerate([
         [(7, 8, 5, 5), (1, 1)],
         [(7, 8, 5, 5), (2, 2)],
         [(7, 8, 5, 5), (4, 4)],
         [(7, 8, 5, 5), (1, 4)],
         [(7, 8, 5, 5), (4, 1)],
         [(80, 90, 5, 5), (1, 2)],
         [(1025, 9, 5, 5), (2, 1)],
         [(1, 1, 5, 1037), (2, 4)],
         [(1, 1, 1045, 5), (4, 2)]]
     ):
         for neib_shape in neib_shapes:
             for dtype in self.dtypes:
                 x = theano.shared(np.random.randn(*shape).astype(dtype))
                 extra = (neib_shape[0] // 2, neib_shape[1] // 2)
                 padded_shape = (x.shape[0], x.shape[1], x.shape[2] + 2 * extra[0], x.shape[3] + 2 * extra[1])
                 padded_x = T.zeros(padded_shape)
                 padded_x = T.set_subtensor(padded_x[:, :, extra[0]:-extra[0], extra[1]:-extra[1]], x)
                 x_using_valid = images2neibs(padded_x, neib_shape, neib_step, mode="valid")
                 x_using_half = images2neibs(x, neib_shape, neib_step, mode="half")
                 f_valid = theano.function([], x_using_valid, mode='FAST_RUN')
                 f_half = theano.function([], x_using_half, mode=self.mode)
                 unittest_tools.assert_allclose(f_valid(), f_half())
Exemple #2
0
    def test_neibs_bad_shape_wrap_centered(self):
        shape = (2, 3, 10, 10)

        for dtype in self.dtypes:
            images = shared(numpy.arange(
                numpy.prod(shape), dtype=dtype
                ).reshape(shape))

            for neib_shape in [(3, 2), (2, 3)]:
                neib_shape = T.as_tensor_variable(neib_shape)

                f = function([], images2neibs(images, neib_shape,
                                              mode="wrap_centered"),
                             mode=self.mode)
                self.assertRaises(TypeError, f)

            for shape in [(2, 3, 2, 3), (2, 3, 3, 2)]:
                images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
                neib_shape = T.as_tensor_variable((3, 3))
                f = function([], images2neibs(images, neib_shape,
                                              mode="wrap_centered"),
                             mode=self.mode)
                self.assertRaises(TypeError, f)

            # Test a valid shapes
            shape = (2, 3, 3, 3)
            images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
            neib_shape = T.as_tensor_variable((3, 3))

            f = function([],
                         images2neibs(images, neib_shape, mode="wrap_centered"),
                         mode=self.mode)
            f()
    def test_neibs_bad_shape_wrap_centered(self):
        shape = (2, 3, 10, 10)

        for dtype in self.dtypes:
            images = shared(numpy.arange(
                numpy.prod(shape), dtype=dtype
                ).reshape(shape))

            for neib_shape in [(3, 2), (2, 3)]:
                neib_shape = T.as_tensor_variable(neib_shape)

                f = function([], images2neibs(images, neib_shape,
                                              mode="wrap_centered"),
                             mode=self.mode)
                self.assertRaises(TypeError, f)

            for shape in [(2, 3, 2, 3), (2, 3, 3, 2)]:
                images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
                neib_shape = T.as_tensor_variable((3, 3))
                f = function([], images2neibs(images, neib_shape,
                                              mode="wrap_centered"),
                             mode=self.mode)
                self.assertRaises(TypeError, f)

            # Test a valid shapes
            shape = (2, 3, 3, 3)
            images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
            neib_shape = T.as_tensor_variable((3, 3))

            f = function([],
                         images2neibs(images, neib_shape, mode="wrap_centered"),
                         mode=self.mode)
            f()
    def DSSIM(p, y, eps=1e-7):
        # Taken/Modified from https://github.com/fchollet/keras/issues/4292
        # Nan issue : T.maximum(x, eps)

        y_patch = neigh.images2neibs(y, [4, 4], mode='ignore_borders')
        p_patch = neigh.images2neibs(p, [4, 4], mode='ignore_borders')

        y_mean = T.mean(y_patch, axis=-1)
        p_mean = T.mean(p_patch, axis=-1)

        y_var = T.var(y_patch, axis=-1, corrected=True)
        p_var = T.var(p_patch, axis=-1, corrected=True)

        y_std = T.sqrt(T.maximum(y_var, eps))
        p_std = T.sqrt(T.maximum(p_var, eps))

        c1 = 0.01**2
        c2 = 0.02**2

        num = (2 * y_mean * p_mean + c1) * (2 * y_std * p_std + c2)
        denom = (T.pow(y_mean, 2) + T.pow(p_mean, 2) + c1) * (y_var + p_var +
                                                              c2)

        ssim = num / T.maximum(denom, eps)

        return T.mean(1.0 - ssim)
Exemple #5
0
 def test_neibs_full_step_by_valid(self):
     for shp_idx, (shape, neib_step, neib_shapes) in enumerate(
         [
             [(7, 8, 5, 5), (1, 1), ((3, 3), (3, 5), (5, 3))],
             [(7, 8, 5, 5), (2, 2), ((3, 3), (3, 5), (5, 3))],
             [(7, 8, 6, 6), (3, 3), ((2, 2), (2, 5), (5, 2))],
             [(7, 8, 6, 6), (1, 3), ((2, 2), (2, 5), (5, 2))],
             [(7, 8, 6, 6), (3, 1), ((2, 2), (2, 5), (5, 2))],
             [(80, 90, 5, 5), (1, 2), ((3, 3), (3, 5), (5, 3))],
             [(1025, 9, 5, 5), (2, 1), ((3, 3), (3, 5), (5, 3))],
             [(1, 1, 11, 1037), (2, 3), ((3, 3), (5, 3))],
             [(1, 1, 1043, 11), (3, 2), ((3, 3), (3, 5))],
         ]
     ):
         for neib_shape in neib_shapes:
             for dtype in self.dtypes:
                 x = theano.shared(np.random.randn(*shape).astype(dtype))
                 extra = (neib_shape[0] - 1, neib_shape[1] - 1)
                 padded_shape = (
                     x.shape[0],
                     x.shape[1],
                     x.shape[2] + 2 * extra[0],
                     x.shape[3] + 2 * extra[1],
                 )
                 padded_x = T.zeros(padded_shape)
                 padded_x = T.set_subtensor(
                     padded_x[:, :, extra[0] : -extra[0], extra[1] : -extra[1]], x
                 )
                 x_using_valid = images2neibs(
                     padded_x, neib_shape, neib_step, mode="valid"
                 )
                 x_using_full = images2neibs(x, neib_shape, neib_step, mode="full")
                 f_valid = theano.function([], x_using_valid, mode="FAST_RUN")
                 f_full = theano.function([], x_using_full, mode=self.mode)
                 unittest_tools.assert_allclose(f_valid(), f_full())
Exemple #6
0
 def test_neibs_half_step_by_valid(self):
     neib_shapes = ((3, 3), (3, 5), (5, 3))
     for shp_idx, (shape, neib_step) in enumerate([
         [(7, 8, 5, 5), (1, 1)],
         [(7, 8, 5, 5), (2, 2)],
         [(7, 8, 5, 5), (4, 4)],
         [(7, 8, 5, 5), (1, 4)],
         [(7, 8, 5, 5), (4, 1)],
         [(80, 90, 5, 5), (1, 2)],
         [(1025, 9, 5, 5), (2, 1)],
         [(1, 1, 5, 1037), (2, 4)],
         [(1, 1, 1045, 5), (4, 2)]]
     ):
         for neib_shape in neib_shapes:
             for dtype in self.dtypes:
                 x = theano.shared(np.random.randn(*shape).astype(dtype))
                 extra = (neib_shape[0] // 2, neib_shape[1] // 2)
                 padded_shape = (x.shape[0], x.shape[1], x.shape[2] + 2 * extra[0], x.shape[3] + 2 * extra[1])
                 padded_x = T.zeros(padded_shape)
                 padded_x = T.set_subtensor(padded_x[:, :, extra[0]:-extra[0], extra[1]:-extra[1]], x)
                 x_using_valid = images2neibs(padded_x, neib_shape, neib_step, mode="valid")
                 x_using_half = images2neibs(x, neib_shape, neib_step, mode="half")
                 f_valid = theano.function([], x_using_valid, mode='FAST_RUN')
                 f_half = theano.function([], x_using_half, mode=self.mode)
                 unittest_tools.assert_allclose(f_valid(), f_half())
def loss_DSSIM_theano(y_true, y_pred):
    # There are additional parameters for this function
    # Note: some of the 'modes' for edge behavior do not yet have a gradient definition in the Theano tree
    # and cannot be used for learning
    y_true = y_true.dimshuffle([0, 3, 1, 2])
    y_pred = y_pred.dimshuffle([0, 3, 1, 2])
    patches_true = images2neibs(y_true, [4, 4])
    patches_pred = images2neibs(y_pred, [4, 4])

    u_true = K.mean(patches_true, axis=-1)
    u_pred = K.mean(patches_pred, axis=-1)

    var_true = K.var(patches_true, axis=-1)
    var_pred = K.var(patches_pred, axis=-1)
    std_true = K.sqrt(var_true + K.epsilon())
    std_pred = K.sqrt(var_pred + K.epsilon())

    c1 = 0.01**2
    c2 = 0.03**2
    ssim = (2 * u_true * u_pred + c1) * (2 * std_pred * std_true + c2)
    denom = (u_true**2 + u_pred**2 + c1) * (var_pred + var_true + c2)
    ssim /= denom  # no need for clipping, c1 and c2 make the denom non-zero

    return (alpha * K.mean((1.0 - ssim) / 2.0) +
            beta * K.mean(K.square(y_pred - y_true), axis=-1))
Exemple #8
0
    def test_infer_shape(self):
        shape = (100, 40, 6, 3)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(x, neib_shape=(2, 1), mode='valid')],
            [images], Images2Neibs)
        self._compile_and_check(
            [x], [images2neibs(x, neib_shape=(2, 3), mode='valid')],
            [images], Images2Neibs)
        shape = (100, 40, 5, 4)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(
                x, neib_shape=(2, 1), mode='ignore_borders')],
            [images], Images2Neibs)
        shape = (100, 40, 5, 3)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(
                x, neib_shape=(2, 3), mode='ignore_borders')],
            [images], Images2Neibs)

        shape = (100, 40, 6, 7)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(
                x, neib_shape=(2, 2), mode='ignore_borders')],
            [images], Images2Neibs)
        shape = (100, 40, 5, 10)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(
                x, neib_shape=(3, 3), mode='wrap_centered')],
            [images], Images2Neibs)
        shape = (100, 40, 6, 4)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(x, neib_shape=(2, 1), mode='half')],
            [images], Images2Neibs)
        self._compile_and_check(
            [x], [images2neibs(x, neib_shape=(2, 3), mode='half')],
            [images], Images2Neibs)
        shape = (100, 40, 6, 5)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(x, neib_shape=(2, 1), mode='full')],
            [images], Images2Neibs)
        self._compile_and_check(
            [x], [images2neibs(x, neib_shape=(2, 3), mode='full')],
            [images], Images2Neibs)
Exemple #9
0
    def test_infer_shape(self):
        shape = (100, 40, 6, 3)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(x, neib_shape=(2, 1), mode='valid')],
            [images], Images2Neibs)
        self._compile_and_check(
            [x], [images2neibs(x, neib_shape=(2, 3), mode='valid')],
            [images], Images2Neibs)
        shape = (100, 40, 5, 4)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(
                x, neib_shape=(2, 1), mode='ignore_borders')],
            [images], Images2Neibs)
        shape = (100, 40, 5, 3)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(
                x, neib_shape=(2, 3), mode='ignore_borders')],
            [images], Images2Neibs)

        shape = (100, 40, 6, 7)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(
                x, neib_shape=(2, 2), mode='ignore_borders')],
            [images], Images2Neibs)
        shape = (100, 40, 5, 10)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(
                x, neib_shape=(3, 3), mode='wrap_centered')],
            [images], Images2Neibs)
        shape = (100, 40, 6, 4)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(x, neib_shape=(2, 1), mode='half')],
            [images], Images2Neibs)
        self._compile_and_check(
            [x], [images2neibs(x, neib_shape=(2, 3), mode='half')],
            [images], Images2Neibs)
        shape = (100, 40, 6, 5)
        images = np.ones(shape).astype('float32')
        x = T.ftensor4()
        self._compile_and_check(
            [x], [images2neibs(x, neib_shape=(2, 1), mode='full')],
            [images], Images2Neibs)
        self._compile_and_check(
            [x], [images2neibs(x, neib_shape=(2, 3), mode='full')],
            [images], Images2Neibs)
Exemple #10
0
def t_mk_pool_ready(t_pool_input, t_pool_shape):
    """
    Prepare pooling input
    :param t_pool_input: 4D theano tensor batch_sz x channels x height x width
    :param t_pool_shape: theano lvector pool_ch x pool_h x pool_w
    :return: aux. sizes and input reshaped for pooling
    """
    # sizes
    # input
    t_batch_sz = t_pool_input.shape[0]
    t_in_ch = t_pool_input.shape[1]
    t_in_h = t_pool_input.shape[2]
    t_in_w = t_pool_input.shape[3]
    # pooling
    t_pool_ch = t_pool_shape[0]
    t_pool_h = t_pool_shape[1]
    t_pool_w = t_pool_shape[2]
    # output
    t_out_ch = (t_in_ch + t_pool_ch - 1) // t_pool_ch
    t_out_h = (t_in_h + t_pool_h - 1) // t_pool_h
    t_out_w = (t_in_w + t_pool_w - 1) // t_pool_w

    # we will need to pad input (probably), so here's the padded shape:
    t_padded_ch = t_out_ch * t_pool_ch
    t_padded_h = t_out_h * t_pool_h
    t_padded_w = t_out_w * t_pool_w
    t_padded_pool_in_z = T.zeros(T.stack([t_batch_sz, t_padded_ch, t_padded_h, t_padded_w]))
    t_padded_pool_in = T.inc_subtensor(t_padded_pool_in_z[:t_batch_sz, :t_in_ch, :t_in_h, :t_in_w], t_pool_input)

    # below is all computed
    # spatial pooling
    t_sp_pooled = images2neibs(t_padded_pool_in, T.stack([t_pool_h, t_pool_w]))
    # spatial pooling output shape
    # has size (B * C * H/h * W/w) x (h*w)
    t_sp_pooled_dims = t_sp_pooled.shape
    # lines per channel
    # H*W / (h*w)
    t_lpc = (t_padded_h * t_padded_w) // (t_pool_h * t_pool_w)
    # shape to collect channels
    t_ch_pool_prep_dims_1 = T.stack([t_sp_pooled_dims[0] // t_lpc, t_lpc, t_sp_pooled_dims[1]])
    # preparing pooling by channels
    # reshape to collect channels in a separate dimension
    t_ch_pool_prep_1 = T.reshape(t_sp_pooled, t_ch_pool_prep_dims_1)
    t_ch_pool_prep_2 = T.shape_padleft(T.transpose(t_ch_pool_prep_1, [1, 0, 2]))
    # prepare for channel pooling
    t_ch_pool_dims = T.stack([t_pool_ch, t_ch_pool_prep_dims_1[-1]])
    t_pool_ready = images2neibs(t_ch_pool_prep_2, t_ch_pool_dims)
    return t_batch_sz, t_in_ch, t_in_h, t_in_w, t_out_ch, t_out_h, t_out_w, t_pool_ready
def plot_top_16(D, sz, imname):
    '''
    Plots the top 16 components from the basis matrix D.
    Each basis vector represents an image block of shape (sz, sz)

    Parameters
    -------------
    D: np.ndarray
        N x n matrix representing the basis vectors of the PCA space
        N is the dimension of the original space (number of pixels in a block)
        n represents the maximum dimension of the PCA space (assumed to be atleast 16)

    sz: Integer
        The height and width of each block

    imname: string
        name of file where image will be saved.
    '''
    #TODO: Obtain top 16 components of D and plot them
    image = T.tensor4('image')
    neibs = nbs.images2neibs(image, neib_shape = (sz, sz))
    transToImage = nbs.neibs2images(neibs, neib_shape = (sz, sz), original_shape = (1,1, sz, sz))
    trans_func = theano.function([neibs], transToImage)
    f, axarr = plt.subplots(4,4)
    for i in range(4):
        for j in range(4):
            plt.axes(axarr[i,j])
            plt.imshow(trans_func(D[:,[i*4+j]].T)[0,0], cmap = 'gray')
    os.chdir(CWD)
    f.savefig(imname)
    plt.close(f)
Exemple #12
0
    def test_neibs(self):
        for shape, pshape in [((10, 7, 18, 18), (2, 2)),
                              ((10, 7, 6, 18), (3, 2)),
                              ((5, 7, 66, 66), (33, 33)),
                              ((5, 7, 68, 66), (34, 33))]:
            for border in ['valid', 'ignore_borders']:
                for dtype in self.dtypes:
                    images = shared(
                        numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape))
                    neib_shape = T.as_tensor_variable(pshape)

                    f = function([],
                                 images2neibs(images, neib_shape, mode=border),
                                 mode=self.mode)

                    # print images.get_value(borrow=True)
                    neibs = f()
                    # print neibs
                    g = function([],
                                 neibs2images(neibs, neib_shape, images.shape),
                                 mode=self.mode)
                    assert any([isinstance(node.op, self.op)
                                for node in f.maker.fgraph.toposort()])

                    # print g()
                    assert numpy.allclose(images.get_value(borrow=True), g())
Exemple #13
0
def reshape_to_dataset(train_set_x, neib_shape):
    from theano.tensor.nnet import neighbours
    imgs = T.tensor4('imgs')
    imgs.tag.test_value = train_set_x
    neibs = []
    for ch in range(train_set_x.shape[1]):
        new_shape = list(train_set_x.shape)
        new_shape[1] = 1
        neibs.append(
            neighbours.images2neibs(imgs[:, ch, :, :].reshape(new_shape),
                                    neib_shape=neib_shape))
    n_channels = train_set_x.shape[1]
    if n_channels == 3:
        neib = T.stack(neibs, axis=-1).flatten(2)
    elif n_channels == 1:
        neib = neibs[0]
    else:
        raise NotImplementedError(
            "images with {0} channels".format(n_channels))
    resh = neib.reshape((train_set_x.shape[0],
                         neib.shape[0] // train_set_x.shape[0], neib.shape[1]))
    # scan iterates over the first dimention
    f = theano.function(inputs=[imgs], outputs=resh.transpose(1, 0, 2))

    return f(train_set_x)
def make_patches(x, patch_size, patch_stride):
    '''Break image `x` up into a bunch of patches.'''

    # TODO: remove dependency on Theano
    import theano as T
    from theano.tensor.nnet.neighbours import images2neibs
    if K.image_data_format() == 'channels_first':
        ch = K.shape(x)[0]
        x = K.expand_dims(x, 0)
    else:
        ch = K.shape(x)[-1]
        x = K.permute_dimensions(x, (2, 0, 1))
        x = K.expand_dims(x, 0)

    patches = images2neibs(x, (patch_size, patch_size),
                           (patch_stride, patch_stride),
                           mode='valid')
    # neibs are sorted per-channel
    patches = K.reshape(
        patches, (ch, K.shape(patches)[0] // ch, patch_size, patch_size))
    if K.image_data_format() == 'channels_first':
        patches = K.permute_dimensions(patches, (1, 0, 2, 3))
    else:
        patches = K.permute_dimensions(patches, (1, 2, 3, 0))
    patches_norm = K.sqrt(
        K.sum(K.square(patches), axis=(1, 2, 3), keepdims=True))
    return patches, patches_norm
Exemple #15
0
def _meanpool ( input, ds, ignore_border = False ):
    """ provide mean pooling """
    out_shp = (input.shape[0], input.shape[1], input.shape[2]/ds[0], input.shape[3]/ds[1])    
    neib = images2neibs(input, neib_shape = ds , 
                                mode = 'valid' if ignore_border is False else 'ignore_borders')
    pooled_vectors = neib.mean( axis = - 1 )
    return T.reshape(pooled_vectors, out_shp, ndim = 4 )    
Exemple #16
0
    def test_neibs(self):
        for shape, pshape in [((10, 7, 18, 18), (2, 2)),
                              ((10, 7, 6, 18), (3, 2)),
                              ((5, 7, 66, 66), (33, 33)),
                              ((5, 7, 68, 66), (34, 33))]:
            for border in ['valid', 'ignore_borders']:
                for dtype in self.dtypes:
                    images = shared(
                        np.arange(np.prod(shape), dtype=dtype).reshape(shape))
                    neib_shape = T.as_tensor_variable(pshape)

                    f = function([],
                                 images2neibs(images, neib_shape, mode=border),
                                 mode=self.mode)

                    # print images.get_value(borrow=True)
                    neibs = f()
                    # print neibs
                    g = function([],
                                 neibs2images(neibs, neib_shape, images.shape),
                                 mode=self.mode)
                    assert any([
                        isinstance(node.op, self.op)
                        for node in f.maker.fgraph.toposort()
                    ])

                    # print g()
                    assert np.allclose(images.get_value(borrow=True), g())
Exemple #17
0
    def get_output(self, train):
        X = self.get_input(train)
        # check if poolsize is symmetric. If not, step in neibs has to be set.

        if self.stride is not None:
            # rows_symmetrical = (X.shape[2] + 1)//2
            # step_val = (X.shape[2] - 1)//2
            sums = images2neibs(X, neib_shape=self.poolsize, neib_step=self.stride).sum(axis=-1)
            counts = T.neq(images2neibs(X, neib_shape=self.poolsize, neib_step=self.stride), 0).sum(axis=-1)
            average = (sums/counts).reshape((X.shape[0], X.shape[1], 2, 1))
        else:
            # rows_symmetrical = (X.shape[2])//2
            sums = images2neibs(X, neib_shape=self.poolsize).sum(axis=-1)
            counts = T.neq(images2neibs(X, neib_shape=self.poolsize), 0).sum(axis=-1)
            average = (sums/counts).reshape((X.shape[0], X.shape[1], 2, 1))
        return average
Exemple #18
0
    def test_neibs_manual(self):
        shape = (2, 3, 4, 4)
        for dtype in self.dtypes:
            images = shared(
                np.arange(np.prod(shape), dtype=dtype).reshape(shape))
            neib_shape = T.as_tensor_variable((2, 2))

            for border in ['valid', 'ignore_borders']:
                f = function([],
                             images2neibs(images, neib_shape, mode=border),
                             mode=self.mode)
                assert any([
                    isinstance(node.op, self.op)
                    for node in f.maker.fgraph.toposort()
                ])

                # print images.get_value(borrow=True)
                neibs = f()
                # print neibs
                assert np.allclose(
                    neibs,
                    [[0, 1, 4, 5], [2, 3, 6, 7], [8, 9, 12, 13],
                     [10, 11, 14, 15], [16, 17, 20, 21], [18, 19, 22, 23],
                     [24, 25, 28, 29], [26, 27, 30, 31], [32, 33, 36, 37],
                     [34, 35, 38, 39], [40, 41, 44, 45], [42, 43, 46, 47],
                     [48, 49, 52, 53], [50, 51, 54, 55], [56, 57, 60, 61],
                     [58, 59, 62, 63], [64, 65, 68, 69], [66, 67, 70, 71],
                     [72, 73, 76, 77], [74, 75, 78, 79], [80, 81, 84, 85],
                     [82, 83, 86, 87], [88, 89, 92, 93], [90, 91, 94, 95]])
                g = function([],
                             neibs2images(neibs, neib_shape, images.shape),
                             mode=self.mode)

                assert np.allclose(images.get_value(borrow=True), g())
def make_patches_grid(x, patch_size, patch_stride):
    '''Break image `x` up into a grid of patches.

    input shape: (channels, rows, cols)
    output shape: (rows, cols, channels, patch_rows, patch_cols)
    '''
    from theano.tensor.nnet.neighbours import images2neibs  # TODO: all K, no T
    x = K.expand_dims(x, 0)
    xs = K.shape(x)
    num_rows = 1 + (xs[-2] - patch_size) // patch_stride
    num_cols = 1 + (xs[-1] - patch_size) // patch_stride
    num_channels = xs[-3]
    patches = images2neibs(
        x, (patch_size, patch_size), (patch_stride, patch_stride),
        mode='valid')
    # neibs are sorted per-channel
    patches = K.reshape(patches,
                        (num_channels, K.shape(patches)[0] // num_channels,
                         patch_size, patch_size))
    patches = K.permute_dimensions(patches, (1, 0, 2, 3))
    # arrange in a 2d-grid (rows, cols, channels, px, py)
    patches = K.reshape(
        patches, (num_rows, num_cols, num_channels, patch_size, patch_size))
    patches_norm = K.sqrt(
        K.sum(K.square(patches), axis=(2, 3, 4), keepdims=True))
    return patches, patches_norm
def make_patches_grid(x, patch_size, patch_stride):
    '''Break image `x` up into a grid of patches.

    input shape: (channels, rows, cols)
    output shape: (rows, cols, channels, patch_rows, patch_cols)
    '''
    from theano.tensor.nnet.neighbours import images2neibs  # TODO: all K, no T
    x = K.expand_dims(x, 0)
    xs = K.shape(x)
    num_rows = 1 + (xs[-2] - patch_size) // patch_stride
    num_cols = 1 + (xs[-1] - patch_size) // patch_stride
    num_channels = xs[-3]
    patches = images2neibs(x, (patch_size, patch_size),
                           (patch_stride, patch_stride),
                           mode='valid')
    # neibs are sorted per-channel
    patches = K.reshape(patches,
                        (num_channels, K.shape(patches)[0] // num_channels,
                         patch_size, patch_size))
    patches = K.permute_dimensions(patches, (1, 0, 2, 3))
    # arrange in a 2d-grid (rows, cols, channels, px, py)
    patches = K.reshape(
        patches, (num_rows, num_cols, num_channels, patch_size, patch_size))
    patches_norm = K.sqrt(
        K.sum(K.square(patches), axis=(2, 3, 4), keepdims=True))
    return patches, patches_norm
Exemple #21
0
    def speed_neibs_full(self):
        shape = (100, 40, 18, 18)
        images = shared(np.arange(np.prod(shape), dtype="float32").reshape(shape))
        neib_shape = T.as_tensor_variable((3, 3))

        f = function([], images2neibs(images, neib_shape, mode="full"), mode=self.mode)

        for i in range(1000):
            f()
Exemple #22
0
    def test_neibs_full_with_inconsistent_borders(self):
        shape = (2, 3, 5, 5)
        images = T.dtensor4()
        images_val = np.arange(np.prod(shape), dtype='float32').reshape(shape)

        f = theano.function([images],
                            T.sqr(images2neibs(images, (2, 2), mode='full')),
                            mode=self.mode)
        self.assertRaises(TypeError, f, images_val)
def main():
    '''
    Read here all images(grayscale) from jaffe folder
    into an numpy array Ims with size (no_images, height, width).
    Make sure the images are read after sorting the filenames
    '''
    
    # didn't use walk
    from glob import glob
    import os
    
    files_list = glob(os.path.join('jaffe/', '*.tiff'))
    im_ind=0
    im = Image.open(files_list[0])
    Ims = np.float32(np.zeros([len(files_list),im.height,im.width]))
    for a_file in sorted(files_list):
        im = Image.open(a_file).convert("L")
        Ims[im_ind,:,:]=np.array(im)        
        im_ind = im_ind+1

    szs = [16, 32, 64]
    num_coeffs = [range(1, 10, 1), range(3, 30, 3), range(5, 50, 5)]

    for sz, nc in zip(szs, num_coeffs):
        '''
        Divide here each image into non-overlapping blocks of shape (sz, sz).
        Flatten each block and arrange all the blocks in a
        (no_images*n_blocks_in_image) x (sz*sz) matrix called X
        '''
        
        # Defining variables
        images = T.tensor4('images')
        neibs = images2neibs(images, neib_shape=(sz,sz))
        
        # Constructing theano function
        window_function = theano.function([images], neibs)
        
        X=window_function(Ims.reshape((1,len(files_list),im.height,im.width)))        
        
        X_mn = np.mean(X, 0)
        X = X - np.repeat(X_mn.reshape(1, -1), X.shape[0], 0)

        '''
        Perform eigendecomposition on X^T X and arrange the eigenvectors
        in decreasing order of eigenvalues into a matrix D
        '''
        V,D = np.linalg.eigh(np.dot(X.T,X))
        D = np.fliplr(D)        
        
        c = np.dot(D.T, X.T)

        for i in range(0, 200, 10):
            plot_mul(c, D, i, X_mn.reshape((sz, sz)),
                     num_coeffs=nc, n_blocks=int(256/sz))

        plot_top_16(D, sz, imname='output/hw1a_top16_{0}.png'.format(sz))
Exemple #24
0
def _meanpool(input, ds, ignore_border=False):
    """ provide mean pooling """
    out_shp = (input.shape[0], input.shape[1], input.shape[2] / ds[0],
               input.shape[3] / ds[1])
    neib = images2neibs(
        input,
        neib_shape=ds,
        mode='valid' if ignore_border is False else 'ignore_borders')
    pooled_vectors = neib.mean(axis=-1)
    return T.reshape(pooled_vectors, out_shp, ndim=4)
Exemple #25
0
    def test_neibs_full_with_inconsistent_borders(self):
        shape = (2, 3, 5, 5)
        images = T.dtensor4()
        images_val = np.arange(np.prod(shape),
                               dtype='float32').reshape(shape)

        f = theano.function([images],
                            T.sqr(images2neibs(images, (2, 2), mode='full')),
                            mode=self.mode)
        self.assertRaises(TypeError, f, images_val)
Exemple #26
0
 def __init__(self, input1, input2):
     x1_sub = input1[:, :, 2:-2, 2:-2]
     x1_flatten = T.flatten(x1_sub)
     x1 = T.extra_ops.repeat(x1_flatten, 25)
     x1 = T.reshape(x1, [T.shape(x1_flatten)[0], 25])
     x2 = neighbours.images2neibs(input2, neib_shape=(5, 5), neib_step=(1, 1))
     diff = x1 - x2
     new_shape = T.shape(x1_sub)*[1, 1, 5, 5]
     diff_img = neighbours.neibs2images(diff, neib_shape=(5, 5), original_shape=[1, 25, 25*5, 5*5])
     self.output = T.nnet.relu(diff_img)
Exemple #27
0
    def test_neibs_full_with_inconsistent_borders(self):
        shape = (2, 3, 5, 5)
        images = T.dtensor4()
        images_val = np.arange(np.prod(shape), dtype="float32").reshape(shape)

        f = theano.function(
            [images], T.sqr(images2neibs(images, (2, 2), mode="full")), mode=self.mode
        )
        with pytest.raises(TypeError):
            f(images_val)
Exemple #28
0
    def test_neibs_bad_shape(self):
        shape = (2, 3, 10, 10)
        for dtype in self.dtypes:
            images = shared(np.arange(np.prod(shape), dtype=dtype).reshape(shape))

            for neib_shape in [(3, 2), (2, 3)]:
                neib_shape = T.as_tensor_variable(neib_shape)
                f = function([], images2neibs(images, neib_shape), mode=self.mode)
                with pytest.raises(TypeError):
                    f()

                # Test that ignore border work in that case.
                f = function(
                    [],
                    images2neibs(images, neib_shape, mode="ignore_borders"),
                    mode=self.mode,
                )
                assert self.op in [type(node.op) for node in f.maker.fgraph.toposort()]
                f()
Exemple #29
0
    def speed_neibs(self):
        shape = (100, 40, 18, 18)
        images = shared(numpy.arange(numpy.prod(shape),
                                     dtype='float32').reshape(shape))
        neib_shape = T.as_tensor_variable((3, 3))

        f = function([], images2neibs(images, neib_shape),
                     mode=self.mode)

        for i in range(1000):
            f()
def make_patches(x, patch_size, patch_stride):
    from theano.tensor.nnet.neighbours import images2neibs
    x = K.expand_dims(x, 0)
    patches = images2neibs(x,
        (patch_size, patch_size), (patch_stride, patch_stride),
        mode='valid')
    # neibs are sorted per-channel
    patches = K.reshape(patches, (K.shape(x)[1], K.shape(patches)[0] // K.shape(x)[1], patch_size, patch_size))
    patches = K.permute_dimensions(patches, (1, 0, 2, 3))
    patches_norm = K.l2_normalize(patches, 1)
    return patches, patches_norm
Exemple #31
0
    def test_neibs_bad_shape(self):
        shape = (2, 3, 10, 10)
        for dtype in self.dtypes:
            images = shared(numpy.arange(
                numpy.prod(shape), dtype=dtype).reshape(shape))

            for neib_shape in [(3, 2), (2, 3)]:
                neib_shape = T.as_tensor_variable(neib_shape)
                f = function([], images2neibs(images, neib_shape),
                             mode=self.mode)
                self.assertRaises(TypeError, f)

                # Test that ignore border work in that case.
                f = function([],
                             images2neibs(images, neib_shape,
                                          mode='ignore_borders'),
                             mode=self.mode)
                assert self.op in [type(node.op)
                                   for node in f.maker.fgraph.toposort()]
                f()
Exemple #32
0
def _maxrandpool ( input, ds, p, ignore_border = False ):
    """ provide random pooling among the top 'p' sorted outputs p = 0 is maxpool """
    rng = numpy.random.RandomState(24546)
    out_shp = (input.shape[0], input.shape[1], input.shape[2]/ds[0], input.shape[3]/ds[1])        
    srng = RandomStreams(rng.randint(2147462579))
    pos = srng.random_integers(size=(1,1), low = ds[0]*ds[1]-1-p, high = ds[0]*ds[1]-1)
    neib = images2neibs(input, neib_shape = ds ,
                                mode = 'valid' if ignore_border is False else 'ignore_borders') 
    neib = neib.sort(axis = -1) 
    pooled_vectors = neib[:,pos]   
    return T.reshape(pooled_vectors, out_shp, ndim = 4 )   
def make_patches(x, patch_size, patch_stride):
    '''Break image `x` up into a bunch of patches.'''
    from theano.tensor.nnet.neighbours import images2neibs
    x = K.expand_dims(x, 0)
    patches = images2neibs(x,
        (patch_size, patch_size), (patch_stride, patch_stride),
        mode='valid')
    # neibs are sorted per-channel
    patches = K.reshape(patches, (K.shape(x)[1], K.shape(patches)[0] // K.shape(x)[1], patch_size, patch_size))
    patches = K.permute_dimensions(patches, (1, 0, 2, 3))
    patches_norm = K.sqrt(K.sum(K.square(patches), axis=(1,2,3), keepdims=True))
    return patches, patches_norm
def make_patches(x, shape):
    x = K.expand_dims(x, 0)

    patches = images2neibs(x, (shape, shape))
    patches = K.reshape(patches, (K.shape(x)[1],
                                  K.shape(patches)[0] / K.shape(x)[1],
                                  shape, shape))
    
    patches_norm = K.sqrt(K.sum(K.square(patches), axis=(1,2,3),
                                keepdims=True))

    return patches, patches_norm
Exemple #35
0
    def speed_neibs_wrap_centered(self):
        shape = (100, 40, 18, 18)
        images = shared(numpy.arange(numpy.prod(shape),
                                     dtype='float32').reshape(shape))
        neib_shape = T.as_tensor_variable((3, 3))

        f = function([],
                     images2neibs(images, neib_shape, mode="wrap_centered"),
                     mode=self.mode)

        for i in range(1000):
            f()
Exemple #36
0
def make_patches(x, shape):
    x = K.expand_dims(x, 0)

    patches = images2neibs(x, (shape, shape))
    patches = K.reshape(
        patches,
        (K.shape(x)[1], K.shape(patches)[0] / K.shape(x)[1], shape, shape))

    patches_norm = K.sqrt(
        K.sum(K.square(patches), axis=(1, 2, 3), keepdims=True))

    return patches, patches_norm
def getImgPatch(X, window):
    '''Return a tiled matrix X, where X is a number of images * blcoks_per_image by window ** 2 size matrix'''
    n = X.shape[0]
    image = T.tensor4('Image')
    '''create function that tile each image into a n_block_per_image ** 2 by window ** 2 size matrix'''
    neibs = nbs.images2neibs(image, neib_shape = (window, window))
    window_function = theano.function([image], neibs)
    X_blocks = None
    X_tmp = copy.copy(X)
    X_tmp.shape = (1, X_tmp.shape[0], X_tmp.shape[1], X_tmp.shape[2])
    X_blocks = window_function(X_tmp)
    return X_blocks
Exemple #38
0
def img_2_neibs_with_chans(inputs_sym, patch_size):
    flat_patches = neighbours.images2neibs(inputs_sym, patch_size, (1, 1))
    topo_flat_patches = T.reshape(
        flat_patches,
        (inputs_sym.shape[0], inputs_sym.shape[1], inputs_sym.shape[2] -
         patch_size[0] + 1, inputs_sym.shape[3] - patch_size[1] + 1,
         patch_size[0], patch_size[1]))

    flat_patches = topo_flat_patches.dimshuffle(0, 2, 3, 1, 4, 5)
    flat_patches = T.reshape(
        flat_patches,
        (T.prod(flat_patches.shape[:3]), T.prod(flat_patches.shape[3:])))
    return flat_patches
    def get_output(self, train):
        X = self.get_input(train)
        # check if poolsize is symmetric. If not, step in neibs has to be set.

        if self.stride is not None:
            # rows_symmetrical = (X.shape[2] + 1)//2
            # step_val = (X.shape[2] - 1)//2
            sums = images2neibs(X,
                                neib_shape=self.poolsize,
                                neib_step=self.stride).sum(axis=-1)
            counts = T.neq(
                images2neibs(X,
                             neib_shape=self.poolsize,
                             neib_step=self.stride), 0).sum(axis=-1)
            average = (sums / counts).reshape((X.shape[0], X.shape[1], 2, 1))
        else:
            # rows_symmetrical = (X.shape[2])//2
            sums = images2neibs(X, neib_shape=self.poolsize).sum(axis=-1)
            counts = T.neq(images2neibs(X, neib_shape=self.poolsize),
                           0).sum(axis=-1)
            average = (sums / counts).reshape((X.shape[0], X.shape[1], 2, 1))
        return average
Exemple #40
0
def make_patches(x, patch_size, patch_stride):
    from theano.tensor.nnet.neighbours import images2neibs
    x = K.expand_dims(x, 0)
    patches = images2neibs(x, (patch_size, patch_size),
                           (patch_stride, patch_stride),
                           mode='valid')
    # neibs are sorted per-channel
    patches = K.reshape(patches,
                        (K.shape(x)[1], K.shape(patches)[0] // K.shape(x)[1],
                         patch_size, patch_size))
    patches = K.permute_dimensions(patches, (1, 0, 2, 3))
    patches_norm = K.l2_normalize(patches, 1)
    return patches, patches_norm
Exemple #41
0
 def __init__(self, input1, input2):
     x1_sub = input1[:, :, 2:-2, 2:-2]
     x1_flatten = T.flatten(x1_sub)
     x1 = T.extra_ops.repeat(x1_flatten, 25)
     x1 = T.reshape(x1, [T.shape(x1_flatten)[0], 25])
     x2 = neighbours.images2neibs(input2,
                                  neib_shape=(5, 5),
                                  neib_step=(1, 1))
     diff = x1 - x2
     new_shape = T.shape(x1_sub) * [1, 1, 5, 5]
     diff_img = neighbours.neibs2images(
         diff, neib_shape=(5, 5), original_shape=[1, 25, 25 * 5, 5 * 5])
     self.output = T.nnet.relu(diff_img)
def main():
    '''
    Read here all images(grayscale) from jaffe folder
    into an numpy array Ims with size (no_images, height, width).
    Make sure the images are read after sorting the filenames
    '''
    imagename_list = os.listdir('jaffe/')
    imagename_list.sort()
    Ims = []
    for img_name in imagename_list:
        Ims.append(np.array(Image.open('jaffe/'+img_name)))
    Ims = np.asarray(Ims)
    print "Data loaded"
    nimgs, height, width = Ims.shape

    szs = [16, 32, 64]
    num_coeffs = [range(1, 10, 1), range(3, 30, 3), range(5, 50, 5)]

    for sz, nc in zip(szs, num_coeffs):
        '''
        Divide here each image into non-overlapping blocks of shape (sz, sz).
        Flatten each block and arrange all the blocks in a
        (no_images * n_blocks_in_image) x (sz*sz) matrix called X
        '''
        images = theano.tensor.tensor4('images')
        neibs = images2neibs(images, neib_shape=(sz, sz))
        window_function = theano.function([images], neibs, allow_input_downcast=True)
        X = window_function(Ims.reshape((nimgs, 1, height, width)))
        '''
        Perform eigendecomposition on X^T X and arrange the eigenvectors
        in decreasing order of eigenvalues into a matrix D
        '''
        print 'Computing eigendecomposition for size %s with nc = %s...' % (sz, nc)
        X_mn = np.mean(X, 0)
        X = X - np.repeat(X_mn.reshape(1, -1), X.shape[0], 0)
        eigen_out = np.linalg.eig(X.T.dot(X))
        eigenvalues = eigen_out[0]
        eigenvectors = eigen_out[1]
        # Sorted Eigenvalues
        col_indx = np.argsort(-eigenvalues)
        # Sorted Eigenvectors
        eigenvectors = eigenvectors[:,col_indx]
        D = eigenvectors[:, col_indx]
        c = np.dot(D.T, X.T)
        print 'Plotting the reconstruction...'
        for i in range(0, 200, 10): # Incrementing by 10
            plot_mul(c=c, D=D, im_num=i, X_mn=X_mn.reshape((sz, sz)),
                     num_coeffs=nc, n_blocks=int(256/sz))

        plot_top_16(D, sz, imname='output/hw1a_top16_{0}.png'.format(sz))
Exemple #43
0
    def test_neibs_valid_with_inconsistent_borders(self):
        shape = (2, 3, 5, 5)
        images = T.dtensor4()
        images_val = numpy.arange(numpy.prod(shape),
                                  dtype='float32').reshape(shape)

        def fn(images):
            return T.sum(T.sqr(images2neibs(images, (2, 2), mode='valid')),
                         axis=[0, 1])

        f = theano.function([images],
                            T.sqr(images2neibs(images, (2, 2), mode='valid')),
                            mode=self.mode)
        self.assertRaises(TypeError, f, images_val)
Exemple #44
0
def make_patches(x, patch_size, patch_stride):
    '''Break image `x` up into a bunch of patches.'''
    from theano.tensor.nnet.neighbours import images2neibs
    patches = images2neibs(x, (patch_size, patch_size),
                           (patch_stride, patch_stride),
                           mode='valid')
    # neibs are sorted per-channel
    patches = K.reshape(patches,
                        (K.shape(x)[1], K.shape(patches)[0] // K.shape(x)[1],
                         patch_size, patch_size))
    patches = K.permute_dimensions(patches, (1, 0, 2, 3))
    patches_norm = K.sqrt(
        K.sum(K.square(patches), axis=(1, 2, 3), keepdims=True))
    return patches, patches_norm
Exemple #45
0
def img_2_neibs_with_chans(inputs_sym, patch_size):
    flat_patches = neighbours.images2neibs(inputs_sym, patch_size, (1,1))
    topo_flat_patches = T.reshape(flat_patches,(inputs_sym.shape[0],
                                            inputs_sym.shape[1],
                                            inputs_sym.shape[2]-patch_size[0]+1,
                                            inputs_sym.shape[3]-patch_size[1]+1,
                                            patch_size[0],
                                            patch_size[1]))


    flat_patches = topo_flat_patches.dimshuffle(0,2,3,1,4,5)
    flat_patches = T.reshape(flat_patches, (T.prod(flat_patches.shape[:3]),
                                                 T.prod(flat_patches.shape[3:])))
    return flat_patches
Exemple #46
0
    def test_neibs_valid_with_inconsistent_borders(self):
        shape = (2, 3, 5, 5)
        images = T.dtensor4()
        images_val = numpy.arange(numpy.prod(shape),
                                  dtype='float32').reshape(shape)

        def fn(images):
            return T.sum(T.sqr(images2neibs(images, (2, 2), mode='valid')),
                         axis=[0, 1])

        f = theano.function([images],
                            T.sqr(images2neibs(images, (2, 2), mode='valid')),
                            mode=self.mode)
        self.assertRaises(TypeError, f, images_val)
    def test_neibs_manual_step(self):
        shape = (2, 3, 5, 5)
        for dtype in self.dtypes:
            images = shared(
                np.asarray(np.arange(np.prod(shape)).reshape(shape),
                           dtype=dtype))
            neib_shape = tt.as_tensor_variable((3, 3))
            neib_step = tt.as_tensor_variable((2, 2))
            for border in ["valid", "ignore_borders"]:
                f = function(
                    [],
                    images2neibs(images, neib_shape, neib_step, mode=border),
                    mode=self.mode,
                )

                neibs = f()
                assert self.op in [
                    type(node.op) for node in f.maker.fgraph.toposort()
                ]

                assert np.allclose(
                    neibs,
                    [
                        [0, 1, 2, 5, 6, 7, 10, 11, 12],
                        [2, 3, 4, 7, 8, 9, 12, 13, 14],
                        [10, 11, 12, 15, 16, 17, 20, 21, 22],
                        [12, 13, 14, 17, 18, 19, 22, 23, 24],
                        [25, 26, 27, 30, 31, 32, 35, 36, 37],
                        [27, 28, 29, 32, 33, 34, 37, 38, 39],
                        [35, 36, 37, 40, 41, 42, 45, 46, 47],
                        [37, 38, 39, 42, 43, 44, 47, 48, 49],
                        [50, 51, 52, 55, 56, 57, 60, 61, 62],
                        [52, 53, 54, 57, 58, 59, 62, 63, 64],
                        [60, 61, 62, 65, 66, 67, 70, 71, 72],
                        [62, 63, 64, 67, 68, 69, 72, 73, 74],
                        [75, 76, 77, 80, 81, 82, 85, 86, 87],
                        [77, 78, 79, 82, 83, 84, 87, 88, 89],
                        [85, 86, 87, 90, 91, 92, 95, 96, 97],
                        [87, 88, 89, 92, 93, 94, 97, 98, 99],
                        [100, 101, 102, 105, 106, 107, 110, 111, 112],
                        [102, 103, 104, 107, 108, 109, 112, 113, 114],
                        [110, 111, 112, 115, 116, 117, 120, 121, 122],
                        [112, 113, 114, 117, 118, 119, 122, 123, 124],
                        [125, 126, 127, 130, 131, 132, 135, 136, 137],
                        [127, 128, 129, 132, 133, 134, 137, 138, 139],
                        [135, 136, 137, 140, 141, 142, 145, 146, 147],
                        [137, 138, 139, 142, 143, 144, 147, 148, 149],
                    ],
                )
def reconstructed_image(D,c,num_coeffs,X_mean,n_blocks,im_num):
    '''
    This function reconstructs an image X_recon_img given the number of
    coefficients for each image specified by num_coeffs
    '''
    
    '''
        Parameters
    ---------------
    c: np.ndarray
        a n x m matrix  representing the coefficients of all the image blocks.
        n represents the maximum dimension of the PCA space.
        m is (number of images x n_blocks**2)

    D: np.ndarray
        an N x n matrix representing the basis vectors of the PCA space
        N is the dimension of the original space (number of pixels in a block)

    im_num: Integer
        index of the image to visualize

    X_mean: np.ndarray
        a matrix representing the mean block.

    num_coeffs: Integer
        an integer that specifies the number of top components to be
        considered while reconstructing
        

    n_blocks: Integer
        number of blocks comprising the image in each direction.
        For example, for a 256x256 image divided into 64x64 blocks, n_blocks will be 4
    '''

    #TODO: Enter code below for reconstructing the image X_recon_img    
    
    c_im = c[:num_coeffs,n_blocks*n_blocks*im_num:n_blocks*n_blocks*(im_num+1)]
    D_im = D[:,:num_coeffs]
    M_coef = np.dot(D_im.T, X_mean.T)
    tmp1 = c_im - np.repeat(M_coef.reshape(-1, 1), n_blocks**2, 1)
    X_blocks = np.dot(D_im, tmp1) + np.repeat(X_mean.reshape(-1,1), n_blocks**2, 1)
    X_blocks = X_blocks.T
    slide_window = int(X_mean.size ** 0.5)
    image = T.tensor4('image')
    neibs = nbs.images2neibs(image, neib_shape = (slide_window, slide_window))
    transToImage = nbs.neibs2images(neibs, neib_shape = (slide_window, slide_window), original_shape = (1,1,IMG_SIZE, IMG_SIZE))
    trans_func = theano.function([neibs], transToImage)
    X_recon_img = trans_func(X_blocks)
    return X_recon_img[0,0]
Exemple #49
0
 def test_neibs_full_step_by_valid(self):
     for shp_idx, (shape, neib_step, neib_shapes) in enumerate([
         [(7, 8, 5, 5), (1, 1), ((3, 3), (3, 5), (5, 3))],
         [(7, 8, 5, 5), (2, 2), ((3, 3), (3, 5), (5, 3))],
         [(7, 8, 6, 6), (3, 3), ((2, 2), (2, 5), (5, 2))],
         [(7, 8, 6, 6), (1, 3), ((2, 2), (2, 5), (5, 2))],
         [(7, 8, 6, 6), (3, 1), ((2, 2), (2, 5), (5, 2))],
         [(80, 90, 5, 5), (1, 2), ((3, 3), (3, 5), (5, 3))],
         [(1025, 9, 5, 5), (2, 1), ((3, 3), (3, 5), (5, 3))],
         [(1, 1, 11, 1037), (2, 3), ((3, 3), (5, 3))],
         [(1, 1, 1043, 11), (3, 2), ((3, 3), (3, 5))]]
     ):
         for neib_shape in neib_shapes:
             for dtype in self.dtypes:
                 x = theano.shared(np.random.randn(*shape).astype(dtype))
                 extra = (neib_shape[0] - 1, neib_shape[1] - 1)
                 padded_shape = (x.shape[0], x.shape[1], x.shape[2] + 2 * extra[0], x.shape[3] + 2 * extra[1])
                 padded_x = T.zeros(padded_shape)
                 padded_x = T.set_subtensor(padded_x[:, :, extra[0]:-extra[0], extra[1]:-extra[1]], x)
                 x_using_valid = images2neibs(padded_x, neib_shape, neib_step, mode="valid")
                 x_using_full = images2neibs(x, neib_shape, neib_step, mode="full")
                 f_valid = theano.function([], x_using_valid, mode='FAST_RUN')
                 f_full = theano.function([], x_using_full, mode=self.mode)
                 unittest_tools.assert_allclose(f_valid(), f_full())
Exemple #50
0
def _maxrandpool(input, ds, p, ignore_border=False):
    """ provide random pooling among the top 'p' sorted outputs p = 0 is maxpool """
    rng = numpy.random.RandomState(24546)
    out_shp = (input.shape[0], input.shape[1], input.shape[2] / ds[0],
               input.shape[3] / ds[1])
    srng = RandomStreams(rng.randint(2147462579))
    pos = srng.random_integers(size=(1, 1),
                               low=ds[0] * ds[1] - 1 - p,
                               high=ds[0] * ds[1] - 1)
    neib = images2neibs(
        input,
        neib_shape=ds,
        mode='valid' if ignore_border is False else 'ignore_borders')
    neib = neib.sort(axis=-1)
    pooled_vectors = neib[:, pos]
    return T.reshape(pooled_vectors, out_shp, ndim=4)
def main():
    '''
    Read here all images(grayscale) from jaffe folder
    into an numpy array Ims with size (no_images, height, width).
    Make sure the images are read after sorting the filenames
    '''

    path='/home/afenichel14/jaffe/'
    img_list=os.listdir(path)
    img_list.sort()
    im=np.asarray([np.array(Image.open(path+i)) for i in img_list])
    no_images, height, width=im.shape
    im=im.reshape(1,no_images,height,width)


    szs = [16, 32, 64]
    num_coeffs = [range(1, 10, 1), range(3, 30, 3), range(5, 50, 5)]

    for sz, nc in zip(szs, num_coeffs):
        '''
        Divide here each image into non-overlapping blocks of shape (sz, sz).
        Flatten each block and arrange all the blocks in a
        (no_images*n_blocks_in_image) x (sz*sz) matrix called X
        '''
 
        images=T.tensor4('images')
        neibs=images2neibs(images,neib_shape=(sz,sz), neib_step=(sz,sz))
        window_function=theano.function([images], neibs)
        X=window_function(im)

        X_mn = np.mean(X, 0)
        X = X - np.repeat(X_mn.reshape(1, -1), X.shape[0], 0)

        '''
        Perform eigendecomposition on X^T X and arrange the eigenvectors
        in decreasing order of eigenvalues into a matrix D
        '''
        w,v=np.linalg.eigh(np.dot(X.T,X))
        eigindex=np.argsort(w)[::-1]
        D=v[:,eigindex]
        c = np.dot(D.T, X.T)

        for i in range(0, 200, 10):
            plot_mul(c, D, i, X_mn.reshape((sz, sz)),
                     num_coeffs=nc, n_blocks=int(256/sz))

        plot_top_16(D, sz, imname='output/hw1a_top16_{0}.png'.format(sz))
Exemple #52
0
    def test_neibs_manual(self):
        shape = (2, 3, 4, 4)
        for dtype in self.dtypes:
            images = shared(
                    numpy.arange(numpy.prod(shape), dtype=dtype
                    ).reshape(shape))
            neib_shape = T.as_tensor_variable((2, 2))

            for border in ['valid', 'ignore_borders']:
                f = function([], images2neibs(images, neib_shape, mode=border),
                             mode=self.mode)
                assert any([isinstance(node.op, self.op)
                            for node in f.maker.fgraph.toposort()])

                # print images.get_value(borrow=True)
                neibs = f()
                # print neibs
                assert numpy.allclose(neibs,
                   [[ 0,  1,  4,  5],
                   [ 2,  3,  6,  7],
                   [ 8,  9, 12, 13],
                   [10, 11, 14, 15],
                   [16, 17, 20, 21],
                   [18, 19, 22, 23],
                   [24, 25, 28, 29],
                   [26, 27, 30, 31],
                   [32, 33, 36, 37],
                   [34, 35, 38, 39],
                   [40, 41, 44, 45],
                   [42, 43, 46, 47],
                   [48, 49, 52, 53],
                   [50, 51, 54, 55],
                   [56, 57, 60, 61],
                   [58, 59, 62, 63],
                   [64, 65, 68, 69],
                   [66, 67, 70, 71],
                   [72, 73, 76, 77],
                   [74, 75, 78, 79],
                   [80, 81, 84, 85],
                   [82, 83, 86, 87],
                   [88, 89, 92, 93],
                   [90, 91, 94, 95]])
                g = function([], neibs2images(neibs, neib_shape, images.shape),
                             mode=self.mode)

                assert numpy.allclose(images.get_value(borrow=True), g())
Exemple #53
0
    def __init__(self, rng, input, input_shape, pool_size):

        #initialize the weights 2.4 is magic from LeCun
        #multiply before to counteract the mean that is calculated later on(we want the sum)
        fan_in = numpy.prod(pool_size)
        W_bound = 4 * 2.4 / fan_in

        self.W = theano.shared(
            numpy.asarray(
                rng.uniform(low = -W_bound,
                            high = W_bound,
                            size = input_shape[1]),
                dtype = theano.config.floatX),
            borrow = True
        )
        

        #the bias weights
        b_values = numpy.zeros((input_shape[1],), dtype=theano.config.floatX)
        self.b = theano.shared(value=b_values, borrow=True)

        subsample_out = downsample.max_pool_2d(
            input = input,
            ds = pool_size,
            ignore_border = True
        )

        pool_sum = neighbours.images2neibs(
            ten4 = input,
            neib_shape = pool_size
        )
        pool_mean = pool_sum.mean(axis=-1)
        pool_out = pool_mean.reshape([input.shape[0], input.shape[1], input.shape[2]/2, input.shape[3]/2])

        #amplitude of activation function tanh. Magic from lecun
        amp = 1.7195
        #slope of activation function tanh at 0. Magic from lecun
        slope = 2.0 / 3.0
        #TODO need to elementwise multiply by shared weights W here
        #a = subsample_out.prod(self.W.dimshuffle('x',0,'x','x')) + self.b.dimshuffle('x', 0, 'x', 'x')
        a = subsample_out + self.b.dimshuffle('x', 0, 'x', 'x')
        self.output = amp * T.tanh(slope * a)
        
        #TODO add self.W when above is solved
        self.params = [self.b]
def get_single_deconv_out(input, filter):

    # reshape inputs
    img = input.reshape((1, 1, input.shape[0], input.shape[1]))
    kernel = filter.reshape((1, K.prod(filter.shape)))

    # construct split function
    # image = T.tensor4("image")
    neibs = images2neibs(img, neib_shape=filter.shape, neib_step=filter.shape)
    # window_function = theano.function([image], neibs)
    #
    # neibs_val = window_function(img_val)

    neibs = neibs*kernel

    # construct merge function
    img_new = neibs2images(neibs, filter.shape, img.shape)

    return img_new[0][0]
Exemple #55
0
    def compute_output(self, network, in_vw):
        # hyperparameters
        pool_fn = network.find_hyperparameter(["pool_function"])
        pool_size = network.find_hyperparameter(["pool_size"])
        stride = network.find_hyperparameter(["pool_stride",
                                              "stride"],
                                             pool_size)
        # TODO parameterize
        # ---
        # TODO assumes pooling across axis 2 and 3
        pooling_axes = (2, 3)
        # maybe have a bool (ignore_borders=False) instead of a string
        pool_mode = "valid"
        pads = (0, 0)

        # calculate shapes
        shape_kwargs = dict(
            axes=pooling_axes,
            pool_shape=pool_size,
            strides=stride,
            pads=pads,
        )
        out_shape = pool_output_shape(
            input_shape=in_vw.shape,
            **shape_kwargs)
        symbolic_out_shape = pool_output_shape(
            input_shape=in_vw.symbolic_shape(),
            **shape_kwargs)

        # compute output
        neibs = images2neibs(ten4=in_vw.variable,
                             neib_shape=pool_size,
                             neib_step=stride,
                             mode=pool_mode)
        feats = pool_fn(neibs, axis=1)
        out_var = feats.reshape(symbolic_out_shape)

        network.create_vw(
            "default",
            variable=out_var,
            shape=out_shape,
            tags={"output"},
        )
Exemple #56
0
    def test_neibs_manual_step(self):
        shape = (2, 3, 5, 5)
        for dtype in self.dtypes:
            images = shared(numpy.asarray(numpy.arange(numpy.prod(
                            shape)).reshape(shape), dtype=dtype))
            neib_shape = T.as_tensor_variable((3, 3))
            neib_step = T.as_tensor_variable((2, 2))
            for border in ['valid', 'ignore_borders']:
                f = function([],
                             images2neibs(images, neib_shape, neib_step,
                                          mode=border),
                             mode=self.mode)

                neibs = f()
                assert self.op in [type(node.op)
                                   for node in f.maker.fgraph.toposort()]

                assert numpy.allclose(neibs, [
                    [0, 1, 2, 5, 6, 7, 10, 11, 12],
                    [2, 3, 4, 7, 8, 9, 12, 13, 14],
                    [10, 11, 12, 15, 16, 17, 20, 21, 22],
                    [12, 13, 14, 17, 18, 19, 22, 23, 24],
                    [25, 26, 27, 30, 31, 32, 35, 36, 37],
                    [27, 28, 29, 32, 33, 34, 37, 38, 39],
                    [35, 36, 37, 40, 41, 42, 45, 46, 47],
                    [37, 38, 39, 42, 43, 44, 47, 48, 49],
                    [50, 51, 52, 55, 56, 57, 60, 61, 62],
                    [52, 53, 54, 57, 58, 59, 62, 63, 64],
                    [60, 61, 62, 65, 66, 67, 70, 71, 72],
                    [62, 63, 64, 67, 68, 69, 72, 73, 74],
                    [75, 76, 77, 80, 81, 82, 85, 86, 87],
                    [77, 78, 79, 82, 83, 84, 87, 88, 89],
                    [85, 86, 87, 90, 91, 92, 95, 96, 97],
                    [87, 88, 89, 92, 93, 94, 97, 98, 99],
                    [100, 101, 102, 105, 106, 107, 110, 111, 112],
                    [102, 103, 104, 107, 108, 109, 112, 113, 114],
                    [110, 111, 112, 115, 116, 117, 120, 121, 122],
                    [112, 113, 114, 117, 118, 119, 122, 123, 124],
                    [125, 126, 127, 130, 131, 132, 135, 136, 137],
                    [127, 128, 129, 132, 133, 134, 137, 138, 139],
                    [135, 136, 137, 140, 141, 142, 145, 146, 147],
                    [137, 138, 139, 142, 143, 144, 147, 148, 149]])
def plot(c, D, n_blocks, X_mn, ax):
    '''
    Plots a reconstruction of a particular image using D as the basis matrix and coeffiecient
    vectors from c

    Parameters
    ------------------------
        c: np.ndarray
            a l x m matrix  representing the coefficients of all blocks in a particular image
            l represents the dimension of the PCA space used for reconstruction
            m represents the number of blocks in an image

        D: np.ndarray
            an N x l matrix representing l basis vectors of the PCA space
            N is the dimension of the original space (number of pixels in a block)

        n_blocks: Integer
            number of blocks comprising the image in each direction.
            For example, for a 256x256 image divided into 64x64 blocks, n_blocks will be 4

        X_mn: basis vectors represent the divergence from the mean so this
            matrix should be added to all reconstructed blocks

        ax: the axis on which the image will be plotted
    '''

    Y = np.dot(c.T, D.T)
    Y  = Y + np.repeat(X_mn.reshape(1, -1), Y.shape[0], 0)

    sz = 256/n_blocks
    images = T.tensor4('images')
    neibs = images2neibs(images, neib_shape=(sz, sz))

    im_new = neibs2images(neibs, (sz, sz), (256,256))
    # Theano function definition
    inv_window = theano.function([neibs], im_new)
    # Function application
    imag = inv_window(Y)

    pyplot.subplot(ax)
    pyplot.imshow(imag, cmap = cm.Greys_r)
def get_deconv_out_new(inputs, filters):
    inputs_shape = inputs.shape
    filters_shape = filters.shape
    # img = K.reshape(inputs, (inputs_shape[0], 1, inputs_shape[1], inputs_shape[2]))
    kernel = K.reshape(filters, (filters_shape[0], 1, filters_shape[1]*filters_shape[2]))
    # def fn(i, k):
    #     i = K.reshape(i, (1, 1, i.shape[0], i.shape[1]))
    #     neibs = images2neibs(i, neib_shape=(filters_shape[-2], filters_shape[-1]),
    #                      neib_step=(filters_shape[-2], filters_shape[-1]))
    #     neibs = neibs*k
    #     return neibs
    # results,_ = theano.scan(fn=fn, sequences=inputs, non_sequences=kernel)

    neibs = images2neibs(inputs, neib_shape=(filters_shape[1], filters_shape[2]),
                         neib_step=(filters_shape[1], filters_shape[2]))

    def fn(k, n):
        return n*k
    results,_ = theano.scan(fn=fn, sequences=kernel, non_sequences=neibs)
    # results = neibs * kernel

    # results_shape = results.shape
    return results
Exemple #59
0
 def fn(images):
     return T.sum(T.sqr(images2neibs(images, (2, 2), mode='valid')),
                  axis=[0, 1])