Skip to content

Commit

Permalink
refactor: replace .hdf5 with .keras format
Browse files Browse the repository at this point in the history
  • Loading branch information
rizoudal committed Aug 19, 2024
1 parent f201b75 commit a14af09
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 54 deletions.
4 changes: 2 additions & 2 deletions aucmedi/automl/block_pred.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def block_predict(config):
standardize_mode=model.meta_standardize,
**paras_datagen)
# Load model
path_model = os.path.join(config["path_modeldir"], "model.last.hdf5")
path_model = os.path.join(config["path_modeldir"], "model.last.keras")
model.load(path_model)
# Start model inference
preds = model.predict(prediction_generator=pred_gen)
Expand All @@ -139,7 +139,7 @@ def block_predict(config):
**paras_datagen)
# Load model
path_model = os.path.join(config["path_modeldir"],
"model.best_loss.hdf5")
"model.best_loss.keras")
model.load(path_model)
# Start model inference via Augmenting
preds = predict_augmenting(model, pred_gen)
Expand Down
6 changes: 3 additions & 3 deletions aucmedi/automl/block_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def block_train(config):
callbacks = []
if config["analysis"] == "standard":
cb_loss = ModelCheckpoint(os.path.join(config["path_modeldir"],
"model.best_loss.hdf5"),
"model.best_loss.keras"),
monitor="val_loss", verbose=1,
save_best_only=True)
callbacks.append(cb_loss)
Expand Down Expand Up @@ -214,7 +214,7 @@ def block_train(config):
# Start model training
hist = model.train(training_generator=train_gen, **paras_train)
# Store model
path_model = os.path.join(config["path_modeldir"], "model.last.hdf5")
path_model = os.path.join(config["path_modeldir"], "model.last.keras")
model.dump(path_model)
elif config["analysis"] == "standard":
# Setup neural network
Expand Down Expand Up @@ -247,7 +247,7 @@ def block_train(config):
validation_generator=val_gen,
**paras_train)
# Store model
path_model = os.path.join(config["path_modeldir"], "model.last.hdf5")
path_model = os.path.join(config["path_modeldir"], "model.last.keras")
model.dump(path_model)
else:
# Sanity check of architecutre config
Expand Down
6 changes: 3 additions & 3 deletions aucmedi/ensemble/bagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def train(self, training_generator, epochs=20, iterations=None,
# Extend Callback list
cb_mc = ModelCheckpoint(os.path.join(self.cache_dir.name,
"cv_" + str(i) + \
".model.hdf5"),
".model.keras"),
monitor="val_loss", verbose=1,
save_best_only=True, mode="min")
cb_cl = CSVLogger(os.path.join(self.cache_dir.name,
Expand Down Expand Up @@ -306,7 +306,7 @@ def predict(self, prediction_generator, aggregate="mean",
for i in range(self.k_fold):
# Identify path to fitted model
path_model = os.path.join(path_model_dir,
"cv_" + str(i) + ".model.hdf5")
"cv_" + str(i) + ".model.keras")

# Gather NeuralNetwork parameters
model_paras = {
Expand Down Expand Up @@ -385,7 +385,7 @@ def load(self, directory_path):
# Check model existence
for i in range(self.k_fold):
path_model = os.path.join(directory_path,
"cv_" + str(i) + ".model.hdf5")
"cv_" + str(i) + ".model.keras")
if not os.path.exists(path_model):
raise FileNotFoundError("Bagging model for fold " + str(i) + \
" does not exist!", path_model)
Expand Down
8 changes: 4 additions & 4 deletions aucmedi/ensemble/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def train(self, training_generator, epochs=20, iterations=None,
callbacks_model = callbacks.copy()
# Extend Callback list
path_model = os.path.join(self.cache_dir.name,
"cv_" + str(i) + ".model.hdf5")
"cv_" + str(i) + ".model.keras")
cb_mc = ModelCheckpoint(path_model,
monitor="val_loss", verbose=1,
save_best_only=True, mode="min")
Expand Down Expand Up @@ -336,7 +336,7 @@ def train_metalearner(self, training_generator):
for i in range(len(self.model_list)):
# Load current model
path_model = os.path.join(path_model_dir,
"cv_" + str(i) + ".model.hdf5")
"cv_" + str(i) + ".model.keras")

# Gather NeuralNetwork parameters
model_paras = {
Expand Down Expand Up @@ -447,7 +447,7 @@ def predict(self, prediction_generator, return_ensemble=False):
# Sequentially iterate over model list
for i in range(len(self.model_list)):
path_model = os.path.join(path_model_dir,
"cv_" + str(i) + ".model.hdf5")
"cv_" + str(i) + ".model.keras")

# Gather NeuralNetwork parameters
model_paras = {
Expand Down Expand Up @@ -554,7 +554,7 @@ def load(self, directory_path):
# Check model existence
for i in range(len(self.model_list)):
path_model = os.path.join(directory_path,
"cv_" + str(i) + ".model.hdf5")
"cv_" + str(i) + ".model.keras")
if not os.path.exists(path_model):
raise FileNotFoundError("Composite model " + str(i) + \
" does not exist!", path_model)
Expand Down
8 changes: 4 additions & 4 deletions aucmedi/ensemble/stacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def train(self, training_generator, epochs=20, iterations=None,
callbacks_model = callbacks.copy()
# Extend Callback list
path_model = os.path.join(self.cache_dir.name,
"nn_" + str(i) + ".model.hdf5")
"nn_" + str(i) + ".model.keras")
cb_mc = ModelCheckpoint(path_model,
monitor="val_loss", verbose=1,
save_best_only=True, mode="min")
Expand Down Expand Up @@ -324,7 +324,7 @@ def train_metalearner(self, training_generator):
for i in range(len(self.model_list)):
# Load current model
path_model = os.path.join(path_model_dir,
"nn_" + str(i) + ".model.hdf5")
"nn_" + str(i) + ".model.keras")

# Gather NeuralNetwork parameters
model_paras = {
Expand Down Expand Up @@ -434,7 +434,7 @@ def predict(self, prediction_generator, return_ensemble=False):
# Sequentially iterate over model list
for i in range(len(self.model_list)):
path_model = os.path.join(path_model_dir,
"nn_" + str(i) + ".model.hdf5")
"nn_" + str(i) + ".model.keras")

# Gather NeuralNetwork parameters
model_paras = {
Expand Down Expand Up @@ -541,7 +541,7 @@ def load(self, directory_path):
# Check model existence
for i in range(len(self.model_list)):
path_model = os.path.join(directory_path,
"nn_" + str(i) + ".model.hdf5")
"nn_" + str(i) + ".model.keras")
if not os.path.exists(path_model):
raise FileNotFoundError("Stacking model " + str(i) + \
" does not exist!", path_model)
Expand Down
4 changes: 2 additions & 2 deletions aucmedi/neural_network/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def reset_weights(self):
def dump(self, file_path):
""" Store model to disk.
Recommended to utilize the file format ".hdf5".
Recommended to utilize the file format ".keras".
Args:
file_path (str): Path to store the model on disk.
Expand All @@ -385,7 +385,7 @@ def load(self, file_path, custom_objects={}):
After loading, the model will be compiled.
If loading a model in ".hdf5" format, it is not necessary to define any custom_objects.
If loading a model in ".keras" format, it is not necessary to define any custom_objects.
Args:
file_path (str): Input path, from which the model will be loaded.
Expand Down
2 changes: 1 addition & 1 deletion aucmedi/xai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
# Get a model
model = NeuralNetwork(n_labels=3, channels=3, architecture="Xception",
input_shape=(299,299))
model.load("model.xray.hdf5")
model.load("model.xray.keras")
# Make some predictions
preds = model.predict(datagen)
Expand Down
2 changes: 1 addition & 1 deletion aucmedi/xai/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def xai_decoder(data_gen, model, preds=None, method="gradcam", layerName=None,
# Get a model
model = NeuralNetwork(n_labels=3, channels=3, architecture="Xception",
input_shape=(299,299))
model.load("model.xray.hdf5")
model.load("model.xray.keras")
# Make some predictions
preds = model.predict(datagen)
Expand Down
8 changes: 4 additions & 4 deletions docs/automl/cli/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ To enable them in other operations, rebuild TensorFlow with the appropriate comp
Epoch 1/10
2022-07-18 12:57:32.516662: I tensorflow/stream_executor/cuda/cuda_dnn.cc:368] Loaded cuDNN version 8100
177/177 [==============================] - ETA: 0s - loss: 0.5103 - auc: 0.9563 - f1_score: 0.7556
Epoch 1: val_loss improved from inf to 0.20766, saving model to model/model.best_loss.hdf5
Epoch 1: val_loss improved from inf to 0.20766, saving model to model/model.best_loss.keras
177/177 [==============================] - 21s 92ms/step - loss: 0.5103 - auc: 0.9563 - f1_score: 0.7556 - val_loss: 0.2077 - val_auc: 0.9864 - val_f1_score: 0.8958 - lr: 1.0000e-04
Epoch 2/10
177/177 [==============================] - ETA: 0s - loss: 0.1932 - auc: 0.9893 - f1_score: 0.8842
Epoch 2: val_loss improved from 0.20766 to 0.18348, saving model to model/model.best_loss.hdf5
Epoch 2: val_loss improved from 0.20766 to 0.18348, saving model to model/model.best_loss.keras
177/177 [==============================] - 15s 84ms/step - loss: 0.1932 - auc: 0.9893 - f1_score: 0.8842 - val_loss: 0.1835 - val_auc: 0.9891 - val_f1_score: 0.9010 - lr: 1.0000e-04
...
Epoch 23/25
Expand Down Expand Up @@ -79,8 +79,8 @@ cwd/
└── model/
├── logs.training.csv
├── meta.training.json
├── model.best_loss.hdf5
├── model.last.hdf5
├── model.best_loss.keras
├── model.last.keras
└── plot.fitting_course.png
```

Expand Down
8 changes: 4 additions & 4 deletions docs/automl/docker/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ To enable them in other operations, rebuild TensorFlow with the appropriate comp
Epoch 1/10
2022-07-18 12:57:32.516662: I tensorflow/stream_executor/cuda/cuda_dnn.cc:368] Loaded cuDNN version 8100
177/177 [==============================] - ETA: 0s - loss: 0.5103 - auc: 0.9563 - f1_score: 0.7556
Epoch 1: val_loss improved from inf to 0.20766, saving model to model/model.best_loss.hdf5
Epoch 1: val_loss improved from inf to 0.20766, saving model to model/model.best_loss.keras
177/177 [==============================] - 21s 92ms/step - loss: 0.5103 - auc: 0.9563 - f1_score: 0.7556 - val_loss: 0.2077 - val_auc: 0.9864 - val_f1_score: 0.8958 - lr: 1.0000e-04
Epoch 2/10
177/177 [==============================] - ETA: 0s - loss: 0.1932 - auc: 0.9893 - f1_score: 0.8842
Epoch 2: val_loss improved from 0.20766 to 0.18348, saving model to model/model.best_loss.hdf5
Epoch 2: val_loss improved from 0.20766 to 0.18348, saving model to model/model.best_loss.keras
177/177 [==============================] - 15s 84ms/step - loss: 0.1932 - auc: 0.9893 - f1_score: 0.8842 - val_loss: 0.1835 - val_auc: 0.9891 - val_f1_score: 0.9010 - lr: 1.0000e-04
...
Epoch 23/25
Expand Down Expand Up @@ -130,8 +130,8 @@ aucmedi.data/
└── model/
├── logs.training.csv
├── meta.training.json
├── model.best_loss.hdf5
├── model.last.hdf5
├── model.best_loss.keras
├── model.last.keras
└── plot.fitting_course.png
```

Expand Down
18 changes: 9 additions & 9 deletions tests/test_automl_block_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_minimal(self):
# Run AutoML training block
block_train(config)

self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.last.hdf5")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.last.keras")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "logs.training.csv")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "meta.training.json")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "plot.fitting_course.png")))
Expand Down Expand Up @@ -144,7 +144,7 @@ def test_minimal_multilabel(self):
# Run AutoML training block
block_train(config)

self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.last.hdf5")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.last.keras")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "logs.training.csv")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "meta.training.json")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "plot.fitting_course.png")))
Expand Down Expand Up @@ -172,7 +172,7 @@ def test_minimal_3D(self):
# Run AutoML training block
block_train(config)

self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.last.hdf5")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.last.keras")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "logs.training.csv")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "meta.training.json")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "plot.fitting_course.png")))
Expand Down Expand Up @@ -202,8 +202,8 @@ def test_standard(self):
# Run AutoML training block
block_train(config)

self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.last.hdf5")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.best_loss.hdf5")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.last.keras")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.best_loss.keras")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "logs.training.csv")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "meta.training.json")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "plot.fitting_course.png")))
Expand All @@ -230,8 +230,8 @@ def test_standard_multilabel(self):
# Run AutoML training block
block_train(config)

self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.last.hdf5")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.best_loss.hdf5")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.last.keras")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.best_loss.keras")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "logs.training.csv")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "meta.training.json")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "plot.fitting_course.png")))
Expand Down Expand Up @@ -259,8 +259,8 @@ def test_standard_3D(self):
# Run AutoML training block
block_train(config)

self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.last.hdf5")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.best_loss.hdf5")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.last.keras")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "model.best_loss.keras")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "logs.training.csv")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "meta.training.json")))
self.assertTrue(os.path.exists(os.path.join(output_dir.name, "plot.fitting_course.png")))
Expand Down
Loading

0 comments on commit a14af09

Please sign in to comment.