-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_loader.py
executable file
·163 lines (120 loc) · 5.44 KB
/
dataset_loader.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import torch
import torchvision
import torch.nn as nn
from torch import allclose
import torchvision.transforms as T
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from torch.testing import assert_allclose
import kornia
from kornia import augmentation as K
import kornia.augmentation.functional as F
import kornia.augmentation.random_generator as rg
from torchvision.transforms import functional as tvF
import sys
import numpy as np
from PIL import Image
from PIL import ImageFilter
import matplotlib.pyplot as plt
CIFAR_MEAN = [0.49139968, 0.48215827, 0.44653124]
CIFAR_STD = [0.24703233, 0.24348505, 0.26158768]
CIFAR_MEAN_ = torch.FloatTensor([CIFAR_MEAN, CIFAR_STD])
CIFAR_STD_ = torch.FloatTensor([CIFAR_MEAN, CIFAR_STD])
# GPU based augmentation: https://colab.research.google.com/drive/1T20UNAG4SdlE2n2wstuhiewve5Q81VpS#scrollTo=-AnIAZjeIP35
class InitalTransformation():
def __init__(self):
self.transform = T.Compose([
T.ToTensor(),
# transforms.Normalize(CIFAR_MEAN_,CIFAR_STD_),
])
def __call__(self, x):
x = self.transform(x)
return x
def get_clf_train_test_dataloaders(dataset = "cifar10", percent_train_sample = 20,
data_dir="./dataset", batch_size = 16,
num_workers = 4, download=True):
tr = torchvision.datasets.CIFAR10(data_dir, train=True, transform=InitalTransformation(), download=True)
samples = list(range(0, int(len(tr)*percent_train_sample/100)))
tr_subset = torch.utils.data.Subset(tr, samples)
train_loader = torch.utils.data.DataLoader(
dataset = tr_subset,
shuffle=True,
batch_size= batch_size,
num_workers = 4 )
test_loader = torch.utils.data.DataLoader(
dataset = torchvision.datasets.CIFAR10(data_dir, train=True,
transform=InitalTransformation(), download=True),
shuffle=True,
batch_size= batch_size,
num_workers = 4
)
return train_loader, test_loader
def get_train_mem_test_dataloaders(dataset = "cifar10", data_dir="./dataset", batch_size = 16,num_workers = 4, download=True):
train_loader = torch.utils.data.DataLoader(
dataset = torchvision.datasets.CIFAR10(data_dir, train=True, transform=InitalTransformation(), download=download),
shuffle=True,
batch_size= batch_size,
num_workers = num_workers
)
memory_loader = torch.utils.data.DataLoader(
dataset = torchvision.datasets.CIFAR10(data_dir, train=False, transform=InitalTransformation(), download=download),
shuffle=False,
batch_size= batch_size,
num_workers = num_workers
)
test_loader = torch.utils.data.DataLoader(
dataset = torchvision.datasets.CIFAR10(data_dir, train=False, transform=InitalTransformation(), download=download),
shuffle=True,
batch_size= batch_size,
num_workers = num_workers
)
return train_loader, memory_loader, test_loader
def gpu_transformer(image_size,s=.2):
train_transform = nn.Sequential(
kornia.augmentation.RandomResizedCrop(image_size,scale=(0.5,1.0)),
kornia.augmentation.RandomHorizontalFlip(p=0.5),
kornia.augmentation.ColorJitter(0.8*s,0.8*s,0.8*s,0.2*s,p=0.3),
# kornia.augmentation.RandomGrayscale(p=0.05),
)
test_transform = nn.Sequential(
kornia.augmentation.RandomResizedCrop(image_size,scale=(0.5,1.0)),
kornia.augmentation.RandomHorizontalFlip(p=0.5),
kornia.augmentation.ColorJitter(0.8*s,0.8*s,0.8*s,0.2*s,p=0.3),
# kornia.augmentation.RandomGrayscale(p=0.05),
)
return train_transform , test_transform
def get_clf_train_test_transform(image_size,s=.2):
train_transform = nn.Sequential(
kornia.augmentation.RandomResizedCrop(image_size,scale=(0.5,1.0)),
kornia.augmentation.RandomHorizontalFlip(p=0.5),
# kornia.augmentation.Normalize(CIFAR_MEAN_,CIFAR_STD_),
)
test_transform = nn.Sequential(
kornia.augmentation.RandomResizedCrop(image_size,scale=(0.5,1.0)),
kornia.augmentation.RandomHorizontalFlip(p=0.5),
# kornia.augmentation.RandomGrayscale(p=0.05),
# kornia.augmentation.Normalize(CIFAR_MEAN_,CIFAR_STD_)
)
return train_transform , test_transform
if __name__=="__main__":
device = torch.device('cuda')
print(f"Running with device: {device}")
data_dir="./dataset"
batch_size = 16
num_workers = 8
download=True
train = True
image_size = (32,32)
s = 1.0
train_transform = nn.Sequential(
kornia.augmentation.RandomResizedCrop(image_size,scale=(0.2,1.0)),
kornia.augmentation.RandomHorizontalFlip(),
kornia.augmentation.ColorJitter(0.8*s,0.8*s,0.8*s,0.2*s,p=0.5),
kornia.augmentation.RandomGrayscale(p=0.2),
)
train_loader,_,_ = get_train_mem_test_dataloaders()
from tqdm import tqdm
local_progress = tqdm(train_loader, desc=f'Epoch {1}/{1}')
for i , sample in enumerate(local_progress):
images = sample[0].to(device)
transformed_images = train_transform(images)