Skip to content

Commit

Permalink
Demo: updated notebooks and helper code to use new model format, whic…
Browse files Browse the repository at this point in the history
…h does not need a separate weights file
  • Loading branch information
sebastian-echeverria committed Jan 15, 2025
1 parent 10e0382 commit 32c4dc5
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 52 deletions.
2 changes: 1 addition & 1 deletion demo/scenarios/2d_evidence_interpretability.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"from demo.scenarios.model_analysis import *\n",
"\n",
"# Load the model/\n",
"loaded_model = load_model(MODEL_FILE_PATH, MODEL_WEIGHTS_PATH)"
"loaded_model = load_model(MODEL_FILE_PATH)"
]
},
{
Expand Down
4 changes: 1 addition & 3 deletions demo/scenarios/2f_evidence_interoperability.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@
"\n",
"def run_and_get_log() -> str:\n",
" \"\"\"Runs the model and gets the log.\"\"\"\n",
" model_predict.run_model(\n",
" SAMPLE_DATASET_DIR, MODEL_FILE_PATH, MODEL_WEIGHTS_PATH\n",
" )\n",
" model_predict.run_model(SAMPLE_DATASET_DIR, MODEL_FILE_PATH)\n",
"\n",
" return model_predict.load_log()"
]
Expand Down
4 changes: 1 addition & 3 deletions demo/scenarios/2g_evidence_resilience.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@
"\n",
"def run_and_get_log() -> str:\n",
" \"\"\"Runs the model and gets the log.\"\"\"\n",
" model_predict.run_model(\n",
" OOD_DATASET_DIR, MODEL_FILE_PATH, MODEL_WEIGHTS_PATH\n",
" )\n",
" model_predict.run_model(OOD_DATASET_DIR, MODEL_FILE_PATH)\n",
"\n",
" return model_predict.load_log()"
]
Expand Down
4 changes: 1 addition & 3 deletions demo/scenarios/2h_evidence_monitorability.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@
"\n",
"def run_and_get_log() -> str:\n",
" \"\"\"Runs the model and gets the log.\"\"\"\n",
" model_predict.run_model(\n",
" OOD_DATASET_DIR, MODEL_FILE_PATH, MODEL_WEIGHTS_PATH\n",
" )\n",
" model_predict.run_model(OOD_DATASET_DIR, MODEL_FILE_PATH)\n",
" return model_predict.load_log()"
]
},
Expand Down
2 changes: 1 addition & 1 deletion demo/scenarios/2i_evidence_performance_time.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
" \"\"\"Returns total time, and average time per inference.\"\"\"\n",
" start = time.time()\n",
" avg_time, _, _ = model_predict.run_model(\n",
" SAMPLE_DATASET_DIR, MODEL_FILE_PATH, MODEL_WEIGHTS_PATH\n",
" SAMPLE_DATASET_DIR, MODEL_FILE_PATH\n",
" )\n",
" end = time.time()\n",
" total = end - start\n",
Expand Down
2 changes: 1 addition & 1 deletion demo/scenarios/get_model.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env bash
curl -o ./model/oxford_flower_model_v6.testing.keras -L "https://docs.google.com/uc?export=download&id=15kAII1kOPGIAI46OP01ecNkq4tdf5yXw"
curl -o ./model/oxford_flower_model.keras -L "https://drive.usercontent.google.com/download?id=17C_RowVjlZXrvY_VHXCTBAHwLZjZl1OD&export=download&authuser=0&confirm=t"
1 change: 0 additions & 1 deletion demo/scenarios/model/model_f3_a.json

This file was deleted.

14 changes: 2 additions & 12 deletions demo/scenarios/model_analysis.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.models import model_from_json


def load_model(model_filename: str, weights_filename: str):
# Load model
json_file = open(model_filename, "r")
loaded_model_json = json_file.read()
loaded_model = model_from_json(loaded_model_json)
json_file.close()

# Load weights into new model
loaded_model.load_weights(weights_filename)

return loaded_model
def load_model(model_filename: str):
return tf.keras.models.load_model(model_filename)


def run_model(im, loaded_model):
Expand Down
26 changes: 5 additions & 21 deletions demo/scenarios/model_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,13 @@ def parse_args():
)
# --model file example: model_f3_a.json
parser.add_argument(
"--model", help="The json formatted model file.", required=True
)
# --weights file example: model_f_a.h5
parser.add_argument(
"--weights",
help="The file that contains the model weights.",
required=True,
"--model", help="The keras formatted model file.", required=True
)
args = parser.parse_args()
return args


def run_model(image_folder_path, model_file, weights_file):
def run_model(image_folder_path, model_file):
"""
This script gets performance metrics (elapsed time and memory consumption) for running inference on a model.
It does NOT check accurate predictions - it simply runs the inference on the provided dataset and outputs statics
Expand All @@ -70,7 +64,6 @@ def run_model(image_folder_path, model_file, weights_file):
import tensorflow as tf

print_and_log(f"TensorFlow version: {tf.__version__}")
from tensorflow.keras.models import model_from_json

# Load dataset
dataset = tf.keras.utils.image_dataset_from_directory(
Expand All @@ -85,24 +78,16 @@ def run_model(image_folder_path, model_file, weights_file):
ru1 = getrusage(RUSAGE_SELF).ru_maxrss

# Load model
json_file = open(model_file, "r")
loaded_model_json = json_file.read()
loaded_model = model_from_json(loaded_model_json)
json_file.close()

# Load weights into new model
loaded_model.load_weights(weights_file)
loaded_model = tf.keras.models.load_model(model_file)
print_and_log("Loaded model from disk!")

ru2 = getrusage(RUSAGE_SELF).ru_maxrss

print_and_log(loaded_model.summary())

mfile_size = os.path.getsize(model_file)
wfile_size = os.path.getsize(weights_file)

print_and_log(f"Size of model json file ({model_file}): {mfile_size} bytes")
print_and_log(f"Size of weights file ({weights_file}): {wfile_size} bytes")
print_and_log(f"Size of model file ({model_file}): {mfile_size} bytes")
print_and_log(
f"Memory used for the entire model loading process: {ru2 - ru1} {r_mem_units_str}."
)
Expand Down Expand Up @@ -208,6 +193,5 @@ def run_model(image_folder_path, model_file, weights_file):
args = parse_args()
image_folder = args.images
model_file = args.model
weights_file = args.weights

run_model(image_folder, model_file, weights_file)
run_model(image_folder, model_file)
7 changes: 1 addition & 6 deletions demo/scenarios/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
os.makedirs(MEDIA_DIR, exist_ok=True)

# The json file of the model to load
MODEL_FILE_PATH = MODELS_DIR / "model_f3_a.json"

# The weights file for the model
MODEL_WEIGHTS_PATH = MODELS_DIR / "oxford_flower_model_v6.testing.keras"
MODEL_FILE_PATH = MODELS_DIR / "oxford_flower_model.keras"

# This is the external script that will load and run the model for inference/prediction.
MODEL_SCRIPT = Path.cwd() / "model_predict.py"
Expand All @@ -41,6 +38,4 @@
SAMPLE_DATASET_DIR,
"--model",
MODEL_FILE_PATH,
"--weights",
MODEL_WEIGHTS_PATH,
]

0 comments on commit 32c4dc5

Please sign in to comment.