draw_result.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import json
  2. import matplotlib.pyplot as plt
  3. import argparse
  4. import sys
  5. # Load the data from the provided JSON file
  6. def draw_pdf(json_file_path,pdf_file_path,bk):
  7. plt.switch_backend('Agg')
  8. json_file_path = json_file_path
  9. with open(json_file_path, 'r') as file:
  10. data = json.load(file)
  11. # Extracting the relevant data
  12. losses = []
  13. accuracy = []
  14. memory = []
  15. for entry in data:
  16. result = entry['result']
  17. losses.append(result['Losses'])
  18. accuracy.append(result['Accuracy'])
  19. memory.append(result['MemoryInfoList'])
  20. # Plotting the data
  21. plt.figure(figsize=(10, 6))
  22. # Plot Losses
  23. plt.plot(losses, label='Losses', marker='o')
  24. # Plot Accuracy
  25. plt.plot(accuracy, label='Accuracy', marker='x')
  26. # Plot Memory Info List
  27. plt.plot(memory, label='Memory', marker='s')
  28. # Adding title and labels
  29. plt.title(bk)
  30. plt.xlabel('Iterations')
  31. plt.ylabel('Values')
  32. plt.legend()
  33. # Display the plot
  34. plt.grid(True)
  35. pdf_file_path = pdf_file_path
  36. plt.savefig(pdf_file_path)
  37. plt.close()
  38. def draw_pdf_all(tensorflow_json_path,mxnet_json_path,losses_pdf_path, accuracy_pdf_path, memory_pdf_path):
  39. with open(tensorflow_json_path, 'r') as file:
  40. tensorflow_data = json.load(file)
  41. with open(mxnet_json_path, 'r') as file:
  42. mxnet_data = json.load(file)
  43. # Extracting the relevant data
  44. tf_losses = [entry['result']['Losses'] for entry in tensorflow_data]
  45. tf_accuracy = [entry['result']['Accuracy'] for entry in tensorflow_data]
  46. tf_memory = [entry['result']['MemoryInfoList'] for entry in tensorflow_data]
  47. mx_losses = [entry['result']['Losses'] for entry in mxnet_data]
  48. mx_accuracy = [entry['result']['Accuracy'] for entry in mxnet_data]
  49. mx_memory = [entry['result']['MemoryInfoList'] for entry in mxnet_data]
  50. # Creating plots for Losses, Accuracy, and Memory
  51. # Separate the plots into individual figures and save each as a PDF
  52. # Plot Losses
  53. plt.figure(figsize=(10, 6))
  54. plt.plot(tf_losses, label='TensorFlow Losses', marker='o')
  55. plt.plot(mx_losses, label='MXNet Losses', marker='x')
  56. plt.title('Losses Over Iterations')
  57. plt.xlabel('Iterations')
  58. plt.ylabel('Losses')
  59. plt.legend()
  60. plt.grid(True)
  61. plt.tight_layout()
  62. plt.savefig(losses_pdf_path)
  63. plt.close()
  64. # Plot Accuracy
  65. plt.figure(figsize=(10, 6))
  66. plt.plot(tf_accuracy, label='TensorFlow Accuracy', marker='o')
  67. plt.plot(mx_accuracy, label='MXNet Accuracy', marker='x')
  68. plt.title('Accuracy Over Iterations')
  69. plt.xlabel('Iterations')
  70. plt.ylabel('Accuracy')
  71. plt.legend()
  72. plt.grid(True)
  73. plt.tight_layout()
  74. plt.savefig(accuracy_pdf_path)
  75. plt.close()
  76. # Plot Memory
  77. plt.figure(figsize=(10, 6))
  78. plt.plot(tf_memory, label='TensorFlow Memory', marker='o')
  79. plt.plot(mx_memory, label='MXNet Memory', marker='x')
  80. plt.title('Memory Usage Over Iterations')
  81. plt.xlabel('Iterations')
  82. plt.ylabel('Memory Info List')
  83. plt.legend()
  84. plt.grid(True)
  85. plt.tight_layout()
  86. plt.savefig(memory_pdf_path)
  87. plt.close()
  88. if __name__ == "__main__":
  89. """Parser of command args"""
  90. parse = argparse.ArgumentParser()
  91. parse.add_argument("--backend", type=str, help="name of backends")
  92. parse.add_argument("--model_path", type=str, help="redis db port")
  93. flags, unparsed = parse.parse_known_args(sys.argv[1:])
  94. bk = ['tensorflow', 'mxnet']
  95. model_path = flags.model_path
  96. for i in bk:
  97. if 'svhn' in model_path or 'fashion2' in model_path:
  98. txt_path = model_path.split("\\")[-1][:-5].split("mut_model")[0]+i+"_train.json"
  99. else:
  100. txt_path = model_path.split("\\")[-1][:-3].split("mut_model")[0]+i+"_train.json"
  101. pdf_path = txt_path.replace(".json",".jpg")
  102. draw_pdf(txt_path,pdf_path,i)
  103. tensorflow_json_path = model_path.split("\\")[-1][:-5].split("mut_model")[0]+'tensorflow_train'+".json"
  104. mxnet_json_path = model_path.split("\\")[-1][:-5].split("mut_model")[0]+'mxnet_train'+".json"
  105. losses_pdf_path = model_path.split("\\")[-1][:-5].split("mut_model")[0]+"losses"+".jpg"
  106. accuracy_pdf_path = model_path.split("\\")[-1][:-5].split("mut_model")[0]+"accuracy"+".jpg"
  107. memory_pdf_path = model_path.split("\\")[-1][:-5].split("mut_model")[0]+"memory"+".jpg"
  108. draw_pdf_all(tensorflow_json_path,mxnet_json_path,losses_pdf_path, accuracy_pdf_path, memory_pdf_path)