A lightweight Keras wrapper that injects Out-of-Distribution detection natively into a TensorFlow/Keras model's computational graph.
Project description
TensorImmune
TensorImmune is a lightweight Keras wrapper that injects Out-of-Distribution (OOD) detection natively into a TensorFlow/Keras model's computational graph. A single exported artifact can flag anomalous inputs at inference time without any external monitoring infrastructure!
Overview
Deploying machine learning models to the real world involves dealing with unexpected or out-of-distribution data. Normally, OOD detection is handled by external monitoring infrastructure or complex multi-model setups. TensorImmune simplifies this by weaving a small, symbiotic autoencoder directly into your model's computational graph.
By observing an intermediate layer's activations (the "monitor layer"), TensorImmune simultaneously trains your primary task and the autoencoder. The model calibrates an anomaly threshold during training. When deployed, the model returns both the primary prediction and an immunity score (or anomaly flag), even when converted to edge formats like TensorFlow Lite!
Installation
Install via pip:
pip install tensorimmune
Quickstart
import tensorflow as tf
from tensorimmune import ImmuneModel
# 1. Define your base Keras model (Sequential or Functional)
base_model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(28, 28, 1)),
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(64, 3, activation='relu'),
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(64, activation='relu', name='feature_bottleneck'),
tf.keras.layers.Dense(10, activation='softmax')
])
# 2. Wrap it with ImmuneModel
# We monitor the 'feature_bottleneck' layer.
# sensitivity=0.95 means we set the anomaly threshold to the 95th percentile
# of the reconstruction errors seen during training.
model = ImmuneModel(
base_model=base_model,
monitor_layer='feature_bottleneck',
sensitivity=0.95
)
# 3. Compile and train normally!
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
# 4. Predict returns task predictions AND anomaly flags
predictions, is_anomaly = model.predict(x_test)
# You can also get the raw continuous immunity score
predictions, immunity_scores = model.predict(x_test, return_score=True)
How Symbiotic Training Works
Under the hood, ImmuneModel creates a feature_extractor that splits the graph at the monitor_layer. It then dynamically builds a small autoencoder to reconstruct the activations of that layer.
By subclassing tf.keras.Model and overriding train_step, TensorImmune trains both the original task loss and the autoencoder's mean squared error (MSE) reconstruction loss simultaneously in a single backward pass. The losses are combined via the immune_loss_weight parameter (which defaults to 1.0).
If the monitor layer outputs a spatial tensor (e.g. from a Conv2D layer), TensorImmune automatically inserts a GlobalAveragePooling layer before the autoencoder to flatten the representations intelligently.
Immunity Score & Sensitivity Calibration
During training, TensorImmune maintains a running record of reconstruction error statistics (mean, variance, count) inside the model's graph.
The sensitivity parameter (between 0 and 1) dictates how strictly the model should flag anomalies. It is treated as a percentile of the training distribution. Using the running statistics and the inverse error function, TensorImmune continuously calibrates a numeric anomaly threshold (e.g. sensitivity=0.95 maps to ~1.64 standard deviations above the mean).
At the end of training, this threshold is automatically stored as a non-trainable tf.Variable directly inside the model. When you save and export the model, the threshold is serialized with it!
Edge Deployment with TFLite
Because TensorImmune builds its architecture and threshold logic using pure tf.keras.layers and tf operations, the resulting model can be converted seamlessly to TensorFlow Lite for edge devices.
import tensorflow as tf
# Get concrete function for TFLite
run_model = tf.function(lambda x: model(x, return_score=False))
concrete_func = run_model.get_concrete_function(
tf.TensorSpec([1, 28, 28, 1], tf.float32)
)
# Convert! No custom ops required.
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
tflite_model = converter.convert()
with open("immune_model.tflite", "wb") as f:
f.write(tflite_model)
The exported TFLite model natively returns the boolean anomaly flag as its second output, making edge integration trivial.
Limitations
It is important to state a known limitation: autoencoder reconstruction error on intermediate features is generally a weaker OOD signal than more rigorous methods (like Mahalanobis distance, Energy-based scoring, or evidential deep learning).
TensorImmune is intended as a lightweight, drop-in first line of defense for edge deployment where you cannot afford external monitoring infrastructure or complex multi-model pipelines. It should not be used as a replacement for rigorous safety-critical OOD research techniques.
Contributing
Contributions are welcome! Please open an issue or submit a pull request if you have ideas for improvements, bug fixes, or new features.
When contributing:
- Ensure your code passes all existing tests.
- Add tests for new features.
- Update the documentation as necessary.
License: MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tensorimmune-0.1.1.tar.gz.
File metadata
- Download URL: tensorimmune-0.1.1.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ffddd94ae10c9b57edc3d424e4864ac5eff89b0a7a33a500e2476efc066ee4e
|
|
| MD5 |
239d0cb9e707c950510de70f9f5fdd47
|
|
| BLAKE2b-256 |
d5972fea308c5b602b79129dc46082d080c2e1d06bdeaf59b0aa0eb863ae27b1
|
File details
Details for the file tensorimmune-0.1.1-py3-none-any.whl.
File metadata
- Download URL: tensorimmune-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce43da16bca957b625d33dc07b9bb1c1fd29bb1de723a9dec140b176c0754759
|
|
| MD5 |
4ff562110dffab214719b21aba039039
|
|
| BLAKE2b-256 |
1b74b33aaf9494c310b9e3c28c2ad3abc85e885a50a21a0b8cca6c168436338c
|