reco2.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import torch
  2. import torchvision.transforms as transforms
  3. from PIL import Image
  4. from lenet5 import Lenet5
  5. import cv2
  6. # 加载预训练的模型
  7. model = Lenet5()
  8. model.load_state_dict(torch.load('lenet5.pt'))
  9. model.eval()
  10. class Lenet5Recognizier:
  11. def __init__(self, model_file="./lenet5.pt"):
  12. super(Lenet5Recognizier, self).__init__()
  13. # 创建模型
  14. self.net = Lenet5()
  15. # cuda
  16. self.state = torch.load(model_file, map_location='cpu')
  17. self.net.load_state_dict(self.state)
  18. def recognizie(imgs):
  19. model = Lenet5()
  20. model.load_state_dict(torch.load('lenet5.pt'))
  21. model.eval()
  22. # 图像预处理
  23. transform = transforms.Compose([
  24. transforms.Resize((150, 150)),
  25. transforms.Grayscale(),
  26. transforms.ToTensor(),
  27. transforms.Normalize(mean=[0.485], std=[0.229])
  28. ])
  29. # 加载要预测的图像
  30. image_path = imgs
  31. img = cv2.imread(image_path)
  32. # img = cv2.resize(img, (300, 200))
  33. img = Image.fromarray(img)
  34. img = transform(img)
  35. # 将图片转换为适合模型输入的张量形状
  36. img = img.unsqueeze(0)
  37. # 使用GPU进行预测(如果可用)
  38. device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  39. model = model.to(device)
  40. img = img.to(device)
  41. # 进行预测
  42. with torch.no_grad():
  43. output = model(img)
  44. probabilities = torch.softmax(output, dim=1) # 对预测结果进行softmax操作
  45. # 获取预测结果
  46. _, predicted = torch.max(output, 1)
  47. class_names = ['布', '石头', '剪刀']
  48. predicted_class = class_names[predicted.item()]
  49. predicted_probabilities = probabilities[0].tolist() # 将概率张量转换为Python列表
  50. return predicted_class,predicted_probabilities
  51. # def recognizie(imgs):
  52. # model = Lenet5()
  53. # model.load_state_dict(torch.load('lenet5.pt'))
  54. # model.eval()
  55. # # 图像预处理
  56. # transform = transforms.Compose([
  57. # transforms.Resize((150, 150)),
  58. # transforms.Grayscale(),
  59. # transforms.ToTensor(),
  60. # transforms.Normalize(mean=[0.485], std=[0.229])
  61. # ])
  62. #
  63. # # 加载要预测的图像
  64. # image_path = imgs
  65. # img = cv2.imread(image_path)
  66. #
  67. # # img = cv2.resize(img, (300, 200))
  68. #
  69. # img = Image.fromarray(img)
  70. # img = transform(img)
  71. #
  72. #
  73. #
  74. # # 将图片转换为适合模型输入的张量形状
  75. # img = img.unsqueeze(0)
  76. #
  77. # # 使用GPU进行预测(如果可用)
  78. # device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  79. # model = model.to(device)
  80. # img = img.to(device)
  81. #
  82. # # 进行预测
  83. # with torch.no_grad():
  84. # output = model(img)
  85. # probabilities = torch.softmax(output, dim=1) # 对预测结果进行softmax操作
  86. #
  87. # # 获取预测结果
  88. # _, predicted = torch.max(output, 1)
  89. # class_names = ['布', '石头', '剪刀']
  90. # predicted_class = class_names[predicted.item()]
  91. # predicted_probabilities = probabilities[0].tolist() # 将概率张量转换为Python列表
  92. # return predicted_class,predicted_probabilities
  93. #
  94. # print(recognizie('1.png'))