generate_img.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import csv
  2. import os
  3. import shutil
  4. def get_ori_img(ori_file_path):
  5. ori_img_list = []
  6. with open(ori_file_path, 'r') as f:
  7. f.seek(0)
  8. for i, line in enumerate(f):
  9. if i == 0:
  10. continue
  11. data = line.split(',')
  12. ori_img_id = data[0]
  13. # ori_carla_angle = float(data[3])
  14. # ori_img_predict_angle = float(data[1])
  15. # ori_img_data = [ori_img_id, ori_img_predict_angle]
  16. ori_img_list.append(ori_img_id)
  17. f.close()
  18. return ori_img_list
  19. def get_err_img(mut_csv_path, ori_csv_path):
  20. img_list = [[], [], [], [], [], []]
  21. temp_str = ''
  22. with open(mut_csv_path, 'r') as f: # get error csv
  23. f.seek(0)
  24. for i, line in enumerate(f):
  25. if i == 0:
  26. continue
  27. data = line.split(',')
  28. if len(data) != 8 and temp_str == '':
  29. temp_str = line
  30. continue
  31. if temp_str != '':
  32. line = temp_str + line
  33. temp_str = ''
  34. data = line.split(',')
  35. img_id = data[0]
  36. predict_angle = float(data[1])
  37. mut_angle = float(data[2])
  38. if data[3][0] == '"':
  39. data[3] = data[3].split('"')[1]
  40. ori_angle = float(data[3])
  41. model_name = data[4]
  42. scene_name = int(data[5])
  43. frame_id = int(data[6])
  44. data = [img_id, predict_angle, mut_angle, ori_angle, model_name, scene_name, frame_id]
  45. img_list[scene_name - 1].append([data, abs(mut_angle - ori_angle)])
  46. list_a = [] # get top 20
  47. for l in img_list:
  48. l.sort(key=lambda x: x[1], reverse=True)
  49. if len(l) > 20:
  50. l = l[:20]
  51. list_a.append(l)
  52. img_list = list_a
  53. ori_img_list = get_ori_img(ori_csv_path)
  54. ret_img_list = [[], [], [], [], [], []]
  55. for l in img_list:
  56. for img in l:
  57. data = img[0]
  58. img_id = data[0]
  59. predict_angle = float(data[1])
  60. mut_angle = float(data[2])
  61. ori_angle = float(data[3])
  62. model_name = data[4]
  63. scene_name = int(data[5])
  64. frame_id = int(data[6])
  65. true_frame_id = frame_id + (scene_name - 1) * 125 # get ori img id
  66. ori_img_id = ori_img_list[true_frame_id]
  67. ret_img_list[scene_name - 1].append(
  68. [img_id, ori_img_id, ori_angle, predict_angle, mut_angle, scene_name, model_name, frame_id])
  69. return ret_img_list
  70. def save_csv(img_list, save_path):
  71. with open(save_path, 'a+', encoding='utf-8') as f:
  72. csv_writer = csv.writer(f)
  73. for line in range(len(img_list)):
  74. csv_writer.writerow(img_list[line])
  75. def save_img(srcfile, save_path):
  76. if not os.path.isfile(srcfile):
  77. print("%s not exist!" % srcfile)
  78. else:
  79. if not os.path.exists(save_path):
  80. os.makedirs(save_path) # 创建路径
  81. shutil.copy(srcfile, save_path) # 复制文件
  82. print("copy %s -> %s" % (srcfile, save_path))
  83. def save_err_img(img_list_by_model, mut_file_path, save_path):
  84. for img in img_list_by_model:
  85. #ori_img_path = ori_file_path + img_by_scene[1]
  86. #save_img(ori_img_path, ori_save_path)
  87. mut_img_path = mut_file_path + img[0]
  88. save_img(mut_img_path, save_path)
  89. # [dave v1, v2, v3, epoch] origin data
  90. ori_file_path = '/home/jlutripper/Document/seed_data_train/'
  91. mut_file_path = '/home/software/FuzzScene/code/Violated images/erimages/model_'
  92. save_path = 'err_img/'
  93. for i in range (1,5):
  94. mut_csv_path = mut_file_path + str(i) + '/error.csv'
  95. ori_img_path = ori_file_path + 'seed' + str(i) +'/'
  96. err_img_list = get_err_img(mut_csv_path = mut_csv_path, ori_csv_path=ori_file_path+'model1_oriMSE.csv')
  97. for j in range (1,7):
  98. save_csv(err_img_list[j-1],save_path = save_path + str(i) + '/' + str(j) +'/list.csv')
  99. save_err_img(err_img_list[j-1], mut_file_path + str(i) + '/', save_path + str(i) + '/' + str(j) + '/')
  100. # 输入: 6*125*4 4个模型 对于6个初始场景的初始预测值
  101. # 4个模型 100个变异种子*125张图中 发现的错误图像
  102. # 输出: 转向角误差从大到小(绝对值)排列的 每个模型 每个场景