def test_HealpyChebyshev(): # create the layer tf.random.set_seed(11) L = tf.random.normal(shape=(3, 3), seed=11) # make sym L = tf.matmul(L, tf.transpose(L)) x = tf.random.normal(shape=(5, 3, 7), seed=12) Fout = 3 K = 4 # create the layer stddev = 1 / np.sqrt(7 * (K + 0.5) / 2) initializer = tf.initializers.RandomNormal(stddev=stddev, seed=13) cheb = healpy_layers.HealpyChebyshev(Fout=Fout, K=K, initializer=initializer) cheb = cheb._get_layer(L) new = cheb(x) cheb = healpy_layers.HealpyChebyshev(Fout=Fout, K=K, initializer=initializer, use_bn=True, use_bias=True) cheb = cheb._get_layer(L) new = cheb(x)
def HealpyChebyshev(self, current_nside, current_indices, K, Fout, initializer, activation, use_bias, use_bn): """ Input: current_nside current_indices + :param K: Order of the polynomial to use :param Fout: Number of features (channels) of the output, default to number of input channels :param initializer: initializer to use for weight initialisation :param activation: the activation function to use after the layer, defaults to linear :param use_bias: Use learnable bias weights :param use_bn: Apply batch norm before adding the bias """ sphere = SphereHealpix(subdivisions=current_nside, indexes=current_indices, nest=True, k=K, lap_type='normalized') current_L = sphere.L layer = hp_layer.HealpyChebyshev(K, Fout, initializer, activation, use_bias, use_bn) actual_layer = layer._get_layer(current_L) return actual_layer
def test_HealpyChebyshev(): # this is the result from Deepsphere with tf 1.x result = np.array([[[-1.0755178, -0.38834727, -1.8771361], [-0.717345, -0.05060194, 0.5165049], [-0.26436844, -1.9551289, 1.5731683]], [[0.20308694, 0.18807065, 0.5316967], [-0.15023257, 0.5063435, -2.1237612], [0.8385707, -0.05848193, -0.99261296]], [[-0.72604334, -1.1806095, -1.2536838], [0.94199276, -0.45402163, -0.37584513], [-0.5897862, -0.99283355, 0.39791816]], [[0.35315517, -0.69710857, 0.89600056], [-1.2446954, 0.17451617, 2.5738995], [-0.48410773, -1.3228154, 0.73148715]], [[-0.47506812, 0.07938811, -0.7293904], [0.9361354, 0.5696295, 1.4792094], [-0.5101731, 0.43999135, 0.34349984]]], dtype=np.float32) # create the layer tf.random.set_seed(11) L = tf.random.normal(shape=(3, 3), seed=11) # make sym L = tf.matmul(L, tf.transpose(L)) x = tf.random.normal(shape=(5, 3, 7), seed=12) Fout = 3 K = 4 # create the layer stddev = 1 / np.sqrt(7 * (K + 0.5) / 2) initializer = tf.initializers.RandomNormal(stddev=stddev, seed=13) cheb = healpy_layers.HealpyChebyshev(Fout=Fout, K=K, initializer=initializer) cheb = cheb._get_layer(L) new = cheb(x) assert np.all(np.abs(new.numpy() - result) < 1e-5) cheb = healpy_layers.HealpyChebyshev(Fout=Fout, K=K, initializer=initializer, use_bn=True, use_bias=True) cheb = cheb._get_layer(L) new = cheb(x)
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)