def test_HealpyMonomial(): # 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 = 0.1 initializer = tf.initializers.RandomNormal(stddev=stddev, seed=13) mon = healpy_layers.HealpyMonomial(Fout=Fout, K=K, initializer=initializer, activation=tf.keras.activations.linear) mon = mon._get_layer(L) new = mon(x) mon = healpy_layers.HealpyMonomial(Fout=Fout, K=K, initializer=initializer, activation=tf.keras.activations.linear, use_bias=True, use_bn=True) mon = mon._get_layer(L) new = mon(x)
def test_HealpyMonomial(): # this is the result from Deepsphere with tf 1.x result = np.array([[[0.04206353, 0.46168754, 0.10546149], [-0.5492798, -0.32608002, 0.5628096], [-0.11329696, -0.7900159, 0.92530084]], [[0.06915615, 0.03369189, 0.0245935], [-0.89208144, -0.11626951, -0.10967396], [0.01909873, -0.16593638, -0.1462554]], [[-0.29119226, -0.12377091, -0.0128078], [0.36727118, 0.30154356, -0.02591037], [-0.23363924, -0.14655769, 0.3258103]], [[0.00471622, -0.03371258, 0.00214787], [0.31400114, -0.57628125, 1.5108933], [0.09324764, -0.75300777, 0.40933472]], [[0.12954447, 0.06049673, 0.15058015], [0.38768154, -0.24916826, 0.43720144], [-0.1512235, 0.01706326, 0.14433491]]], 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 = 0.1 initializer = tf.initializers.RandomNormal(stddev=stddev, seed=13) mon = healpy_layers.HealpyMonomial(Fout=Fout, K=K, initializer=initializer, activation=tf.keras.activations.linear) mon = mon._get_layer(L) new = mon(x) assert np.all(np.abs(new.numpy() - result) < 1e-5) mon = healpy_layers.HealpyMonomial(Fout=Fout, K=K, initializer=initializer, activation=tf.keras.activations.linear, use_bias=True, use_bn=True) mon = mon._get_layer(L) new = mon(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)