Update app.py
Browse files
app.py
CHANGED
|
@@ -1,265 +1,533 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
-
from io import BytesIO
|
| 6 |
-
from NNVisualiser import NNVisualiser
|
| 7 |
-
import glob
|
| 8 |
-
import inspect
|
| 9 |
-
from tensorflow.keras.models import save_model
|
| 10 |
-
import tempfile
|
| 11 |
import re
|
|
|
|
| 12 |
import zipfile
|
| 13 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
return
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
def
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
st.
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
#
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""JeevanRuth ANN Model Explorer — Streamlit research demonstration.
|
| 2 |
+
|
| 3 |
+
Supports the original seven-folder model hierarchy:
|
| 4 |
+
repository / initialisation / sample size / batch size / epochs /
|
| 5 |
+
function / initial neuron count / saved Keras artefact.
|
| 6 |
+
|
| 7 |
+
This app NEVER performs JRNNOA optimisation; it explores saved model variants.
|
| 8 |
+
"""
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import io
|
| 12 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
import re
|
| 14 |
+
import threading
|
| 15 |
import zipfile
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
from tempfile import TemporaryDirectory
|
| 18 |
+
from typing import Any
|
| 19 |
+
|
| 20 |
+
import streamlit as st
|
| 21 |
|
| 22 |
+
ROOT = Path(__file__).resolve().parent
|
| 23 |
+
FIELDS = (
|
| 24 |
+
"repository", "initialisation", "sample_size", "batch_size",
|
| 25 |
+
"epochs", "function", "neurons",
|
| 26 |
+
)
|
| 27 |
+
LABELS = {
|
| 28 |
+
"repository": "Model collection / variant",
|
| 29 |
+
"initialisation": "Weight initialisation",
|
| 30 |
+
"sample_size": "Training sample size",
|
| 31 |
+
"batch_size": "Batch size",
|
| 32 |
+
"epochs": "Training epochs",
|
| 33 |
+
"function": "Target function",
|
| 34 |
+
"neurons": "Initial hidden-neuron count (catalogue label)",
|
| 35 |
+
}
|
| 36 |
+
PLOTS = {
|
| 37 |
+
"Activation · network overview": ("plotActivationConsolidatedForNetwork", "network"),
|
| 38 |
+
"Scaling · network overview": ("plotScaleConsolidatedForNetwork", "network"),
|
| 39 |
+
"Translation · network overview": ("plotTranslationConsolidatedForNetwork", "network"),
|
| 40 |
+
"Inputs · network overview": ("plotInputConsolidatedForNetwork", "network"),
|
| 41 |
+
"Activation · selected layer": ("plotActivationConsolidatedForLayer", "layer"),
|
| 42 |
+
"Scaling · selected layer": ("plotScaleConsolidatedForLayer", "layer"),
|
| 43 |
+
"Activation · selected neuron": ("plotActivationForNeuron", "neuron"),
|
| 44 |
+
"Transformation flow · selected neuron": ("plotFlowForNeuron", "neuron"),
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def natural_key(value: str) -> tuple:
|
| 49 |
+
"""Sort mixed numerical text in human-friendly order."""
|
| 50 |
+
return tuple((0, int(part)) if part.isdigit() else (1, part.casefold())
|
| 51 |
+
for part in re.split(r"(\d+)", str(value)))
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def resolve_saved_model(directory: Path) -> Path | None:
|
| 55 |
+
"""Locate exactly one supported Keras artefact at a catalogue leaf.
|
| 56 |
+
|
| 57 |
+
A directory is accepted as TensorFlow SavedModel only when it contains
|
| 58 |
+
saved_model.pb. A single .keras/.h5 checkpoint is also supported.
|
| 59 |
+
Ambiguous directories are excluded rather than selecting a random file.
|
| 60 |
+
"""
|
| 61 |
+
if (directory / "saved_model.pb").is_file():
|
| 62 |
+
return directory
|
| 63 |
+
candidates = sorted(
|
| 64 |
+
(p for p in directory.iterdir() if p.is_file() and p.suffix.lower() in {".keras", ".h5"}),
|
| 65 |
+
key=lambda p: natural_key(p.name),
|
| 66 |
+
)
|
| 67 |
+
return candidates[0] if len(candidates) == 1 else None
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def scan_catalogue(root: Path) -> list[dict[str, str]]:
|
| 71 |
+
"""Discover validated artefacts in the user's pre-existing hierarchy.
|
| 72 |
+
|
| 73 |
+
Does not traverse saved-model contents, hidden folders, or output folders.
|
| 74 |
+
"""
|
| 75 |
+
records: list[dict[str, str]] = []
|
| 76 |
+
root = root.resolve()
|
| 77 |
+
for current, foldernames, filenames in os.walk(root):
|
| 78 |
+
path = Path(current)
|
| 79 |
+
relative = path.relative_to(root)
|
| 80 |
+
depth = len(relative.parts)
|
| 81 |
+
foldernames[:] = sorted(
|
| 82 |
+
[name for name in foldernames if not name.startswith(".")
|
| 83 |
+
and name not in {"__pycache__", ".venv", "venv", "plots", "outputs"}],
|
| 84 |
+
key=natural_key,
|
| 85 |
+
)
|
| 86 |
+
if depth == len(FIELDS):
|
| 87 |
+
foldernames.clear()
|
| 88 |
+
artefact = resolve_saved_model(path)
|
| 89 |
+
if artefact is not None:
|
| 90 |
+
item = dict(zip(FIELDS, relative.parts))
|
| 91 |
+
item["path"] = str(artefact)
|
| 92 |
+
records.append(item)
|
| 93 |
+
elif depth > len(FIELDS):
|
| 94 |
+
foldernames.clear()
|
| 95 |
+
records.sort(key=lambda item: tuple(natural_key(item[name]) for name in FIELDS))
|
| 96 |
+
return records
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def choices(records: list[dict], field: str, **constraints: str) -> list[str]:
|
| 100 |
+
"""Only show values that occur in valid artefact records."""
|
| 101 |
+
return sorted({row[field] for row in records
|
| 102 |
+
if all(row.get(k) == v for k, v in constraints.items())}, key=natural_key)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def matches(records: list[dict], **constraints: str) -> list[dict]:
|
| 106 |
+
return [row for row in records
|
| 107 |
+
if all(row.get(k) == v for k, v in constraints.items())]
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def variant_name(repository: str) -> str:
|
| 111 |
+
"""Human-friendly description; never assume which files were optimised."""
|
| 112 |
+
norm = repository.lower().replace("-", " ").replace("_", " ")
|
| 113 |
+
if re.search(r"un\s*optimi[sz]ed|non\s*optimi[sz]ed|original|baseline|before", norm):
|
| 114 |
+
return "Unoptimised / baseline (repository label)"
|
| 115 |
+
if re.search(r"optimi[sz]ed|compressed|after|post", norm):
|
| 116 |
+
return "Optimised (repository label)"
|
| 117 |
+
return "Model collection (optimisation status not inferred)"
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def archive_model(path: Path) -> tuple[bytes, str, str]:
|
| 121 |
+
"""Download the ORIGINAL artefact, rather than re-serialising the model."""
|
| 122 |
+
if path.is_file():
|
| 123 |
+
return path.read_bytes(), path.name, "application/octet-stream"
|
| 124 |
+
output = io.BytesIO()
|
| 125 |
+
with zipfile.ZipFile(output, "w", zipfile.ZIP_DEFLATED) as archive:
|
| 126 |
+
for file in sorted(path.rglob("*")):
|
| 127 |
+
if file.is_file():
|
| 128 |
+
archive.write(file, arcname=str(Path(path.name) / file.relative_to(path)))
|
| 129 |
+
return output.getvalue(), path.name + ".zip", "application/zip"
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def plot_archive(images: list[tuple[str, bytes]]) -> bytes:
|
| 133 |
+
output = io.BytesIO()
|
| 134 |
+
with zipfile.ZipFile(output, "w", zipfile.ZIP_DEFLATED) as archive:
|
| 135 |
+
for filename, content in images:
|
| 136 |
+
archive.writestr(filename, content)
|
| 137 |
+
return output.getvalue()
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
@st.cache_data(show_spinner=False)
|
| 141 |
+
def cached_catalogue(root_string: str) -> list[dict[str, str]]:
|
| 142 |
+
return scan_catalogue(Path(root_string))
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
@st.cache_resource(show_spinner=False, max_entries=3)
|
| 146 |
+
def load_keras_model(path_string: str):
|
| 147 |
+
from tensorflow import keras
|
| 148 |
+
# compile=False avoids requiring the original optimiser/loss definitions.
|
| 149 |
+
return keras.models.load_model(path_string, compile=False)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
@st.cache_resource(show_spinner=False)
|
| 153 |
+
def plot_lock() -> threading.RLock:
|
| 154 |
+
# NNVisualiser uses CLASS-WIDE output settings; serialise plot generation.
|
| 155 |
+
return threading.RLock()
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def layer_sizes_for_visualiser(visualiser: Any) -> list[int]:
|
| 159 |
+
coordinate_network = visualiser.getNetworkCoordinates()
|
| 160 |
+
return [len(layer.getNeuronCoordinatesList())
|
| 161 |
+
for layer in coordinate_network.getLayerCoordinatesList()]
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def render_plot(model: Any, method_name: str, level: str,
|
| 165 |
+
layer: int = 0, neuron: int = 0) -> list[tuple[str, bytes]]:
|
| 166 |
+
"""Capture only the current request's files, never shared *.png files."""
|
| 167 |
+
import matplotlib.pyplot as plt
|
| 168 |
+
from NNVisualiser import NNVisualiser
|
| 169 |
+
with plot_lock():
|
| 170 |
+
previous_figures = set(plt.get_fignums())
|
| 171 |
+
with TemporaryDirectory(prefix="jrnn_plots_") as directory:
|
| 172 |
+
old_path = NNVisualiser.getFilePath()
|
| 173 |
+
old_save = NNVisualiser.isSavePlots()
|
| 174 |
+
old_format = NNVisualiser.getFileFormat()
|
| 175 |
+
try:
|
| 176 |
+
# NNVisualiser concatenates the directory and plot title.
|
| 177 |
+
NNVisualiser.setFilePath(directory + os.sep)
|
| 178 |
+
NNVisualiser.setFileFormat("png")
|
| 179 |
+
NNVisualiser.setSavePlots(True)
|
| 180 |
+
visualiser = NNVisualiser(model)
|
| 181 |
+
method = getattr(visualiser, method_name)
|
| 182 |
+
if level == "neuron":
|
| 183 |
+
method(layer, neuron)
|
| 184 |
+
elif level == "layer":
|
| 185 |
+
method(layer)
|
| 186 |
+
else:
|
| 187 |
+
method()
|
| 188 |
+
files = sorted(Path(directory).glob("*.png"), key=lambda x: natural_key(x.name))
|
| 189 |
+
return [(p.name, p.read_bytes()) for p in files]
|
| 190 |
+
finally:
|
| 191 |
+
NNVisualiser.setFilePath(old_path)
|
| 192 |
+
NNVisualiser.setSavePlots(old_save)
|
| 193 |
+
NNVisualiser.setFileFormat(old_format)
|
| 194 |
+
for figure_number in set(plt.get_fignums()) - previous_figures:
|
| 195 |
+
plt.close(figure_number)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
def get_model_info(model: Any) -> dict:
|
| 199 |
+
layers = [(layer.__class__.__name__, getattr(layer, "units", None))
|
| 200 |
+
for layer in model.layers]
|
| 201 |
+
dense_units = [int(units) for kind, units in layers if kind == "Dense" and units is not None]
|
| 202 |
+
return {
|
| 203 |
+
"parameters": int(model.count_params()),
|
| 204 |
+
"dense_layers": len(dense_units),
|
| 205 |
+
"hidden_units": sum(dense_units[:-1]) if len(dense_units) >= 2 else None,
|
| 206 |
+
"input_shape": str(model.input_shape),
|
| 207 |
+
"output_shape": str(model.output_shape),
|
| 208 |
+
"layer_summary": ", ".join(f"{kind}({units})" if units is not None else kind
|
| 209 |
+
for kind, units in layers),
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def display_metrics(model: Any, label: str):
|
| 214 |
+
info = get_model_info(model)
|
| 215 |
+
st.markdown(f"**{label}**")
|
| 216 |
+
a, b = st.columns(2)
|
| 217 |
+
a.metric("Parameters", f"{info['parameters']:,}")
|
| 218 |
+
b.metric("Hidden dense units", f"{info['hidden_units']:,}" if info['hidden_units'] is not None else "Not determined")
|
| 219 |
+
st.caption("Input: " + info["input_shape"] + " · Output: " + info["output_shape"])
|
| 220 |
+
with st.expander("Layer details", expanded=False):
|
| 221 |
+
st.code(info["layer_summary"])
|
| 222 |
+
return info
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def selected_dropdown(field: str, records: list[dict], state: dict) -> str:
|
| 226 |
+
values = choices(records, field, **state)
|
| 227 |
+
if not values:
|
| 228 |
+
st.error("There are no available models for this configuration.")
|
| 229 |
+
st.stop()
|
| 230 |
+
chosen = st.selectbox(LABELS[field], values, key="choose_" + field)
|
| 231 |
+
state[field] = chosen
|
| 232 |
+
return chosen
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def main() -> None:
|
| 236 |
+
st.set_page_config(
|
| 237 |
+
page_title="JeevanRuth ANN Model Explorer",
|
| 238 |
+
page_icon="🔬",
|
| 239 |
+
layout="wide",
|
| 240 |
+
initial_sidebar_state="expanded",
|
| 241 |
+
)
|
| 242 |
+
st.markdown("""<style>
|
| 243 |
+
.block-container {max-width:1180px; padding-top:1.7rem}
|
| 244 |
+
h1 {letter-spacing:-.035em}
|
| 245 |
+
div[data-testid="stMetric"] {background:#F4F8FB;padding:12px;border-radius:10px}
|
| 246 |
+
@media (max-width:700px) {.block-container {padding:1rem}}
|
| 247 |
+
</style>""", unsafe_allow_html=True)
|
| 248 |
+
|
| 249 |
+
st.title("JeevanRuth ANN Model Explorer")
|
| 250 |
+
st.caption("Explore original and JRNNOA-optimised shallow neural networks using NNVisualiser and NeuralNetworkCoordinates")
|
| 251 |
+
st.markdown("**Research demonstration** · Single-input, single-hidden-layer ReLU models · "
|
| 252 |
+
"[JRNNOA publication](https://doi.org/10.1007/s41870-025-02545-6)")
|
| 253 |
+
|
| 254 |
+
with st.sidebar:
|
| 255 |
+
st.header("Select an experiment")
|
| 256 |
+
st.caption("Options are read from saved model artefacts; unavailable combinations are excluded.")
|
| 257 |
+
if st.button("Refresh model catalogue", help="Rescan folders after uploading model files"):
|
| 258 |
+
cached_catalogue.clear()
|
| 259 |
+
st.rerun()
|
| 260 |
+
|
| 261 |
+
records = cached_catalogue(str(ROOT))
|
| 262 |
+
if not records:
|
| 263 |
+
st.error("No saved models found in the expected seven-level directory layout.")
|
| 264 |
+
with st.expander("Expected model directory structure", expanded=True):
|
| 265 |
+
st.code("collection/initialisation/sample_size/batch_size/epochs/function/neurons/saved_model.pb\n"
|
| 266 |
+
"# OR: .../neurons/model.keras", language="text")
|
| 267 |
+
st.write("Place the original model folders in the Space repository and press **Refresh model catalogue**.")
|
| 268 |
+
st.stop()
|
| 269 |
+
|
| 270 |
+
experiment: dict[str, str] = {}
|
| 271 |
+
with st.sidebar:
|
| 272 |
+
# The target function is more meaningful to visitors than technical parameters.
|
| 273 |
+
for field in ("function", "initialisation", "sample_size", "batch_size", "epochs", "neurons"):
|
| 274 |
+
selected_dropdown(field, records, experiment)
|
| 275 |
+
variants = choices(records, "repository", **experiment)
|
| 276 |
+
active_repo = st.selectbox("Model collection / variant", variants, key="choose_repository")
|
| 277 |
+
st.caption(variant_name(active_repo))
|
| 278 |
+
|
| 279 |
+
current = matches(records, repository=active_repo, **experiment)
|
| 280 |
+
if len(current) != 1:
|
| 281 |
+
st.error("The catalogue contains an ambiguous model selection. Please rescan the repository.")
|
| 282 |
+
st.stop()
|
| 283 |
+
record = current[0]
|
| 284 |
+
alternatives = matches(records, **experiment)
|
| 285 |
+
alternatives = [r for r in alternatives if r["repository"] != active_repo]
|
| 286 |
+
paired = None
|
| 287 |
+
if alternatives:
|
| 288 |
+
with st.sidebar:
|
| 289 |
+
compare = st.checkbox("Compare a second model", value=False)
|
| 290 |
+
if compare:
|
| 291 |
+
other_repos = sorted({r["repository"] for r in alternatives}, key=natural_key)
|
| 292 |
+
other = st.selectbox("Second collection", other_repos)
|
| 293 |
+
other_rows = [r for r in alternatives if r["repository"] == other]
|
| 294 |
+
if len(other_rows) == 1:
|
| 295 |
+
paired = other_rows[0]
|
| 296 |
+
|
| 297 |
+
signature = tuple(experiment.get(k) for k in ("function", "initialisation", "sample_size", "batch_size", "epochs", "neurons")) + (active_repo, paired["repository"] if paired else None)
|
| 298 |
+
if st.session_state.get("active_signature") != signature:
|
| 299 |
+
st.session_state.active_signature = signature
|
| 300 |
+
st.session_state.pop("plot_result", None)
|
| 301 |
+
st.session_state.pop("model_export", None)
|
| 302 |
+
st.session_state.pop("curve_result", None)
|
| 303 |
+
|
| 304 |
+
tab_explore, tab_curve, tab_methods, tab_papers, tab_guide = st.tabs([
|
| 305 |
+
"Explore & visualise", "Function comparison", "Methodology", "Publications & citation", "How to use",
|
| 306 |
+
])
|
| 307 |
+
with tab_explore:
|
| 308 |
+
st.subheader("1. Inspect the selected model")
|
| 309 |
+
st.caption("Selected catalogue entry: " + " / ".join(record[k] for k in FIELDS))
|
| 310 |
+
with st.spinner("Loading the selected saved model…"):
|
| 311 |
+
try:
|
| 312 |
+
model = load_keras_model(record["path"])
|
| 313 |
+
except Exception as exc:
|
| 314 |
+
st.error("Model loading failed. Verify the Keras/TensorFlow versions and saved-model format.")
|
| 315 |
+
st.code(str(exc))
|
| 316 |
+
st.stop()
|
| 317 |
+
main_info = display_metrics(model, active_repo)
|
| 318 |
+
comparison_model = None
|
| 319 |
+
if paired:
|
| 320 |
+
st.divider()
|
| 321 |
+
st.subheader("2. Compare the matching catalogue entry")
|
| 322 |
+
st.caption("Models share the directory labels for function, initialisation, sample size, batch size, epochs and initial neuron count. "
|
| 323 |
+
"This does not, by itself, prove before/after lineage or equal predictions.")
|
| 324 |
+
try:
|
| 325 |
+
comparison_model = load_keras_model(paired["path"])
|
| 326 |
+
other_info = display_metrics(comparison_model, paired["repository"])
|
| 327 |
+
if main_info["parameters"] > 0:
|
| 328 |
+
delta = 100 * (main_info["parameters"] - other_info["parameters"]) / main_info["parameters"]
|
| 329 |
+
st.info(f"Parameter-count difference (selected minus second): {delta:+.1f}% of the selected model. "
|
| 330 |
+
"This is a structural comparison, not a validated accuracy result.")
|
| 331 |
+
except Exception as exc:
|
| 332 |
+
st.error("The second saved model could not be loaded: " + str(exc))
|
| 333 |
+
comparison_model = None
|
| 334 |
+
|
| 335 |
+
st.divider()
|
| 336 |
+
st.subheader("3. Visualise internal computations")
|
| 337 |
+
plot_title = st.selectbox("Visualisation", list(PLOTS), index=0)
|
| 338 |
+
method, level = PLOTS[plot_title]
|
| 339 |
+
layer = 0
|
| 340 |
+
neuron = 0
|
| 341 |
+
if level in {"layer", "neuron"}:
|
| 342 |
+
try:
|
| 343 |
+
from NNVisualiser import NNVisualiser
|
| 344 |
+
coordinates = NNVisualiser(model)
|
| 345 |
+
sizes = layer_sizes_for_visualiser(coordinates)
|
| 346 |
+
usable_layers = [i for i, count in enumerate(sizes) if count > 0]
|
| 347 |
+
layer = st.selectbox("Layer index (NNVisualiser coordinates)", usable_layers)
|
| 348 |
+
if level == "neuron":
|
| 349 |
+
neuron = st.selectbox("Neuron index", list(range(sizes[layer])))
|
| 350 |
+
except Exception as exc:
|
| 351 |
+
st.error("Could not inspect NNVisualiser layer/neuronal coordinates: " + str(exc))
|
| 352 |
+
st.stop()
|
| 353 |
+
|
| 354 |
+
if st.button("Generate visualisation", type="primary"):
|
| 355 |
+
results = []
|
| 356 |
+
try:
|
| 357 |
+
with st.spinner("Generating plots from model coordinates…"):
|
| 358 |
+
results.append((active_repo, render_plot(model, method, level, layer, neuron)))
|
| 359 |
+
if comparison_model is not None:
|
| 360 |
+
# A layer/neuron index may be invalid after optimisation.
|
| 361 |
+
if level == "network":
|
| 362 |
+
results.append((paired["repository"], render_plot(comparison_model, method, level)))
|
| 363 |
+
else:
|
| 364 |
+
from NNVisualiser import NNVisualiser
|
| 365 |
+
sizes_other = layer_sizes_for_visualiser(NNVisualiser(comparison_model))
|
| 366 |
+
can_plot = layer < len(sizes_other) and (level != "neuron" or neuron < sizes_other[layer])
|
| 367 |
+
if can_plot:
|
| 368 |
+
results.append((paired["repository"], render_plot(comparison_model, method, level, layer, neuron)))
|
| 369 |
+
else:
|
| 370 |
+
st.warning("The chosen layer or neuron does not exist in the second model, so its plot was omitted.")
|
| 371 |
+
st.session_state.plot_result = (signature, method, level, layer, neuron, results)
|
| 372 |
+
except Exception as exc:
|
| 373 |
+
st.session_state.pop("plot_result", None)
|
| 374 |
+
st.error("NNVisualiser could not generate this plot: " + str(exc))
|
| 375 |
+
|
| 376 |
+
cached_plot = st.session_state.get("plot_result")
|
| 377 |
+
if cached_plot and cached_plot[:5] == (signature, method, level, layer, neuron):
|
| 378 |
+
output = cached_plot[5]
|
| 379 |
+
if not any(images for _, images in output):
|
| 380 |
+
st.warning("No PNG output was generated by this NNVisualiser method.")
|
| 381 |
+
else:
|
| 382 |
+
columns = st.columns(len(output))
|
| 383 |
+
all_images = []
|
| 384 |
+
for column, (repo_name, images) in zip(columns, output):
|
| 385 |
+
with column:
|
| 386 |
+
st.markdown(f"**{repo_name}**")
|
| 387 |
+
for i, (filename, data) in enumerate(images):
|
| 388 |
+
st.image(data, caption=filename, use_container_width=True)
|
| 389 |
+
all_images.append((f"{repo_name}/{i+1:02d}_{filename}", data))
|
| 390 |
+
st.download_button("Download generated plots (.zip)",
|
| 391 |
+
data=plot_archive(all_images), file_name="jeevanruth_visualisations.zip",
|
| 392 |
+
mime="application/zip")
|
| 393 |
+
st.divider()
|
| 394 |
+
with st.expander("Download saved model artefact"):
|
| 395 |
+
st.caption("Exports the original checkpoint. SavedModel folders are packaged as ZIP; standalone Keras files retain their format.")
|
| 396 |
+
if st.button("Prepare model download"):
|
| 397 |
+
try:
|
| 398 |
+
st.session_state.model_export = (signature, archive_model(Path(record["path"])))
|
| 399 |
+
except Exception as exc:
|
| 400 |
+
st.error("Unable to prepare download: " + str(exc))
|
| 401 |
+
export = st.session_state.get("model_export")
|
| 402 |
+
if export and export[0] == signature:
|
| 403 |
+
payload, filename, mime = export[1]
|
| 404 |
+
st.download_button("Download model", data=payload, file_name=filename, mime=mime)
|
| 405 |
+
|
| 406 |
+
with tab_curve:
|
| 407 |
+
st.subheader("Prediction curves on a specified input domain")
|
| 408 |
+
st.caption("Choose the original experiment's input interval. The suggested −1 to 1 range is only an illustrative default, NOT a verified domain for these checkpoints.")
|
| 409 |
+
with st.form("domain_form"):
|
| 410 |
+
xmin_col, xmax_col, n_col = st.columns(3)
|
| 411 |
+
xmin = xmin_col.number_input("Minimum input x", value=-1.0, format="%.4f")
|
| 412 |
+
xmax = xmax_col.number_input("Maximum input x", value=1.0, format="%.4f")
|
| 413 |
+
samples = n_col.slider("Evaluation points", 101, 1001, 301, step=100)
|
| 414 |
+
evaluate = st.form_submit_button("Evaluate selected checkpoint(s)", type="primary")
|
| 415 |
+
if evaluate:
|
| 416 |
+
import numpy as np
|
| 417 |
+
if not (np.isfinite(xmin) and np.isfinite(xmax) and xmin < xmax):
|
| 418 |
+
st.error("Provide a finite input range with a strictly increasing minimum and maximum.")
|
| 419 |
+
else:
|
| 420 |
+
try:
|
| 421 |
+
xvals = np.linspace(xmin, xmax, samples, dtype=np.float32).reshape(-1, 1)
|
| 422 |
+
if model.input_shape[-1] != 1 or model.output_shape[-1] != 1:
|
| 423 |
+
raise ValueError("The selected model does not expose a scalar input and scalar output.")
|
| 424 |
+
y1 = np.asarray(model.predict(xvals, verbose=0)).reshape(-1)
|
| 425 |
+
if len(y1) != samples or not np.all(np.isfinite(y1)):
|
| 426 |
+
raise ValueError("Selected model produced missing or non-finite predictions.")
|
| 427 |
+
y2 = None
|
| 428 |
+
if comparison_model is not None:
|
| 429 |
+
if comparison_model.input_shape[-1] != 1 or comparison_model.output_shape[-1] != 1:
|
| 430 |
+
raise ValueError("The second model does not expose a scalar input and scalar output.")
|
| 431 |
+
y2 = np.asarray(comparison_model.predict(xvals, verbose=0)).reshape(-1)
|
| 432 |
+
if len(y2) != samples or not np.all(np.isfinite(y2)):
|
| 433 |
+
raise ValueError("Second model produced missing or non-finite predictions.")
|
| 434 |
+
st.session_state.curve_result = (signature, float(xmin), float(xmax), int(samples), xvals.flatten(), y1, y2)
|
| 435 |
+
except Exception as exc:
|
| 436 |
+
st.session_state.pop("curve_result", None)
|
| 437 |
+
st.error("Unable to evaluate these models on the requested interval: " + str(exc))
|
| 438 |
+
result = st.session_state.get("curve_result")
|
| 439 |
+
if result and result[0] == signature:
|
| 440 |
+
import numpy as np
|
| 441 |
+
import pandas as pd
|
| 442 |
+
_, left, right, num, xvalues, y_selected, y_second = result
|
| 443 |
+
data = {active_repo: y_selected}
|
| 444 |
+
if y_second is not None:
|
| 445 |
+
data[paired["repository"]] = y_second
|
| 446 |
+
frame = pd.DataFrame(data, index=pd.Index(xvalues, name="Input x"))
|
| 447 |
+
st.line_chart(frame)
|
| 448 |
+
st.caption(f"Evaluated on [{left:.4g}, {right:.4g}] at {num} evenly spaced inputs. Curves are model predictions, not true function values.")
|
| 449 |
+
if y_second is not None:
|
| 450 |
+
disagreement = np.asarray(y_selected) - np.asarray(y_second)
|
| 451 |
+
one, two = st.columns(2)
|
| 452 |
+
one.metric("MSE between predictions", f"{np.mean(disagreement ** 2):.5g}")
|
| 453 |
+
two.metric("Maximum absolute difference", f"{np.max(np.abs(disagreement)):.5g}")
|
| 454 |
+
st.warning("These are agreement statistics between two saved models. They are NOT MSE against a reference target and do not establish that JRNNOA preserved accuracy. Confirm checkpoint lineage before attributing differences to optimisation.")
|
| 455 |
+
output_frame = frame.reset_index()
|
| 456 |
+
st.download_button("Download sampled predictions (.csv)", data=output_frame.to_csv(index=False).encode("utf-8"),
|
| 457 |
+
file_name="jeevanruth_function_comparison.csv", mime="text/csv")
|
| 458 |
+
|
| 459 |
+
with tab_methods:
|
| 460 |
+
st.subheader("What this collection demonstrates")
|
| 461 |
+
st.markdown("""
|
| 462 |
+
**Neural network family.** The original catalogue describes single-input, single-hidden-layer
|
| 463 |
+
ReLU networks with one scalar output (often denoted **1–N–1**), trained to approximate
|
| 464 |
+
one-dimensional functions on bounded intervals. Folder labels identify the training
|
| 465 |
+
configuration and the original neuron-count setting.
|
| 466 |
+
|
| 467 |
+
**JRNNOA.** The JeevanRuth Neural Network Optimisation Algorithm investigates removal of
|
| 468 |
+
inactive neurons and merging of suitable neurons in the studied single-input,
|
| 469 |
+
single-output ReLU architecture. This Space **loads previously saved networks**;
|
| 470 |
+
it does not retrain or optimise them when you choose a model.
|
| 471 |
+
|
| 472 |
+
**Interpretability tools.** NeuralNetworkCoordinates provides coordinate-level
|
| 473 |
+
representations of neuron and layer transformations. NNVisualiser uses these coordinates
|
| 474 |
+
to display input, scaling, translation, activation and flow plots.
|
| 475 |
+
|
| 476 |
+
**Interpreting comparisons.** A reduction in stored parameters is a structural
|
| 477 |
+
observation, not evidence of equal predictive accuracy. Before/after equivalence
|
| 478 |
+
should be evaluated over the same input domain with independent error tests.
|
| 479 |
+
""")
|
| 480 |
+
st.info("Actual dataset provenance, function domains, optimisation settings and evaluation metrics must be taken from the corresponding experiment records. They are not inferred from folder names.")
|
| 481 |
+
|
| 482 |
+
with tab_papers:
|
| 483 |
+
st.subheader("Publications and software")
|
| 484 |
+
st.markdown("""
|
| 485 |
+
**Optimisation methodology**
|
| 486 |
+
Caxton Emerald S and T. Vengattaraman. *Neural network optimization via inactive neuron removal and neuron merging in single input single output architectures.* International Journal of Information Technology (2025).
|
| 487 |
+
[DOI: 10.1007/s41870-025-02545-6](https://doi.org/10.1007/s41870-025-02545-6)
|
| 488 |
+
|
| 489 |
+
**NNVisualiser**
|
| 490 |
+
Caxton Emerald S and T. Vengattaraman. *NNVisualiser: An Emerging Framework for Visualising Artificial Neural Networks.* IEEE ICDSAAI (2025).
|
| 491 |
+
[DOI: 10.1109/ICDSAAI65575.2025.11011573](https://doi.org/10.1109/ICDSAAI65575.2025.11011573)
|
| 492 |
+
|
| 493 |
+
**NeuralNetworkCoordinates**
|
| 494 |
+
Caxton Emerald S and T. Vengattaraman. *Neural Network Coordinates: A Python Package for Studying Artificial Neural Networks.* Springer proceedings (2026).
|
| 495 |
+
[DOI: 10.1007/978-981-95-0626-2_15](https://doi.org/10.1007/978-981-95-0626-2_15)
|
| 496 |
+
|
| 497 |
+
**Software:** [NNVisualiser on PyPI](https://pypi.org/project/NNVisualiser/) ·
|
| 498 |
+
[NeuralNetworkCoordinates on PyPI](https://pypi.org/project/NeuralNetworkCoordinates/) ·
|
| 499 |
+
[GitHub](https://github.com/CaxtonEmerald-S) ·
|
| 500 |
+
[Researcher ORCID](https://orcid.org/0000-0002-7763-3987)
|
| 501 |
+
""")
|
| 502 |
+
with st.expander("Citation (BibTeX)"):
|
| 503 |
+
st.code("""@article{EmeraldVengattaraman2025JRNNOA,
|
| 504 |
+
author = {Caxton Emerald S and T. Vengattaraman},
|
| 505 |
+
title = {Neural network optimization via inactive neuron removal and neuron merging in single input single output architectures},
|
| 506 |
+
journal = {International Journal of Information Technology},
|
| 507 |
+
year = {2025},
|
| 508 |
+
doi = {10.1007/s41870-025-02545-6}
|
| 509 |
+
}""", language="bibtex")
|
| 510 |
+
|
| 511 |
+
with tab_guide:
|
| 512 |
+
st.subheader("How to explore the model collection")
|
| 513 |
+
st.markdown("""
|
| 514 |
+
1. Choose a **target function**, then the experimental training settings in the sidebar.
|
| 515 |
+
2. Select an available **model collection**; a catalogue entry exists only if a saved checkpoint is present.
|
| 516 |
+
3. Inspect the architecture and parameter count. Where another collection has the same
|
| 517 |
+
experiment labels, enable **Compare a second model**.
|
| 518 |
+
4. Choose a network, layer or neuron visualisation and select **Generate visualisation**.
|
| 519 |
+
5. Download plots or the original model checkpoint for further analysis.
|
| 520 |
+
|
| 521 |
+
**Important:** The initial neuron-count folder name need not equal the final number of
|
| 522 |
+
active neurons after JRNNOA. Use the model's reported architecture for the latter.
|
| 523 |
+
|
| 524 |
+
**Reproducibility:** For formal comparisons, report the target function and domain,
|
| 525 |
+
initialisation, sample size, batch size, epochs, original and optimised checkpoint
|
| 526 |
+
identifiers, the tested input grid and an error metric (e.g. MSE and maximum absolute
|
| 527 |
+
difference). This app does not compute fidelity metrics without those experimental inputs.
|
| 528 |
+
""")
|
| 529 |
+
st.caption("For research and educational use. Review the repository's licence before redistribution or commercial use.")
|
| 530 |
+
|
| 531 |
+
|
| 532 |
+
if __name__ == "__main__":
|
| 533 |
+
main()
|