コード例 #1
0
class RandomMutator:
    class Mutator:
        def __init__(self, name, total=0):
            self.name = name
            self.total = total
            self.delta_bigger_than_zero = 0

    def __init__(self, mutate_ops=None):
        self.mylogger = Logger()
        self.mylogger.info(f"Using {self.__class__.__name__} as selection strategy!")
        self._mutators = [self.Mutator(name=op) for op in mutate_ops]

    @property
    def mutators(self):
        mus = {}
        for mu in self._mutators:
            mus[mu.name] = mu
        return mus

    def choose_mutator(self, mu1=None):
        return self._mutators[np.random.randint(0, len(self._mutators))].name

    def index(self, mutator_name):
        for i, mu in enumerate(self._mutators):
            if mu.name == mutator_name:
                return i
        return -1
コード例 #2
0
 def __init__(self, mutate_ops=None):
     self.mylogger = Logger()
     self.mylogger.info(f"Using {self.__class__.__name__} as selection strategy!")
     if mutate_ops is None:
         from scripts.mutation.model_mutation_generators import all_mutate_ops
         mutate_ops = all_mutate_ops()
     self.p = 1 / len(mutate_ops)
     self._mutators = [self.Mutator(name=op) for op in mutate_ops]
コード例 #3
0
class MCMC:
    class Mutator:
        def __init__(self, name, total=0, delta_bigger_than_zero=0, epsilon=1e-7):
            self.name = name
            self.total = total
            self.delta_bigger_than_zero = delta_bigger_than_zero
            self.epsilon = epsilon

        @property
        def score(self, epsilon=1e-7):
            mylogger = Logger()
            rate = self.delta_bigger_than_zero / (self.total + epsilon)
            mylogger.info("Name:{}, rate:{}".format(self.name, rate))
            return rate

    def __init__(self, mutate_ops=None):
        self.mylogger = Logger()
        self.mylogger.info(f"Using {self.__class__.__name__} as selection strategy!")
        if mutate_ops is None:
            from scripts.mutation.model_mutation_generators import all_mutate_ops
            mutate_ops = all_mutate_ops()
        self.p = 1 / len(mutate_ops)
        self._mutators = [self.Mutator(name=op) for op in mutate_ops]

    @property
    def mutators(self):
        mus = {}
        for mu in self._mutators:
            mus[mu.name] = mu
        return mus

    def choose_mutator(self, mu1=None):
        if mu1 is None:
            # which means it's the first mutation
            return self._mutators[np.random.randint(0, len(self._mutators))].name
        else:
            self.sort_mutators()
            k1 = self.index(mu1)
            k2 = -1
            prob = 0
            while np.random.rand() >= prob:
                k2 = np.random.randint(0, len(self._mutators))
                prob = (1 - self.p) ** (k2 - k1)
            mu2 = self._mutators[k2]
            return mu2.name

    def sort_mutators(self):
        import random
        random.shuffle(self._mutators)
        self._mutators.sort(key=lambda mutator: mutator.score, reverse=True)

    def index(self, mutator_name):
        for i, mu in enumerate(self._mutators):
            if mu.name == mutator_name:
                return i
        return -1
コード例 #4
0
# -*-coding:UTF-8-*-
"""get prediction for each backend
"""
import sys
import os
import redis
import pickle
import argparse
import configparser
from scripts.tools.utils import DataUtils
from scripts.logger.lemon_logger import Logger
import warnings

main_logger = Logger()


def custom_objects():

    def no_activation(x):
        return x

    def leakyrelu(x):
        import keras.backend as K
        return K.relu(x, alpha=0.01)

    objects = {}
    objects['no_activation'] = no_activation
    objects['leakyrelu'] = leakyrelu
    return objects

コード例 #5
0
                       help="list of backends")
    parse.add_argument(
        "--mutate_num",
        type=int,
        help="number of variant models generated by each mutation operator")
    parse.add_argument("--mutate_ratio", type=float, help="ratio of mutation")
    parse.add_argument("--exp", type=str, help="experiments identifiers")
    parse.add_argument("--test_size", type=int, help="amount of testing image")
    parse.add_argument("--config_name", type=str, help="config name")
    flags, unparsed = parse.parse_known_args(sys.argv[1:])

    warnings.filterwarnings("ignore")
    lemon_cfg = configparser.ConfigParser()
    lemon_cfg.read(f"./config/{flags.config_name}")

    mutate_logger = Logger()
    pool = redis.ConnectionPool(host=lemon_cfg['redis']['host'],
                                port=lemon_cfg['redis']['port'],
                                db=lemon_cfg['redis'].getint('redis_db'))
    redis_conn = redis.Redis(connection_pool=pool)

    for k in redis_conn.keys():
        if flags.exp in k.decode("utf-8"):
            redis_conn.delete(k)

    experiment_dir = os.path.join(flags.output_dir,
                                  flags.exp)  # exp : like lenet5-mnist
    mut_dir = os.path.join(experiment_dir, "mut_model")
    crash_dir = os.path.join(experiment_dir, "crash")
    nan_dir = os.path.join(experiment_dir, "nan")
    inner_output_dir = os.path.join(experiment_dir, "inner_output")
コード例 #6
0
import math
from typing import *
from scripts.mutation.mutation_utils import *
from scripts.mutation.layer_matching import LayerMatching
import random
import os
import warnings
from scripts.logger.lemon_logger import Logger
import datetime

warnings.filterwarnings("ignore")
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'  # 只显示 warning 和 Error
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = ""

mylogger = Logger()


def _assert_indices(mutated_layer_indices: List[int], depth_layer: int):

    assert max(mutated_layer_indices
               ) < depth_layer, "Max index should be less than layer depth"
    assert min(mutated_layer_indices
               ) >= 0, "Min index should be greater than or equal to zero"


def _shuffle_conv2d(weights, mutate_ratio):
    new_weights = []
    for val in weights:
        # val is bias if len(val.shape) == 1
        if len(val.shape) > 1:
コード例 #7
0
        pickle.dump(layers_output, fw)


if __name__ == "__main__":
    """Parser of command args"""
    parse = argparse.ArgumentParser()
    parse.add_argument("--backend", type=str, help="name of backends")
    parse.add_argument("--exp", type=str, help="experiments identifiers")
    parse.add_argument("--output_dir",
                       type=str,
                       help="relative path of output dir(from root dir)")
    parse.add_argument("--data_index", type=int, help="redis db port")
    parse.add_argument("--config_name", type=str, help="config name")
    parse.add_argument("--model_idntfr", type=str, help="redis db port")
    flags, unparsed = parse.parse_known_args(sys.argv[1:])
    mylogger = Logger()
    """Load Configuration"""
    warnings.filterwarnings("ignore")
    lemon_cfg = configparser.ConfigParser()
    lemon_cfg.read(f"./config/{flags.config_name}")
    parameters = lemon_cfg['parameters']
    gpu_ids = parameters['gpu_ids']
    gpu_list = parameters['gpu_ids'].split(",")
    """Init cuda"""
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = gpu_ids
    warnings.filterwarnings("ignore")

    batch_size = 64
    """Switch backend"""
    bk_list = ['tensorflow', 'theano', 'cntk', 'mxnet']
コード例 #8
0
 def score(self, epsilon=1e-7):
     mylogger = Logger()
     rate = self.delta_bigger_than_zero / (self.total + epsilon)
     mylogger.info("Name:{}, rate:{}".format(self.name, rate))
     return rate
コード例 #9
0
ファイル: mutation_executor.py プロジェクト: Kerry-W/LEMON-1
        mutate_ops=parameters['mutate_ops'],
        exps=parameters['exps'].lstrip().rstrip().split(" "),
        origin_model_dir=parameters['origin_model_dir'],
        output_dir=parameters['output_dir'],
        backend=parameters['backend'],
        mutate_num=parameters.getint('mutate_num'),
        mutate_ratio=parameters.getfloat('mutate_ratio'),
        test_size=parameters.getint('test_size'),
        threshold=parameters.getfloat('threshold'),
        redis_db=lemon_cfg['parameters'].getint('redis_db'),
        python_prefix=parameters['python_prefix'].rstrip("/"))

    if not os.path.exists(flags.output_dir):
        os.makedirs(flags.output_dir)

    main_log = Logger()
    """Lemon process"""
    main_log.logger.info("Success: Lemon start successfully!")
    start_time = datetime.datetime.now()
    for exp_identifier in flags.exps:
        """Make directory"""
        experiment_dir = os.path.join(
            flags.output_dir, exp_identifier)  # exp : like lenet5-mnist
        mut_dir = os.path.join(experiment_dir, "mut_model")
        crash_dir = os.path.join(experiment_dir, "crash")
        nan_dir = os.path.join(experiment_dir, "nan")
        inner_output_dir = os.path.join(experiment_dir, "inner_output")
        metrics_result_dir = os.path.join(experiment_dir, "metrics_result")

        if not os.path.exists(experiment_dir):
            os.makedirs(experiment_dir)
コード例 #10
0
 def __init__(self, mutate_ops=None):
     self.mylogger = Logger()
     self.mylogger.info(f"Using {self.__class__.__name__} as selection strategy!")
     self._mutators = [self.Mutator(name=op) for op in mutate_ops]