-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
78 lines (59 loc) · 2.61 KB
/
data.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
import torch
from torch.utils.data import Dataset, DataLoader
import torchvision
import torchvision.transforms as transforms
import os
from PIL import Image
from tqdm import tqdm
stats = ((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
to_tensor_normalize = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize(*stats)
]
)
augment_transform = transforms.Compose(
[
transforms.RandomHorizontalFlip(p=0.5),
transforms.ColorJitter(brightness=0.3, contrast=0.3, saturation=0.3, hue=0.1),
transforms.RandomAffine((-10, 10)),
transforms.RandomCrop(32, padding=4),
to_tensor_normalize
]
)
def get_loader_splits(batch_size: int = 64, valid_batch_size: int = 128,
augment: bool = True, augment_valid: bool = False):
train_transform = augment_transform if augment else to_tensor_normalize
valid_transform = to_tensor_normalize if not augment_valid else augment_transform
# Built-in dataset (the same as Kaggle)
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True, transform=train_transform)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=valid_transform)
# Trainloader
trainloader = DataLoader(trainset, batch_size=batch_size,
shuffle=True, num_workers=2)
validloader = DataLoader(testset, batch_size=valid_batch_size, shuffle=True, num_workers=2)
return trainloader, validloader
class CifarKaggleTestset(Dataset):
def __init__(self, data_root, augment: bool = True):
self.augment = augment
self.samples = []
self.indexes = []
for image_name in tqdm(os.listdir(data_root)):
image_path = os.path.join(data_root, image_name)
with open(image_path, 'r'):
img = Image.open(image_path)
self.samples.append(img.convert('RGB'))
self.indexes.append(int(image_name[:-4]))
img.close()
def __len__(self):
return len(self.samples)
def __getitem__(self, idx):
if self.augment:
return augment_transform(self.samples[idx]), self.indexes[idx]
else:
return to_tensor_normalize(self.samples[idx]), self.indexes[idx]
def get_kaggle_testloader(data_root: str, augment: bool = True, batch_size: int = 128):
testset = CifarKaggleTestset(data_root, augment=augment)
return DataLoader(testset, batch_size=batch_size)