forked from abhishekkrthakur/captcha-recognition-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
49 lines (38 loc) · 1.31 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
import albumentations
import torch
import numpy as np
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
class ClassificationDataset:
def __init__(self, image_paths, targets, resize=None):
# resize = (height, width)
self.image_paths = image_paths
self.targets = targets
self.resize = resize
mean = (0.485, 0.456, 0.406)
std = (0.229, 0.224, 0.225)
self.aug = albumentations.Compose(
[
albumentations.Normalize(
mean, std, max_pixel_value=255.0, always_apply=True
)
]
)
def __len__(self):
return len(self.image_paths)
def __getitem__(self, item):
image = Image.open(self.image_paths[item]).convert("RGB")
targets = self.targets[item]
if self.resize is not None:
image = image.resize(
(self.resize[1], self.resize[0]), resample=Image.BILINEAR
)
image = np.array(image)
augmented = self.aug(image=image)
image = augmented["image"]
image = np.transpose(image, (2, 0, 1)).astype(np.float32)
return {
"images": torch.tensor(image, dtype=torch.float),
"targets": torch.tensor(targets, dtype=torch.long),
}