♻️ [Refactor] dataset, apply tensorlize
Browse files- yolo/tools/data_loader.py +2 -29
- yolo/utils/dataset_utils.py +14 -0
yolo/tools/data_loader.py
CHANGED
|
@@ -18,38 +18,11 @@ from yolo.utils.dataset_utils import (
|
|
| 18 |
create_image_metadata,
|
| 19 |
locate_label_paths,
|
| 20 |
scale_segmentation,
|
|
|
|
| 21 |
)
|
| 22 |
from yolo.utils.logger import logger
|
| 23 |
|
| 24 |
|
| 25 |
-
def tensorlize(data):
|
| 26 |
-
# TODO Move Tensorlize to helper
|
| 27 |
-
img_paths, bboxes = zip(*data)
|
| 28 |
-
max_box = max(bbox.size(0) for bbox in bboxes)
|
| 29 |
-
padded_bbox_list = []
|
| 30 |
-
for bbox in bboxes:
|
| 31 |
-
padding = torch.full((max_box, 5), -1, dtype=torch.float32)
|
| 32 |
-
padding[: bbox.size(0)] = bbox
|
| 33 |
-
padded_bbox_list.append(padding)
|
| 34 |
-
bboxes = np.stack(padded_bbox_list)
|
| 35 |
-
img_paths = np.array(img_paths)
|
| 36 |
-
return img_paths, bboxes
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
def tensorlize(data):
|
| 40 |
-
# TODO Move Tensorlize to helper
|
| 41 |
-
img_paths, bboxes = zip(*data)
|
| 42 |
-
max_box = max(bbox.size(0) for bbox in bboxes)
|
| 43 |
-
padded_bbox_list = []
|
| 44 |
-
for bbox in bboxes:
|
| 45 |
-
padding = torch.full((max_box, 5), -1, dtype=torch.float32)
|
| 46 |
-
padding[: bbox.size(0)] = bbox
|
| 47 |
-
padded_bbox_list.append(padding)
|
| 48 |
-
bboxes = np.stack(padded_bbox_list)
|
| 49 |
-
img_paths = np.array(img_paths)
|
| 50 |
-
return img_paths, bboxes
|
| 51 |
-
|
| 52 |
-
|
| 53 |
class YoloDataset(Dataset):
|
| 54 |
def __init__(self, data_cfg: DataConfig, dataset_cfg: DatasetConfig, phase: str = "train2017"):
|
| 55 |
augment_cfg = data_cfg.data_augment
|
|
@@ -160,7 +133,7 @@ class YoloDataset(Dataset):
|
|
| 160 |
return torch.zeros((0, 5))
|
| 161 |
|
| 162 |
def get_data(self, idx):
|
| 163 |
-
img_path, bboxes = self.
|
| 164 |
valid_mask = bboxes[:, 0] != -1
|
| 165 |
with Image.open(img_path) as img:
|
| 166 |
img = img.convert("RGB")
|
|
|
|
| 18 |
create_image_metadata,
|
| 19 |
locate_label_paths,
|
| 20 |
scale_segmentation,
|
| 21 |
+
tensorlize,
|
| 22 |
)
|
| 23 |
from yolo.utils.logger import logger
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
class YoloDataset(Dataset):
|
| 27 |
def __init__(self, data_cfg: DataConfig, dataset_cfg: DatasetConfig, phase: str = "train2017"):
|
| 28 |
augment_cfg = data_cfg.data_augment
|
|
|
|
| 133 |
return torch.zeros((0, 5))
|
| 134 |
|
| 135 |
def get_data(self, idx):
|
| 136 |
+
img_path, bboxes = self.img_paths[idx], self.bboxes[idx]
|
| 137 |
valid_mask = bboxes[:, 0] != -1
|
| 138 |
with Image.open(img_path) as img:
|
| 139 |
img = img.convert("RGB")
|
yolo/utils/dataset_utils.py
CHANGED
|
@@ -5,6 +5,7 @@ from pathlib import Path
|
|
| 5 |
from typing import Any, Dict, List, Optional, Tuple
|
| 6 |
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
|
| 9 |
from yolo.tools.data_conversion import discretize_categories
|
| 10 |
from yolo.utils.logger import logger
|
|
@@ -111,3 +112,16 @@ def scale_segmentation(
|
|
| 111 |
seg_array_with_cat.append(scaled_flat_seg_data)
|
| 112 |
|
| 113 |
return seg_array_with_cat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from typing import Any, Dict, List, Optional, Tuple
|
| 6 |
|
| 7 |
import numpy as np
|
| 8 |
+
import torch
|
| 9 |
|
| 10 |
from yolo.tools.data_conversion import discretize_categories
|
| 11 |
from yolo.utils.logger import logger
|
|
|
|
| 112 |
seg_array_with_cat.append(scaled_flat_seg_data)
|
| 113 |
|
| 114 |
return seg_array_with_cat
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def tensorlize(data):
|
| 118 |
+
img_paths, bboxes = zip(*data)
|
| 119 |
+
max_box = max(bbox.size(0) for bbox in bboxes)
|
| 120 |
+
padded_bbox_list = []
|
| 121 |
+
for bbox in bboxes:
|
| 122 |
+
padding = torch.full((max_box, 5), -1, dtype=torch.float32)
|
| 123 |
+
padding[: bbox.size(0)] = bbox
|
| 124 |
+
padded_bbox_list.append(padding)
|
| 125 |
+
bboxes = np.stack(padded_bbox_list)
|
| 126 |
+
img_paths = np.array(img_paths)
|
| 127 |
+
return img_paths, bboxes
|