yolov7_cls.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # 根据yolov7改编:https://github.com/WongKinYiu/yolov7
  2. import torch
  3. import os
  4. import sys
  5. project_root = '/home/yhsun/classification-main/'
  6. sys.path.append(project_root)
  7. # print("Project root added to sys.path:", project_root)
  8. # Verify that we can access the model package directly
  9. import model
  10. from model.layer import cbs, elan, mp, sppcspc, linear_head
  11. class yolov7_cls(torch.nn.Module):
  12. def __init__(self, args):
  13. super().__init__()
  14. dim_dict = {'n': 8, 's': 16, 'm': 32, 'l': 64}
  15. n_dict = {'n': 1, 's': 1, 'm': 2, 'l': 3}
  16. dim = dim_dict[args.model_type]
  17. n = n_dict[args.model_type]
  18. output_class = args.output_class
  19. # 网络结构
  20. if not args.prune: # 正常版本
  21. self.l0 = cbs(3, dim, 1, 1)
  22. self.l1 = cbs(dim, 2 * dim, 3, 2) # input_size/2
  23. self.l2 = cbs(2 * dim, 2 * dim, 1, 1)
  24. self.l3 = cbs(2 * dim, 4 * dim, 3, 2) # input_size/4
  25. self.l4 = elan(4 * dim, 8 * dim, n)
  26. self.l5 = mp(8 * dim, 8 * dim) # input_size/8
  27. self.l6 = elan(8 * dim, 16 * dim, n)
  28. self.l7 = mp(16 * dim, 16 * dim) # input_size/16
  29. self.l8 = elan(16 * dim, 32 * dim, n)
  30. self.l9 = mp(32 * dim, 32 * dim) # input_size/32
  31. self.l10 = elan(32 * dim, 32 * dim, n)
  32. self.l11 = sppcspc(32 * dim, 16 * dim)
  33. self.l12 = cbs(16 * dim, 8 * dim, 1, 1)
  34. self.linear_head = linear_head(8 * dim, output_class)
  35. else: # 剪枝版本
  36. config = args.prune_num
  37. self.l0 = cbs(3, config[0], 1, 1)
  38. self.l1 = cbs(config[0], config[1], 3, 2) # input_size/2
  39. self.l2 = cbs(config[1], config[2], 1, 1)
  40. self.l3 = cbs(config[2], config[3], 3, 2) # input_size/4
  41. self.l4 = elan(config[3], None, n, config[4:7 + 2 * n])
  42. self.l5 = mp(config[6 + 2 * n], None, config[7 + 2 * n:10 + 2 * n]) # input_size/8
  43. self.l6 = elan(config[7 + 2 * n] + config[9 + 2 * n], None, n, config[10 + 2 * n:13 + 4 * n])
  44. self.l7 = mp(config[12 + 4 * n], None, config[13 + 4 * n:16 + 4 * n]) # input_size/16
  45. self.l8 = elan(config[13 + 4 * n] + config[15 + 4 * n], None, n, config[16 + 4 * n:19 + 6 * n])
  46. self.l9 = mp(config[18 + 6 * n], None, config[19 + 6 * n:22 + 6 * n]) # input_size/32
  47. self.l10 = elan(config[19 + 6 * n] + config[21 + 6 * n], None, n, config[22 + 6 * n:25 + 8 * n])
  48. self.l11 = sppcspc(config[24 + 8 * n], None, config[25 + 8 * n:32 + 8 * n])
  49. self.l12 = cbs(config[31 + 8 * n], config[32 + 8 * n], 1, 1)
  50. self.linear_head = linear_head(config[32 + 8 * n], output_class)
  51. def forward(self, x):
  52. x = self.l0(x)
  53. x = self.l1(x)
  54. x = self.l2(x)
  55. x = self.l3(x)
  56. x = self.l4(x)
  57. x = self.l5(x)
  58. x = self.l6(x)
  59. x = self.l7(x)
  60. x = self.l8(x)
  61. x = self.l9(x)
  62. x = self.l10(x)
  63. x = self.l11(x)
  64. x = self.l12(x)
  65. x = self.linear_head(x)
  66. return x
  67. if __name__ == '__main__':
  68. import argparse
  69. parser = argparse.ArgumentParser(description='')
  70. parser.add_argument('--prune', default=False, type=bool)
  71. parser.add_argument('--model_type', default='n', type=str)
  72. parser.add_argument('--input_size', default=32, type=int)
  73. parser.add_argument('--output_class', default=10, type=int)
  74. args = parser.parse_args()
  75. model = yolov7_cls(args)
  76. tensor = torch.rand(2, 3, args.input_size, args.input_size, dtype=torch.float32)
  77. pred = model(tensor)
  78. print(model)
  79. print(pred.shape)