Skip to content

Commit e02f6db

Browse files
committed
Initial commit of tensorflow plugin
1 parent 8d6b305 commit e02f6db

24 files changed

+2196
-316
lines changed

.github/pull-request-template.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Select Your Pull Request Template
22
Please go to 'Preview' view using the toggle above, and then select the appropriate pull request template by clicking on the link from the list below:
33

4-
## New Feature added to Existing Connector
5-
If you have added a new feature to an existing Connector, [please use this template](?expand=1&template=new-feature-template.md)
4+
## New Feature added to Existing Plugin
5+
If you have added a new feature to an existing Plugin, [please use this template](?expand=1&template=new-feature-template.md)
66

77
## New Tests
88
If you have added some new Unit or Integration tests, [please use this template](?expand=1&template=new-tests-template.md)

.github/workflows/test_integration.yml

Lines changed: 0 additions & 35 deletions
This file was deleted.

.github/workflows/test_macos.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ jobs:
3434
export SIMVUE_URL=${{ secrets.SIMVUE_URL }}
3535
export SIMVUE_TOKEN=${{ secrets.SIMVUE_TOKEN }}
3636
poetry install --all-extras
37-
poetry run pytest tests/unit/
37+
poetry run pytest tests/

.github/workflows/test_ubuntu.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ jobs:
3434
export SIMVUE_URL=${{ secrets.SIMVUE_URL }}
3535
export SIMVUE_TOKEN=${{ secrets.SIMVUE_TOKEN }}
3636
poetry install --all-extras
37-
poetry run pytest tests/unit/
37+
poetry run pytest tests/

.github/workflows/test_windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ jobs:
3535
export SIMVUE_URL=${{ secrets.SIMVUE_URL }}
3636
export SIMVUE_TOKEN=${{ secrets.SIMVUE_TOKEN }}
3737
poetry install --all-extras
38-
poetry run pytest tests/unit/
38+
poetry run pytest tests/

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Change log
2+
3+
## [v1.0.0](https://github.com/simvue-io/plugins-tensorflow/releases/tag/v1.0.0) - 2025-03-07
4+
5+
* Initial release of TensorFlow Plugin.

CITATION.cff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ keywords:
4444
license: Apache-2.0
4545
commit: 64ff8a5344232d44fc7da5b6ff601d3023497977
4646
version: 2.0.0a3
47-
date-released: '2025-03-04'
47+
date-released: '2025-03-04'

README.md

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Simvue Connectors - Template
1+
# Simvue Plugins - TensorFlow
22

33
<br/>
44

@@ -11,7 +11,7 @@
1111
</p>
1212

1313
<p align="center">
14-
This is a template repository which allows you to quickly create new Plugins which provide Simvue tracking and monitoring functionality to Python-based simulations.
14+
This plugin allows you to easily add Simvue tracking and monitoring functionality to the training and testing of ML models built using TensorFlow.
1515
</p>
1616

1717
<div align="center">
@@ -25,64 +25,15 @@ This is a template repository which allows you to quickly create new Plugins whi
2525
<a href="https://docs.simvue.io"><b>Documentation</b></a>
2626
</h3>
2727

28-
## How to use this template
29-
30-
### Naming your plugin
31-
First, make a name for your new plugin. Typically, the module name is of the form `simvue-{software_name}`, and the connector class itself is of the form `{SoftwareName}Run`. Update the `pyproject.toml` file with the name of your module, and also update the directory currently called `simvue_template` with your module name.
32-
33-
### Creating the code
34-
Ideally, the plugin class which you want users to interact with should be made in the `plugin.py` file inside your module, with any extra functionality which it needs to work (but you don't want inside the class itself) put in files inside the `extras` directory. However since there is no set format for plugins (unlike the more rigid structure of connectors), this may change depending on your needs. Make sure you document thoroughly in the README and then examples how you intend for your plugin to be used. Check out any of our premade plugins for ideas:
35-
36-
* [TensorFlow](https://github.com/simvue-io/plugins-tensorflow)
37-
38-
Also look at the `CONTRIBUTING.md` file for expected coding standards.
39-
40-
41-
### Writing examples
42-
In the `examples` directory, please provide at least one example of your plugin being used to track your simulation software. Create this example inside a function so that it can be used in the integration tests.
43-
44-
### Writing tests
45-
You should create two types of tests:
46-
47-
* Unit tests: Check each element of your plugin independently, such as file parsers and callbacks, each method etc. These should use Pytest.
48-
* Integration tests: These check the end-to-end functionality of your plugin when used with the actual simulation software. You should parametrize the test to include offline mode, as well as online. You can use the example(s) which you created earlier as the basis for these tests.
49-
50-
### CI Workflows
51-
Inside the `.github` directory, there are a number of workflows already created. You should edit these to work for your plugin. They include:
52-
53-
* `test_macos`, `test_ubuntu`, `test_windows`: These run the unit and integration tests, should not need to be altered
54-
* `deploy`: Automates deployment to test-PyPI and PyPI for tagged releases (see below). You need to update the module names in this file - see the curly brackets.
55-
56-
### Deployment
57-
When you are happy with your plugin and are ready to deploy it to PyPI for the first time, you need to do the following:
58-
59-
* Install `poetry` and `twine` if you haven't already: `pip install poetry twine`
60-
* Check your `pyproject.toml` file is valid by running `poetry check`
61-
* Install your module: `poetry install`
62-
* Build the distribution: `poetry build`
63-
* Go to `test.pypi.org`, create an account, and get a token
64-
* Upload your package with Twine: `twine upload -r testpypi dist/*`
65-
* Enter the token when prompted
66-
* Go to `https://test.pypi.org/project/{your-package-name}`, check it has been published
67-
* Click 'Manage Project'
68-
* If you wish to enable automatic deployments, click 'Publishing' -> 'Add a new publisher' and fill in the details for your repository, setting Workflow name to `deploy.yaml` and Environment name to `test_pypi`
69-
70-
If this was all successful, repeat with the real PyPI instance at `pypi.org`, using `twine upload dist/*`, and setting the Environment name in the publisher settings to `pypi`.
71-
72-
From now on, you can do deployments automatically. Simply:
73-
74-
* Update the `pyproject.toml` with a new version number, eg `v1.0.1`
75-
* Update the CHANGELOG to reflect your newest changes
76-
* Tag a branch with a semantic version number, eg `git tag v1.0.1`
77-
* Push the tag: `git push origin v1.0.1`
78-
79-
This should automatically start the deployment workflow - check that it completes successfully on the Github UI.
80-
81-
### Updating the README
82-
When finished, delete all of the information above under the 'How to use this template' heading. Then update the information below to be relevant for your plugin:
83-
8428
## Implementation
85-
{List here how your Plugin works, and the things about the simulation it tracks by default.}
29+
This package provides a custom `TensorVue` callback, which inherits from TensorFlow's `Callback` class. This will do the following when training, testing or validating a model:
30+
31+
* Uploads the Python script creating the model as a Code Artifact
32+
* Uploads the model config as an Input Artifact
33+
* Uploads parameters about the model as Metadata
34+
* Uploads the Training Accuracy and Loss after each batch to an Epoch runUploads the Training and Validation Accuracy and Loss after each Epoch to the Simulation run
35+
* Uploads model checkpoints after each Epoch to the corresponding Epoch run as Output Artifacts(if enabled by the user)
36+
* Uploads the final model to the Simulation run as an Output Artifact
8637

8738
## Installation
8839
To install and use this plugin, first create a virtual environment:
@@ -95,7 +46,7 @@ source venv/bin/activate
9546
```
9647
And then use pip to install this module:
9748
```
98-
pip install {your_module_name_here}
49+
pip install simvue-tensorflow
9950
```
10051

10152
## Configuration
@@ -113,7 +64,60 @@ token = "..."
11364
The exact contents of both of the above options can be obtained directly by clicking the **Create new run** button on the web UI. Note that the environment variables have preference over the config file.
11465

11566
## Usage example
116-
{Give an example of how to use your plugin, with details such as the actual simulation being run abstracted away to make it as generic as possible.}
67+
68+
```python
69+
import tensorflow as tf
70+
from tensorflow import keras
71+
import numpy
72+
import matplotlib.pyplot as plt
73+
74+
# Firstly we import our Tensorflow integration:
75+
import simvue_tensorflow.plugin as sv_tf
76+
77+
# Load the training and test data
78+
(img_train, label_train), (img_test, label_test) = keras.datasets.fashion_mnist.load_data()
79+
80+
# Normalize pixel values between 0 and 1
81+
img_train = img_train.astype('float32') / 255.0
82+
img_test = img_test.astype('float32') / 255.0
83+
84+
# Create a basic model
85+
model = keras.Sequential()
86+
87+
model.add(keras.layers.Flatten(input_shape=(28, 28)))
88+
model.add(keras.layers.Dense(32, activation='relu'))
89+
model.add(keras.layers.Dense(10))
90+
91+
model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.01),
92+
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
93+
metrics=['accuracy'])
94+
95+
# At the most basic level, all we need to do is initialize our callback, providing a run name
96+
tensorvue = sv_tf.TensorVue("recognising_clothes_basic")
97+
98+
# Train the model.
99+
model.fit(
100+
img_train,
101+
label_train,
102+
epochs=5,
103+
validation_split=0.2,
104+
# Add the tensorvue class as a callback
105+
callbacks=[tensorvue,]
106+
)
107+
108+
# That's it! Check your Simvue dashboard and you should see:
109+
# - A 'simulation' run, which summarises the overall training performance
110+
# - A number of 'epoch' runs, which show the training performed in each epoch
111+
112+
# You can also use the TensorVue callback to record results from model.evaluate
113+
# Above we do it all in one step during the fitting, but you can also do it afterwards:
114+
results = model.evaluate(
115+
img_test,
116+
label_test,
117+
# Add the tensorvue class as a callback
118+
callbacks=[tensorvue,]
119+
)
120+
```
117121

118122
## License
119123

examples/basic_integration.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
TensorFlow Connector Example
3+
===============================
4+
This is a basic example of the TensorVue Connector class.
5+
6+
This example trains a CNN which is trained on a 'mnist' dataset to recognise images of items of clothing
7+
8+
To run this example:
9+
- Clone this repository: git clone https://github.com/simvue-io/plugins-tensorflow.git
10+
- Move into TensorFlow examples directory: cd examples/tensorflow
11+
- Create a simvue.toml file, copying in your information from the Simvue server: vi simvue.toml
12+
- Install Poetry: pip install poetry
13+
- Install required modules: poetry install -E tensorflow
14+
- Run the example script: poetry run python basic_integration.py
15+
16+
For a more in depth example, see: https://docs.simvue.io/examples/tensorflow/
17+
"""
18+
import tensorflow as tf
19+
from tensorflow import keras
20+
import numpy
21+
import matplotlib.pyplot as plt
22+
23+
# Firstly we import our Tensorflow integration:
24+
import simvue_tensorflow.plugin as sv_tf
25+
26+
# Load the training and test data
27+
(img_train, label_train), (img_test, label_test) = keras.datasets.fashion_mnist.load_data()
28+
29+
# Normalize pixel values between 0 and 1
30+
img_train = img_train.astype('float32') / 255.0
31+
img_test = img_test.astype('float32') / 255.0
32+
33+
# Create a basic model
34+
model = keras.Sequential()
35+
36+
model.add(keras.layers.Flatten(input_shape=(28, 28)))
37+
model.add(keras.layers.Dense(32, activation='relu'))
38+
model.add(keras.layers.Dense(10))
39+
40+
model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.01),
41+
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
42+
metrics=['accuracy'])
43+
44+
# At the most basic level, all we need to do is initialize our callback, providing a run name
45+
tensorvue = sv_tf.TensorVue("recognising_clothes_basic")
46+
47+
# Train the model.
48+
model.fit(
49+
img_train,
50+
label_train,
51+
epochs=5,
52+
validation_split=0.2,
53+
# Add the tensorvue class as a callback
54+
callbacks=[tensorvue,]
55+
)
56+
57+
# That's it! Check your Simvue dashboard and you should see:
58+
# - A 'simulation' run, which summarises the overall training performance
59+
# - A number of 'epoch' runs, which show the training performed in each epoch
60+
61+
# You can also use the TensorVue callback to record results from model.evaluate
62+
# Above we do it all in one step during the fitting, but you can also do it afterwards:
63+
results = model.evaluate(
64+
img_test,
65+
label_test,
66+
# Add the tensorvue class as a callback
67+
callbacks=[tensorvue,]
68+
)
69+
# You should now also see an evaluation run, which records accuracy and loss from the test set separately
70+
71+
# Save the entire model as a `.keras` zip archive.
72+
model.save('my_model.keras')

0 commit comments

Comments
 (0)