-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataset.py
141 lines (100 loc) · 5.67 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import numpy as np
from torchvision import transforms
from torchvision.datasets import CIFAR10, CIFAR100, FashionMNIST, ImageFolder
from torch.utils.data import DataLoader, Subset
__all__ = ['cifar10_dataloaders', 'cifar100_dataloaders', 'tiny_imagenet_dataloaders',
'cifar10_dataloaders_val', 'cifar100_dataloaders_val', 'tiny_imagenet_dataloaders_val']
def cifar10_dataloaders(batch_size=128, data_dir='datasets/cifar10', dataset=False):
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])
test_transform = transforms.Compose([
transforms.ToTensor(),
])
train_set = Subset(CIFAR10(data_dir, train=True, transform=train_transform, download=True), list(range(45000)))
val_set = Subset(CIFAR10(data_dir, train=True, transform=test_transform, download=True), list(range(45000, 50000)))
test_set = CIFAR10(data_dir, train=False, transform=test_transform, download=True)
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=2, drop_last=False, pin_memory=True)
val_loader = DataLoader(val_set, batch_size=batch_size, shuffle=False, num_workers=2, pin_memory=True)
test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=False, num_workers=2, pin_memory=True)
if dataset:
print('return train dataset')
train_dataset = CIFAR10(data_dir, train=True, transform=train_transform, download=True)
return train_dataset, val_loader, test_loader
else:
return train_loader, val_loader, test_loader
def cifar100_dataloaders(batch_size=128, data_dir='datasets/cifar100', dataset=False):
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])
test_transform = transforms.Compose([
transforms.ToTensor(),
])
train_set = Subset(CIFAR100(data_dir, train=True, transform=train_transform, download=True), list(range(45000)))
val_set = Subset(CIFAR100(data_dir, train=True, transform=test_transform, download=True), list(range(45000, 50000)))
test_set = CIFAR100(data_dir, train=False, transform=test_transform, download=True)
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=2, drop_last=False, pin_memory=True)
val_loader = DataLoader(val_set, batch_size=batch_size, shuffle=False, num_workers=2, pin_memory=True)
test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=False, num_workers=2, pin_memory=True)
if dataset:
print('return train dataset')
train_dataset = CIFAR100(data_dir, train=True, transform=train_transform, download=True)
return train_dataset, val_loader, test_loader
else:
return train_loader, val_loader, test_loader
def tiny_imagenet_dataloaders(batch_size=64, data_dir='datasets/tiny-imagenet-200', dataset=False, split_file=None):
train_transform = transforms.Compose([
transforms.RandomCrop(64, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])
test_transform = transforms.Compose([
transforms.ToTensor(),
])
train_path = os.path.join(data_dir, 'train')
val_path = os.path.join(data_dir, 'val')
if not split_file:
split_file = 'npy_files/tiny-imagenet-train-val.npy'
split_permutation = list(np.load(split_file))
train_set = Subset(ImageFolder(train_path, transform=train_transform), split_permutation[:90000])
val_set = Subset(ImageFolder(train_path, transform=test_transform), split_permutation[90000:])
test_set = ImageFolder(val_path, transform=test_transform)
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=2, pin_memory=True)
val_loader = DataLoader(val_set, batch_size=batch_size, shuffle=False, num_workers=2, pin_memory=True)
test_loader = DataLoader(test_set, batch_size=batch_size, shuffle=False, num_workers=2, pin_memory=True)
if dataset:
print('return train dataset')
train_dataset = ImageFolder(train_path, transform=train_transform)
return train_dataset, val_loader, test_loader
else:
return train_loader, val_loader, test_loader
def cifar10_dataloaders_val(batch_size=128, data_dir='datasets/cifar10'):
test_transform = transforms.Compose([
transforms.ToTensor(),
])
train_set = Subset(CIFAR10(data_dir, train=True, transform=test_transform, download=True), list(range(45000)))
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=False, num_workers=2, pin_memory=True)
return train_loader
def cifar100_dataloaders_val(batch_size=128, data_dir='datasets/cifar100'):
test_transform = transforms.Compose([
transforms.ToTensor(),
])
train_set = Subset(CIFAR100(data_dir, train=True, transform=test_transform, download=True), list(range(45000)))
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=False, num_workers=2, pin_memory=True)
return train_loader
def tiny_imagenet_dataloaders_val(batch_size=64, data_dir='datasets/tiny-imagenet-200', split_file=None):
test_transform = transforms.Compose([
transforms.ToTensor(),
])
train_path = os.path.join(data_dir, 'train')
if not split_file:
split_file = 'npy_files/tiny-imagenet-train-val.npy'
split_permutation = list(np.load(split_file))
train_set = Subset(ImageFolder(train_path, transform=test_transform), split_permutation[:90000])
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=False, num_workers=2, pin_memory=True)
return train_loader