Collate function for detection task. Concatanate bboxes, labels and metadata from different samples in the first dimension instead of stacking them to have a batch-size dimension. Args: batch (tuple or list): data batch to collate. Returns: (tuple): collated dete
(batch)
| 46 | |
| 47 | |
| 48 | def detection_collate(batch): |
| 49 | """ |
| 50 | Collate function for detection task. Concatanate bboxes, labels and |
| 51 | metadata from different samples in the first dimension instead of |
| 52 | stacking them to have a batch-size dimension. |
| 53 | Args: |
| 54 | batch (tuple or list): data batch to collate. |
| 55 | Returns: |
| 56 | (tuple): collated detection data batch. |
| 57 | """ |
| 58 | inputs, labels, video_idx, extra_data = zip(*batch) |
| 59 | inputs, video_idx = default_collate(inputs), default_collate(video_idx) |
| 60 | labels = torch.tensor(np.concatenate(labels, axis=0)).float() |
| 61 | |
| 62 | collated_extra_data = {} |
| 63 | for key in extra_data[0].keys(): |
| 64 | data = [d[key] for d in extra_data] |
| 65 | if key == "boxes" or key == "ori_boxes": |
| 66 | # Append idx info to the bboxes before concatenating them. |
| 67 | bboxes = [ |
| 68 | np.concatenate( |
| 69 | [np.full((data[i].shape[0], 1), float(i)), data[i]], axis=1 |
| 70 | ) |
| 71 | for i in range(len(data)) |
| 72 | ] |
| 73 | bboxes = np.concatenate(bboxes, axis=0) |
| 74 | collated_extra_data[key] = torch.tensor(bboxes).float() |
| 75 | elif key == "metadata": |
| 76 | collated_extra_data[key] = torch.tensor( |
| 77 | list(itertools.chain(*data)) |
| 78 | ).view(-1, 2) |
| 79 | else: |
| 80 | collated_extra_data[key] = default_collate(data) |
| 81 | |
| 82 | return inputs, labels, video_idx, collated_extra_data |
| 83 | |
| 84 | |
| 85 | def construct_loader(cfg, split, is_precise_bn=False): |
nothing calls this directly
no outgoing calls
no test coverage detected