Skip to content

Commit

Permalink
switch more tf to keras
Browse files Browse the repository at this point in the history
  • Loading branch information
henrysky committed Jan 3, 2024
1 parent ff27c10 commit 7c5e612
Show file tree
Hide file tree
Showing 13 changed files with 411 additions and 802 deletions.
4 changes: 2 additions & 2 deletions astroNN/nn/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def __call__(self, model):
self.model = model
else:
raise TypeError(
f"FastMCInference expects tensorflow.keras Model, you gave {type(model)}"
f"FastMCInference expects keras Model, you gave {type(model)}"
)
new_input = keras.layers.Input(shape=(self.model.input_shape[1:]), name="input")
mc_model = keras.models.Model(
Expand Down Expand Up @@ -339,7 +339,7 @@ def __init__(self, model, n=100, **kwargs):
self.n = n
else:
raise TypeError(
f"FastMCInference expects tensorflow.keras Model, you gave {type(model)}"
f"FastMCInference expects keras Model, you gave {type(model)}"
)

super(FastMCInferenceV2_internal, self).__init__(model, **kwargs)
Expand Down
594 changes: 357 additions & 237 deletions astroNN/nn/losses.py

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions astroNN/nn/metrics.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# ---------------------------------------------------------------#
# astroNN.nn.metrics: metrics
# ---------------------------------------------------------------#
import tensorflow as tf

from astroNN.nn.losses import binary_accuracy, binary_accuracy_from_logits
from astroNN.nn.losses import categorical_accuracy
from astroNN.nn.losses import mean_absolute_error
Expand Down
2 changes: 1 addition & 1 deletion astroNN/shared/nn_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def cpu_fallback(flag=True):
"""
A function to force Tensorflow to use CPU even Nvidia GPU present
A function to force Keras backend to use CPU even Nvidia GPU is presented
:param flag: `True` to fallback to CPU, `False` to un-manage CPU or GPU
:type flag: bool
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ h5py
matplotlib
astroquery
pandas
torch
tensorflow-cpu
tensorflow-probability
scipy
scikit-learn
tqdm
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'astroNN', 'astroNN Documentation',
author, 'astroNN', 'Deep Learning for Astronomers with Tensorflow',
author, 'astroNN', 'Deep Learning for Astronomers with Keras',
'Miscellaneous'),
]

Expand Down
2 changes: 0 additions & 2 deletions tests/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
magicnumber = -9999.0
multiprocessing_generator = False
environmentvariablewarning = True
tensorflow_keras = auto

[NeuralNet]
custommodelpath = custom_models.py
cpufallback = False
gpu_mem_ratio = True
6 changes: 3 additions & 3 deletions tests/test_apogee_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from astroNN.nn.callbacks import ErrorOnNaN
from astroNN.shared.downloader_tools import TqdmUpTo

import keras as tfk
mnist = tfk.datasets.mnist
utils = tfk.utils
import keras
mnist = keras.datasets.mnist
utils = keras.utils

_URL_ORIGIN = "https://www.astro.utoronto.ca/~hleung/shared/ci_data/"
filename = "apogee_dr14_green.h5"
Expand Down
60 changes: 30 additions & 30 deletions tests/test_loss_func.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import unittest

import keras
import numpy as np
import numpy.testing as npt
import tensorflow as tf

from astroNN.shared.nn_tools import cpu_fallback

Expand Down Expand Up @@ -36,16 +36,16 @@
class LossFuncTestCase(unittest.TestCase):
def test_loss_magic(self):
# =============Magic correction term============= #
y_true = tf.constant(
y_true = keras.backend.numpy.array(
[[2.0, MAGIC_NUMBER, MAGIC_NUMBER], [2.0, MAGIC_NUMBER, 4.0]]
)
npt.assert_array_equal(magic_correction_term(y_true).numpy(), [3.0, 1.5])

def test_loss_mse(self):
# =============MSE/MAE============= #
y_pred = tf.constant([[2.0, 3.0, 4.0], [2.0, 3.0, 7.0]])
y_pred_2 = tf.constant([[2.0, 9.0, 4.0], [2.0, 0.0, 7.0]])
y_true = tf.constant([[2.0, MAGIC_NUMBER, 4.0], [2.0, MAGIC_NUMBER, 4.0]])
y_pred = keras.backend.numpy.array([[2.0, 3.0, 4.0], [2.0, 3.0, 7.0]])
y_pred_2 = keras.backend.numpy.array([[2.0, 9.0, 4.0], [2.0, 0.0, 7.0]])
y_true = keras.backend.numpy.array([[2.0, MAGIC_NUMBER, 4.0], [2.0, MAGIC_NUMBER, 4.0]])

npt.assert_almost_equal(
mean_absolute_error(y_true, y_pred).numpy(), [0.0, 3.0 / 2.0]
Expand All @@ -66,15 +66,15 @@ def test_loss_mse(self):

def test_loss_mean_err(self):
# =============Mean Error============= #
y_pred = tf.constant([[1.0, 3.0, 4.0], [2.0, 3.0, 7.0]])
y_true = tf.constant([[2.0, MAGIC_NUMBER, 3.0], [2.0, MAGIC_NUMBER, 7.0]])
y_pred = keras.backend.numpy.array([[1.0, 3.0, 4.0], [2.0, 3.0, 7.0]])
y_true = keras.backend.numpy.array([[2.0, MAGIC_NUMBER, 3.0], [2.0, MAGIC_NUMBER, 7.0]])

npt.assert_almost_equal(mean_error(y_true, y_pred).numpy(), [0.0, 0.0])

def test_loss_acurrancy(self):
# =============Accuracy============= #
y_pred = tf.constant([[1.0, 0.0, 0.0], [1.0, 0.0, 0.0]])
y_true = tf.constant([[1.0, MAGIC_NUMBER, 1.0], [0.0, MAGIC_NUMBER, 1.0]])
y_pred = keras.backend.numpy.array([[1.0, 0.0, 0.0], [1.0, 0.0, 0.0]])
y_true = keras.backend.numpy.array([[1.0, MAGIC_NUMBER, 1.0], [0.0, MAGIC_NUMBER, 1.0]])

npt.assert_array_equal(categorical_accuracy(y_true, y_pred).numpy(), [1.0, 0.0])
npt.assert_almost_equal(
Expand All @@ -83,9 +83,9 @@ def test_loss_acurrancy(self):

def test_loss_abs_error(self):
# =============Abs Percentage Accuracy============= #
y_pred = tf.constant([[1.0, 0.0, 0.0], [1.0, 0.0, 0.0]])
y_pred_2 = tf.constant([[1.0, 9.0, 0.0], [1.0, -1.0, 0.0]])
y_true = tf.constant([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 1.0]])
y_pred = keras.backend.numpy.array([[1.0, 0.0, 0.0], [1.0, 0.0, 0.0]])
y_pred_2 = keras.backend.numpy.array([[1.0, 9.0, 0.0], [1.0, -1.0, 0.0]])
y_true = keras.backend.numpy.array([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 1.0]])

npt.assert_array_almost_equal(
mean_absolute_percentage_error(y_true, y_pred).numpy(),
Expand All @@ -101,9 +101,9 @@ def test_loss_abs_error(self):

def test_loss_percentage_error(self):
# =============Percentage Accuracy============= #
y_pred = tf.constant([[1.0, 0.0, 0.0], [1.0, 0.0, 0.0]])
y_pred_2 = tf.constant([[1.0, 9.0, 0.0], [1.0, -1.0, 0.0]])
y_true = tf.constant([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 1.0]])
y_pred = keras.backend.numpy.array([[1.0, 0.0, 0.0], [1.0, 0.0, 0.0]])
y_pred_2 = keras.backend.numpy.array([[1.0, 9.0, 0.0], [1.0, -1.0, 0.0]])
y_true = keras.backend.numpy.array([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 1.0]])

npt.assert_array_almost_equal(
mean_percentage_error(y_true, y_pred).numpy(), [50.0, 50.0], decimal=3
Expand All @@ -117,9 +117,9 @@ def test_loss_percentage_error(self):

def test_loss_log_error(self):
# =============Mean Squared Log Error============= #
y_pred = tf.constant([[1.0, 0.0, 0.0], [1.0, 0.0, 0.0]])
y_pred_2 = tf.constant([[1.0, 9.0, 0.0], [1.0, -1.0, 0.0]])
y_true = tf.constant([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 1.0]])
y_pred = keras.backend.numpy.array([[1.0, 0.0, 0.0], [1.0, 0.0, 0.0]])
y_pred_2 = keras.backend.numpy.array([[1.0, 9.0, 0.0], [1.0, -1.0, 0.0]])
y_true = keras.backend.numpy.array([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 1.0]])

npt.assert_array_almost_equal(
mean_squared_logarithmic_error(y_true, y_pred).numpy(),
Expand All @@ -135,15 +135,15 @@ def test_loss_log_error(self):

def test_loss_zeros(self):
# =============Zeros Loss============= #
y_pred = tf.constant([[1.0, 0.0, 0.0], [5.0, -9.0, 2.0]])
y_true = tf.constant([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 1.0]])
y_pred = keras.backend.numpy.array([[1.0, 0.0, 0.0], [5.0, -9.0, 2.0]])
y_true = keras.backend.numpy.array([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 1.0]])

npt.assert_array_almost_equal(zeros_loss(y_true, y_pred).numpy(), [0.0, 0.0])

def test_categorical_crossentropy(self):
# Truth with Magic number is wrong
y_pred = tf.constant([[1.0, 0.0, 1.0], [2.0, 1.0, 0.0]])
y_true = tf.constant([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 0.0]])
y_pred = keras.backend.numpy.array([[1.0, 0.0, 1.0], [2.0, 1.0, 0.0]])
y_true = keras.backend.numpy.array([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 0.0]])

y_pred_softmax = tf.nn.softmax(y_pred)

Expand All @@ -154,9 +154,9 @@ def test_categorical_crossentropy(self):
)

def test_binary_crossentropy(self):
y_pred = tf.constant([[0.5, 0.0, 1.0], [2.0, 0.0, -1.0]])
y_pred_2 = tf.constant([[0.5, 2.0, 1.0], [2.0, 2.0, -1.0]])
y_true = tf.constant([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 0.0]])
y_pred = keras.backend.numpy.array([[0.5, 0.0, 1.0], [2.0, 0.0, -1.0]])
y_pred_2 = keras.backend.numpy.array([[0.5, 2.0, 1.0], [2.0, 2.0, -1.0]])
y_true = keras.backend.numpy.array([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 0.0]])
y_pred_sigmoid = tf.nn.sigmoid(y_pred)
y_pred_2_sigmoid = tf.nn.sigmoid(y_pred_2)

Expand All @@ -179,15 +179,15 @@ def test_binary_crossentropy(self):
)

def test_negative_log_likelihood(self):
y_pred = tf.constant([[0.5, 0.0, 1.0], [2.0, 0.0, -1.0]])
y_true = tf.constant([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 0.0]])
y_pred = keras.backend.numpy.array([[0.5, 0.0, 1.0], [2.0, 0.0, -1.0]])
y_true = keras.backend.numpy.array([[1.0, MAGIC_NUMBER, 1.0], [1.0, MAGIC_NUMBER, 0.0]])

npt.assert_array_almost_equal(
nll(y_true, y_pred).numpy(), 0.34657377, decimal=3
)

def test_median(self):
y_pred = tf.constant([[1., 2., 3., 4., 5.], [1., 2., 3., 4., 5.]])
y_pred = keras.backend.numpy.array([[1., 2., 3., 4., 5.], [1., 2., 3., 4., 5.]])
npt.assert_array_almost_equal(median(y_pred), np.median(y_pred), decimal=3)
npt.assert_array_almost_equal(median(y_pred, axis=1), np.median(y_pred, axis=1), decimal=3)
npt.assert_array_almost_equal(median(y_pred, axis=0), np.median(y_pred, axis=0), decimal=3)
Expand All @@ -197,8 +197,8 @@ def test_mad_std(self):
self.assertEqual(np.round(mad_std(test_array, np.zeros_like(test_array), axis=None)), 1.)

def test_median_metrics(self):
y_pred = tf.constant([[1.0, 0.0, 0.0], [1.0, 0.0, 0.0]])
y_true = tf.constant([[1.0, 9.0, 0.0], [1.0, -1.0, 0.0]])
y_pred = keras.backend.numpy.array([[1.0, 0.0, 0.0], [1.0, 0.0, 0.0]])
y_true = keras.backend.numpy.array([[1.0, 9.0, 0.0], [1.0, -1.0, 0.0]])

npt.assert_array_almost_equal(median_error(y_true, y_pred, axis=None), np.median(y_true - y_pred), decimal=3)
npt.assert_array_almost_equal(median_absolute_deviation(y_true, y_pred, axis=None), np.median(np.abs(y_true - y_pred)), decimal=3)
Expand Down
30 changes: 6 additions & 24 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@
from importlib import import_module

import numpy as np
import keras as tfk
import keras

import astroNN
from astroNN.config import config_path
from astroNN.models import Cifar10CNN, Galaxy10CNN, MNIST_BCNN
from astroNN.models import load_folder
from astroNN.nn.callbacks import ErrorOnNaN

mnist = tfk.datasets.mnist
utils = tfk.utils

# Data preparation
(x_train, y_train), (x_test, y_test) = mnist.load_data()
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# To convert to desirable type
x_train = x_train.astype(np.float32)
x_test = x_test.astype(np.float32)
y_train = utils.to_categorical(y_train, 10)
y_train = keras.utils.to_categorical(y_train, 10)
y_train = y_train.astype(np.float32)
y_test = y_test.astype(np.float32)
x_train_color = np.stack([x_train, x_train, x_train], axis=-1)
Expand All @@ -41,7 +38,7 @@ def test_mnist(self):
pred = mnist_test.test(x_test)
test_num = y_test.shape[0]
assert (np.sum(np.argmax(pred, axis=1) == y_test)) / test_num > 0.9 # assert accurancy
mnist_test.evaluate(x_test, utils.to_categorical(y_test, 10))
mnist_test.evaluate(x_test, keras.utils.to_categorical(y_test, 10))

# create model instance for binary classification
mnist_test = Cifar10CNN()
Expand All @@ -54,7 +51,7 @@ def test_mnist(self):
mnist_test.save('mnist_test')
mnist_reloaded = load_folder("mnist_test")
prediction_loaded = mnist_reloaded.test(x_test)
eval_result = mnist_reloaded.evaluate(x_test, utils.to_categorical(y_test, 10))
eval_result = mnist_reloaded.evaluate(x_test, keras.utils.to_categorical(y_test, 10))

# Cifar10_CNN without dropout is deterministic
np.testing.assert_array_equal(prediction, prediction_loaded)
Expand Down Expand Up @@ -115,7 +112,7 @@ def test_bayesian_mnist(self):
net.save('mnist_bcnn_test')
net.plot_dense_stats()
plt.close() # Travis-CI memory error??
net.evaluate(x_test, utils.to_categorical(y_test, 10))
net.evaluate(x_test, keras.utils.to_categorical(y_test, 10))

pred, pred_err = net.test(x_test)
test_num = y_test.shape[0]
Expand Down Expand Up @@ -173,21 +170,6 @@ def test_custom_model(self):
sys.path.insert(0, head)
CustomModel_Test = getattr(import_module(tail.strip('.py')), str('CustomModel_Test'))

# disable due to travis error
# create model instance
# custom_model = CustomModel_Test()
# custom_model.max_epochs = 1
#
# custom_model.train(x_train[:200], y_train[:200])
#
# prediction = custom_model.test(x_test[:200])
# custom_model.save('custom_model_testing_folder')
#
# custom_model_loaded = load_folder("custom_model_testing_folder")
# prediction_loaded = custom_model_loaded.test(x_test[:200])
# # CustomModel_Test is deterministic
# np.testing.assert_array_equal(prediction, prediction_loaded)


class Models_TestCase6(unittest.TestCase):
def test_nomodel(self):
Expand Down
20 changes: 10 additions & 10 deletions tests/test_numpy_tools.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import unittest

import keras
import astropy.units as u
import numpy as np
import numpy.testing as npt
import tensorflow as tf

from astroNN.config import MAGIC_NUMBER
from astroNN.nn.numpy import mean_absolute_percentage_error, mean_absolute_error, median_absolute_error, \
Expand All @@ -14,17 +14,17 @@
# noinspection PyUnresolvedReferences
class MyTestCase(unittest.TestCase):
def test_sigmoid(self):
# make sure its the same as tensorflow
# make sure its the same as keras implementation
x = np.array([-1., 2., 3., 4.])
astroNN_x = sigmoid(x)
tf_x = tf.nn.sigmoid(x)
tf_x = keras.backend.nn.sigmoid(x)
npt.assert_array_almost_equal(tf_x.numpy(), astroNN_x)

# make sure identity transform
npt.assert_array_almost_equal(sigmoid_inv(sigmoid(x)), x)

# for list
# make sure its the same as tensorflow
# make sure its the same as keras implementation
x = [-1., 2., 3., 4.]
astroNN_x_list = sigmoid(x)
npt.assert_array_almost_equal(astroNN_x_list, astroNN_x)
Expand All @@ -33,7 +33,7 @@ def test_sigmoid(self):
npt.assert_array_almost_equal(sigmoid_inv(sigmoid(x)), x)

# for float
# make sure its the same as tensorflow
# make sure its the same as keras implementation
x = 0.
astroNN_x = sigmoid(x)
npt.assert_array_equal(0.5, astroNN_x)
Expand All @@ -42,10 +42,10 @@ def test_sigmoid(self):
npt.assert_array_almost_equal(sigmoid_inv(sigmoid(x)), x)

def test_relu(self):
# make sure its the same as tensorflow
# make sure its the same as keras implementation
x = np.array([-1., 2., 3., 4.])
astroNN_x = relu(x)
tf_x = tf.nn.relu(x)
tf_x = keras.backend.nn.relu(x)
npt.assert_array_equal(tf_x.numpy(), astroNN_x)

def test_kl_divergence(self):
Expand All @@ -55,15 +55,15 @@ def test_kl_divergence(self):
self.assertEqual(kl_divergence(x.tolist(), x.tolist()), 0.)

def test_regularizator(self):
# make sure its the same as tensorflow
# make sure its the same as keras implementation
x = np.array([-1., 2., 3., 4.])
reg = 0.2

astroNN_x = l1(x, l1=reg)
astroNN_x_2 = l2(x, l2=reg)

l1_reg = tf.keras.regularizers.l1(l=reg)
l2_reg = tf.keras.regularizers.l2(l=reg)
l1_reg = keras.regularizers.l1(l=reg)
l2_reg = keras.regularizers.l2(l=reg)
tf_x = l1_reg(x)
tf_x_2 = l2_reg(x)

Expand Down
Loading

0 comments on commit 7c5e612

Please sign in to comment.