Exemple #1
0
def test_HealpyPseudoConv_Transpose():
    # we get a random map to conv
    n_pix = hp.nside2npix(8)
    np.random.seed(11)
    m_in = np.random.normal(size=n_pix)

    # layer
    hp_conv = healpy_layers.HealpyPseudoConv_Transpose(3, 5)
    m_conv_tf = hp_conv(m_in[None, :, None])

    assert m_conv_tf.numpy().shape == (1, n_pix * int(4**3), 5)
 def HealpyPseudoConv_Transpose(self, current_nside, current_indices, p,
                                Fout, kernel_initializer):
     """
     :param p: Boost factor >=1 of the nside -> number of nodes increases by 4^p, note that the layer only checks if the dimensionality of the input is evenly divisible by 4^p and not if the ordering is correct (should be nested ordering)
     :param Fout: number of output channels
     :param kernel_initializer: initializer for kernel init
     """
     layer = hp_layer.HealpyPseudoConv_Transpose(p, Fout,
                                                 kernel_initializer)
     new_nside = int(current_nside * 2**layer.p)
     self.current_indices = self._transform_indices(nside_in=current_nside,
                                                    nside_out=new_nside,
                                                    indices=current_indices)
     self.current_nside = new_nside
     return layer
def test_HealpyGCNN():
    # clear session
    tf.keras.backend.clear_session()

    # we get a random map
    nside_in = 256
    n_pix = hp.nside2npix(nside_in)
    np.random.seed(11)
    m_in = np.random.normal(size=[3, n_pix, 1]).astype(np.float32)
    indices = np.arange(n_pix)


    # define some layers
    layers = [hp_nn.HealpyPseudoConv(p=1, Fout=4),
              hp_nn.HealpyPool(p=1),
              hp_nn.HealpyChebyshev(K=5, Fout=8),
              hp_nn.HealpyPseudoConv(p=2, Fout=16),
              hp_nn.HealpyPseudoConv_Transpose(p=2, Fout=16),
              hp_nn.HealpyPseudoConv(p=2, Fout=16),
              hp_nn.HealpyMonomial(K=5, Fout=32),
              hp_nn.Healpy_ResidualLayer("CHEBY", layer_kwargs={"K": 5}),
              tf.keras.layers.Flatten(),
              tf.keras.layers.Dense(4)]

    tf.random.set_seed(11)
    model = HealpyGCNN(nside=nside_in, indices=indices, layers=layers)
    model.build(input_shape=(3, n_pix, 1))
    model.summary(line_length=128)

    out = model(m_in)

    assert out.numpy().shape == (3,4)

    # now we check if we can save this
    with tempfile.TemporaryDirectory() as tempdir:
        # save the current weight
        model.save_weights(tempdir)

        # create new model
        tf.random.set_seed(12)
        model = HealpyGCNN(nside=nside_in, indices=indices, layers=layers)
        model.build(input_shape=(3, n_pix, 1))
        out_new = model(m_in)

        # output should be different
        assert not np.all(np.isclose(out.numpy(), out_new.numpy()))

        # restore weights
        model.load_weights(tempdir)

        # now it should be the same
        out_new = model(m_in)
        assert np.all(np.isclose(out.numpy(), out_new.numpy(), atol=1e-6))

    # test the use 4 graphing
    with pytest.raises(NotImplementedError):
        model = HealpyGCNN(nside=nside_in, indices=indices, layers=layers, n_neighbors=12)

    # more channels
    tf.keras.backend.clear_session()

    # we get a random map
    nside_in = 256
    n_pix = hp.nside2npix(nside_in)
    np.random.seed(11)
    m_in = np.random.normal(size=[3, n_pix, 2]).astype(np.float32)
    indices = np.arange(n_pix)

    # define some layers
    layers = [hp_nn.HealpyPseudoConv(p=1, Fout=4),
              hp_nn.HealpyPool(p=1),
              hp_nn.HealpyChebyshev(K=5, Fout=8),
              hp_nn.HealpyPseudoConv(p=2, Fout=16),
              hp_nn.HealpyPseudoConv_Transpose(p=2, Fout=16),
              hp_nn.HealpyPseudoConv(p=2, Fout=16),
              hp_nn.HealpyMonomial(K=5, Fout=32),
              hp_nn.Healpy_ResidualLayer("CHEBY", layer_kwargs={"K": 5}),
              tf.keras.layers.Flatten(),
              tf.keras.layers.Dense(4)]

    tf.random.set_seed(11)
    model = HealpyGCNN(nside=nside_in, indices=indices, layers=layers)
    model.build(input_shape=(3, n_pix, 2))
    model.summary(line_length=128)

    out = model(m_in)

    assert out.numpy().shape == (3, 4)