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

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

Contributors (1)