-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
82 lines (67 loc) · 2.8 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import paddle
import paddle.vision.transforms as T
import numpy as np
from PIL import Image
from ppcls.data.preprocess.ops.autoaugment import ImageNetPolicy
from ppcls.data.preprocess.ops.random_erasing import RandomErasing
from utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
class CycleMLPdataset(paddle.io.Dataset):
def __init__(self, img_dir, txtpath, mode='train', transform=None):
"""
Image classification reading class
args:
img_dir: Image folder.
txtpath: TXT file path.
transform: Data enhancement
"""
super(CycleMLPdataset, self).__init__()
assert mode in ['train', 'val', 'test'], "mode is one of ['train', 'val', 'test]"
self.mode = mode
self.transform = transform
self.data = []
with open(txtpath, 'r') as f:
for line in f.readlines():
if mode != 'test':
img_path, label = line.strip().split(' ')
self.data.append([img_dir + '/' + img_path, label])
else:
self.data.append(img_dir + '/' + line.strip())
def __getitem__(self, idx):
if self.mode != 'test':
img = Image.open(self.data[idx][0]).convert('RGB')
label = self.data[idx][1]
if self.transform:
img = self.transform(img)
return img.astype('float32'), np.array(label, dtype='int64')
else:
img = Image.open(self.data[idx]).convert('RGB')
if self.transform:
img = self.transform(img)
return img.astype('float32')
def __len__(self):
return len(self.data)
def build_transfrom(is_train, args):
transform = []
resize_im = args.input_size > 32
if is_train:
transform.extend([
T.RandomResizedCrop(size=args.input_size, interpolation=args.train_interpolation),
T.RandomHorizontalFlip(),
ImageNetPolicy(),
T.ColorJitter(*([args.color_jitter]*3)),
T.ToTensor(),
T.Normalize(mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD),
RandomErasing(EPSILON=args.reprob, mean=IMAGENET_DEFAULT_MEAN)
])
if not resize_im:
transform.append(T.RandomCrop(args.input_size, padding=4))
else:
if resize_im:
size = int((256 / 224) * args.input_size)
transform.append(T.Resize(size, interpolation=args.train_interpolation))
transform.append(T.CenterCrop(size=args.input_size))
transform.extend([
T.ToTensor(),
T.Normalize(mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD)
])
return T.Compose(transform)