analysis_inter_inputs.py 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import sys
  2. import os
  3. import numpy as np
  4. import math
  5. import pandas as pd
  6. import warnings
  7. import configparser
  8. from itertools import combinations
  9. warnings.filterwarnings("ignore")
  10. os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2' # 只显示 warning 和 Error
  11. os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
  12. os.environ["CUDA_VISIBLE_DEVICES"] = ""
  13. if __name__ == '__main__':
  14. # get experiments configuration
  15. config_name = sys.argv[1]
  16. lemon_cfg = configparser.ConfigParser()
  17. lemon_cfg.read(f"./config/{config_name}")
  18. parameters = lemon_cfg['parameters']
  19. output_dir = parameters['output_dir']
  20. output_dir = output_dir[:-1] if output_dir.endswith("/") else output_dir
  21. threshold = parameters.getfloat('threshold')
  22. current_container = output_dir.rstrip("/").split("/")[-1]
  23. pre_backends = parameters['backend'].split(" ")
  24. backends_str = "-".join(pre_backends)
  25. backend_pairs = [f"{pair[0]}_{pair[1]}" for pair in combinations(pre_backends, 2)]
  26. compare_columns = ["original","mutant(max)"]
  27. """Get all exps"""
  28. exps = parameters['exps'].lstrip().rstrip().split(" ")
  29. exps.sort(key=lambda x:x)
  30. backends_exp_indntfrs = {bk_p:{exp_id:list() for exp_id in exps} for bk_p in backend_pairs}
  31. for exp in exps:
  32. print('#########{}#########'.format(exp))
  33. exp_dir = os.path.join(output_dir, exp)
  34. metrics_dir = os.path.join(exp_dir, "metrics_result")
  35. exp_metrics_path = os.path.join(metrics_dir, "{}_D_MAD_result.csv".format(exp))
  36. with open(exp_metrics_path, "r") as fr:
  37. lines = fr.readlines()[1:]
  38. for line in lines:
  39. line_split = line.split(",")
  40. # incon_idntfr like mobilenet.1.00.224-imagenet_origin0_theano_cntk_input1494
  41. incon_idntfr, incon_value = line_split[0], float(line_split[1])
  42. # print(incon_value)
  43. incon_idntfr_split = incon_idntfr.split("_")
  44. incon_tuple = (incon_idntfr, incon_value )
  45. backend_pair,input_key = "{}_{}".format(incon_idntfr_split[2],incon_idntfr_split[3]),incon_idntfr_split[-1]
  46. if not math.isnan(incon_value):
  47. backends_exp_indntfrs[backend_pair][exp].append(incon_tuple)
  48. all_inputs_name = {bk_p: {exp_id: set() for exp_id in exps} for bk_p in backend_pairs}
  49. for bk_pair in backend_pairs:
  50. exp_idntfrs = backends_exp_indntfrs[bk_pair]
  51. for exp_id,incon_idntfrs in exp_idntfrs.items():
  52. for incon_tuple in incon_idntfrs:
  53. (incon_idntfr, incon_value) = incon_tuple
  54. incon_idntfr_split = incon_idntfr.split("_")
  55. input_k = incon_idntfr_split[-1]
  56. model_type = incon_idntfr_split[1]
  57. if incon_value >= threshold:
  58. all_inputs_name[bk_pair][exp_id].add(input_k)
  59. for bk_pair in backend_pairs:
  60. exp_idntfrs = backends_exp_indntfrs[bk_pair]
  61. exp_analysis = {}
  62. for exp_id,incon_idntfrs in exp_idntfrs.items():
  63. exp_analysis[exp_id] = {input_name:[0,0] for input_name in all_inputs_name[bk_pair][exp_id]}
  64. for incon_tuple in incon_idntfrs:
  65. (incon_idntfr, incon_value) = incon_tuple
  66. incon_idntfr_split = incon_idntfr.split("_")
  67. input_k = incon_idntfr_split[-1]
  68. if input_k in exp_analysis[exp_id].keys():
  69. model_type = incon_idntfr_split[1]
  70. if model_type == "origin0":
  71. exp_analysis[exp_id][input_k][0] = incon_value
  72. else:
  73. if incon_value > exp_analysis[exp_id][input_k][1]:
  74. exp_analysis[exp_id][input_k][1] = incon_value
  75. input_keys = list(exp_analysis[exp_id].keys())
  76. input_keys.sort()
  77. data = np.zeros((len(input_keys), 2), dtype=np.float)
  78. tuples = []
  79. df = pd.DataFrame(data, columns=compare_columns,index=input_keys)
  80. for input_idx in input_keys:
  81. df.loc[input_idx,compare_columns[0]] = exp_analysis[exp_id][input_idx][0]
  82. df.loc[input_idx,compare_columns[1]] = exp_analysis[exp_id][input_idx][1]
  83. # print(df)
  84. save_dir = os.path.join(output_dir, exp_id,"{}_inter".format(exp_id))
  85. if not os.path.exists(save_dir):
  86. os.makedirs(save_dir)
  87. df.to_csv(os.path.join(save_dir,"{}.csv".format(bk_pair)))