-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
51 lines (41 loc) · 1.56 KB
/
dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from pathlib import Path
import torch.nn
import torchvision
from lxml import etree
from torch import Tensor
from torch.utils.data import Dataset
from torchvision import transforms
class RandomAllImageDataset(Dataset):
"""Dataset class"""
def __init__(self, images: list[Path], targets: list[Path], split: str):
self.images_pths = images.copy()
self.targets_xml = targets.copy()
self.split = split
self.targets_xml.sort()
self.images_pths.sort()
def __getitem__(self, index) -> tuple[Tensor, tuple[int, int, int, int]]:
img_pth = self.images_pths[index]
target_xml = self.targets_xml[index]
targets = RandomAllImageDataset.parse(target_xml)
img = torchvision.io.read_image(str(img_pth))
if self.split == "train":
tfs = transforms.Compose([
transforms.Resize((256, 256)),
])
img = tfs(img)
targets = tuple(float(x / 256) for x in targets)
return img, targets
def __len__(self):
return len(self.targets_xml)
@staticmethod
def parse(pth: Path) -> tuple[int, int, int, int]:
"""Given a path to a xml file for this dataset, return a tuple of the bounding box
Args:
pth (Path): _description_
"""
root = etree.fromstring(pth.read_text())
xmin = int(root.xpath("//xmin")[0].text)
xmax = int(root.xpath("//xmax")[0].text)
ymin = int(root.xpath("//ymin")[0].text)
ymax = int(root.xpath("//ymax")[0].text)
return xmax, ymax, xmin, ymin