-
Notifications
You must be signed in to change notification settings - Fork 51
/
data.py
52 lines (40 loc) · 1.9 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
import os
import torch
import torch.nn.parallel
import torch.optim
import torch.utils.data
from torchvision import datasets, transforms
__imagenet_stats = {'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225]}
def inception_preproccess(input_size, normalize=__imagenet_stats):
return transforms.Compose([
transforms.RandomResizedCrop(input_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(**normalize)
])
def scale_crop(input_size, scale_size=None, normalize=__imagenet_stats):
t_list = [
transforms.CenterCrop(input_size),
transforms.ToTensor(),
transforms.Normalize(**normalize),
]
if scale_size != input_size:
t_list = [transforms.Resize(scale_size)] + t_list
return transforms.Compose(t_list)
def get_transform(augment=True, input_size=224):
normalize = __imagenet_stats
scale_size = int(input_size / 0.875)
if augment:
return inception_preproccess(input_size=input_size, normalize=normalize)
else:
return scale_crop(input_size=input_size, scale_size=scale_size, normalize=normalize)
def get_loaders(dataroot, val_batch_size, train_batch_size, input_size, workers):
val_data = datasets.ImageFolder(root=os.path.join(dataroot, 'val'), transform=get_transform(False, input_size))
val_loader = torch.utils.data.DataLoader(val_data, batch_size=val_batch_size, shuffle=False, num_workers=workers,
pin_memory=True)
train_data = datasets.ImageFolder(root=os.path.join(dataroot, 'train'),
transform=get_transform(input_size=input_size))
train_loader = torch.utils.data.DataLoader(train_data, batch_size=train_batch_size, shuffle=True,
num_workers=workers, pin_memory=True)
return train_loader, val_loader