plateNet.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import torch.nn as nn
  2. import torch
  3. import torch.nn.functional as F
  4. class myNet_ocr(nn.Module):
  5. def __init__(self,cfg=None,num_classes=78,export=False):
  6. super(myNet_ocr, self).__init__()
  7. if cfg is None:
  8. cfg =[32,32,64,64,'M',128,128,'M',196,196,'M',256,256]
  9. # cfg =[32,32,'M',64,64,'M',128,128,'M',256,256]
  10. self.feature = self.make_layers(cfg, True)
  11. self.export = export
  12. # self.classifier = nn.Linear(cfg[-1], num_classes)
  13. # self.loc = nn.MaxPool2d((2, 2), (5, 1), (0, 1),ceil_mode=True)
  14. # self.loc = nn.AvgPool2d((2, 2), (5, 2), (0, 1),ceil_mode=False)
  15. self.loc = nn.MaxPool2d((5, 2), (1, 1),(0,1),ceil_mode=False)
  16. self.newCnn=nn.Conv2d(cfg[-1],num_classes,1,1)
  17. # self.newBn=nn.BatchNorm2d(num_classes)
  18. def make_layers(self, cfg, batch_norm=False):
  19. layers = []
  20. in_channels = 3
  21. for i in range(len(cfg)):
  22. if i == 0:
  23. conv2d =nn.Conv2d(in_channels, cfg[i], kernel_size=5,stride =1)
  24. if batch_norm:
  25. layers += [conv2d, nn.BatchNorm2d(cfg[i]), nn.ReLU(inplace=True)]
  26. else:
  27. layers += [conv2d, nn.ReLU(inplace=True)]
  28. in_channels = cfg[i]
  29. else :
  30. if cfg[i] == 'M':
  31. layers += [nn.MaxPool2d(kernel_size=3, stride=2,ceil_mode=True)]
  32. else:
  33. conv2d = nn.Conv2d(in_channels, cfg[i], kernel_size=3, padding=(1,1),stride =1)
  34. if batch_norm:
  35. layers += [conv2d, nn.BatchNorm2d(cfg[i]), nn.ReLU(inplace=True)]
  36. else:
  37. layers += [conv2d, nn.ReLU(inplace=True)]
  38. in_channels = cfg[i]
  39. return nn.Sequential(*layers)
  40. def forward(self, x):
  41. x = self.feature(x)
  42. x=self.loc(x)
  43. x=self.newCnn(x)
  44. # x=self.newBn(x)
  45. if self.export:
  46. conv = x.squeeze(2) # b *512 * width
  47. conv = conv.transpose(2,1) # [w, b, c]
  48. # conv =conv.argmax(dim=2)
  49. return conv
  50. else:
  51. b, c, h, w = x.size()
  52. assert h == 1, "the height of conv must be 1"
  53. conv = x.squeeze(2) # b *512 * width
  54. conv = conv.permute(2, 0, 1) # [w, b, c]
  55. # output = F.log_softmax(self.rnn(conv), dim=2)
  56. output = torch.softmax(conv, dim=2)
  57. return output
  58. myCfg = [32,'M',64,'M',96,'M',128,'M',256]
  59. class myNet(nn.Module):
  60. def __init__(self,cfg=None,num_classes=3):
  61. super(myNet, self).__init__()
  62. if cfg is None:
  63. cfg = myCfg
  64. self.feature = self.make_layers(cfg, True)
  65. self.classifier = nn.Linear(cfg[-1], num_classes)
  66. def make_layers(self, cfg, batch_norm=False):
  67. layers = []
  68. in_channels = 3
  69. for i in range(len(cfg)):
  70. if i == 0:
  71. conv2d =nn.Conv2d(in_channels, cfg[i], kernel_size=5,stride =1)
  72. if batch_norm:
  73. layers += [conv2d, nn.BatchNorm2d(cfg[i]), nn.ReLU(inplace=True)]
  74. else:
  75. layers += [conv2d, nn.ReLU(inplace=True)]
  76. in_channels = cfg[i]
  77. else :
  78. if cfg[i] == 'M':
  79. layers += [nn.MaxPool2d(kernel_size=3, stride=2,ceil_mode=True)]
  80. else:
  81. conv2d = nn.Conv2d(in_channels, cfg[i], kernel_size=3, padding=1,stride =1)
  82. if batch_norm:
  83. layers += [conv2d, nn.BatchNorm2d(cfg[i]), nn.ReLU(inplace=True)]
  84. else:
  85. layers += [conv2d, nn.ReLU(inplace=True)]
  86. in_channels = cfg[i]
  87. return nn.Sequential(*layers)
  88. def forward(self, x):
  89. x = self.feature(x)
  90. x = nn.AvgPool2d(kernel_size=3, stride=1)(x)
  91. x = x.view(x.size(0), -1)
  92. y = self.classifier(x)
  93. return y
  94. class MyNet_color(nn.Module):
  95. def __init__(self, class_num=6):
  96. super(MyNet_color, self).__init__()
  97. self.class_num = class_num
  98. self.backbone = nn.Sequential(
  99. nn.Conv2d(in_channels=3, out_channels=16, kernel_size=(5, 5), stride=(1, 1)), # 0
  100. torch.nn.BatchNorm2d(16),
  101. nn.ReLU(),
  102. nn.MaxPool2d(kernel_size=(2, 2)),
  103. nn.Dropout(0),
  104. nn.Flatten(),
  105. nn.Linear(480, 64),
  106. nn.Dropout(0),
  107. nn.ReLU(),
  108. nn.Linear(64, class_num),
  109. nn.Dropout(0),
  110. nn.Softmax(1)
  111. )
  112. def forward(self, x):
  113. logits = self.backbone(x)
  114. return logits
  115. class myNet_ocr_color(nn.Module):
  116. def __init__(self,cfg=None,num_classes=78,export=False,color_num=None):
  117. super(myNet_ocr_color, self).__init__()
  118. if cfg is None:
  119. cfg =[32,32,64,64,'M',128,128,'M',196,196,'M',256,256]
  120. # cfg =[32,32,'M',64,64,'M',128,128,'M',256,256]
  121. self.feature = self.make_layers(cfg, True)
  122. self.export = export
  123. self.color_num=color_num
  124. self.conv_out_num=12 #颜色第一个卷积层输出通道12
  125. if self.color_num:
  126. self.conv1=nn.Conv2d(cfg[-1],self.conv_out_num,kernel_size=3,stride=2)
  127. self.bn1=nn.BatchNorm2d(self.conv_out_num)
  128. self.relu1=nn.ReLU(inplace=True)
  129. self.gap =nn.AdaptiveAvgPool2d(output_size=1)
  130. self.color_classifier=nn.Conv2d(self.conv_out_num,self.color_num,kernel_size=1,stride=1)
  131. self.color_bn = nn.BatchNorm2d(self.color_num)
  132. self.flatten = nn.Flatten()
  133. self.loc = nn.MaxPool2d((5, 2), (1, 1),(0,1),ceil_mode=False)
  134. self.newCnn=nn.Conv2d(cfg[-1],num_classes,1,1)
  135. # self.newBn=nn.BatchNorm2d(num_classes)
  136. def make_layers(self, cfg, batch_norm=False):
  137. layers = []
  138. in_channels = 3
  139. for i in range(len(cfg)):
  140. if i == 0:
  141. conv2d =nn.Conv2d(in_channels, cfg[i], kernel_size=5,stride =1)
  142. if batch_norm:
  143. layers += [conv2d, nn.BatchNorm2d(cfg[i]), nn.ReLU(inplace=True)]
  144. else:
  145. layers += [conv2d, nn.ReLU(inplace=True)]
  146. in_channels = cfg[i]
  147. else :
  148. if cfg[i] == 'M':
  149. layers += [nn.MaxPool2d(kernel_size=3, stride=2,ceil_mode=True)]
  150. else:
  151. conv2d = nn.Conv2d(in_channels, cfg[i], kernel_size=3, padding=(1,1),stride =1)
  152. if batch_norm:
  153. layers += [conv2d, nn.BatchNorm2d(cfg[i]), nn.ReLU(inplace=True)]
  154. else:
  155. layers += [conv2d, nn.ReLU(inplace=True)]
  156. in_channels = cfg[i]
  157. return nn.Sequential(*layers)
  158. def forward(self, x):
  159. x = self.feature(x)
  160. if self.color_num:
  161. x_color=self.conv1(x)
  162. x_color=self.bn1(x_color)
  163. x_color =self.relu1(x_color)
  164. x_color = self.color_classifier(x_color)
  165. x_color = self.color_bn(x_color)
  166. x_color =self.gap(x_color)
  167. x_color = self.flatten(x_color)
  168. x=self.loc(x)
  169. x=self.newCnn(x)
  170. if self.export:
  171. conv = x.squeeze(2) # b *512 * width
  172. conv = conv.transpose(2,1) # [w, b, c]
  173. if self.color_num:
  174. return conv,x_color
  175. return conv
  176. else:
  177. b, c, h, w = x.size()
  178. assert h == 1, "the height of conv must be 1"
  179. conv = x.squeeze(2) # b *512 * width
  180. conv = conv.permute(2, 0, 1) # [w, b, c]
  181. output = F.log_softmax(conv, dim=2)
  182. if self.color_num:
  183. return output,x_color
  184. return output
  185. if __name__ == '__main__':
  186. x = torch.randn(1,3,48,216)
  187. model = myNet_ocr(num_classes=78,export=True)
  188. out = model(x)
  189. print(out.shape)