loi_heads.py 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. from typing import Dict, List, Optional, Tuple
  2. import matplotlib.pyplot as plt
  3. import torch
  4. import torch.nn.functional as F
  5. import torchvision
  6. # from scipy.optimize import linear_sum_assignment
  7. from torch import nn, Tensor
  8. from libs.vision_libs.ops import boxes as box_ops, roi_align
  9. import libs.vision_libs.models.detection._utils as det_utils
  10. from collections import OrderedDict
  11. from models.line_detect.heads.head_losses import point_inference, compute_point_loss, line_iou_loss, \
  12. lines_point_pair_loss, features_align, line_inference, compute_mask_loss, arc_inference, compute_circle_loss, \
  13. circle_inference
  14. def fastrcnn_loss(class_logits, box_regression, labels, regression_targets):
  15. # type: (Tensor, Tensor, List[Tensor], List[Tensor]) -> Tuple[Tensor, Tensor]
  16. """
  17. Computes the loss for Faster R-CNN.
  18. Args:
  19. class_logits (Tensor)
  20. box_regression (Tensor)
  21. labels (list[BoxList])
  22. regression_targets (Tensor)
  23. Returns:
  24. classification_loss (Tensor)
  25. box_loss (Tensor)
  26. """
  27. # print(f'compute fastrcnn_loss:{labels}')
  28. labels = torch.cat(labels, dim=0)
  29. regression_targets = torch.cat(regression_targets, dim=0)
  30. classification_loss = F.cross_entropy(class_logits, labels)
  31. # get indices that correspond to the regression targets for
  32. # the corresponding ground truth labels, to be used with
  33. # advanced indexing
  34. sampled_pos_inds_subset = torch.where(labels > 0)[0]
  35. labels_pos = labels[sampled_pos_inds_subset]
  36. N, num_classes = class_logits.shape
  37. box_regression = box_regression.reshape(N, box_regression.size(-1) // 4, 4)
  38. box_loss = F.smooth_l1_loss(
  39. box_regression[sampled_pos_inds_subset, labels_pos],
  40. regression_targets[sampled_pos_inds_subset],
  41. beta=1 / 9,
  42. reduction="sum",
  43. )
  44. box_loss = box_loss / labels.numel()
  45. return classification_loss, box_loss
  46. def maskrcnn_inference(x, labels):
  47. # type: (Tensor, List[Tensor]) -> List[Tensor]
  48. """
  49. From the results of the CNN, post process the masks
  50. by taking the ins corresponding to the class with max
  51. probability (which are of fixed size and directly output
  52. by the CNN) and return the masks in the ins field of the BoxList.
  53. Args:
  54. x (Tensor): the ins logits
  55. labels (list[BoxList]): bounding boxes that are used as
  56. reference, one for ech image
  57. Returns:
  58. results (list[BoxList]): one BoxList for each image, containing
  59. the extra field ins
  60. """
  61. mask_prob = x.sigmoid()
  62. # select masks corresponding to the predicted classes
  63. num_masks = x.shape[0]
  64. boxes_per_image = [label.shape[0] for label in labels]
  65. labels = torch.cat(labels)
  66. index = torch.arange(num_masks, device=labels.device)
  67. mask_prob = mask_prob[index, labels][:, None]
  68. mask_prob = mask_prob.split(boxes_per_image, dim=0)
  69. return mask_prob
  70. def project_masks_on_boxes(gt_masks, boxes, matched_idxs, M):
  71. # type: (Tensor, Tensor, Tensor, int) -> Tensor
  72. """
  73. Given segmentation masks and the bounding boxes corresponding
  74. to the location of the masks in the image, this function
  75. crops and resizes the masks in the position defined by the
  76. boxes. This prepares the masks for them to be fed to the
  77. loss computation as the targets.
  78. """
  79. matched_idxs = matched_idxs.to(boxes)
  80. rois = torch.cat([matched_idxs[:, None], boxes], dim=1)
  81. gt_masks = gt_masks[:, None].to(rois)
  82. return roi_align(gt_masks, rois, (M, M), 1.0)[:, 0]
  83. def maskrcnn_loss(mask_logits, proposals, gt_masks, gt_labels, mask_matched_idxs):
  84. # type: (Tensor, List[Tensor], List[Tensor], List[Tensor], List[Tensor]) -> Tensor
  85. """
  86. Args:
  87. proposals (list[BoxList])
  88. mask_logits (Tensor)
  89. targets (list[BoxList])
  90. Return:
  91. mask_loss (Tensor): scalar tensor containing the loss
  92. """
  93. discretization_size = mask_logits.shape[-1]
  94. labels = [gt_label[idxs] for gt_label, idxs in zip(gt_labels, mask_matched_idxs)]
  95. mask_targets = [
  96. project_masks_on_boxes(m, p, i, discretization_size) for m, p, i in zip(gt_masks, proposals, mask_matched_idxs)
  97. ]
  98. labels = torch.cat(labels, dim=0)
  99. mask_targets = torch.cat(mask_targets, dim=0)
  100. # torch.mean (in binary_cross_entropy_with_logits) doesn't
  101. # accept empty tensors, so handle it separately
  102. if mask_targets.numel() == 0:
  103. return mask_logits.sum() * 0
  104. mask_loss = F.binary_cross_entropy_with_logits(
  105. mask_logits[torch.arange(labels.shape[0], device=labels.device), labels], mask_targets
  106. )
  107. return mask_loss
  108. def keypoints_to_heatmap(keypoints, rois, heatmap_size):
  109. # type: (Tensor, Tensor, int) -> Tuple[Tensor, Tensor]
  110. offset_x = rois[:, 0]
  111. offset_y = rois[:, 1]
  112. scale_x = heatmap_size / (rois[:, 2] - rois[:, 0])
  113. scale_y = heatmap_size / (rois[:, 3] - rois[:, 1])
  114. offset_x = offset_x[:, None]
  115. offset_y = offset_y[:, None]
  116. scale_x = scale_x[:, None]
  117. scale_y = scale_y[:, None]
  118. x = keypoints[..., 0]
  119. y = keypoints[..., 1]
  120. x_boundary_inds = x == rois[:, 2][:, None]
  121. y_boundary_inds = y == rois[:, 3][:, None]
  122. x = (x - offset_x) * scale_x
  123. x = x.floor().long()
  124. y = (y - offset_y) * scale_y
  125. y = y.floor().long()
  126. x[x_boundary_inds] = heatmap_size - 1
  127. y[y_boundary_inds] = heatmap_size - 1
  128. valid_loc = (x >= 0) & (y >= 0) & (x < heatmap_size) & (y < heatmap_size)
  129. vis = keypoints[..., 2] > 0
  130. valid = (valid_loc & vis).long()
  131. lin_ind = y * heatmap_size + x
  132. heatmaps = lin_ind * valid
  133. return heatmaps, valid
  134. def _onnx_heatmaps_to_keypoints(
  135. maps, maps_i, roi_map_width, roi_map_height, widths_i, heights_i, offset_x_i, offset_y_i
  136. ):
  137. num_keypoints = torch.scalar_tensor(maps.size(1), dtype=torch.int64)
  138. width_correction = widths_i / roi_map_width
  139. height_correction = heights_i / roi_map_height
  140. roi_map = F.interpolate(
  141. maps_i[:, None], size=(int(roi_map_height), int(roi_map_width)), mode="bicubic", align_corners=False
  142. )[:, 0]
  143. w = torch.scalar_tensor(roi_map.size(2), dtype=torch.int64)
  144. pos = roi_map.reshape(num_keypoints, -1).argmax(dim=1)
  145. x_int = pos % w
  146. y_int = (pos - x_int) // w
  147. x = (torch.tensor(0.5, dtype=torch.float32) + x_int.to(dtype=torch.float32)) * width_correction.to(
  148. dtype=torch.float32
  149. )
  150. y = (torch.tensor(0.5, dtype=torch.float32) + y_int.to(dtype=torch.float32)) * height_correction.to(
  151. dtype=torch.float32
  152. )
  153. xy_preds_i_0 = x + offset_x_i.to(dtype=torch.float32)
  154. xy_preds_i_1 = y + offset_y_i.to(dtype=torch.float32)
  155. xy_preds_i_2 = torch.ones(xy_preds_i_1.shape, dtype=torch.float32)
  156. xy_preds_i = torch.stack(
  157. [
  158. xy_preds_i_0.to(dtype=torch.float32),
  159. xy_preds_i_1.to(dtype=torch.float32),
  160. xy_preds_i_2.to(dtype=torch.float32),
  161. ],
  162. 0,
  163. )
  164. # TODO: simplify when indexing without rank will be supported by ONNX
  165. base = num_keypoints * num_keypoints + num_keypoints + 1
  166. ind = torch.arange(num_keypoints)
  167. ind = ind.to(dtype=torch.int64) * base
  168. end_scores_i = (
  169. roi_map.index_select(1, y_int.to(dtype=torch.int64))
  170. .index_select(2, x_int.to(dtype=torch.int64))
  171. .view(-1)
  172. .index_select(0, ind.to(dtype=torch.int64))
  173. )
  174. return xy_preds_i, end_scores_i
  175. @torch.jit._script_if_tracing
  176. def _onnx_heatmaps_to_keypoints_loop(
  177. maps, rois, widths_ceil, heights_ceil, widths, heights, offset_x, offset_y, num_keypoints
  178. ):
  179. xy_preds = torch.zeros((0, 3, int(num_keypoints)), dtype=torch.float32, device=maps.device)
  180. end_scores = torch.zeros((0, int(num_keypoints)), dtype=torch.float32, device=maps.device)
  181. for i in range(int(rois.size(0))):
  182. xy_preds_i, end_scores_i = _onnx_heatmaps_to_keypoints(
  183. maps, maps[i], widths_ceil[i], heights_ceil[i], widths[i], heights[i], offset_x[i], offset_y[i]
  184. )
  185. xy_preds = torch.cat((xy_preds.to(dtype=torch.float32), xy_preds_i.unsqueeze(0).to(dtype=torch.float32)), 0)
  186. end_scores = torch.cat(
  187. (end_scores.to(dtype=torch.float32), end_scores_i.to(dtype=torch.float32).unsqueeze(0)), 0
  188. )
  189. return xy_preds, end_scores
  190. def heatmaps_to_keypoints(maps, rois):
  191. """Extract predicted keypoint locations from heatmaps. Output has shape
  192. (#rois, 4, #keypoints) with the 4 rows corresponding to (x, y, logit, prob)
  193. for each keypoint.
  194. """
  195. # This function converts a discrete image coordinate in a HEATMAP_SIZE x
  196. # HEATMAP_SIZE image to a continuous keypoint coordinate. We maintain
  197. # consistency with keypoints_to_heatmap_labels by using the conversion from
  198. # Heckbert 1990: c = d + 0.5, where d is a discrete coordinate and c is a
  199. # continuous coordinate.
  200. offset_x = rois[:, 0]
  201. offset_y = rois[:, 1]
  202. widths = rois[:, 2] - rois[:, 0]
  203. heights = rois[:, 3] - rois[:, 1]
  204. widths = widths.clamp(min=1)
  205. heights = heights.clamp(min=1)
  206. widths_ceil = widths.ceil()
  207. heights_ceil = heights.ceil()
  208. num_keypoints = maps.shape[1]
  209. if torchvision._is_tracing():
  210. xy_preds, end_scores = _onnx_heatmaps_to_keypoints_loop(
  211. maps,
  212. rois,
  213. widths_ceil,
  214. heights_ceil,
  215. widths,
  216. heights,
  217. offset_x,
  218. offset_y,
  219. torch.scalar_tensor(num_keypoints, dtype=torch.int64),
  220. )
  221. return xy_preds.permute(0, 2, 1), end_scores
  222. xy_preds = torch.zeros((len(rois), 3, num_keypoints), dtype=torch.float32, device=maps.device)
  223. end_scores = torch.zeros((len(rois), num_keypoints), dtype=torch.float32, device=maps.device)
  224. for i in range(len(rois)):
  225. roi_map_width = int(widths_ceil[i].item())
  226. roi_map_height = int(heights_ceil[i].item())
  227. width_correction = widths[i] / roi_map_width
  228. height_correction = heights[i] / roi_map_height
  229. roi_map = F.interpolate(
  230. maps[i][:, None], size=(roi_map_height, roi_map_width), mode="bicubic", align_corners=False
  231. )[:, 0]
  232. # roi_map_probs = scores_to_probs(roi_map.copy())
  233. w = roi_map.shape[2]
  234. pos = roi_map.reshape(num_keypoints, -1).argmax(dim=1)
  235. x_int = pos % w
  236. y_int = torch.div(pos - x_int, w, rounding_mode="floor")
  237. # assert (roi_map_probs[k, y_int, x_int] ==
  238. # roi_map_probs[k, :, :].max())
  239. x = (x_int.float() + 0.5) * width_correction
  240. y = (y_int.float() + 0.5) * height_correction
  241. xy_preds[i, 0, :] = x + offset_x[i]
  242. xy_preds[i, 1, :] = y + offset_y[i]
  243. xy_preds[i, 2, :] = 1
  244. end_scores[i, :] = roi_map[torch.arange(num_keypoints, device=roi_map.device), y_int, x_int]
  245. return xy_preds.permute(0, 2, 1), end_scores
  246. def keypointrcnn_loss(keypoint_logits, proposals, gt_keypoints, keypoint_matched_idxs):
  247. # type: (Tensor, List[Tensor], List[Tensor], List[Tensor]) -> Tensor
  248. N, K, H, W = keypoint_logits.shape
  249. if H != W:
  250. raise ValueError(
  251. f"keypoint_logits height and width (last two elements of shape) should be equal. Instead got H = {H} and W = {W}"
  252. )
  253. discretization_size = H
  254. heatmaps = []
  255. valid = []
  256. for proposals_per_image, gt_kp_in_image, midx in zip(proposals, gt_keypoints, keypoint_matched_idxs):
  257. kp = gt_kp_in_image[midx]
  258. heatmaps_per_image, valid_per_image = keypoints_to_heatmap(kp, proposals_per_image, discretization_size)
  259. heatmaps.append(heatmaps_per_image.view(-1))
  260. valid.append(valid_per_image.view(-1))
  261. keypoint_targets = torch.cat(heatmaps, dim=0)
  262. valid = torch.cat(valid, dim=0).to(dtype=torch.uint8)
  263. valid = torch.where(valid)[0]
  264. # torch.mean (in binary_cross_entropy_with_logits) doesn't
  265. # accept empty tensors, so handle it sepaartely
  266. if keypoint_targets.numel() == 0 or len(valid) == 0:
  267. return keypoint_logits.sum() * 0
  268. keypoint_logits = keypoint_logits.view(N * K, H * W)
  269. keypoint_loss = F.cross_entropy(keypoint_logits[valid], keypoint_targets[valid])
  270. return keypoint_loss
  271. def keypointrcnn_inference(x, boxes):
  272. # type: (Tensor, List[Tensor]) -> Tuple[List[Tensor], List[Tensor]]
  273. kp_probs = []
  274. kp_scores = []
  275. boxes_per_image = [box.size(0) for box in boxes]
  276. x2 = x.split(boxes_per_image, dim=0)
  277. for xx, bb in zip(x2, boxes):
  278. kp_prob, scores = heatmaps_to_keypoints(xx, bb)
  279. kp_probs.append(kp_prob)
  280. kp_scores.append(scores)
  281. return kp_probs, kp_scores
  282. def _onnx_expand_boxes(boxes, scale):
  283. # type: (Tensor, float) -> Tensor
  284. w_half = (boxes[:, 2] - boxes[:, 0]) * 0.5
  285. h_half = (boxes[:, 3] - boxes[:, 1]) * 0.5
  286. x_c = (boxes[:, 2] + boxes[:, 0]) * 0.5
  287. y_c = (boxes[:, 3] + boxes[:, 1]) * 0.5
  288. w_half = w_half.to(dtype=torch.float32) * scale
  289. h_half = h_half.to(dtype=torch.float32) * scale
  290. boxes_exp0 = x_c - w_half
  291. boxes_exp1 = y_c - h_half
  292. boxes_exp2 = x_c + w_half
  293. boxes_exp3 = y_c + h_half
  294. boxes_exp = torch.stack((boxes_exp0, boxes_exp1, boxes_exp2, boxes_exp3), 1)
  295. return boxes_exp
  296. # the next two functions should be merged inside Masker
  297. # but are kept here for the moment while we need them
  298. # temporarily for paste_mask_in_image
  299. def expand_boxes(boxes, scale):
  300. # type: (Tensor, float) -> Tensor
  301. if torchvision._is_tracing():
  302. return _onnx_expand_boxes(boxes, scale)
  303. w_half = (boxes[:, 2] - boxes[:, 0]) * 0.5
  304. h_half = (boxes[:, 3] - boxes[:, 1]) * 0.5
  305. x_c = (boxes[:, 2] + boxes[:, 0]) * 0.5
  306. y_c = (boxes[:, 3] + boxes[:, 1]) * 0.5
  307. w_half *= scale
  308. h_half *= scale
  309. boxes_exp = torch.zeros_like(boxes)
  310. boxes_exp[:, 0] = x_c - w_half
  311. boxes_exp[:, 2] = x_c + w_half
  312. boxes_exp[:, 1] = y_c - h_half
  313. boxes_exp[:, 3] = y_c + h_half
  314. return boxes_exp
  315. @torch.jit.unused
  316. def expand_masks_tracing_scale(M, padding):
  317. # type: (int, int) -> float
  318. return torch.tensor(M + 2 * padding).to(torch.float32) / torch.tensor(M).to(torch.float32)
  319. def expand_masks(mask, padding):
  320. # type: (Tensor, int) -> Tuple[Tensor, float]
  321. M = mask.shape[-1]
  322. if torch._C._get_tracing_state(): # could not import is_tracing(), not sure why
  323. scale = expand_masks_tracing_scale(M, padding)
  324. else:
  325. scale = float(M + 2 * padding) / M
  326. padded_mask = F.pad(mask, (padding,) * 4)
  327. return padded_mask, scale
  328. def paste_mask_in_image(mask, box, im_h, im_w):
  329. # type: (Tensor, Tensor, int, int) -> Tensor
  330. TO_REMOVE = 1
  331. w = int(box[2] - box[0] + TO_REMOVE)
  332. h = int(box[3] - box[1] + TO_REMOVE)
  333. w = max(w, 1)
  334. h = max(h, 1)
  335. # Set shape to [batchxCxHxW]
  336. mask = mask.expand((1, 1, -1, -1))
  337. # Resize ins
  338. mask = F.interpolate(mask, size=(h, w), mode="bilinear", align_corners=False)
  339. mask = mask[0][0]
  340. im_mask = torch.zeros((im_h, im_w), dtype=mask.dtype, device=mask.device)
  341. x_0 = max(box[0], 0)
  342. x_1 = min(box[2] + 1, im_w)
  343. y_0 = max(box[1], 0)
  344. y_1 = min(box[3] + 1, im_h)
  345. im_mask[y_0:y_1, x_0:x_1] = mask[(y_0 - box[1]): (y_1 - box[1]), (x_0 - box[0]): (x_1 - box[0])]
  346. return im_mask
  347. def _onnx_paste_mask_in_image(mask, box, im_h, im_w):
  348. one = torch.ones(1, dtype=torch.int64)
  349. zero = torch.zeros(1, dtype=torch.int64)
  350. w = box[2] - box[0] + one
  351. h = box[3] - box[1] + one
  352. w = torch.max(torch.cat((w, one)))
  353. h = torch.max(torch.cat((h, one)))
  354. # Set shape to [batchxCxHxW]
  355. mask = mask.expand((1, 1, mask.size(0), mask.size(1)))
  356. # Resize ins
  357. mask = F.interpolate(mask, size=(int(h), int(w)), mode="bilinear", align_corners=False)
  358. mask = mask[0][0]
  359. x_0 = torch.max(torch.cat((box[0].unsqueeze(0), zero)))
  360. x_1 = torch.min(torch.cat((box[2].unsqueeze(0) + one, im_w.unsqueeze(0))))
  361. y_0 = torch.max(torch.cat((box[1].unsqueeze(0), zero)))
  362. y_1 = torch.min(torch.cat((box[3].unsqueeze(0) + one, im_h.unsqueeze(0))))
  363. unpaded_im_mask = mask[(y_0 - box[1]): (y_1 - box[1]), (x_0 - box[0]): (x_1 - box[0])]
  364. # TODO : replace below with a dynamic padding when support is added in ONNX
  365. # pad y
  366. zeros_y0 = torch.zeros(y_0, unpaded_im_mask.size(1))
  367. zeros_y1 = torch.zeros(im_h - y_1, unpaded_im_mask.size(1))
  368. concat_0 = torch.cat((zeros_y0, unpaded_im_mask.to(dtype=torch.float32), zeros_y1), 0)[0:im_h, :]
  369. # pad x
  370. zeros_x0 = torch.zeros(concat_0.size(0), x_0)
  371. zeros_x1 = torch.zeros(concat_0.size(0), im_w - x_1)
  372. im_mask = torch.cat((zeros_x0, concat_0, zeros_x1), 1)[:, :im_w]
  373. return im_mask
  374. @torch.jit._script_if_tracing
  375. def _onnx_paste_masks_in_image_loop(masks, boxes, im_h, im_w):
  376. res_append = torch.zeros(0, im_h, im_w)
  377. for i in range(masks.size(0)):
  378. mask_res = _onnx_paste_mask_in_image(masks[i][0], boxes[i], im_h, im_w)
  379. mask_res = mask_res.unsqueeze(0)
  380. res_append = torch.cat((res_append, mask_res))
  381. return res_append
  382. def paste_masks_in_image(masks, boxes, img_shape, padding=1):
  383. # type: (Tensor, Tensor, Tuple[int, int], int) -> Tensor
  384. masks, scale = expand_masks(masks, padding=padding)
  385. boxes = expand_boxes(boxes, scale).to(dtype=torch.int64)
  386. im_h, im_w = img_shape
  387. if torchvision._is_tracing():
  388. return _onnx_paste_masks_in_image_loop(
  389. masks, boxes, torch.scalar_tensor(im_h, dtype=torch.int64), torch.scalar_tensor(im_w, dtype=torch.int64)
  390. )[:, None]
  391. res = [paste_mask_in_image(m[0], b, im_h, im_w) for m, b in zip(masks, boxes)]
  392. if len(res) > 0:
  393. ret = torch.stack(res, dim=0)[:, None]
  394. else:
  395. ret = masks.new_empty((0, 1, im_h, im_w))
  396. return ret
  397. class RoIHeads(nn.Module):
  398. __annotations__ = {
  399. "box_coder": det_utils.BoxCoder,
  400. "proposal_matcher": det_utils.Matcher,
  401. "fg_bg_sampler": det_utils.BalancedPositiveNegativeSampler,
  402. }
  403. def __init__(
  404. self,
  405. box_roi_pool,
  406. box_head,
  407. box_predictor,
  408. # Faster R-CNN training
  409. fg_iou_thresh,
  410. bg_iou_thresh,
  411. batch_size_per_image,
  412. positive_fraction,
  413. bbox_reg_weights,
  414. # Faster R-CNN inference
  415. score_thresh,
  416. nms_thresh,
  417. detections_per_img,
  418. # Line
  419. line_roi_pool=None,
  420. line_head=None,
  421. line_predictor=None,
  422. # point parameters
  423. point_roi_pool=None,
  424. point_head=None,
  425. point_predictor=None,
  426. ins_head=None,
  427. ins_predictor=None,
  428. ins_roi_pool=None,
  429. # arc parameters
  430. arc_roi_pool=None,
  431. arc_head=None,
  432. arc_predictor=None,
  433. # Mask
  434. mask_roi_pool=None,
  435. mask_head=None,
  436. mask_predictor=None,
  437. keypoint_roi_pool=None,
  438. keypoint_head=None,
  439. keypoint_predictor=None,
  440. detect_point=True,
  441. detect_line=False,
  442. detect_arc=False,
  443. detect_circle=False,
  444. ):
  445. super().__init__()
  446. self.box_similarity = box_ops.box_iou
  447. # assign ground-truth boxes for each proposal
  448. self.proposal_matcher = det_utils.Matcher(fg_iou_thresh, bg_iou_thresh, allow_low_quality_matches=False)
  449. self.fg_bg_sampler = det_utils.BalancedPositiveNegativeSampler(batch_size_per_image, positive_fraction)
  450. if bbox_reg_weights is None:
  451. bbox_reg_weights = (10.0, 10.0, 5.0, 5.0)
  452. self.box_coder = det_utils.BoxCoder(bbox_reg_weights)
  453. self.box_roi_pool = box_roi_pool
  454. self.box_head = box_head
  455. self.box_predictor = box_predictor
  456. self.score_thresh = score_thresh
  457. self.nms_thresh = nms_thresh
  458. self.detections_per_img = detections_per_img
  459. self.line_roi_pool = line_roi_pool
  460. self.line_head = line_head
  461. self.line_predictor = line_predictor
  462. self.point_roi_pool = point_roi_pool
  463. self.point_head = point_head
  464. self.point_predictor = point_predictor
  465. self.arc_roi_pool = arc_roi_pool
  466. self.arc_head = arc_head
  467. self.arc_predictor = arc_predictor
  468. self.ins_roi_pool = ins_roi_pool
  469. self.ins_head = ins_head
  470. self.ins_predictor = ins_predictor
  471. self.mask_roi_pool = mask_roi_pool
  472. self.mask_head = mask_head
  473. self.mask_predictor = mask_predictor
  474. self.keypoint_roi_pool = keypoint_roi_pool
  475. self.keypoint_head = keypoint_head
  476. self.keypoint_predictor = keypoint_predictor
  477. self.detect_point =detect_point
  478. self.detect_line =detect_line
  479. self.detect_arc =detect_arc
  480. self.detect_circle=detect_circle
  481. self.channel_compress = nn.Sequential(
  482. nn.Conv2d(256, 8, kernel_size=1),
  483. nn.BatchNorm2d(8),
  484. nn.ReLU(inplace=True)
  485. )
  486. def has_mask(self):
  487. if self.mask_roi_pool is None:
  488. return False
  489. if self.mask_head is None:
  490. return False
  491. if self.mask_predictor is None:
  492. return False
  493. return True
  494. def has_keypoint(self):
  495. if self.keypoint_roi_pool is None:
  496. return False
  497. if self.keypoint_head is None:
  498. return False
  499. if self.keypoint_predictor is None:
  500. return False
  501. return True
  502. def has_line(self):
  503. # if self.line_roi_pool is None:
  504. # return False
  505. if self.line_head is None:
  506. return False
  507. # if self.line_predictor is None:
  508. # return False
  509. return True
  510. def has_point(self):
  511. # if self.line_roi_pool is None:
  512. # return False
  513. if self.point_head is None:
  514. return False
  515. # if self.line_predictor is None:
  516. # return False
  517. return True
  518. def has_arc(self):
  519. # if self.line_roi_pool is None:
  520. # return False
  521. if self.arc_head is None:
  522. return False
  523. # if self.line_predictor is None:
  524. # return False
  525. return True
  526. def has_ins(self):
  527. # if self.line_roi_pool is None:
  528. # return False
  529. if self.ins_head is None:
  530. return False
  531. # if self.line_predictor is None:
  532. # return False
  533. return True
  534. def assign_targets_to_proposals(self, proposals, gt_boxes, gt_labels):
  535. # type: (List[Tensor], List[Tensor], List[Tensor]) -> Tuple[List[Tensor], List[Tensor]]
  536. matched_idxs = []
  537. labels = []
  538. for proposals_in_image, gt_boxes_in_image, gt_labels_in_image in zip(proposals, gt_boxes, gt_labels):
  539. if gt_boxes_in_image.numel() == 0:
  540. # Background image
  541. device = proposals_in_image.device
  542. clamped_matched_idxs_in_image = torch.zeros(
  543. (proposals_in_image.shape[0],), dtype=torch.int64, device=device
  544. )
  545. labels_in_image = torch.zeros((proposals_in_image.shape[0],), dtype=torch.int64, device=device)
  546. else:
  547. # set to self.box_similarity when https://github.com/pytorch/pytorch/issues/27495 lands
  548. match_quality_matrix = box_ops.box_iou(gt_boxes_in_image, proposals_in_image)
  549. matched_idxs_in_image = self.proposal_matcher(match_quality_matrix)
  550. clamped_matched_idxs_in_image = matched_idxs_in_image.clamp(min=0)
  551. labels_in_image = gt_labels_in_image[clamped_matched_idxs_in_image]
  552. labels_in_image = labels_in_image.to(dtype=torch.int64)
  553. # Label background (below the low threshold)
  554. bg_inds = matched_idxs_in_image == self.proposal_matcher.BELOW_LOW_THRESHOLD
  555. labels_in_image[bg_inds] = 0
  556. # Label ignore proposals (between low and high thresholds)
  557. ignore_inds = matched_idxs_in_image == self.proposal_matcher.BETWEEN_THRESHOLDS
  558. labels_in_image[ignore_inds] = -1 # -1 is ignored by sampler
  559. matched_idxs.append(clamped_matched_idxs_in_image)
  560. labels.append(labels_in_image)
  561. return matched_idxs, labels
  562. def subsample(self, labels):
  563. # type: (List[Tensor]) -> List[Tensor]
  564. sampled_pos_inds, sampled_neg_inds = self.fg_bg_sampler(labels)
  565. sampled_inds = []
  566. for img_idx, (pos_inds_img, neg_inds_img) in enumerate(zip(sampled_pos_inds, sampled_neg_inds)):
  567. img_sampled_inds = torch.where(pos_inds_img | neg_inds_img)[0]
  568. sampled_inds.append(img_sampled_inds)
  569. return sampled_inds
  570. def add_gt_proposals(self, proposals, gt_boxes):
  571. # type: (List[Tensor], List[Tensor]) -> List[Tensor]
  572. proposals = [torch.cat((proposal, gt_box)) for proposal, gt_box in zip(proposals, gt_boxes)]
  573. return proposals
  574. def check_targets(self, targets):
  575. # type: (Optional[List[Dict[str, Tensor]]]) -> None
  576. if targets is None:
  577. raise ValueError("targets should not be None")
  578. if not all(["boxes" in t for t in targets]):
  579. raise ValueError("Every element of targets should have a boxes key")
  580. if not all(["labels" in t for t in targets]):
  581. raise ValueError("Every element of targets should have a labels key")
  582. if self.has_mask():
  583. if not all(["masks" in t for t in targets]):
  584. raise ValueError("Every element of targets should have a masks key")
  585. def select_training_samples(
  586. self,
  587. proposals, # type: List[Tensor]
  588. targets, # type: Optional[List[Dict[str, Tensor]]]
  589. ):
  590. # type: (...) -> Tuple[List[Tensor], List[Tensor], List[Tensor], List[Tensor]]
  591. self.check_targets(targets)
  592. if targets is None:
  593. raise ValueError("targets should not be None")
  594. dtype = proposals[0].dtype
  595. device = proposals[0].device
  596. gt_boxes = [t["boxes"].to(dtype) for t in targets]
  597. gt_labels = [t["labels"] for t in targets]
  598. # append ground-truth bboxes to propos
  599. proposals = self.add_gt_proposals(proposals, gt_boxes)
  600. # get matching gt indices for each proposal
  601. matched_idxs, labels = self.assign_targets_to_proposals(proposals, gt_boxes, gt_labels)
  602. # sample a fixed proportion of positive-negative proposals
  603. sampled_inds = self.subsample(labels)
  604. matched_gt_boxes = []
  605. num_images = len(proposals)
  606. for img_id in range(num_images):
  607. img_sampled_inds = sampled_inds[img_id]
  608. proposals[img_id] = proposals[img_id][img_sampled_inds]
  609. labels[img_id] = labels[img_id][img_sampled_inds]
  610. matched_idxs[img_id] = matched_idxs[img_id][img_sampled_inds]
  611. gt_boxes_in_image = gt_boxes[img_id]
  612. if gt_boxes_in_image.numel() == 0:
  613. gt_boxes_in_image = torch.zeros((1, 4), dtype=dtype, device=device)
  614. matched_gt_boxes.append(gt_boxes_in_image[matched_idxs[img_id]])
  615. regression_targets = self.box_coder.encode(matched_gt_boxes, proposals)
  616. return proposals, matched_idxs, labels, regression_targets
  617. def postprocess_detections(
  618. self,
  619. class_logits, # type: Tensor
  620. box_regression, # type: Tensor
  621. proposals, # type: List[Tensor]
  622. image_shapes, # type: List[Tuple[int, int]]
  623. ):
  624. # type: (...) -> Tuple[List[Tensor], List[Tensor], List[Tensor]]
  625. device = class_logits.device
  626. num_classes = class_logits.shape[-1]
  627. boxes_per_image = [boxes_in_image.shape[0] for boxes_in_image in proposals]
  628. pred_boxes = self.box_coder.decode(box_regression, proposals)
  629. pred_scores = F.softmax(class_logits, -1)
  630. pred_boxes_list = pred_boxes.split(boxes_per_image, 0)
  631. pred_scores_list = pred_scores.split(boxes_per_image, 0)
  632. all_boxes = []
  633. all_scores = []
  634. all_labels = []
  635. for boxes, scores, image_shape in zip(pred_boxes_list, pred_scores_list, image_shapes):
  636. boxes = box_ops.clip_boxes_to_image(boxes, image_shape)
  637. # create labels for each prediction
  638. labels = torch.arange(num_classes, device=device)
  639. labels = labels.view(1, -1).expand_as(scores)
  640. # remove predictions with the background label
  641. boxes = boxes[:, 1:]
  642. scores = scores[:, 1:]
  643. labels = labels[:, 1:]
  644. # batch everything, by making every class prediction be a separate instance
  645. boxes = boxes.reshape(-1, 4)
  646. scores = scores.reshape(-1)
  647. labels = labels.reshape(-1)
  648. # remove low scoring boxes
  649. inds = torch.where(scores > self.score_thresh)[0]
  650. boxes, scores, labels = boxes[inds], scores[inds], labels[inds]
  651. # remove empty boxes
  652. keep = box_ops.remove_small_boxes(boxes, min_size=1e-2)
  653. boxes, scores, labels = boxes[keep], scores[keep], labels[keep]
  654. # non-maximum suppression, independently done per class
  655. keep = box_ops.batched_nms(boxes, scores, labels, self.nms_thresh)
  656. # keep only topk scoring predictions
  657. keep = keep[: self.detections_per_img]
  658. boxes, scores, labels = boxes[keep], scores[keep], labels[keep]
  659. all_boxes.append(boxes)
  660. all_scores.append(scores)
  661. all_labels.append(labels)
  662. return all_boxes, all_scores, all_labels
  663. def forward(
  664. self,
  665. features, # type: Dict[str, Tensor]
  666. proposals, # type: List[Tensor]
  667. image_shapes, # type: List[Tuple[int, int]]
  668. targets=None, # type: Optional[List[Dict[str, Tensor]]]
  669. ):
  670. # type: (...) -> Tuple[List[Dict[str, Tensor]], Dict[str, Tensor]]
  671. """
  672. Args:
  673. features (List[Tensor])
  674. proposals (List[Tensor[N, 4]])
  675. image_shapes (List[Tuple[H, W]])
  676. targets (List[Dict])
  677. """
  678. print(f'roihead forward!!!')
  679. if targets is not None:
  680. for t in targets:
  681. # TODO: https://github.com/pytorch/pytorch/issues/26731
  682. floating_point_types = (torch.float, torch.double, torch.half)
  683. if not t["boxes"].dtype in floating_point_types:
  684. raise TypeError(f"target boxes must of float type, instead got {t['boxes'].dtype}")
  685. if not t["labels"].dtype == torch.int64:
  686. raise TypeError(f"target labels must of int64 type, instead got {t['labels'].dtype}")
  687. if self.has_keypoint():
  688. if not t["keypoints"].dtype == torch.float32:
  689. raise TypeError(f"target keypoints must of float type, instead got {t['keypoints'].dtype}")
  690. if self.training:
  691. proposals, matched_idxs, labels, regression_targets = self.select_training_samples(proposals, targets)
  692. else:
  693. if targets is not None:
  694. proposals, matched_idxs, labels, regression_targets = self.select_training_samples(proposals, targets)
  695. else:
  696. labels = None
  697. regression_targets = None
  698. matched_idxs = None
  699. device=features['0'].device
  700. box_features = self.box_roi_pool(features, proposals, image_shapes)
  701. box_features = self.box_head(box_features)
  702. class_logits, box_regression = self.box_predictor(box_features)
  703. result: List[Dict[str, torch.Tensor]] = []
  704. losses = {}
  705. # _, C, H, W = features['0'].shape # 忽略 batch_size,因为我们只关心 C, H, W
  706. if self.training:
  707. if labels is None:
  708. raise ValueError("labels cannot be None")
  709. if regression_targets is None:
  710. raise ValueError("regression_targets cannot be None")
  711. print(f'boxes compute losses')
  712. loss_classifier, loss_box_reg = fastrcnn_loss(class_logits, box_regression, labels, regression_targets)
  713. losses = {"loss_classifier": loss_classifier, "loss_box_reg": loss_box_reg}
  714. else:
  715. if targets is not None:
  716. loss_classifier, loss_box_reg = fastrcnn_loss(class_logits, box_regression, labels, regression_targets)
  717. losses = {"loss_classifier": loss_classifier, "loss_box_reg": loss_box_reg}
  718. boxes, scores, labels = self.postprocess_detections(class_logits, box_regression, proposals,
  719. image_shapes)
  720. num_images = len(boxes)
  721. for i in range(num_images):
  722. result.append(
  723. {
  724. "boxes": boxes[i],
  725. "labels": labels[i],
  726. "scores": scores[i],
  727. }
  728. )
  729. if self.has_line() and self.detect_line:
  730. print(f'roi_heads forward has_line()!!!!')
  731. # print(f'labels:{labels}')
  732. line_proposals = [p["boxes"] for p in result]
  733. point_proposals = [p["boxes"] for p in result]
  734. print(f'boxes_proposals:{len(line_proposals)}')
  735. # if line_proposals is None or len(line_proposals) == 0:
  736. # # 返回空特征或者跳过该部分计算
  737. # return torch.empty(0, C, H, W).to(features['0'].device)
  738. if self.training:
  739. # during training, only focus on positive boxes
  740. num_images = len(proposals)
  741. print(f'num_images:{num_images}')
  742. line_proposals = []
  743. point_proposals = []
  744. arc_proposals = []
  745. pos_matched_idxs = []
  746. line_pos_matched_idxs = []
  747. point_pos_matched_idxs = []
  748. if matched_idxs is None:
  749. raise ValueError("if in trainning, matched_idxs should not be None")
  750. for img_id in range(num_images):
  751. pos = torch.where(labels[img_id] > 0)[0]
  752. line_pos=torch.where(labels[img_id] ==2)[0]
  753. # point_pos=torch.where(labels[img_id] ==1)[0]
  754. line_proposals.append(proposals[img_id][line_pos])
  755. # point_proposals.append(proposals[img_id][point_pos])
  756. line_pos_matched_idxs.append(matched_idxs[img_id][line_pos])
  757. # point_pos_matched_idxs.append(matched_idxs[img_id][point_pos])
  758. # pos_matched_idxs.append(matched_idxs[img_id][pos])
  759. else:
  760. if targets is not None:
  761. pos_matched_idxs = []
  762. num_images = len(proposals)
  763. line_proposals = []
  764. line_pos_matched_idxs = []
  765. print(f'val num_images:{num_images}')
  766. if matched_idxs is None:
  767. raise ValueError("if in trainning, matched_idxs should not be None")
  768. for img_id in range(num_images):
  769. # pos = torch.where(labels[img_id] > 0)[0]
  770. line_pos = torch.where(labels[img_id] == 2)[0]
  771. line_proposals.append(proposals[img_id][line_pos])
  772. line_pos_matched_idxs.append(matched_idxs[img_id][line_pos])
  773. else:
  774. pos_matched_idxs = None
  775. line_proposals_valid=self.check_proposals(line_proposals)
  776. if line_proposals_valid:
  777. feature_logits = self.line_forward3(features, image_shapes, line_proposals)
  778. loss_line = None
  779. loss_line_iou =None
  780. if self.training:
  781. if targets is None or pos_matched_idxs is None:
  782. raise ValueError("both targets and pos_matched_idxs should not be None when in training mode")
  783. gt_lines = [t["lines"] for t in targets if "lines" in t]
  784. # print(f'gt_lines:{gt_lines[0].shape}')
  785. h, w = targets[0]["img_size"]
  786. img_size = h
  787. gt_lines_tensor=torch.zeros(0,0)
  788. if len(gt_lines)>0:
  789. gt_lines_tensor = torch.cat(gt_lines)
  790. print(f'gt_lines_tensor:{gt_lines_tensor.shape}')
  791. if gt_lines_tensor.shape[0]>0 :
  792. print(f'start to lines_point_pair_loss')
  793. loss_line = lines_point_pair_loss(
  794. feature_logits, line_proposals, gt_lines, line_pos_matched_idxs
  795. )
  796. loss_line_iou = line_iou_loss(feature_logits, line_proposals, gt_lines, line_pos_matched_idxs, img_size)
  797. if loss_line is None:
  798. print(f'loss_line is None111')
  799. loss_line = torch.tensor(0.0, device=device)
  800. if loss_line_iou is None:
  801. print(f'loss_line_iou is None111')
  802. loss_line_iou = torch.tensor(0.0, device=device)
  803. loss_line = {"loss_line": loss_line}
  804. loss_line_iou = {'loss_line_iou': loss_line_iou}
  805. else:
  806. if targets is not None:
  807. h, w = targets[0]["img_size"]
  808. img_size = h
  809. gt_lines = [t["lines"] for t in targets if "lines" in t]
  810. gt_lines_tensor = torch.zeros(0, 0)
  811. if len(gt_lines)>0:
  812. gt_lines_tensor = torch.cat(gt_lines)
  813. if gt_lines_tensor.shape[0] > 0 and feature_logits is not None:
  814. loss_line = lines_point_pair_loss(
  815. feature_logits, line_proposals, gt_lines, line_pos_matched_idxs
  816. )
  817. print(f'compute_line_loss:{loss_line}')
  818. loss_line_iou = line_iou_loss(feature_logits , line_proposals, gt_lines, line_pos_matched_idxs,
  819. img_size)
  820. if loss_line is None:
  821. print(f'loss_line is None')
  822. loss_line=torch.tensor(0.0,device=device)
  823. if loss_line_iou is None:
  824. print(f'loss_line_iou is None')
  825. loss_line_iou=torch.tensor(0.0,device=device)
  826. loss_line = {"loss_line": loss_line}
  827. loss_line_iou = {'loss_line_iou': loss_line_iou}
  828. else:
  829. loss_line = {}
  830. loss_line_iou = {}
  831. if feature_logits is None or line_proposals is None:
  832. raise ValueError(
  833. "both keypoint_logits and keypoint_proposals should not be None when not in training mode"
  834. )
  835. if feature_logits is not None:
  836. lines_probs, lines_scores = line_inference(feature_logits,line_proposals)
  837. for keypoint_prob, kps, r in zip(lines_probs, lines_scores, result):
  838. r["lines"] = keypoint_prob
  839. r["lines_scores"] = kps
  840. print(f'loss_line11111:{loss_line}')
  841. losses.update(loss_line)
  842. losses.update(loss_line_iou)
  843. print(f'losses:{losses}')
  844. if self.has_point() and self.detect_point:
  845. print(f'roi_heads forward has_point()!!!!')
  846. # print(f'labels:{labels}')
  847. point_proposals = [p["boxes"] for p in result]
  848. print(f'boxes_proposals:{len(point_proposals)}')
  849. # if line_proposals is None or len(line_proposals) == 0:
  850. # # 返回空特征或者跳过该部分计算
  851. # return torch.empty(0, C, H, W).to(features['0'].device)
  852. if self.training:
  853. # during training, only focus on positive boxes
  854. num_images = len(proposals)
  855. print(f'num_images:{num_images}')
  856. point_proposals = []
  857. point_pos_matched_idxs = []
  858. if matched_idxs is None:
  859. raise ValueError("if in trainning, matched_idxs should not be None")
  860. for img_id in range(num_images):
  861. point_pos=torch.where(labels[img_id] ==1)[0]
  862. point_proposals.append(proposals[img_id][point_pos])
  863. point_pos_matched_idxs.append(matched_idxs[img_id][point_pos])
  864. else:
  865. if targets is not None:
  866. num_images = len(proposals)
  867. point_proposals = []
  868. point_pos_matched_idxs = []
  869. print(f'val num_images:{num_images}')
  870. if matched_idxs is None:
  871. raise ValueError("if in trainning, matched_idxs should not be None")
  872. for img_id in range(num_images):
  873. point_pos = torch.where(labels[img_id] == 1)[0]
  874. point_proposals.append(proposals[img_id][point_pos])
  875. point_pos_matched_idxs.append(matched_idxs[img_id][point_pos])
  876. else:
  877. pos_matched_idxs = None
  878. point_proposals_valid = self.check_proposals(point_proposals)
  879. if point_proposals_valid:
  880. feature_logits = self.point_forward1(features, image_shapes, point_proposals)
  881. loss_point=None
  882. if self.training:
  883. if targets is None or point_pos_matched_idxs is None:
  884. raise ValueError("both targets and pos_matched_idxs should not be None when in training mode")
  885. gt_points = [t["points"] for t in targets if "points" in t]
  886. print(f'gt_points:{gt_points[0].shape}')
  887. h, w = targets[0]["img_size"]
  888. img_size = h
  889. gt_points_tensor = torch.zeros(0, 0)
  890. if len(gt_points) > 0:
  891. gt_points_tensor = torch.cat(gt_points)
  892. print(f'gt_points_tensor:{gt_points_tensor.shape}')
  893. if gt_points_tensor.shape[0] > 0:
  894. print(f'start to compute point_loss')
  895. loss_point=compute_point_loss(feature_logits,point_proposals,gt_points,point_pos_matched_idxs)
  896. if loss_point is None:
  897. print(f'loss_point is None111')
  898. loss_point = torch.tensor(0.0, device=device)
  899. loss_point = {"loss_point": loss_point}
  900. else:
  901. if targets is not None:
  902. h, w = targets[0]["img_size"]
  903. img_size = h
  904. gt_points = [t["points"] for t in targets if "points" in t]
  905. gt_points_tensor = torch.zeros(0, 0)
  906. if len(gt_points) > 0:
  907. gt_points_tensor = torch.cat(gt_points)
  908. print(f'gt_points_tensor:{gt_points_tensor.shape}')
  909. if gt_points_tensor.shape[0] > 0:
  910. print(f'start to compute point_loss')
  911. loss_point = compute_point_loss(feature_logits, point_proposals, gt_points,
  912. point_pos_matched_idxs)
  913. if loss_point is None:
  914. print(f'loss_point is None111')
  915. loss_point = torch.tensor(0.0, device=device)
  916. loss_point = {"loss_point": loss_point}
  917. else:
  918. loss_point = {}
  919. if feature_logits is None or point_proposals is None:
  920. raise ValueError(
  921. "both keypoint_logits and keypoint_proposals should not be None when not in training mode"
  922. )
  923. if feature_logits is not None:
  924. points_probs, points_scores = point_inference(feature_logits,point_proposals)
  925. for keypoint_prob, kps, r in zip(points_probs, points_scores, result):
  926. r["points"] = keypoint_prob
  927. r["points_scores"] = kps
  928. print(f'loss_point:{loss_point}')
  929. losses.update(loss_point)
  930. print(f'losses:{losses}')
  931. if self.has_arc() and self.detect_arc:
  932. print(f'roi_heads forward has_arc()!!!!')
  933. # print(f'labels:{labels}')
  934. arc_proposals = [p["boxes"] for p in result]
  935. print(f'boxes_proposals:{len(arc_proposals)}')
  936. print(f'boxes_proposals:{len(arc_proposals)}')
  937. # if line_proposals is None or len(line_proposals) == 0:
  938. # # 返回空特征或者跳过该部分计算
  939. # return torch.empty(0, C, H, W).to(features['0'].device)
  940. if self.training:
  941. # during training, only focus on positive boxes
  942. num_images = len(proposals)
  943. print(f'num_images:{num_images}')
  944. arc_proposals = []
  945. arc_pos_matched_idxs = []
  946. if matched_idxs is None:
  947. raise ValueError("if in trainning, matched_idxs should not be None")
  948. for img_id in range(num_images):
  949. arc_pos=torch.where(labels[img_id] ==3)[0]
  950. arc_proposals.append(proposals[img_id][arc_pos])
  951. arc_pos_matched_idxs.append(matched_idxs[img_id][arc_pos])
  952. else:
  953. if targets is not None:
  954. num_images = len(proposals)
  955. arc_proposals = []
  956. arc_pos_matched_idxs = []
  957. print(f'val num_images:{num_images}')
  958. if matched_idxs is None:
  959. raise ValueError("if in trainning, matched_idxs should not be None")
  960. for img_id in range(num_images):
  961. arc_pos = torch.where(labels[img_id] == 3)[0]
  962. arc_proposals.append(proposals[img_id][arc_pos])
  963. arc_pos_matched_idxs.append(matched_idxs[img_id][arc_pos])
  964. else:
  965. arc_pos_matched_idxs = None
  966. arc_proposals_valid=self.check_proposals(arc_proposals)
  967. if arc_proposals_valid:
  968. feature_logits = self.arc_forward1(features, image_shapes, arc_proposals)
  969. loss_arc=None
  970. if self.training:
  971. if targets is None or arc_pos_matched_idxs is None:
  972. raise ValueError("both targets and pos_matched_idxs should not be None when in training mode")
  973. gt_arcs = [t["arc_mask"] for t in targets if "arc_mask" in t]
  974. print(f'gt_arcs:{gt_arcs[0].shape}')
  975. h, w = targets[0]["img_size"]
  976. img_size = h
  977. # gt_arcs_tensor = torch.zeros(0, 0)
  978. # if len(gt_arcs) > 0:
  979. # gt_arcs_tensor = torch.cat(gt_arcs)
  980. # print(f'gt_arcs_tensor:{gt_arcs_tensor.shape}')
  981. #
  982. # if gt_arcs_tensor.shape[0] > 0:
  983. # print(f'start to compute point_loss')
  984. if len(gt_arcs) > 0 and feature_logits is not None:
  985. loss_arc = compute_mask_loss(feature_logits, arc_proposals, gt_arcs, arc_pos_matched_idxs)
  986. if loss_arc is None:
  987. print(f'loss_arc is None111')
  988. loss_arc = torch.tensor(0.0, device=device)
  989. loss_arc = {"loss_arc": loss_arc}
  990. else:
  991. if targets is not None:
  992. h, w = targets[0]["img_size"]
  993. img_size = h
  994. gt_arcs = [t["arc_mask"] for t in targets if "arc_mask" in t]
  995. print(f'gt_arcs:{gt_arcs[0].shape}')
  996. h, w = targets[0]["img_size"]
  997. img_size = h
  998. # gt_arcs_tensor = torch.zeros(0, 0)
  999. # if len(gt_arcs) > 0:
  1000. # gt_arcs_tensor = torch.cat(gt_arcs)
  1001. # print(f'gt_arcs_tensor:{gt_arcs_tensor.shape}')
  1002. # if gt_arcs_tensor.shape[0] > 0 and feature_logits is not None:
  1003. # print(f'start to compute arc_loss')
  1004. if len(gt_arcs) > 0 and feature_logits is not None:
  1005. print(f'start to compute arc_loss')
  1006. loss_arc = compute_mask_loss(feature_logits, arc_proposals, gt_arcs, arc_pos_matched_idxs)
  1007. if loss_arc is None:
  1008. print(f'loss_arc is None111')
  1009. loss_arc = torch.tensor(0.0, device=device)
  1010. loss_arc = {"loss_arc": loss_arc}
  1011. else:
  1012. loss_arc = {}
  1013. if feature_logits is None or arc_proposals is None:
  1014. # raise ValueError(
  1015. # "both arc_feature_logits and arc_proposals should not be None when not in training mode"
  1016. # )
  1017. print(f'error :both arc_feature_logits and arc_proposals should not be None when not in training mode"')
  1018. pass
  1019. if feature_logits is not None and arc_proposals is not None:
  1020. arcs_probs, arcs_scores, arcs_point = arc_inference(feature_logits,arc_proposals, th=0)
  1021. for keypoint_prob, kps, kp, r in zip(arcs_probs, arcs_scores, arcs_point, result):
  1022. # r["arcs"] = keypoint_prob
  1023. r["arcs"] = feature_logits
  1024. r["arcs_scores"] = kps
  1025. r["arcs_point"] = feature_logits
  1026. # print(f'loss_point:{loss_point}')
  1027. losses.update(loss_arc)
  1028. print(f'losses:{losses}')
  1029. if self.has_ins and self.detect_circle:
  1030. print(f'roi_heads forward has_circle()!!!!')
  1031. # print(f'labels:{labels}')
  1032. circle_proposals = [p["boxes"] for p in result]
  1033. print(f'boxes_proposals:{len(circle_proposals)}')
  1034. # if line_proposals is None or len(line_proposals) == 0:
  1035. # # 返回空特征或者跳过该部分计算
  1036. # return torch.empty(0, C, H, W).to(features['0'].device)
  1037. if self.training:
  1038. # during training, only focus on positive boxes
  1039. num_images = len(proposals)
  1040. print(f'num_images:{num_images}')
  1041. circle_proposals = []
  1042. circle_pos_matched_idxs = []
  1043. if matched_idxs is None:
  1044. raise ValueError("if in trainning, matched_idxs should not be None")
  1045. for img_id in range(num_images):
  1046. circle_pos = torch.where(labels[img_id] == 4)[0]
  1047. circle_proposals.append(proposals[img_id][circle_pos])
  1048. circle_pos_matched_idxs.append(matched_idxs[img_id][circle_pos])
  1049. else:
  1050. if targets is not None:
  1051. num_images = len(proposals)
  1052. circle_proposals = []
  1053. circle_pos_matched_idxs = []
  1054. print(f'val num_images:{num_images}')
  1055. if matched_idxs is None:
  1056. raise ValueError("if in trainning, matched_idxs should not be None")
  1057. for img_id in range(num_images):
  1058. circle_pos = torch.where(labels[img_id] == 4)[0]
  1059. circle_proposals.append(proposals[img_id][circle_pos])
  1060. circle_pos_matched_idxs.append(matched_idxs[img_id][circle_pos])
  1061. else:
  1062. pos_matched_idxs = None
  1063. # circle_proposals_tensor=torch.cat(circle_proposals)
  1064. circle_proposals_valid = self.check_proposals(circle_proposals)
  1065. if circle_proposals_valid:
  1066. print(f'features from backbone:{features['0'].shape}')
  1067. feature_logits = self.ins_forward1(features, image_shapes, circle_proposals)
  1068. loss_circle = None
  1069. loss_circle_extra=None
  1070. if self.training:
  1071. if targets is None or circle_pos_matched_idxs is None:
  1072. raise ValueError("both targets and pos_matched_idxs should not be None when in training mode")
  1073. gt_circles = [t["circle_masks"] for t in targets if "circle_masks" in t]
  1074. gt_labels = [t["labels"] for t in targets]
  1075. print(f'gt_circle:{gt_circles[0].shape}')
  1076. h, w = targets[0]["img_size"]
  1077. img_size = h
  1078. gt_circles_tensor = torch.zeros(0, 0)
  1079. if len(gt_circles) > 0:
  1080. gt_circles_tensor = torch.cat(gt_circles)
  1081. print(f'gt_circles_tensor:{gt_circles_tensor.shape}')
  1082. if gt_circles_tensor.shape[0] > 0:
  1083. print(f'start to compute circle_loss')
  1084. loss_circle = compute_mask_loss(feature_logits, circle_proposals, gt_circles, circle_pos_matched_idxs)
  1085. # loss_circle_extra=compute_circle_extra_losses(feature_logits, circle_proposals, gt_circles, circle_pos_matched_idxs)
  1086. if loss_circle is None:
  1087. print(f'loss_circle is None111')
  1088. loss_circle = torch.tensor(0.0, device=device)
  1089. if loss_circle_extra is None:
  1090. print(f'loss_circle_extra is None111')
  1091. loss_circle_extra = torch.tensor(0.0, device=device)
  1092. loss_circle = {"loss_circle": loss_circle}
  1093. loss_circle_extra = {"loss_circle_extra": loss_circle_extra}
  1094. else:
  1095. if targets is not None:
  1096. h, w = targets[0]["img_size"]
  1097. img_size = h
  1098. gt_circles = [t["circle_masks"] for t in targets if "circle_masks" in t]
  1099. gt_labels = [t["labels"] for t in targets]
  1100. gt_circles_tensor = torch.zeros(0, 0)
  1101. if len(gt_circles) > 0:
  1102. gt_circles_tensor = torch.cat(gt_circles)
  1103. print(f'gt_circles_tensor:{gt_circles_tensor.shape}')
  1104. if gt_circles_tensor.shape[0] > 0:
  1105. print(f'start to compute circle_loss')
  1106. loss_circle = compute_mask_loss(feature_logits, circle_proposals, gt_circles,
  1107. circle_pos_matched_idxs)
  1108. # loss_circle_extra = compute_circle_extra_losses(feature_logits, circle_proposals, gt_circles,circle_pos_matched_idxs)
  1109. if loss_circle is None:
  1110. print(f'loss_circle is None111')
  1111. loss_circle = torch.tensor(0.0, device=device)
  1112. if loss_circle_extra is None:
  1113. print(f'loss_circle_extra is None111')
  1114. loss_circle_extra = torch.tensor(0.0, device=device)
  1115. loss_circle = {"loss_circle": loss_circle}
  1116. loss_circle_extra = {"loss_circle_extra": loss_circle_extra}
  1117. else:
  1118. loss_circle = {}
  1119. loss_circle_extra = {}
  1120. if feature_logits is None or circle_proposals is None:
  1121. raise ValueError(
  1122. "both keypoint_logits and keypoint_proposals should not be None when not in training mode"
  1123. )
  1124. if feature_logits is not None:
  1125. circles_probs, circles_scores, circle_points = arc_inference(feature_logits,
  1126. circle_proposals, th=0)
  1127. # print(f'circles_probs:{circles_probs.shape}, circles_scores:{circles_scores.shape}')
  1128. proposals_per_image = [box.size(0) for box in circle_proposals]
  1129. print(f'circle_proposals_per_image:{proposals_per_image}')
  1130. feature_logits_props = []
  1131. start_idx = 0
  1132. for num_p in proposals_per_image:
  1133. current_features = feature_logits[start_idx:start_idx + num_p]
  1134. merged_feature = torch.sum(current_features, dim=0, keepdim=True)
  1135. feature_logits_props.append(merged_feature)
  1136. start_idx += num_p
  1137. for keypoint_prob, kps, r, f in zip(circles_probs, circles_scores, result,
  1138. feature_logits_props):
  1139. r["circles"] = keypoint_prob
  1140. r["circles_scores"] = kps
  1141. print(f'circles feature map:{f.shape}')
  1142. r["features"] = f.squeeze(0)
  1143. print(f'loss_circle:{loss_circle}')
  1144. print(f'loss_circle_extra:{loss_circle_extra}')
  1145. losses.update(loss_circle)
  1146. losses.update(loss_circle_extra)
  1147. print(f'losses:{losses}')
  1148. if self.has_mask():
  1149. mask_proposals = [p["boxes"] for p in result]
  1150. if self.training:
  1151. if matched_idxs is None:
  1152. raise ValueError("if in training, matched_idxs should not be None")
  1153. # during training, only focus on positive boxes
  1154. num_images = len(proposals)
  1155. mask_proposals = []
  1156. pos_matched_idxs = []
  1157. for img_id in range(num_images):
  1158. pos = torch.where(labels[img_id] > 0)[0]
  1159. mask_proposals.append(proposals[img_id][pos])
  1160. pos_matched_idxs.append(matched_idxs[img_id][pos])
  1161. else:
  1162. pos_matched_idxs = None
  1163. if self.mask_roi_pool is not None:
  1164. mask_features = self.mask_roi_pool(features, mask_proposals, image_shapes)
  1165. mask_features = self.mask_head(mask_features)
  1166. mask_logits = self.mask_predictor(mask_features)
  1167. else:
  1168. raise Exception("Expected mask_roi_pool to be not None")
  1169. loss_mask = {}
  1170. if self.training:
  1171. if targets is None or pos_matched_idxs is None or mask_logits is None:
  1172. raise ValueError("targets, pos_matched_idxs, mask_logits cannot be None when training")
  1173. gt_masks = [t["masks"] for t in targets]
  1174. gt_labels = [t["labels"] for t in targets]
  1175. rcnn_loss_mask = maskrcnn_loss(mask_logits, mask_proposals, gt_masks, gt_labels, pos_matched_idxs)
  1176. loss_mask = {"loss_mask": rcnn_loss_mask}
  1177. else:
  1178. labels = [r["labels"] for r in result]
  1179. masks_probs = maskrcnn_inference(mask_logits, labels)
  1180. for mask_prob, r in zip(masks_probs, result):
  1181. r["masks"] = mask_prob
  1182. losses.update(loss_mask)
  1183. # keep none checks in if conditional so torchscript will conditionally
  1184. # compile each branch
  1185. if self.has_keypoint():
  1186. keypoint_proposals = [p["boxes"] for p in result]
  1187. if self.training:
  1188. # during training, only focus on positive boxes
  1189. num_images = len(proposals)
  1190. keypoint_proposals = []
  1191. pos_matched_idxs = []
  1192. if matched_idxs is None:
  1193. raise ValueError("if in trainning, matched_idxs should not be None")
  1194. for img_id in range(num_images):
  1195. pos = torch.where(labels[img_id] > 0)[0]
  1196. keypoint_proposals.append(proposals[img_id][pos])
  1197. pos_matched_idxs.append(matched_idxs[img_id][pos])
  1198. else:
  1199. pos_matched_idxs = None
  1200. keypoint_features = self.line_roi_pool(features, keypoint_proposals, image_shapes)
  1201. keypoint_features = self.line_head(keypoint_features)
  1202. keypoint_logits = self.line_predictor(keypoint_features)
  1203. loss_keypoint = {}
  1204. if self.training:
  1205. if targets is None or pos_matched_idxs is None:
  1206. raise ValueError("both targets and pos_matched_idxs should not be None when in training mode")
  1207. gt_keypoints = [t["keypoints"] for t in targets]
  1208. rcnn_loss_keypoint = keypointrcnn_loss(
  1209. keypoint_logits, keypoint_proposals, gt_keypoints, pos_matched_idxs
  1210. )
  1211. loss_keypoint = {"loss_keypoint": rcnn_loss_keypoint}
  1212. else:
  1213. if keypoint_logits is None or keypoint_proposals is None:
  1214. raise ValueError(
  1215. "both keypoint_logits and keypoint_proposals should not be None when not in training mode"
  1216. )
  1217. keypoints_probs, lines_scores = keypointrcnn_inference(keypoint_logits, keypoint_proposals)
  1218. for keypoint_prob, kps, r in zip(keypoints_probs, lines_scores, result):
  1219. r["keypoints"] = keypoint_prob
  1220. r["keypoints_scores"] = kps
  1221. losses.update(loss_keypoint)
  1222. return result, losses
  1223. def check_proposals(self, proposals):
  1224. valid = True
  1225. for proposal in proposals:
  1226. # print(f'per circle_proposal:{circle_proposal.shape}')
  1227. if proposal.shape[0] == 0:
  1228. valid = False
  1229. return valid
  1230. def line_forward1(self, features, image_shapes, line_proposals):
  1231. print(f'line_proposals:{len(line_proposals)}')
  1232. # cs_features= features['0']
  1233. # print(f'features-0:{features['0'].shape}')
  1234. cs_features = self.channel_compress(features['0'])
  1235. filtered_proposals = [proposal for proposal in line_proposals if proposal.shape[0] > 0]
  1236. if len(filtered_proposals) > 0:
  1237. filtered_proposals_tensor = torch.cat(filtered_proposals)
  1238. print(f'filtered_proposals_tensor:{filtered_proposals_tensor.shape}')
  1239. line_proposals_tensor = torch.cat(line_proposals)
  1240. print(f'line_proposals_tensor:{line_proposals_tensor.shape}')
  1241. roi_features = features_align(cs_features, line_proposals, image_shapes)
  1242. if roi_features is not None:
  1243. print(f'line_features from align:{roi_features.shape}')
  1244. feature_logits = self.line_head(roi_features)
  1245. print(f'feature_logits from line_head:{feature_logits.shape}')
  1246. return feature_logits
  1247. def line_forward2(self, features, image_shapes, line_proposals):
  1248. print(f'line_proposals:{len(line_proposals)}')
  1249. # cs_features= features['0']
  1250. # print(f'features-0:{features['0'].shape}')
  1251. # cs_features = self.channel_compress(features['0'])
  1252. cs_features=features['0']
  1253. filtered_proposals = [proposal for proposal in line_proposals if proposal.shape[0] > 0]
  1254. if len(filtered_proposals) > 0:
  1255. filtered_proposals_tensor = torch.cat(filtered_proposals)
  1256. print(f'filtered_proposals_tensor:{filtered_proposals_tensor.shape}')
  1257. line_proposals=filtered_proposals
  1258. line_proposals_tensor = torch.cat(line_proposals)
  1259. print(f'line_proposals_tensor:{line_proposals_tensor.shape}')
  1260. feature_logits = self.line_head(cs_features)
  1261. print(f'feature_logits from line_head:{feature_logits.shape}')
  1262. roi_features = features_align(feature_logits, line_proposals, image_shapes)
  1263. if roi_features is not None:
  1264. print(f'roi_features from align:{roi_features.shape}')
  1265. return roi_features
  1266. def line_forward3(self, features, image_shapes, line_proposals):
  1267. print(f'line_proposals:{len(line_proposals)}')
  1268. # cs_features= features['0']
  1269. # print(f'features-0:{features['0'].shape}')
  1270. # cs_features = self.channel_compress(features['0'])
  1271. cs_features=features['0']
  1272. # cs_features = features
  1273. # filtered_proposals = [proposal for proposal in line_proposals if proposal.shape[0] > 0]
  1274. #
  1275. # if len(filtered_proposals) > 0:
  1276. # filtered_proposals_tensor = torch.cat(filtered_proposals)
  1277. # print(f'filtered_proposals_tensor:{filtered_proposals_tensor.shape}')
  1278. # line_proposals=filtered_proposals
  1279. # line_proposals_tensor = torch.cat(line_proposals)
  1280. # print(f'line_proposals_tensor:{line_proposals_tensor.shape}')
  1281. feature_logits = self.line_predictor(cs_features)
  1282. print(f'feature_logits from line_head:{feature_logits.shape}')
  1283. roi_features = features_align(feature_logits, line_proposals, image_shapes)
  1284. if roi_features is not None:
  1285. print(f'roi_features from align:{roi_features.shape}')
  1286. return roi_features
  1287. def point_forward1(self, features, image_shapes, proposals):
  1288. print(f'point_proposals:{len(proposals)}')
  1289. # cs_features= features['0']
  1290. # print(f'features-0:{features['0'].shape}')
  1291. # cs_features = self.channel_compress(features['0'])
  1292. cs_features=features['0']
  1293. # filtered_proposals = [proposal for proposal in proposals if proposal.shape[0] > 0]
  1294. #
  1295. # if len(filtered_proposals) > 0:
  1296. # filtered_proposals_tensor = torch.cat(filtered_proposals)
  1297. # print(f'filtered_proposals_tensor:{filtered_proposals_tensor.shape}')
  1298. # proposals=filtered_proposals
  1299. # point_proposals_tensor = torch.cat(proposals)
  1300. # print(f'point_proposals_tensor:{point_proposals_tensor.shape}')
  1301. feature_logits = self.point_predictor(cs_features)
  1302. print(f'feature_logits from line_head:{feature_logits.shape}')
  1303. roi_features = features_align(feature_logits, proposals, image_shapes)
  1304. if roi_features is not None:
  1305. print(f'roi_features from align:{roi_features.shape}')
  1306. return roi_features
  1307. def ins_forward1(self, features, image_shapes, proposals):
  1308. print(f'circle_proposals:{len(proposals)}')
  1309. # cs_features= features['0']
  1310. # print(f'features-0:{features['0'].shape}')
  1311. # cs_features = self.channel_compress(features['0'])
  1312. # cs_features=features['0']
  1313. cs_features = features
  1314. # filtered_proposals = [proposal for proposal in proposals if proposal.shape[0] > 0]
  1315. #
  1316. # if len(filtered_proposals) > 0:
  1317. # filtered_proposals_tensor = torch.cat(filtered_proposals)
  1318. # print(f'filtered_proposals_tensor:{filtered_proposals_tensor.shape}')
  1319. # proposals=filtered_proposals
  1320. # point_proposals_tensor = torch.cat(proposals)
  1321. # print(f'point_proposals_tensor:{point_proposals_tensor.shape}')
  1322. feature_logits = self.ins_head(cs_features)
  1323. print(f'feature_logits from circle_head:{feature_logits.shape}')
  1324. roi_features = features_align(feature_logits, proposals, image_shapes)
  1325. if roi_features is not None:
  1326. print(f'roi_features from align:{roi_features.shape}')
  1327. return roi_features
  1328. def arc_forward1(self, features, image_shapes, proposals):
  1329. print(f'arc_proposals:{len(proposals)}')
  1330. # cs_features= features['0']
  1331. # print(f'features-0:{features['0'].shape}')
  1332. # cs_features = self.channel_compress(features['0'])
  1333. # cs_features=features['0']
  1334. cs_features = features
  1335. # filtered_proposals = [proposal for proposal in proposals if proposal.shape[0] > 0]
  1336. #
  1337. # if len(filtered_proposals) > 0:
  1338. # filtered_proposals_tensor = torch.cat(filtered_proposals)
  1339. # print(f'filtered_proposals_tensor:{filtered_proposals_tensor.shape}')
  1340. # proposals=filtered_proposals
  1341. # point_proposals_tensor = torch.cat(proposals)
  1342. # print(f'point_proposals_tensor:{point_proposals_tensor.shape}')
  1343. feature_logits = self.arc_predictor(cs_features)
  1344. print(f'feature_logits from arc_head:{feature_logits.shape}')
  1345. roi_features = features_align(feature_logits, proposals, image_shapes)
  1346. if roi_features is not None:
  1347. print(f'roi_features from align:{roi_features.shape}')
  1348. return roi_features