Пример #1
0
def test_HealpyPool():
    # we get a random map to pool
    n_pix = hp.nside2npix(4)
    np.random.seed(11)
    m_in = np.random.normal(size=n_pix)

    # check exception
    with pytest.raises(IOError):
        avg_layer = healpy_layers.HealpyPool(0, pool_type="MAX")
    with pytest.raises(IOError):
        avg_layer = healpy_layers.HealpyPool(2, pool_type="HUHU")

    # we pool with healpy
    m_avg = hp.ud_grade(map_in=m_in,
                        nside_out=2,
                        order_in="NEST",
                        order_out="NEST",
                        power=None)

    # avg layer
    avg_layer = healpy_layers.HealpyPool(1, pool_type="AVG")
    m_avg_tf = avg_layer(m_in[None, :, None])

    assert np.all(np.abs(m_avg - m_avg_tf.numpy().ravel()) < 1e-5)

    # maxpool normal
    m_max = np.max(m_in.reshape((n_pix // 4, 4)), axis=1)

    # max layer
    max_layer = healpy_layers.HealpyPool(1, pool_type="MAX")
    m_max_tf = max_layer(m_in[None, :, None])

    assert np.all(np.abs(m_max - m_max_tf.numpy().ravel()) < 1e-5)
Пример #2
0
 def HealpyPool(self, current_nside, current_indices, p, pool):
     """
     Input:
     current_nside
     current_indices
     +
     p         reduction factor >=1 of the nside -> number of nodes reduces 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).
     pool      type of pooling, can be "MAX" or  "AVG"
     
     Layer Parameters: (That have been set)
     None        
     """
     layer = hp_layer.HealpyPool(p, pool)
     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)
def test_HealpyGCNN_plotting():
    # create dir for plots
    os.makedirs("./tests/test_plots", exist_ok=True)

    # 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.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()

    with pytest.raises(ValueError):
        filters1 = model.get_gsp_filters(3)

    # get some filters
    filters1 = model.get_gsp_filters("chebyshev")
    filters2 = model.get_gsp_filters("gcnn__residual_layer")

    # plot some filters (coeff)
    ax = model.plot_chebyshev_coeffs("chebyshev")
    base_path, _ = os.path.split(__file__)
    plt.savefig(os.path.join(base_path, "test_plots/plot_chebyshev_coeffs_cheby5.png"))
    plt.clf()
    ax = model.plot_chebyshev_coeffs("gcnn__residual_layer")
    plt.savefig(os.path.join(base_path, "test_plots/plot_chebyshev_coeffs_res.png"))
    plt.clf()

    # plot some filters (spectral)
    ax = model.plot_filters_spectral("chebyshev")
    plt.savefig(os.path.join(base_path, "test_plots/plot_filters_spectral_cheby5.png"))
    plt.clf()
    ax = model.plot_filters_spectral("gcnn__residual_layer")
    plt.savefig(os.path.join(base_path, "test_plots/plot_filters_spectral_res.png"))
    plt.clf()

    # plot some filters (section)
    figs = model.plot_filters_section("chebyshev", ind_in=[0], ind_out=[0])
    figs[0].savefig(os.path.join(base_path, "test_plots/plot_filters_section_cheby5.png"))
    plt.clf()
    figs = model.plot_filters_section("gcnn__residual_layer", ind_in=[0], ind_out=[0])
    figs[0].savefig(os.path.join(base_path, "test_plots/plot_filters_section_res_1.png"))
    plt.clf()

    # plot some filters (gnomonic)
    figs = model.plot_filters_gnomonic("chebyshev", ind_in=[0], ind_out=[0])
    figs[0].savefig(os.path.join(base_path, "test_plots/plot_filters_gnomonic_cheby5.png"))
    plt.clf()
    figs = model.plot_filters_gnomonic("gcnn__residual_layer", ind_in=[0,1,2], ind_out=[0])
    figs[0].savefig(os.path.join(base_path, "test_plots/plot_filters_gnomonic_res_1.png"))
    plt.clf()

    # get the output
    out = model(m_in)

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