postprocess.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import torch
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from torchvision import transforms
  5. def box_line(pred):
  6. '''
  7. :param pred: 预测结果
  8. :return:
  9. box与line一一对应
  10. {'box': [0.0, 34.23157501220703, 151.70858764648438, 125.10173797607422], 'line': array([[ 1.9720564, 81.73457 ],
  11. [ 1.9933801, 41.730167 ]], dtype=float32)}
  12. '''
  13. box_line = [[] for _ in range((len(pred) - 1))]
  14. for idx, box_ in enumerate(pred[0:-1]):
  15. box = box_['boxes'] # 是一个tensor
  16. line = pred[-1]['wires']['lines'][idx].cpu().numpy() / 128 * 512
  17. score = pred[-1]['wires']['score'][idx]
  18. for i in box:
  19. aaa = {}
  20. aaa['box'] = i.tolist()
  21. aaa['line'] = []
  22. score_max = 0.0
  23. for j in range(len(line)):
  24. if (line[j][0][0] >= i[0] and line[j][1][0] >= i[0] and line[j][0][0] <= i[2] and
  25. line[j][1][0] <= i[2] and line[j][0][1] >= i[1] and line[j][1][1] >= i[1] and
  26. line[j][0][1] <= i[3] and line[j][1][1] <= i[3]):
  27. if score[j] > score_max:
  28. aaa['line'] = line[j]
  29. score_max = score[j]
  30. box_line[idx].append(aaa)
  31. def box_line_(pred):
  32. '''
  33. 形式同pred
  34. '''
  35. for idx, box_ in enumerate(pred[0:-1]):
  36. box = box_['boxes'] # 是一个tensor
  37. line = pred[-1]['wires']['lines'][idx].cpu().numpy() / 128 * 512
  38. score = pred[-1]['wires']['score'][idx]
  39. line_ = []
  40. for i in box:
  41. score_max = 0.0
  42. tmp = [[0.0, 0.0], [0.0, 0.0]]
  43. for j in range(len(line)):
  44. if (line[j][0][0] >= i[0] and line[j][1][0] >= i[0] and line[j][0][0] <= i[2] and
  45. line[j][1][0] <= i[2] and line[j][0][1] >= i[1] and line[j][1][1] >= i[1] and
  46. line[j][0][1] <= i[3] and line[j][1][1] <= i[3]):
  47. if score[j] > score_max:
  48. tmp = line[j]
  49. score_max = score[j]
  50. line_.append(tmp)
  51. processed_list = torch.tensor(line_)
  52. pred[idx]['line'] = processed_list
  53. return pred
  54. def show_(imgs, pred, epoch, writer):
  55. col = [
  56. '#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c',
  57. '#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5',
  58. '#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f',
  59. '#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5',
  60. '#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3',
  61. '#fdb462', '#b3de69', '#fccde5', '#bc80bd', '#ccebc5',
  62. '#ffed6f', '#8da0cb', '#e78ac3', '#e5c494', '#b3b3b3',
  63. '#fdbf6f', '#ff7f00', '#cab2d6', '#637939', '#b5cf6b',
  64. '#cedb9c', '#8c6d31', '#e7969c', '#d6616b', '#7b4173',
  65. '#ad494a', '#843c39', '#dd8452', '#f7f7f7', '#cccccc',
  66. '#969696', '#525252', '#f7fcfd', '#e5f5f9', '#ccece6',
  67. '#99d8c9', '#66c2a4', '#2ca25f', '#008d4c', '#005a32',
  68. '#f7fcf0', '#e0f3db', '#ccebc5', '#a8ddb5', '#7bccc4',
  69. '#4eb3d3', '#2b8cbe', '#08589e', '#f7fcfd', '#e0ecf4',
  70. '#bfd3e6', '#9ebcda', '#8c96c6', '#8c6bb4', '#88419d',
  71. '#810f7c', '#4d004b', '#f7f7f7', '#efefef', '#d9d9d9',
  72. '#bfbfbf', '#969696', '#737373', '#525252', '#252525',
  73. '#000000', '#ffffff', '#ffeda0', '#fed976', '#feb24c',
  74. '#fd8d3c', '#fc4e2a', '#e31a1c', '#bd0026', '#800026',
  75. '#ff6f61', '#ff9e64', '#ff6347', '#ffa07a', '#fa8072'
  76. ]
  77. print(len(col))
  78. im = imgs[0].permute(1, 2, 0)
  79. boxes = pred[0]['boxes'].cpu().numpy()
  80. line = pred[0]['line'].cpu().numpy()
  81. # 可视化预测结
  82. fig, ax = plt.subplots(figsize=(10, 10))
  83. ax.imshow(np.array(im))
  84. for idx, box in enumerate(boxes):
  85. x0, y0, x1, y1 = box
  86. ax.add_patch(
  87. plt.Rectangle((x0, y0), x1 - x0, y1 - y0, fill=False, edgecolor=col[idx], linewidth=1))
  88. for idx, (a, b) in enumerate(line):
  89. ax.scatter(a[0], a[1], c=col[99 - idx], s=2)
  90. ax.scatter(b[0], b[1], c=col[99 - idx], s=2)
  91. ax.plot([a[0], b[0]], [a[1], b[1]], c=col[idx], linewidth=1)
  92. # 将Matplotlib图像转换为Tensor
  93. fig.canvas.draw()
  94. image_from_plot = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8).reshape(
  95. fig.canvas.get_width_height()[::-1] + (3,))
  96. plt.close()
  97. img2 = transforms.ToTensor()(image_from_plot)
  98. writer.add_image("all", img2, epoch)