You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

yolo.py 15 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """YOLOv4 for MS-COCO.
  4. # Reference:
  5. - [tensorflow-yolov4-tflite](
  6. https://github.com/hunglc007/tensorflow-yolov4-tflite)
  7. """
  8. import numpy as np
  9. import tensorlayer as tl
  10. from tensorlayer.layers.activation import Mish
  11. from tensorlayer.layers import Conv2d, MaxPool2d, BatchNorm2d, ZeroPad2d, UpSampling2d, Concat, Elementwise
  12. from tensorlayer.layers import Module, SequentialLayer
  13. from tensorlayer import logging
  14. __all__ = ['YOLOv4']
  15. INPUT_SIZE = 416
  16. weights_url = {'link': 'https://pan.baidu.com/s/1MC1dmEwpxsdgHO1MZ8fYRQ', 'password': 'idsz'}
  17. class Convolutional(Module):
  18. """
  19. Create Convolution layer
  20. Because it is only a stack of reference layers, there is no build, so self._built=True
  21. """
  22. def __init__(self, filters_shape, downsample=False, activate=True, bn=True, activate_type='leaky', name=None):
  23. super(Convolutional, self).__init__()
  24. self.act = activate
  25. self.act_type = activate_type
  26. self.downsample = downsample
  27. self.bn = bn
  28. self._built = True
  29. if downsample:
  30. padding = 'VALID'
  31. strides = 2
  32. else:
  33. strides = 1
  34. padding = 'SAME'
  35. if bn:
  36. b_init = None
  37. else:
  38. b_init = tl.initializers.constant(value=0.0)
  39. self.zeropad = ZeroPad2d(((1, 0), (1, 0)))
  40. self.conv = Conv2d(
  41. n_filter=filters_shape[-1], in_channels=filters_shape[2], filter_size=(filters_shape[0], filters_shape[1]),
  42. strides=(strides, strides), padding=padding, b_init=b_init, name=name
  43. )
  44. if bn:
  45. if activate ==True:
  46. if activate_type == 'leaky':
  47. self.batchnorm2d = BatchNorm2d(act='leaky_relu0.1', num_features=filters_shape[-1])
  48. elif activate_type == 'mish':
  49. self.batchnorm2d = BatchNorm2d(act=Mish, num_features=filters_shape[-1])
  50. else:
  51. self.batchnorm2d = BatchNorm2d(act=None, num_features=filters_shape[-1])
  52. def forward(self, input):
  53. if self.downsample:
  54. input = self.zeropad(input)
  55. output = self.conv(input)
  56. if self.bn:
  57. output = self.batchnorm2d(output)
  58. return output
  59. class residual_block(Module):
  60. def __init__(self, input_channel, filter_num1, filter_num2, activate_type='leaky'):
  61. super(residual_block, self).__init__()
  62. self.conv1 = Convolutional(filters_shape=(1, 1, input_channel, filter_num1), activate_type=activate_type)
  63. self.conv2 = Convolutional(filters_shape=(3, 3, filter_num1, filter_num2), activate_type=activate_type)
  64. self.add = Elementwise(tl.add)
  65. def forward(self, inputs):
  66. output = self.conv1(inputs)
  67. output = self.conv2(output)
  68. output = self.add([inputs, output])
  69. return output
  70. def residual_block_num(num, input_channel, filter_num1, filter_num2, activate_type='leaky'):
  71. residual_list = []
  72. for i in range(num):
  73. residual_list.append(residual_block(input_channel, filter_num1, filter_num2, activate_type=activate_type))
  74. return SequentialLayer(residual_list)
  75. class cspdarknet53(Module):
  76. def __init__(self):
  77. super(cspdarknet53, self).__init__()
  78. self._built = True
  79. self.conv1_1 = Convolutional((3, 3, 3, 32), activate_type='mish')
  80. self.conv1_2 = Convolutional((3, 3, 32, 64), downsample=True, activate_type='mish')
  81. self.conv1_3 = Convolutional((1, 1, 64, 64), activate_type='mish', name='conv_rote_block_1')
  82. self.conv1_4 = Convolutional((1, 1, 64, 64), activate_type='mish')
  83. self.residual_1 = residual_block_num(1, 64, 32, 64, activate_type="mish")
  84. self.conv2_1 = Convolutional((1, 1, 64, 64), activate_type='mish')
  85. self.concat = Concat()
  86. self.conv2_2 = Convolutional((1, 1, 128, 64), activate_type='mish')
  87. self.conv2_3 = Convolutional((3, 3, 64, 128), downsample=True, activate_type='mish')
  88. self.conv2_4 = Convolutional((1, 1, 128, 64), activate_type='mish', name='conv_rote_block_2')
  89. self.conv2_5 = Convolutional((1, 1, 128, 64), activate_type='mish')
  90. self.residual_2 = residual_block_num(2, 64, 64, 64, activate_type='mish')
  91. self.conv3_1 = Convolutional((1, 1, 64, 64), activate_type='mish')
  92. self.conv3_2 = Convolutional((1, 1, 128, 128), activate_type='mish')
  93. self.conv3_3 = Convolutional((3, 3, 128, 256), downsample=True, activate_type='mish')
  94. self.conv3_4 = Convolutional((1, 1, 256, 128), activate_type='mish', name='conv_rote_block_3')
  95. self.conv3_5 = Convolutional((1, 1, 256, 128), activate_type='mish')
  96. self.residual_3 = residual_block_num(8, 128, 128, 128, activate_type="mish")
  97. self.conv4_1 = Convolutional((1, 1, 128, 128), activate_type='mish')
  98. self.conv4_2 = Convolutional((1, 1, 256, 256), activate_type='mish')
  99. self.conv4_3 = Convolutional((3, 3, 256, 512), downsample=True, activate_type='mish')
  100. self.conv4_4 = Convolutional((1, 1, 512, 256), activate_type='mish', name='conv_rote_block_4')
  101. self.conv4_5 = Convolutional((1, 1, 512, 256), activate_type='mish')
  102. self.residual_4 = residual_block_num(8, 256, 256, 256, activate_type="mish")
  103. self.conv5_1 = Convolutional((1, 1, 256, 256), activate_type='mish')
  104. self.conv5_2 = Convolutional((1, 1, 512, 512), activate_type='mish')
  105. self.conv5_3 = Convolutional((3, 3, 512, 1024), downsample=True, activate_type='mish')
  106. self.conv5_4 = Convolutional((1, 1, 1024, 512), activate_type='mish', name='conv_rote_block_5')
  107. self.conv5_5 = Convolutional((1, 1, 1024, 512), activate_type='mish')
  108. self.residual_5 = residual_block_num(4, 512, 512, 512, activate_type="mish")
  109. self.conv6_1 = Convolutional((1, 1, 512, 512), activate_type='mish')
  110. self.conv6_2 = Convolutional((1, 1, 1024, 1024), activate_type='mish')
  111. self.conv6_3 = Convolutional((1, 1, 1024, 512))
  112. self.conv6_4 = Convolutional((3, 3, 512, 1024))
  113. self.conv6_5 = Convolutional((1, 1, 1024, 512))
  114. self.maxpool1 = MaxPool2d(filter_size=(13, 13), strides=(1, 1))
  115. self.maxpool2 = MaxPool2d(filter_size=(9, 9), strides=(1, 1))
  116. self.maxpool3 = MaxPool2d(filter_size=(5, 5), strides=(1, 1))
  117. self.conv7_1 = Convolutional((1, 1, 2048, 512))
  118. self.conv7_2 = Convolutional((3, 3, 512, 1024))
  119. self.conv7_3 = Convolutional((1, 1, 1024, 512))
  120. def forward(self, input_data):
  121. input_data = self.conv1_1(input_data)
  122. input_data = self.conv1_2(input_data)
  123. route = input_data
  124. route = self.conv1_3(route)
  125. input_data = self.conv1_4(input_data)
  126. input_data = self.residual_1(input_data)
  127. input_data = self.conv2_1(input_data)
  128. input_data = self.concat([input_data, route])
  129. input_data = self.conv2_2(input_data)
  130. input_data = self.conv2_3(input_data)
  131. route = input_data
  132. route = self.conv2_4(route)
  133. input_data = self.conv2_5(input_data)
  134. input_data = self.residual_2(input_data)
  135. input_data = self.conv3_1(input_data)
  136. input_data = self.concat([input_data, route])
  137. input_data = self.conv3_2(input_data)
  138. input_data = self.conv3_3(input_data)
  139. route = input_data
  140. route = self.conv3_4(route)
  141. input_data = self.conv3_5(input_data)
  142. input_data = self.residual_3(input_data)
  143. input_data = self.conv4_1(input_data)
  144. input_data = self.concat([input_data, route])
  145. input_data = self.conv4_2(input_data)
  146. route_1 = input_data
  147. input_data = self.conv4_3(input_data)
  148. route = input_data
  149. route = self.conv4_4(route)
  150. input_data = self.conv4_5(input_data)
  151. input_data = self.residual_4(input_data)
  152. input_data = self.conv5_1(input_data)
  153. input_data = self.concat([input_data, route])
  154. input_data = self.conv5_2(input_data)
  155. route_2 = input_data
  156. input_data = self.conv5_3(input_data)
  157. route = input_data
  158. route = self.conv5_4(route)
  159. input_data = self.conv5_5(input_data)
  160. input_data = self.residual_5(input_data)
  161. input_data = self.conv6_1(input_data)
  162. input_data = self.concat([input_data, route])
  163. input_data = self.conv6_2(input_data)
  164. input_data = self.conv6_3(input_data)
  165. input_data = self.conv6_4(input_data)
  166. input_data = self.conv6_5(input_data)
  167. maxpool1 = self.maxpool1(input_data)
  168. maxpool2 = self.maxpool2(input_data)
  169. maxpool3 = self.maxpool3(input_data)
  170. input_data = self.concat([maxpool1, maxpool2, maxpool3, input_data])
  171. input_data = self.conv7_1(input_data)
  172. input_data = self.conv7_2(input_data)
  173. input_data = self.conv7_3(input_data)
  174. return route_1, route_2, input_data
  175. class YOLOv4_model(Module):
  176. def __init__(self, NUM_CLASS):
  177. super(YOLOv4_model, self).__init__()
  178. self.cspdarnnet = cspdarknet53()
  179. self.conv1_1 = Convolutional((1, 1, 512, 256))
  180. self.upsamle = UpSampling2d(scale=2)
  181. self.conv1_2 = Convolutional((1, 1, 512, 256), name='conv_yolo_1')
  182. self.concat = Concat()
  183. self.conv2_1 = Convolutional((1, 1, 512, 256))
  184. self.conv2_2 = Convolutional((3, 3, 256, 512))
  185. self.conv2_3 = Convolutional((1, 1, 512, 256))
  186. self.conv2_4 = Convolutional((3, 3, 256, 512))
  187. self.conv2_5 = Convolutional((1, 1, 512, 256))
  188. self.conv3_1 = Convolutional((1, 1, 256, 128))
  189. self.conv3_2 = Convolutional((1, 1, 256, 128), name='conv_yolo_2')
  190. self.conv4_1 = Convolutional((1, 1, 256, 128))
  191. self.conv4_2 = Convolutional((3, 3, 128, 256))
  192. self.conv4_3 = Convolutional((1, 1, 256, 128))
  193. self.conv4_4 = Convolutional((3, 3, 128, 256))
  194. self.conv4_5 = Convolutional((1, 1, 256, 128))
  195. self.conv5_1 = Convolutional((3, 3, 128, 256), name='conv_route_1')
  196. self.conv5_2 = Convolutional((1, 1, 256, 3 * (NUM_CLASS + 5)), activate=False, bn=False)
  197. self.conv6_1 = Convolutional((3, 3, 128, 256), downsample=True, name='conv_route_2')
  198. self.conv6_2 = Convolutional((1, 1, 512, 256))
  199. self.conv6_3 = Convolutional((3, 3, 256, 512))
  200. self.conv6_4 = Convolutional((1, 1, 512, 256))
  201. self.conv6_5 = Convolutional((3, 3, 256, 512))
  202. self.conv6_6 = Convolutional((1, 1, 512, 256))
  203. self.conv7_1 = Convolutional((3, 3, 256, 512), name='conv_route_3')
  204. self.conv7_2 = Convolutional((1, 1, 512, 3 * (NUM_CLASS + 5)), activate=False, bn=False)
  205. self.conv7_3 = Convolutional((3, 3, 256, 512), downsample=True, name='conv_route_4')
  206. self.conv8_1 = Convolutional((1, 1, 1024, 512))
  207. self.conv8_2 = Convolutional((3, 3, 512, 1024))
  208. self.conv8_3 = Convolutional((1, 1, 1024, 512))
  209. self.conv8_4 = Convolutional((3, 3, 512, 1024))
  210. self.conv8_5 = Convolutional((1, 1, 1024, 512))
  211. self.conv9_1 = Convolutional((3, 3, 512, 1024))
  212. self.conv9_2 = Convolutional((1, 1, 1024, 3 * (NUM_CLASS + 5)), activate=False, bn=False)
  213. def forward(self, inputs):
  214. route_1, route_2, conv = self.cspdarnnet(inputs)
  215. route = conv
  216. conv = self.conv1_1(conv)
  217. conv = self.upsamle(conv)
  218. route_2 = self.conv1_2(route_2)
  219. conv = self.concat([route_2, conv])
  220. conv = self.conv2_1(conv)
  221. conv = self.conv2_2(conv)
  222. conv = self.conv2_3(conv)
  223. conv = self.conv2_4(conv)
  224. conv = self.conv2_5(conv)
  225. route_2 = conv
  226. conv = self.conv3_1(conv)
  227. conv = self.upsamle(conv)
  228. route_1 = self.conv3_2(route_1)
  229. conv = self.concat([route_1, conv])
  230. conv = self.conv4_1(conv)
  231. conv = self.conv4_2(conv)
  232. conv = self.conv4_3(conv)
  233. conv = self.conv4_4(conv)
  234. conv = self.conv4_5(conv)
  235. route_1 = conv
  236. conv = self.conv5_1(conv)
  237. conv_sbbox = self.conv5_2(conv)
  238. conv = self.conv6_1(route_1)
  239. conv = self.concat([conv, route_2])
  240. conv = self.conv6_2(conv)
  241. conv = self.conv6_3(conv)
  242. conv = self.conv6_4(conv)
  243. conv = self.conv6_5(conv)
  244. conv = self.conv6_6(conv)
  245. route_2 = conv
  246. conv = self.conv7_1(conv)
  247. conv_mbbox = self.conv7_2(conv)
  248. conv = self.conv7_3(route_2)
  249. conv = self.concat([conv, route])
  250. conv = self.conv8_1(conv)
  251. conv = self.conv8_2(conv)
  252. conv = self.conv8_3(conv)
  253. conv = self.conv8_4(conv)
  254. conv = self.conv8_5(conv)
  255. conv = self.conv9_1(conv)
  256. conv_lbbox = self.conv9_2(conv)
  257. return conv_sbbox, conv_mbbox, conv_lbbox
  258. def YOLOv4(NUM_CLASS, pretrained=False):
  259. """Pre-trained YOLOv4 model.
  260. Parameters
  261. ------------
  262. NUM_CLASS : int
  263. Number of classes in final prediction.
  264. pretrained : boolean
  265. Whether to load pretrained weights. Default False.
  266. Examples
  267. ---------
  268. Object Detection with YOLOv4, see `computer_vision.py
  269. <https://github.com/tensorlayer/tensorlayer/blob/master/tensorlayer/app/computer_vision.py>`__
  270. With TensorLayer
  271. >>> # get the whole model, without pre-trained YOLOv4 parameters
  272. >>> yolov4 = YOLOv4(NUM_CLASS=80, pretrained=False)
  273. >>> # get the whole model, restore pre-trained YOLOv4 parameters
  274. >>> yolov4 = YOLOv4(NUM_CLASS=80, pretrained=True)
  275. >>> # use for inferencing
  276. >>> output = yolov4(img)
  277. """
  278. network = YOLOv4_model(NUM_CLASS=NUM_CLASS)
  279. if pretrained:
  280. restore_params(network, model_path='model/yolov4_model.npz')
  281. return network
  282. def restore_params(network, model_path='models.npz'):
  283. logging.info("Restore pre-trained weights")
  284. try:
  285. npz = np.load(model_path, allow_pickle=True)
  286. except:
  287. print("Download the model file, placed in the /model ")
  288. print("Weights download: ", weights_url['link'], "password:", weights_url['password'])
  289. txt_path = 'model/yolov4_weights3_config.txt'
  290. f = open(txt_path, "r")
  291. line = f.readlines()
  292. for i in range(len(line)):
  293. network.all_weights[i].assign(npz[line[i].strip()])
  294. logging.info(" Loading weights %s in %s" % (network.all_weights[i].shape, network.all_weights[i].name))
  295. def tl2_weights_to_tl3_weights(
  296. weights_2_path='model/weights_2.txt', weights_3_path='model/weights_3.txt',
  297. txt_path='model/yolov4_weights_config.txt'
  298. ):
  299. weights_2_path = weights_2_path
  300. weights_3_path = weights_3_path
  301. txt_path = txt_path
  302. f1 = open(weights_2_path, "r")
  303. f2 = open(weights_3_path, "r")
  304. f3 = open(txt_path, "r")
  305. line1 = f1.readlines()
  306. line2 = f2.readlines()
  307. line3 = f3.readlines()
  308. _dicts = {}
  309. for i in range(len(line1)):
  310. _dicts[line1[i].strip()] = line3[i].strip()
  311. for j in range(len(line2)):
  312. print(_dicts[line2[j].strip()])

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.