-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini_imagenet.py
163 lines (120 loc) · 6.5 KB
/
mini_imagenet.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
162
163
import torch
from torchvision import datasets, transforms
class AUGMENT:
def __init__(self, directory, dataset, size=84):
self.size = size
self.dataset = dataset
self.directory = directory
self.tensor = transforms.ToTensor()
def original(self):
transform = transforms.Compose([transforms.Resize(self.size), self.tensor])
dataset = self.dataset(root=self.directory, transform=transform)
torch.manual_seed(0)
train_set, _ = torch.utils.data.random_split(dataset, [50_000, 10_000])
return train_set
# 1. crop + jitter + transform
def augment_1(self):
crop = transforms.RandomResizedCrop(size=(self.size, self.size), scale=(0.5, 1.0))
jitter = transforms.ColorJitter(brightness=(.5, 1.5), contrast=(.5, 1.5), saturation=(.5, 1.5))
transform = transforms.Compose([transforms.Resize(self.size), crop, jitter, self.tensor])
dataset = self.dataset(root=self.directory, transform=transform)
torch.manual_seed(0)
train_set_augment, _ = torch.utils.data.random_split(dataset, [50_000, 10_000])
return train_set_augment
# 2. affine + transform
def augment_2(self):
affine = transforms.RandomAffine(degrees=(-60, 60), translate=(0.0, .25), scale=(.9, 1.1))
transform = transforms.Compose([transforms.Resize(self.size), affine, self.tensor])
dataset = self.dataset(root=self.directory, transform=transform)
torch.manual_seed(0)
train_set_augment, _ = torch.utils.data.random_split(dataset, [50_000, 10_000])
return train_set_augment
def mini_imagenet(root, batch_size=64, num_workers=0, size=84):
transform = transforms.Compose([transforms.Resize(size), transforms.ToTensor()])
dataset = datasets.ImageFolder(root=root + 'mini-imagenet/', transform=transform)
torch.manual_seed(0)
train_set, test_set = torch.utils.data.random_split(dataset, [50_000, 10_000])
train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=num_workers)
test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, shuffle=False, num_workers=num_workers)
return train_loader, test_loader
def mini_imagenet_augment_32(root, batch_size=64, num_workers=0):
train_set = []
augment = AUGMENT(directory=root + 'mini-imagenet/', dataset=datasets.ImageFolder, size=32)
train_set.append(augment.original())
train_set.append(augment.augment_1())
train_set.append(augment.augment_2())
train_set = torch.utils.data.ConcatDataset(train_set)
train_loader = torch.utils.data.DataLoader(train_set,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers)
transform = transforms.Compose([transforms.Resize(32), transforms.ToTensor()])
dataset = datasets.ImageFolder(root=root + 'mini-imagenet/', transform=transform)
torch.manual_seed(0)
_, test_set = torch.utils.data.random_split(dataset, [50_000, 10_000])
test_loader = torch.utils.data.DataLoader(test_set,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers)
return train_loader, test_loader
def mini_imagenet_augment_64(root, batch_size=64, num_workers=0):
train_set = []
augment = AUGMENT(directory=root + 'mini-imagenet/', dataset=datasets.ImageFolder, size=64)
train_set.append(augment.original())
train_set.append(augment.augment_1())
train_set.append(augment.augment_2())
train_set = torch.utils.data.ConcatDataset(train_set)
train_loader = torch.utils.data.DataLoader(train_set,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers)
transform = transforms.Compose([transforms.Resize(64), transforms.ToTensor()])
dataset = datasets.ImageFolder(root=root + 'mini-imagenet/', transform=transform)
torch.manual_seed(0)
_, test_set = torch.utils.data.random_split(dataset, [50_000, 10_000])
test_loader = torch.utils.data.DataLoader(test_set,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers)
return train_loader, test_loader
def mini_imagenet_augment_84(root, batch_size=64, num_workers=0):
train_set = []
augment = AUGMENT(directory=root + 'mini-imagenet/', dataset=datasets.ImageFolder, size=84)
train_set.append(augment.original())
train_set.append(augment.augment_1())
train_set.append(augment.augment_2())
train_set = torch.utils.data.ConcatDataset(train_set)
train_loader = torch.utils.data.DataLoader(train_set,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers)
transform = transforms.Compose([transforms.Resize(84), transforms.ToTensor()])
dataset = datasets.ImageFolder(root=root + 'mini-imagenet/', transform=transform)
torch.manual_seed(0)
_, test_set = torch.utils.data.random_split(dataset, [50_000, 10_000])
test_loader = torch.utils.data.DataLoader(test_set,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers)
return train_loader, test_loader
if __name__ == '__main__':
import numpy as np
import matplotlib.pyplot as plt
def imshow(img, name, n):
img = np.transpose(img.numpy(), (1, 2, 0))
plt.subplot(1, 2, n)
plt.title(name)
plt.imshow(img)
train, test = mini_imagenet(root='./data/', batch_size=8)
a = 0
for x, y in train:
a += x.shape[0]
print(a)
for i, (i1, i2) in enumerate(zip(train, test)):
plt.figure('figure')
imshow(i1[0][i], name='train image', n=1)
imshow(i2[0][i], name='test image', n=2)
print(i1[0].shape)
print(i1[1][i])
print(i2[1][i])
plt.show()