Domain-specific generation of test inputs for robustness testing of ML models
Project description
InSynth
Robustness testing of Keras models using domain-specific input generation in Python
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
Table of Contents
About The Project
The robustness of machine learning models is crucial to their safe and reliable operation in real-world applications. However, conducting robustness tests is hard as it requires evaluating the model under test repeatedly on different datasets.
InSynth provides an easy-to-use, efficient and reliable framework for conducting robustness tests.
It works by applying a set of domain-specific input generation techniques (image, audio or text) to the seed dataset, and then evaluating the model under test on the generated inputs. Then, a set of coverage criteria are evaluated to determine how well each dataset covers the model. Finally, a report is generated comparing the models' performance and coverage on different generated datasets.
Built With
Getting Started
This section describes the steps to follow when you want to get started with the InSynth project.
Prerequisites
Before installing InSynth, make sure you have the following software applications installed and updated to the latest version.
Installation
To install InSynth, only one step is required.
Run the following command to install the python package from the PyPI repository:
pip install insynth
Usage
InSynth can be used in a variety of ways depending on the goal you are trying to achieve.
Data Generation
To mutate an existing dataset using any of the perturbators provided in the framework, follow the steps below.
- Import the perturbator (e.g. the
ImageNoisePerturbator) from the respective module.from insynth.perturbators.image import ImageNoisePerturbator
- Create an instance of the perturbator.
perturbator = ImageNoisePerturbator()
- Create a
PILimage object from a file stored on disk and apply the perturbator to it.seed_image = Image.open('path/to/image.jpg') mutated_image = perturbator.apply(seed_image)
For audio perturbators, the same procedure applies but using thelibrosa.loadmethod. Similarly, text perturbators expect the seed text to be provided as a string. - Save the mutated image to disk or display it.
mutated_image.save('path/to/mutated_image.jpg') mutated_image.show()
Coverage Criteria Calculation
To calculate the coverage criteria for a model, follow the steps below.
- Import the coverage criteria (e.g. the
CoverageCriteria) from the respective module.from insynth.calculators.neuron import StrongNeuronActivationCoverageCalculator
- Create an instance of the coverage criteria and pass the model to be tested to the constructor.
coverage_calculator = StrongNeuronActivationCoverageCalculator(model)
If applicable, run theupdate_neuron_boundsmethod to determine the neuron bounds of the model.coverage_calculator.update_neuron_bounds(training_dataset)
- Run the
update_coveragemethod to update model coverage for the given input.coverage_calculator.update_coverage(input_data)
- Run the
get_coveragemethod to retrieve the current model coverage.coverage = coverage_calculator.get_coverage()
- Print the coverage to the console.
print(coverage)
Robustness Testing
The previous two sections describe how to generate a mutated dataset and calculate the coverage criteria for a model. These are prerequisites for testing the robustness of a model. In order to conduct a full end-to-end robustness test, the runner class is provided in InSynth.
- Import the runner class from the respective module.
from insynth.runners import BasicImageRunner
- Create an instance of the runner class and pass the list of perturbators, the list of coverage calculators and the model to the constructor in addition to the dataset inputs and target variables.
runner = BasicImageRunner(list_of_perturbators, list_of_coverage_calculators, dataset_x, dataset_y, model)
Note that thedataset_xparameter should be a method returning a python generator iterating over all samples to enable the processing of large datasets which do not fit into memory.dataset_x = lambda: (x for x in dataset)
- Run the
runmethod to conduct the end-to-end robustness test.report, robustness_score = runner.run()
- Use the
reportvariable to analyse the test results or use therobustness_scorevariable to retrieve a single robustness measure of the model.print(report) print(robustness_score)
If you want to apply all available perturbators and coverage calculators for a given domain, utilize the respective ComprehensiveRunner classes.
For more examples, please refer to the Documentation
Reproduce Experimental Results
The experimental results from the thesis can be reproduced by running the corresponding scripts in the experiments directory.
The performance comparison experiment is conducted in the reproduce_coverage_speed_comparison.py script.
The performance and coverage experiment is conducted in the reproduce_imagenet_results.py, reproduce_speaker_recognition_results.py and reproduce_sentiment_analysis_results.py scripts.
To generate the speaker recognition and sentiment analysis models, first the generate_model_speaker_recognition.py and generate_model_sentiment_analysis.py scripts have to be run.
The perturbation strength experiment is conducted in the reproduce_imagenet_sensitivity_results.py, reproduce_speaker_recognition_sensitivity_results.py and reproduce_sentiment_analysis_sensitivity_results.py scripts.
Lastly, the diagrams used in the thesis can be generated by running the result_analysis.ipynb notebook.
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE for more information.
Contact
Marian Lambert
Project Link: https://github.com/mlxyz/insynth
Project details
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 insynth-1.0.2.tar.gz.
File metadata
- Download URL: insynth-1.0.2.tar.gz
- Upload date:
- Size: 23.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b83fa76dd944cee62ff83e56ca8257c661c4e5f9a9279a2a153f4cc57734629
|
|
| MD5 |
f69bf58ebf897648f6743dcaf9f64326
|
|
| BLAKE2b-256 |
913b139225916ccbd8942f33da2c6f2a60bbaa2bf89abfeec43ad001abda8138
|
File details
Details for the file insynth-1.0.2-py3-none-any.whl.
File metadata
- Download URL: insynth-1.0.2-py3-none-any.whl
- Upload date:
- Size: 35.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ba3a433709990e4272fa97f6bb6cd41cbf75b5b6c8ffd04777f40244fdd65e1
|
|
| MD5 |
a778f63f45531b6a332c64b96d60fddc
|
|
| BLAKE2b-256 |
752351183c9613963600b6cb783cab88dc84f70f731b53274ef5314c9ebcb250
|