predict_lm.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import time
  2. import skimage
  3. from models.line_detect.postprocess import show_predict, show_box, show_box_or_line, show_box_and_line, \
  4. show_line_optimized, show_line
  5. import os
  6. import torch
  7. from PIL import Image
  8. import matplotlib.pyplot as plt
  9. import matplotlib as mpl
  10. import numpy as np
  11. from models.line_detect.line_net import linenet_resnet50_fpn
  12. from torchvision import transforms
  13. # from models.wirenet.postprocess import postprocess
  14. from models.wirenet.postprocess import postprocess
  15. from rtree import index
  16. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  17. def load_best_model(model, save_path, device):
  18. if os.path.exists(save_path):
  19. checkpoint = torch.load(save_path, map_location=device)
  20. model.load_state_dict(checkpoint['model_state_dict'])
  21. # if optimizer is not None:
  22. # optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
  23. epoch = checkpoint['epoch']
  24. loss = checkpoint['loss']
  25. print(f"Loaded best model from {save_path} at epoch {epoch} with loss {loss:.4f}")
  26. else:
  27. print(f"No saved model found at {save_path}")
  28. return model
  29. def box_line_(imgs, pred):
  30. im = imgs.permute(1, 2, 0).cpu().numpy()
  31. line_data = pred[-1]['wires']['lines'][0].cpu().numpy() / 128 * 512
  32. line_scores = pred[-1]['wires']['score'].cpu().numpy()[0]
  33. diag = (im.shape[0] ** 2 + im.shape[1] ** 2) ** 0.5
  34. line, score = postprocess(line_data, line_scores, diag * 0.01, 0, False)
  35. for idx, box_ in enumerate(pred[0:-1]):
  36. box = box_['boxes'] # 是一个tensor
  37. line_ = []
  38. score_ = []
  39. for i in box:
  40. score_max = 0.0
  41. tmp = [[0.0, 0.0], [0.0, 0.0]]
  42. for j in range(len(line)):
  43. if (line[j][0][1] >= i[0] and line[j][1][1] >= i[0] and
  44. line[j][0][1] <= i[2] and line[j][1][1] <= i[2] and
  45. line[j][0][0] >= i[1] and line[j][1][0] >= i[1] and
  46. line[j][0][0] <= i[3] and line[j][1][0] <= i[3]):
  47. if score[j] > score_max:
  48. tmp = line[j]
  49. score_max = score[j]
  50. line_.append(tmp)
  51. score_.append(score_max)
  52. processed_list = torch.tensor(line_)
  53. pred[idx]['line'] = processed_list
  54. processed_s_list = torch.tensor(score_)
  55. pred[idx]['line_score'] = processed_s_list
  56. return pred
  57. def predict(pt_path, model, img):
  58. model = load_best_model(model, pt_path, device)
  59. model.eval()
  60. if isinstance(img, str):
  61. img = Image.open(img).convert("RGB")
  62. transform = transforms.ToTensor()
  63. img_tensor = transform(img) # [3, 512, 512]
  64. # img_ = img_tensor
  65. # 将图像调整为512x512大小
  66. t_start = time.time()
  67. im = img_tensor.permute(1, 2, 0) # [512, 512, 3]
  68. im_resized = skimage.transform.resize(im.cpu().numpy().astype(np.float32), (512, 512)) # (512, 512, 3)
  69. img_ = torch.tensor(im_resized).permute(2, 0, 1)
  70. t_end = time.time()
  71. print(f'switch img used:{t_end - t_start}')
  72. with torch.no_grad():
  73. predictions = model([img_.to(device)])
  74. # print(predictions)
  75. t_start = time.time()
  76. pred = box_line_(img_, predictions)
  77. t_end = time.time()
  78. print(f'Matched boxes and lines used: {t_end - t_start:.4f} seconds')
  79. show_predict(img_, pred, t_start)
  80. if __name__ == '__main__':
  81. t_start = time.time()
  82. print(f'start to predict:{t_start}')
  83. model = linenet_resnet50_fpn().to(device)
  84. pt_path = r'D:\python\PycharmProjects\20250214\weight\resnet50_best_e100.pth'
  85. # img_path = f'D:\python\PycharmProjects\data2\images/train/2024-11-27-15-43-13_SaveImage.png' # 工件图
  86. # img_path = f'D:\python\PycharmProjects\data\images/train/00558656_3.png' # wireframe图
  87. img_path = r'C:\Users\m2337\Desktop\p\9.jpg'
  88. predict(pt_path, model, img_path)
  89. t_end = time.time()
  90. print(f'predict used:{t_end - t_start}')