Disclaimer - this repo. is no longer maintained and updated (May 2019), which was developed with the UROP, Detecting Deep Learning Software Defects (Spring 2019), HKUST.
Disclaimer - This repo. provides the tool (interface) for conducting mutation testing on machine learning. Experiments related to UROP/ML mutation-testing are not stored here. Feel free to design your own experiments and hope this repo. helps!
This repository aims to implement mutation operators for Deep Learning Mutation Testing.
The objective is to provide a tool for mutation testing on Deep Learning system. In this repository, 8 source-level mutation operators and 8 model level mutation operators will be implemented.
The concept of these mutation operators is introduced in the paper, DeepMutation: Mutation Testing of Deep Learning Systems , where the link is attached in references. However, the coding implementation of each mutation operator is not explicitly explained. In this repository, we clarify the vague part and document how each mutation operator is actually implemented, aiming to present a convenient tool for mutation testing on Deep Learning system.
Model-level mutation operators directly mutate the structure and weights of DNN without training procedure, which is more efficient for mutated model generation. Model-level mutation operators automatically analysis structure of given DNN, mutate on a copy of the original DNN, and return the mutated copy of the original DNN.
GF - Gaussian Fuzzing:
Target: Trained model (Weight)
Brief Operator Description: Fuzz a portion of weights in trained model
Implementation:
Syntax:
mutated_model = model_mut_opts.GF_mut(model, mutation_ratio, prob_distribution='normal', STD=0.1, lower_bound=None, upper_bound=None, lam=None, mutated_layer_indices=None)
Example:
# Without specification of standard deviation parameter and type of probability distribution, normal distribution is used and STD is set to 0.1 as default
GF_model = model_mut_opts.GF_mut(model, 0.01)
# With specification of probability distribution type as normal distribution and STD value as 2
GF_model = model_mut_opts.GF_mut(model, 0.01, prob_distribution='normal', STD=2)
# With specification of probability distribution type as uniform distribution and corresponding lower and upper bound
GF_model = model_mut_opts.GF_mut(model, 0.01, prob_distribution='uniform', lower_bound=-1, upper_bound=1)
# Users can also indicate the indices of layers to be mutated
GF_model = model_mut_opts.GF_mut(model, 0.01, prob_distribution='normal', STD=2, mutated_layer_indices=[0, 1])
Remarks:
WS - Weight Shuffling:
Target: Trained model (Neuron)
Brief Operator Description: Shuffle weights of selected neurons. which connect to previous layer
Implementation:
Syntax:
mutated_model = model_mut_opts.WS_mut(model, mutation_ratio, mutated_layer_indices=None)
Example:
WS_model = model_mut_opts.WS_mut(model, 0.01)
# Users can also indicate the indices of layers to be mutated
WS_model = model_mut_opts.WS_mut(model, 0.01, mutated_layer_indices=[0, 1])
Remarks:
NEB - Neuron Effect Block:
Target: Trained model (Neuron)
Brief Operator Description: Block effect of selected neurons on the next layer
Implementation:
Syntax:
mutated_model = model_mut_opts.NEB_mut(model, mutation_ratio, mutated_layer_indices=None)
Example:
NEB_model = model_mut_opts.NEB_mut(model, 0.01)
# Users can also indicate the indices of layers to be mutated
NEB_model = model_mut_opts.NEB_mut(model, 0.01, mutated_layer_indices=[0, 1])
Remarks:
NAI - Neuron Activation Inverse:
Target: Trained model (Neuron)
Brief Operator Description: Invert (the sign) of activation status of selected neurons
Implementation:
Syntax:
mutated_model = model_mut_opts.NAI_mut(model, mutation_ratio, mutated_layer_indices=None)
Example:
NAI_model = model_mut_opts.NAI_mut(model, 0.01)
# Users can also indicate the indices of layers to be mutated
NAI_model = model_mut_opts.NAI_mut(model, 0.01, mutated_layer_indices=[0, 1])
Remarks:
NS - Neuron Switch:
Target: Trained model (Neuron)
Brief Operator Description: Switch two neurons (shuffle neurons) of the same layer
Implementation:
Syntax:
mutated_model = model_mut_opts.NS_mut(model, mutation_ratio, mutated_layer_indices=None)
Example:
NS_model = model_mut_opts.NS_mut(model, 0.01)
# Users can also indicate the indices of layers to be mutated
NS_model = model_mut_opts.NS_mut(model, 0.01, mutated_layer_indices=[0, 1])
Remarks:
LD - Layer Deactivation:
Target: Trained model (Layer)
Brief Operator Description: Deactivate the effects of selected layers which satisfies conditions
Implementation:
Syntax:
mutated_model = model_mut_opts.LD_mut(model, mutated_layer_indices=None)
Example:
LD_model = model_mut_opts.LD_mut(model)
# Users can also indicate the indices of layers to be mutated
LD_model = model_mut_opts.LD_mut(model, mutated_layer_indices=[0, 1])
Remarks:
LAm - Layer Addition (model-level):
Target: Trained model (Layer)
Brief Operator Description: Add (copy) layers to suitable spots in deep learning model
Implementation:
mutated_model = model_mut_opts.LAm_mut(model, mutated_layer_indices=None)
Example:
LAm_model = model_mut_opts.LAm_mut(model)
LAm_model = model_mut_opts.LAm_mut(model, mutated_layer_indices=[0, 1])
Remarks:
1. The weights of the newly added layer in LAm are copied from the previous layer.
AFRm - Activation Function Removal (model-level):
Target: Trained model (Layer)
Brief Operator Description: Remove activation functions of selected layers
Implementation:
Syntax:
mutated_model = model_mut_opts.AFRm_mut(model, mutated_layer_indices=None)
Example:
AFRm_model = model_mut_opts.AFRm_mut(model)
# Users can also indicate the indices of layers to be mutated
AFRm_model = model_mut_opts.AFRm_mut(model, mutated_layer_indices=[0, 1])
Remarks:
Source-level mutation operators mutate either the original training dataset or the original training program. A training dataset mutant or training program mutant can further participate in the training process to generate a mutated model for mutation testing.
For each of the mutation operators, there are several user-configurable parameters can be specified. See the description of individual operators Implementation for more details.
DR - Data Repetition:
Target : Training dataset
Brief Operator Description: DR operator duplicates a portion of training dataset
Implementation:
Syntax:
mutated_dataset, copied_model = source_mut_opts.DR_mut(training_dataset, model, mutation_ratio)
Example:
(DR_train_datas, DR_train_labels), DR_model = source_mut_opts.DR_mut((train_datas, train_labels), model, 0.01)
LE - Label Error:
Target: Training dataset
Brief Operator Description: LE operator falsifies a portion of results (e.g., labels) in traning dataset
Implementation:
Syntax:
mutated_dataset, copied_model = source_mut_opts.LE_mut(training_dataset, model, label_lower_bound, label_upper_bound, mutation_ratio)
Example:
(LE_train_datas, LE_train_labels), LE_model = source_mut_opts.LE_mut((train_datas, train_labels), model, 0, 9, 0.01)
DM - Data Missing :
Target: Training dataset
Brief Operator Description: Remove a portion of training dataset
Implementation:
Syntax:
mutated_dataset, copied_model = source_mut_opts.DM_mut(training_dataset, model, mutation_ratio)
Example:
(DM_train_datas, DM_train_labels), DM_model = source_mut_opts.DM_mut((train_datas, train_labels), model, 0.01)
DF - Data Shuffle:
Target: Training dataset
Brief Operator Description: Shuffle a portion of samples among training dataset
Implementation:
Syntax:
mutated_dataset, copied_model = source_mut_opts.DF_mut(training_dataset, model, mutation_ratio)
Example:
(DF_train_datas, DF_train_labels), DF_model = source_mut_opts.DF_mut((train_datas, train_labels), model, 0.01)
Remarks:
NP - Noise Perturb:
Target: Training dataset
Brief Operator Description: Add noise to a portion of samples among training dataset
Implementation:
Syntax:
mutated_dataset, copied_model = source_mut_opts.NP_mut(training_dataset, model, mutation_ratio, STD=0.1)
Example:
# Without specification of standard deviation parameter (STD), STD is set to 0.1 as default
(NP_train_datas, NP_train_labels), NP_model = source_mut_opts.NP_mut((train_datas, train_labels), model, 0.01)
# Usage with specification of STD value as 2
(NP_train_datas, NP_train_labels), NP_model = source_mut_opts.NP_mut((train_datas, train_labels), model, 0.01, STD=2)
LR - Layer Removal:
Target: Training program
Brief Operator Description: Remove a randomly selected layer which satisfies conditions
Implementation:
Syntax:
copied_dataset, mutated_model = source_mut_opts.LR_mut(training_dataset, model)
Example:
(LR_train_datas, LR_train_labels), LR_model = source_mut_opts.LR_mut((train_datas, train_labels), model)
Remarks:
LAs - Layer Addition for source-level:
Target: Training program
Brief Operator Description: Randomly add a layer to one of suitable spots in the deep learning model
Implementation:
Syntax:
copied_dataset, mutated_model = source_mut_opts.LAs_mut(training_dataset, model)
Example:
(LAs_train_datas, LAs_train_labels), LAs_model = source_mut_opts.LAs_mut((train_datas, train_labels), model)
Remarks:
AFRs - Activation Function Removal for source-level:
Target: Training program
Brief Operator Description: Remove activation layers of a randomly selected layer
Implementation:
Syntax:
copied_dataset, mutated_model = source_mut_opts.AFRs_mut(training_dataset, model)
Example:
(AFRs_train_datas, AFRs_train_labels), AFRs_model = source_mut_opts.AFRs_mut((train_datas, train_labels), model)
Remarks:
Assumption
Suggestion
Files below are ordered in alphabetical order.
example_model_level.ipynb
This file illustrates the usage of source-level mutation operators, where usage of each mutation operator is separated into blocks for a better demonstration.
example_source_level.ipynb
This file illustrates the usage of model-level mutation operators, where usage of each mutation operator is separated into blocks for a better demonstration.
model_mut_model_generators.py
This file provides an interface for generating mutated models by model-level mutation operators. It is implemented for my own convenience to facilitate debugging and experiments. Users can solely use mutation operators without the usage of this file.
model_mut_operators.py
This file consists of two classes, ModelMutationOperators and ModelMutationOperatorsUtils. ModelMutationOperators represents the logic of mutation for each model-level mutation operator, whereas ModelMutationOperatorsUtils extracts all tedious manipulation out from ModelMutationOperators to keep the code readable and maintainable. The class ModelMutationOperators is the interface for users to directly mutate their trained models.
network.py
This file encapsulates functionalities related to neural network training into two classes, FCNetwork and CNNNetwork. For instance, dataset processing, model compilation, training process, and model evaluation. Two fully-connected neural networks and two convolutional neural networks are provided respectively in class FCNetwork and class CNNNetwork. However, this file is implemented for facilitating debugging and experiments for my own convenience. Users can use their own neural network architectures, hyperparameters configuration, and datasets.
source_mut_model_generators.py
This file provides an interface for generating mutated models by source-level mutation operators. It is implemented for my own convenience to facilitate debugging and experiments. Users can solely use mutation operators and train with either mutated dataset or mutated model by themselves without the usage of this file to generate mutated models.
source_mut_operators.py
This file consists of two classes, SourceMutationOperators and SourceMutationOperatorsUtils. SourceMutationOperators represents the logic of mutation for each source-level mutation operator, whereas SourceMutationOperatorsUtils extracts all tedious manipulation out from SourceMutationOperators to keep the code readable and maintainable. The class SourceMutationOperators is the interface for users to mutate either their untrained models or datasets.
utils.py
This file currently consists of three classes, GeneralUtils, ModelUtils, and ExaminationalUtils.
Python: 3.5.1
Tensorflow: 1.11.0
Keras: 2.2.4
NumPy: 1.15.1
Lei Ma, Fuyuan Zhang, Jiyuan Sun, Minhui Xue, Bo Li, Felix Juefei-Xu, Chao Xie, Li Li, Yang Liu, Jianjun Zhao, et al.
DeepMutation: Mutation testing of Deep Learning Systems.
https://arxiv.org/pdf/1805.05206.pdf
Jingyi Wang, Guoliang Dongy, Jun Sun, Xinyu Wangy, Peixin Zhangy
Adversarial Sample Detection for Deep Neural Network through Model Mutation Testing
https://arxiv.org/pdf/1812.05793.pdf