rongrunxiang 5 meses atrás
pai
commit
3ad41b0e6d

+ 3 - 0
README.md

@@ -2,3 +2,6 @@
 需要根据.gitignore 下载对应被忽略的大文件
 - env
 - model
+
+1. 配置server/backend/config.yml,将其设置为真实路径
+2. 配置server/backend/tool.py,将读取文件设置为真实文件路径

+ 89 - 0
server/LEMON-master/run/mutation_executor.py

@@ -18,6 +18,95 @@ os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
 os.environ["CUDA_VISIBLE_DEVICES"] = ""
 """Setting random seed"""
 
+def lemon_process(exp, root_dir, output_dir, mutate_num, config_name):
+    lemon_cfg = configparser.ConfigParser()
+    cfg_path = os.path.join(root_dir, "config", config_name)
+    lemon_cfg.read(cfg_path)
+    parameters = lemon_cfg["parameters"]
+    flags = argparse.Namespace(
+        mutate_ops=parameters["mutate_ops"],
+        exps=exp.lstrip().rstrip().split(" "),
+        origin_model_dir=os.path.join(root_dir, parameters["origin_model_dir"]),
+        output_dir=output_dir,
+        backend=parameters["backend"],
+        mutate_num=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")
+        print(experiment_dir)
+        if not os.path.exists(experiment_dir):
+            os.makedirs(experiment_dir)
+        if not os.path.exists(mut_dir):
+            os.makedirs(mut_dir)
+        if not os.path.exists(crash_dir):
+            os.makedirs(crash_dir)
+        if not os.path.exists(nan_dir):
+            os.makedirs(nan_dir)
+        if not os.path.exists(inner_output_dir):
+            os.makedirs(inner_output_dir)
+        if not os.path.exists(metrics_result_dir):
+            os.makedirs(metrics_result_dir)
+
+        try:
+            """Mutate and get output of different backends"""
+            main_log.info("INFO:Lemon mutation starting!")
+            main_log.info("INFO:Lemon for exp: {}".format(exp_identifier))
+            if "svhn" in exp_identifier or "fashion2" in exp_identifier:
+                origin_model_name = "{}_origin.hdf5".format(exp_identifier)
+            else:
+                origin_model_name = "{}_origin.h5".format(exp_identifier)
+            origin_model_file = os.path.join(flags.origin_model_dir, origin_model_name)
+            mutate_lemon = (
+                "{}/lemon/bin/python -u -m mutate_lemon --mutate_op {} --model {} --output_dir {}"
+                " --backends {} --mutate_num {} --mutate_ratio {} --exp {} --test_size {} --redis_db {} --config_name {}".format(
+                    flags.python_prefix,
+                    flags.mutate_ops,
+                    origin_model_file,
+                    flags.output_dir,
+                    flags.backend,
+                    flags.mutate_num,
+                    flags.mutate_ratio,
+                    exp_identifier,
+                    flags.test_size,
+                    flags.redis_db,
+                    config_name,
+                )
+            )
+            os.system(mutate_lemon)
+            print(mutate_lemon)
+
+        except Exception:
+            main_log.error("Error: Lemon for exp:{} Failed!".format(exp_identifier))
+            main_log.exception(sys.exc_info())
+
+    end_time = datetime.datetime.now()
+    time_delta = end_time - start_time
+    h, m, s = utils.ToolUtils.get_HH_mm_ss(time_delta)
+    main_log.info(
+        "INFO:Lemon is done: Time used: {} hour,{} min,{} sec".format(h, m, s)
+    )
+
 if __name__ == "__main__":
 
     # config_name = sys.argv[1]

+ 41 - 0
server/backend/app.py

@@ -0,0 +1,41 @@
+from flask import Flask, request
+from tool import runtool
+import yaml
+import os
+
+app = Flask(__name__)
+
+with open('config.yml', 'r') as file:
+    config = yaml.load(file, Loader=yaml.FullLoader)
+
+env = os.getenv('FLASK_ENV', 'development')
+app.config.update(config['default'])
+if env in config:
+    app.config.update(config[env])
+
+
+@app.route('/')
+def hello_world():  # put application's code here
+    return 'Hello World!'
+
+@app.route('/models', methods=['GET'])
+def getModels():
+    models = ['lexnet-cifar10', 'lenet5-fashion-mnist', 'fashion2', 'svhn', 'lenet5-mnist',
+            'alexnet-cifar10', 'mobilenet.1.00.224-imagenet', 'vgg16-imagenet']
+    return models
+
+@app.route('/run', methods=['POST'])
+def run():
+    data = request.get_json()
+    exp = data['exp']
+    mutate_num = data['mutate_num']
+    root_dir = app.config['root_dir']
+    output_dir = app.config['output_dir']
+    config_name = app.config['config_name']
+    data = runtool(exp, root_dir, output_dir, config_name, mutate_num)
+
+    print(data)
+    return data
+
+if __name__ == '__main__':
+    app.run()

+ 4 - 0
server/backend/config.yml

@@ -0,0 +1,4 @@
+default:
+  root_dir: server/LEMON-master
+  output_dir: server/LEMON-master/lemon_outputs
+  config_name: server/LEMON-master/config/demo.conf

+ 1 - 0
server/backend/requirements.txt

@@ -0,0 +1 @@
+flask~=3.0.3

+ 48 - 0
server/backend/tool.py

@@ -0,0 +1,48 @@
+import sys
+import os
+import json
+from mutation_executor import lemon_process
+
+def mock(exp:str,root_dir:str,output_dir:str,mutate_num:int,config_name:str):
+    print("running")
+    print("exp:", exp)
+    print("root_dir:", root_dir)
+    print("out_dir:", output_dir)
+    print("mutate_num:", mutate_num)
+    print("config_name:", config_name)
+    data = [
+        {
+            "model": "lenet5-fashion-mnist",
+            "method": "origin0",
+            "result": {
+                "Losses": 0.3032414233312011,
+                "Accuracy": 0.9062593914568424,
+                "MemoryInfoList": 1.2329671382904053
+            }
+        }
+    ]
+    return data
+
+def runtool(exp:str,root_dir:str,output_dir:str,mutate_num:int,config_name:str):
+    print("running")
+    print("exp:", exp)
+    print("root_dir:", root_dir)
+    print("out_dir:", output_dir)
+    print("mutate_num:", mutate_num)
+    print("config_name:", config_name)
+
+    base_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+    print(base_dir)
+    target_dir = os.path.join(base_dir, 'server', 'LEMON-master', 'run')
+    print(target_dir)
+    sys.path.append(target_dir)
+
+
+    lemon_process(exp,root_dir,output_dir,mutate_num,config_name)
+    #TODO:设置为真实的文件路径
+    with open("server/LEMON-master/lemon_outputs/mxnet.json","r") as file1:#tensorflow.json
+        data1 = json.load(file1)
+    with open("server/LEMON-master/lemon_outputs/tensorflow.json","r") as file2
+        data2 = json.load(file2)
+    combined_data = [data1,data2]
+    return combined_data