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_net.py 24 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. # !/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. """
  4. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================
  15. """
  16. import oneflow as flow
  17. import oneflow.core.operator.op_conf_pb2 as op_conf_util
  18. from oneflow_yolov3.ops.upsample_nearest import upsample_nearest
  19. from oneflow_yolov3.ops.yolo_detect import yolo_detect
  20. from oneflow_yolov3.ops.yolo_nms import yolo_nms
  21. layer_number = 1
  22. route_dict = {}
  23. yolo_pos_result = []
  24. yolo_prob_result = []
  25. yolo_loss_result = []
  26. num_classes = 80
  27. ignore_thresh = 0.7
  28. truth_thresh = 1.0
  29. image_height = 608
  30. image_width = 608
  31. max_out_boxes = 200
  32. nms = True
  33. # nms=False
  34. nms_threshold = 0.45
  35. #anchor_boxes_size_list=[flow.detection.anchor_boxes_size(10, 13), flow.detection.anchor_boxes_size(16, 30), flow.detection.anchor_boxes_size(33, 23), flow.detection.anchor_boxes_size(30,61), flow.detection.anchor_boxes_size(62, 45), flow.detection.anchor_boxes_size(59, 119), flow.detection.anchor_boxes_size(116,90), flow.detection.anchor_boxes_size(156, 198), flow.detection.anchor_boxes_size(373, 326)]
  36. anchor_boxes_size_list = [10, 13, 16, 30, 33, 23, 30,
  37. 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326]
  38. yolo_box_diff_conf = [{'image_height': image_height,
  39. 'image_width': image_width,
  40. 'layer_height': 19,
  41. 'layer_width': 19,
  42. 'ignore_thresh': ignore_thresh,
  43. 'truth_thresh': truth_thresh,
  44. 'anchor_boxes_size': anchor_boxes_size_list,
  45. 'box_mask': [6,
  46. 7,
  47. 8]},
  48. {'image_height': image_height,
  49. 'image_width': image_width,
  50. 'layer_height': 38,
  51. 'layer_width': 38,
  52. 'ignore_thresh': ignore_thresh,
  53. 'truth_thresh': truth_thresh,
  54. 'anchor_boxes_size': anchor_boxes_size_list,
  55. 'box_mask': [3,
  56. 4,
  57. 5]},
  58. {'image_height': image_height,
  59. 'image_width': image_width,
  60. 'layer_height': 76,
  61. 'layer_width': 76,
  62. 'ignore_thresh': ignore_thresh,
  63. 'truth_thresh': truth_thresh,
  64. 'anchor_boxes_size': anchor_boxes_size_list,
  65. 'box_mask': [0,
  66. 1,
  67. 2]}]
  68. # to confirm wh pos, gr 12.19 check with 11 ~/yolov3/predict.job
  69. yolo_conf = [{'layer_height': 19,
  70. 'layer_width': 19,
  71. 'prob_thresh': 0.5,
  72. 'num_classes': 80,
  73. 'anchor_boxes_size': [116,
  74. 90,
  75. 156,
  76. 198,
  77. 373,
  78. 326]},
  79. {'layer_height': 38,
  80. 'layer_width': 38,
  81. 'prob_thresh': 0.5,
  82. 'num_classes': 80,
  83. 'anchor_boxes_size': [30,
  84. 61,
  85. 62,
  86. 45,
  87. 59,
  88. 119]},
  89. {'layer_height': 76,
  90. 'layer_width': 76,
  91. 'prob_thresh': 0.5,
  92. 'num_classes': 80,
  93. 'anchor_boxes_size': [10,
  94. 13,
  95. 16,
  96. 30,
  97. 33,
  98. 23]}]
  99. def _conv2d_layer(
  100. name,
  101. input,
  102. filters,
  103. kernel_size=3,
  104. strides=1,
  105. padding="SAME",
  106. group_num=1,
  107. data_format="NCHW",
  108. dilation_rate=1,
  109. activation=op_conf_util.kRelu,
  110. use_bias=False,
  111. weight_initializer=flow.random_uniform_initializer(),
  112. bias_initializer=flow.random_uniform_initializer(),
  113. trainable=True,
  114. ):
  115. if data_format == "NCHW":
  116. weight_shape = (
  117. int(filters), int(
  118. input.static_shape[1]), int(
  119. kernel_size[0]), int(
  120. kernel_size[0]))
  121. elif data_format == "NHWC":
  122. weight_shape = (
  123. int(filters), int(
  124. kernel_size[0]), int(
  125. kernel_size[0]), int(
  126. input.static_shape[3]))
  127. else:
  128. raise ValueError('data_format must be "NCHW" or "NHWC".')
  129. weight = flow.get_variable(
  130. name + "-weight",
  131. shape=weight_shape,
  132. dtype=input.dtype,
  133. initializer=weight_initializer,
  134. trainable=trainable,
  135. )
  136. output = flow.nn.conv2d(
  137. input, weight, strides, padding, data_format, dilation_rate, name=name
  138. )
  139. if use_bias:
  140. bias = flow.get_variable(
  141. name + "-bias",
  142. shape=(filters,),
  143. dtype=input.dtype,
  144. initializer=bias_initializer,
  145. model_name="bias",
  146. trainable=trainable,
  147. )
  148. output = flow.nn.bias_add(output, bias, data_format)
  149. if activation is not None:
  150. if activation == op_conf_util.kRelu:
  151. output = flow.keras.activations.relu(output)
  152. else:
  153. raise NotImplementedError
  154. return output
  155. def _batch_norm(
  156. inputs,
  157. axis,
  158. momentum,
  159. epsilon,
  160. center=True,
  161. scale=True,
  162. trainable=True,
  163. name=None):
  164. return flow.layers.batch_normalization(
  165. inputs=inputs,
  166. axis=axis,
  167. momentum=momentum,
  168. epsilon=epsilon,
  169. center=center,
  170. scale=scale,
  171. trainable=trainable,
  172. name=name
  173. )
  174. def _leaky_relu(input, alpha=None, name=None):
  175. return flow.nn.leaky_relu(input, alpha=alpha, name=None)
  176. # return flow.math.relu(input)
  177. def _upsample(input, name=None):
  178. # return flow.detection.upsample_nearest(input, name=name, scale=2,
  179. # data_format="channels_first")
  180. return upsample_nearest(
  181. input,
  182. name=name,
  183. scale=2,
  184. data_format="channels_first")
  185. def conv_unit(
  186. data,
  187. num_filter=1,
  188. kernel=(
  189. 1,
  190. 1),
  191. stride=(
  192. 1,
  193. 1),
  194. pad="same",
  195. data_format="NCHW",
  196. use_bias=False,
  197. trainable=True,
  198. prefix=''):
  199. conv = _conv2d_layer(
  200. name=prefix + '-conv',
  201. input=data,
  202. filters=num_filter,
  203. kernel_size=kernel,
  204. strides=stride,
  205. padding='same',
  206. data_format=data_format,
  207. dilation_rate=1,
  208. activation=None,
  209. use_bias=use_bias,
  210. trainable=trainable)
  211. bn = _batch_norm(
  212. conv,
  213. axis=1,
  214. momentum=0.99,
  215. epsilon=1.0001e-5,
  216. trainable=trainable,
  217. name=prefix +
  218. '-bn')
  219. leaky_relu = _leaky_relu(bn, alpha=0.1, name=prefix + '-leakyRelu')
  220. return leaky_relu
  221. def ResidualBlock(data, prefix, filter, trainable):
  222. global layer_number
  223. layer_number += 1
  224. blob = conv_unit(
  225. data,
  226. num_filter=filter,
  227. kernel=[
  228. 1,
  229. 1],
  230. stride=[
  231. 1,
  232. 1],
  233. pad="same",
  234. data_format="NCHW",
  235. use_bias=False,
  236. trainable=trainable,
  237. prefix='yolo-layer' +
  238. str(layer_number))
  239. layer_number += 1
  240. blob = conv_unit(
  241. blob,
  242. num_filter=filter *
  243. 2,
  244. kernel=[
  245. 3,
  246. 3],
  247. stride=[
  248. 1,
  249. 1],
  250. pad="same",
  251. data_format="NCHW",
  252. use_bias=False,
  253. trainable=trainable,
  254. prefix='yolo-layer' +
  255. str(layer_number))
  256. layer_number += 1
  257. shortcut = flow.math.add(
  258. data,
  259. blob,
  260. name='yolo-layer' +
  261. str(layer_number) +
  262. '-shortcut')
  263. return shortcut
  264. def ResidualStage(data, prefix, n, filters, trainable):
  265. global layer_number
  266. layer_number += 1
  267. blob = conv_unit(
  268. data,
  269. num_filter=filters *
  270. 2,
  271. kernel=[
  272. 3,
  273. 3],
  274. stride=[
  275. 2,
  276. 2],
  277. pad="same",
  278. data_format="NCHW",
  279. use_bias=False,
  280. trainable=trainable,
  281. prefix='yolo-layer' +
  282. str(layer_number))
  283. for i in range(n):
  284. blob = ResidualBlock(blob, "%s_%d" %
  285. (prefix, i), filters, trainable=trainable)
  286. return blob
  287. def DarknetNetConvXBody(in_blob, trainable, on_stage_end=lambda x: x):
  288. global layer_number
  289. filter = [32, 64, 128, 256, 512]
  290. block_counts = [1, 2, 8, 8, 4]
  291. blob = in_blob
  292. for i in range(len(block_counts)):
  293. blob = ResidualStage(blob, "block%d" % i, block_counts[i],
  294. filter[i], trainable=trainable)
  295. if i == 2:
  296. route_dict['layer_36'] = blob
  297. if i == 3:
  298. route_dict['layer_61'] = blob
  299. if i == 4:
  300. route_dict['layer_74'] = blob
  301. on_stage_end(blob)
  302. return blob
  303. def YoloBlock(in_blob, prefix, filter, stage_idx, block_idx, trainable):
  304. global layer_number
  305. layer_number += 1
  306. blob = conv_unit(
  307. in_blob,
  308. num_filter=filter,
  309. kernel=[
  310. 1,
  311. 1],
  312. stride=[
  313. 1,
  314. 1],
  315. pad="same",
  316. data_format="NCHW",
  317. use_bias=False,
  318. prefix='yolo-layer' +
  319. str(layer_number))
  320. if stage_idx == 0 and block_idx == 2:
  321. route_dict['layer_79'] = blob
  322. if stage_idx == 1 and block_idx == 2:
  323. route_dict['layer_91'] = blob
  324. layer_number += 1
  325. blob = conv_unit(
  326. blob,
  327. num_filter=filter *
  328. 2,
  329. kernel=[
  330. 3,
  331. 3],
  332. stride=[
  333. 1,
  334. 1],
  335. pad="same",
  336. data_format="NCHW",
  337. use_bias=False,
  338. prefix='yolo-layer' +
  339. str(layer_number))
  340. return blob
  341. def YoloStage(in_blob, prefix, n, filters, stage_idx, trainable):
  342. global layer_number
  343. blob = in_blob
  344. for i in range(n):
  345. blob = YoloBlock(
  346. blob, "%s_%d" %
  347. (prefix, i), filters, stage_idx, i, trainable=trainable)
  348. layer_number += 1
  349. blob = _conv2d_layer(
  350. name='yolo-layer' +
  351. str(layer_number) +
  352. '-conv',
  353. input=blob,
  354. filters=255,
  355. kernel_size=[
  356. 1,
  357. 1],
  358. strides=[
  359. 1,
  360. 1],
  361. padding='same',
  362. data_format="NCHW",
  363. dilation_rate=1,
  364. activation=None,
  365. use_bias=True,
  366. trainable=trainable)
  367. return blob
  368. def YoloPredictLayer(in_blob, origin_image_info, i, trainable):
  369. global layer_number
  370. layer_name = 'yolo-layer' + str(layer_number)
  371. # placeholder for a reshape from (n,h,w,255)->(n,h,w*3,85)
  372. blob = flow.transpose(
  373. in_blob,
  374. name=layer_name +
  375. '-yolo_transpose',
  376. perm=[
  377. 0,
  378. 2,
  379. 3,
  380. 1])
  381. reshape_blob = flow.reshape(blob, shape=(
  382. blob.shape[0], -1, 85), name=layer_name + '-yolo_reshape')
  383. position = flow.slice(
  384. reshape_blob, [
  385. None, 0, 0], [
  386. None, -1, 4], name=layer_name + '-yolo_slice_pos')
  387. xy = flow.slice(
  388. position, [
  389. None, 0, 0], [
  390. None, -1, 2], name=layer_name + '-yolo_slice_xy')
  391. wh = flow.slice(
  392. position, [
  393. None, 0, 2], [
  394. None, -1, 2], name=layer_name + '-yolo_slice_wh')
  395. xy = flow.math.sigmoid(xy, name=layer_name + '-yolo_ligistic_xy')
  396. position = flow.concat([xy, wh], axis=2, name=layer_name + '-yolo_concat')
  397. confidence = flow.slice(
  398. reshape_blob, [
  399. None, 0, 4], [
  400. None, -1, 81], name=layer_name + '-yolo_slice_prob')
  401. confidence = flow.math.sigmoid(
  402. confidence,
  403. name=layer_name +
  404. '-yolo_ligistic_prob')
  405. #[out_bbox, out_probs, valid_num] = flow.detection.yolo_detect(bbox=position, probs=confidence, origin_image_info=origin_image_info, image_height=608, image_width=608, layer_height=yolo_conf[i]['layer_height'], layer_width=yolo_conf[i]['layer_width'], prob_thresh=0.5, num_classes=80, max_out_boxes = max_out_boxes, anchor_boxes=yolo_conf[i]['anchor_boxes_size'])
  406. [out_bbox,
  407. out_probs,
  408. valid_num] = yolo_detect(bbox=position,
  409. probs=confidence,
  410. origin_image_info=origin_image_info,
  411. image_height=608,
  412. image_width=608,
  413. layer_height=yolo_conf[i]['layer_height'],
  414. layer_width=yolo_conf[i]['layer_width'],
  415. prob_thresh=0.5,
  416. num_classes=80,
  417. max_out_boxes=max_out_boxes,
  418. anchor_boxes=yolo_conf[i]['anchor_boxes_size'],
  419. name=str(layer_name) + "yolo_detect")
  420. print("out_bbox.shape", out_bbox.shape)
  421. return out_bbox, out_probs, valid_num
  422. def YoloTrainLayer(in_blob, gt_bbox_blob, gt_label_blob, gt_valid_num_blob, i):
  423. global layer_number
  424. layer_name = 'yolo-layer' + str(layer_number)
  425. # placeholder for a reshape from (n,h,w,255)->(n,h,w*3,85)
  426. blob = flow.transpose(
  427. in_blob,
  428. name=layer_name +
  429. '-yolo_transpose',
  430. perm=[
  431. 0,
  432. 2,
  433. 3,
  434. 1])
  435. reshape_blob = flow.reshape(blob, shape=(
  436. blob.shape[0], -1, 85), name=layer_name + '-yolo_reshape')
  437. position = flow.slice(
  438. reshape_blob, [
  439. None, 0, 0], [
  440. None, -1, 4], name=layer_name + '-yolo_slice_pos')
  441. xy = flow.slice(
  442. position, [
  443. None, 0, 0], [
  444. None, -1, 2], name=layer_name + '-yolo_slice_xy')
  445. wh = flow.slice(
  446. position, [
  447. None, 0, 2], [
  448. None, -1, 2], name=layer_name + '-yolo_slice_wh')
  449. xy = flow.math.logistic(xy, name=layer_name + '-yolo_ligistic_xy')
  450. #xy = flow.math.sigmoid(xy, name = layer_name + '-yolo_ligistic_xy')
  451. position = flow.concat([xy, wh], axis=2, name=layer_name + '-yolo_concat')
  452. confidence = flow.slice(
  453. reshape_blob, [
  454. None, 0, 4], [
  455. None, -1, 81], name=layer_name + '-yolo_slice_prob')
  456. confidence = flow.math.logistic(
  457. confidence,
  458. name=layer_name +
  459. '-yolo_ligistic_prob')
  460. #confidence = flow.math.sigmoid(confidence, name = layer_name+ '-yolo_ligistic_prob')
  461. objness = flow.slice(
  462. confidence, [
  463. None, 0, 0], [
  464. None, -1, 1], name=layer_name + '-yolo_slice_objness')
  465. clsprob = flow.slice(
  466. confidence, [
  467. None, 0, 1], [
  468. None, -1, 80], name=layer_name + '-yolo_slice_clsprob')
  469. bbox_loc_diff, pos_inds, pos_cls_label, neg_inds, valid_num = flow.detection.yolo_box_diff(position, gt_bbox_blob, gt_label_blob, gt_valid_num_blob, image_height=yolo_box_diff_conf[i]['image_height'], image_width=yolo_box_diff_conf[i]['image_width'], layer_height=yolo_box_diff_conf[i]['layer_height'], layer_width=yolo_box_diff_conf[
  470. i]['layer_width'], ignore_thresh=yolo_box_diff_conf[i]['ignore_thresh'], truth_thresh=yolo_box_diff_conf[i]['truth_thresh'], box_mask=yolo_box_diff_conf[i]['box_mask'], anchor_boxes_size=yolo_box_diff_conf[i]['anchor_boxes_size'], name=layer_name + '-yolo_box_loss') # placeholder for yolobox layer
  471. bbox_objness_out, bbox_clsprob_out = flow.detection.yolo_prob_loss(
  472. objness, clsprob, pos_inds, pos_cls_label, neg_inds, valid_num, num_classes=80, name=layer_name + '-yolo_prob_loss')
  473. bbox_loss = flow.concat([bbox_loc_diff,
  474. bbox_objness_out,
  475. bbox_clsprob_out],
  476. axis=2,
  477. name=layer_name + '-loss_concat')
  478. bbox_loss_reduce_sum = flow.math.reduce_sum(
  479. bbox_loss, axis=[1, 2], name=layer_name + '-bbox_loss_reduce_sum')
  480. return bbox_loss_reduce_sum
  481. def YoloNetBody(
  482. in_blob,
  483. gt_bbox_blob=None,
  484. gt_label_blob=None,
  485. gt_valid_num_blob=None,
  486. origin_image_info=None,
  487. trainable=False):
  488. global layer_number
  489. filter = [512, 256, 128]
  490. block_counts = [3, 3, 3]
  491. blob = in_blob
  492. yolo_result = []
  493. for i in range(len(filter)):
  494. if i == 0:
  495. blob = route_dict['layer_74']
  496. elif i == 1:
  497. layer_number += 1
  498. # placeholder for route layer
  499. layer_number += 1
  500. blob = conv_unit(
  501. route_dict['layer_79'],
  502. num_filter=filter[i],
  503. kernel=[
  504. 1,
  505. 1],
  506. stride=[
  507. 1,
  508. 1],
  509. pad="same",
  510. data_format="NCHW",
  511. use_bias=False,
  512. trainable=trainable,
  513. prefix='yolo-layer' +
  514. str(layer_number))
  515. layer_number += 1
  516. blob = _upsample(blob, name='upsample' + str(i))
  517. layer_number += 1
  518. blob = flow.concat([blob, route_dict['layer_61']],
  519. name='route' + str(i), axis=1)
  520. elif i == 2:
  521. layer_number += 1
  522. # placeholder for route layer
  523. layer_number += 1
  524. blob = conv_unit(
  525. route_dict['layer_91'],
  526. num_filter=filter[i],
  527. kernel=[
  528. 1,
  529. 1],
  530. stride=[
  531. 1,
  532. 1],
  533. pad="same",
  534. data_format="NCHW",
  535. use_bias=False,
  536. trainable=trainable,
  537. prefix='yolo-layer' +
  538. str(layer_number))
  539. layer_number += 1
  540. blob = _upsample(blob, name='upsample' + str(i))
  541. layer_number += 1
  542. blob = flow.concat([blob, route_dict['layer_36']],
  543. axis=1, name='route' + str(i))
  544. yolo_blob = YoloStage(blob, "%d" % i, block_counts[i],
  545. filter[i], i, trainable=trainable)
  546. layer_number += 1
  547. if not trainable:
  548. yolo_position, yolo_prob, valid_num = YoloPredictLayer(
  549. yolo_blob, origin_image_info, i, trainable=trainable)
  550. yolo_pos_result.append(yolo_position)
  551. yolo_prob_result.append(yolo_prob)
  552. else:
  553. loss = YoloTrainLayer(
  554. yolo_blob,
  555. gt_bbox_blob,
  556. gt_label_blob,
  557. gt_valid_num_blob,
  558. i)
  559. yolo_loss_result.append(loss)
  560. if not trainable:
  561. yolo_positions = flow.concat(
  562. yolo_pos_result,
  563. axis=1,
  564. name="concat_pos") # (b, n_boxes, 4)
  565. yolo_probs = flow.concat(yolo_prob_result, axis=1) # (b, n_boxes, 81)
  566. print(yolo_positions.shape)
  567. print(yolo_probs.shape)
  568. if nms:
  569. yolo_probs_transpose = flow.transpose(
  570. yolo_probs, perm=[0, 2, 1]) # (b, 81, n_boxes)
  571. pre_nms_top_k_inds = flow.math.top_k(
  572. yolo_probs_transpose, k=20000) # (b, 81, n_boxes)
  573. pre_nms_top_k_inds1 = flow.reshape(
  574. pre_nms_top_k_inds,
  575. shape=(
  576. pre_nms_top_k_inds.shape[0],
  577. pre_nms_top_k_inds.shape[1] *
  578. pre_nms_top_k_inds.shape[2]),
  579. name="reshape1") # (b, 81*n_boxes)
  580. gathered_yolo_positions = flow.gather(
  581. yolo_positions,
  582. pre_nms_top_k_inds1,
  583. axis=1,
  584. batch_dims=1) # (b, 81*n_boxes, 4)
  585. gathered_yolo_positions = flow.reshape(
  586. gathered_yolo_positions,
  587. shape=(
  588. gathered_yolo_positions.shape[0],
  589. yolo_probs.shape[2],
  590. yolo_positions.shape[1],
  591. yolo_positions.shape[2]),
  592. name="reshape2") # (b, 81, n_boxes, 4)
  593. gathered_yolo_probs = flow.gather(
  594. yolo_probs_transpose,
  595. pre_nms_top_k_inds,
  596. axis=2,
  597. batch_dims=2) # (b, 81, n_boxes)
  598. nms_val = yolo_nms(
  599. gathered_yolo_positions,
  600. gathered_yolo_probs,
  601. iou_threshold=nms_threshold,
  602. keep_n=-1,
  603. batch_dims=2,
  604. name="nms") # b, 81, n_boxes
  605. nms_val_cast = flow.cast(
  606. nms_val, dtype=flow.float) # (b, 81, n_boxes)
  607. nms_val_reshape = flow.reshape(
  608. nms_val_cast,
  609. shape=(
  610. nms_val.shape[0],
  611. nms_val.shape[1],
  612. nms_val.shape[2],
  613. 1)) # (b, 81, n_boxes, 1)
  614. final_boxes = flow.math.multiply(
  615. gathered_yolo_positions,
  616. nms_val_reshape) # (b, 81, 270, 4)
  617. final_probs = flow.math.multiply(
  618. gathered_yolo_probs, nms_val_cast) # (b, 81, 270)
  619. return final_boxes, final_probs
  620. return yolo_positions, yolo_probs
  621. else:
  622. return yolo_loss_result
  623. def YoloPredictNet(data, origin_image_info, trainable=False):
  624. print("nms:", nms)
  625. global layer_number
  626. #data = flow.transpose(data, perm=[0, 3, 1, 2])
  627. blob = conv_unit(
  628. data,
  629. num_filter=32,
  630. kernel=[
  631. 3,
  632. 3],
  633. stride=[
  634. 1,
  635. 1],
  636. pad="same",
  637. data_format="NCHW",
  638. use_bias=False,
  639. trainable=trainable,
  640. prefix='yolo-layer' +
  641. str(layer_number))
  642. blob = DarknetNetConvXBody(blob, trainable, lambda x: x)
  643. yolo_pos_result, yolo_prob_result = YoloNetBody(
  644. in_blob=blob, origin_image_info=origin_image_info, trainable=trainable)
  645. return yolo_pos_result, yolo_prob_result
  646. def YoloTrainNet(data, gt_box, gt_label, gt_valid_num, trainable=True):
  647. global layer_number
  648. #data = flow.transpose(data, perm=[0, 3, 1, 2])
  649. blob = conv_unit(
  650. data,
  651. num_filter=32,
  652. kernel=[
  653. 3,
  654. 3],
  655. stride=[
  656. 1,
  657. 1],
  658. pad="same",
  659. data_format="NCHW",
  660. use_bias=False,
  661. trainable=trainable,
  662. prefix='yolo-layer' +
  663. str(layer_number))
  664. blob = DarknetNetConvXBody(blob, trainable, lambda x: x)
  665. yolo_loss_result = YoloNetBody(
  666. in_blob=blob,
  667. gt_bbox_blob=gt_box,
  668. gt_label_blob=gt_label,
  669. gt_valid_num_blob=gt_valid_num,
  670. trainable=trainable)
  671. return yolo_loss_result

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能