-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
158 lines (142 loc) · 5.8 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
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
from __future__ import print_function
import zipfile
import os
import PIL
import torchvision.transforms as transforms
# once the images are loaded, how do we pre-process them before being passed into the network
# by default, we resize the images to 32 x 32 in size
# and normalize them to mean = 0 and standard-deviation = 1 based on statistics collected from
# the training set
train_transforms = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToPILImage(),
transforms.RandomApply([
transforms.RandomRotation(20, resample=PIL.Image.BICUBIC),
transforms.RandomAffine(0, translate=(0.2, 0.2), resample=PIL.Image.BICUBIC),
transforms.RandomAffine(0, shear=20, resample=PIL.Image.BICUBIC),
transforms.RandomAffine(0, scale=(0.8, 1.2), resample=PIL.Image.BICUBIC)]),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
data_transforms = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
# Resize, normalize and jitter image brightness
jitter_brightness = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ColorJitter(brightness=5),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
# Resize, normalize and jitter image saturation
jitter_saturation = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ColorJitter(saturation=5),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
# Resize, normalize and jitter image contrast
jitter_contrast = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ColorJitter(contrast=5),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
# Resize, normalize and jitter image hues
jitter_hue = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ColorJitter(hue=0.4),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
# Resize, normalize and rotate image
rotate = transforms.Compose([
transforms.Resize((32, 32)),
transforms.RandomRotation(15),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
# Resize, normalize and flip image horizontally and vertically
hvflip = transforms.Compose([
transforms.Resize((32, 32)),
transforms.RandomHorizontalFlip(1),
transforms.RandomVerticalFlip(1),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
# Resize, normalize and flip image horizontally
hflip = transforms.Compose([
transforms.Resize((32, 32)),
transforms.RandomHorizontalFlip(1),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
# Resize, normalize and flip image vertically
vflip = transforms.Compose([
transforms.Resize((32, 32)),
transforms.RandomVerticalFlip(1),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
# Resize, normalize and shear image
shear = transforms.Compose([
transforms.Resize((32, 32)),
transforms.RandomAffine(degrees = 15,shear=2),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
# Resize, normalize and translate image
translate = transforms.Compose([
transforms.Resize((32, 32)),
transforms.RandomAffine(degrees = 15,translate=(0.1,0.1)),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
# Resize, normalize and crop image
center = transforms.Compose([
transforms.Resize((36, 36)),
transforms.CenterCrop(32),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
# Resize, normalize and convert image to grayscale
grayscale = transforms.Compose([
transforms.Resize((32, 32)),
transforms.Grayscale(num_output_channels=3),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), ( 0.2672, 0.2564, 0.2629))
])
def initialize_data(folder):
train_zip = folder + '/train_images.zip'
test_zip = folder + '/test_images.zip'
if not os.path.exists(train_zip) or not os.path.exists(test_zip):
raise(RuntimeError("Could not find " + train_zip + " and " + test_zip
+ ', please download them from https://www.kaggle.com/c/nyu-cv-fall-2018/data '))
# extract train_data.zip to train_data
train_folder = folder + '/train_images'
if not os.path.isdir(train_folder):
print(train_folder + ' not found, extracting ' + train_zip)
zip_ref = zipfile.ZipFile(train_zip, 'r')
zip_ref.extractall(folder)
zip_ref.close()
# extract test_data.zip to test_data
test_folder = folder + '/test_images'
if not os.path.isdir(test_folder):
print(test_folder + ' not found, extracting ' + test_zip)
zip_ref = zipfile.ZipFile(test_zip, 'r')
zip_ref.extractall(folder)
zip_ref.close()
# make validation_data by using images 00000*, 00001* and 00002* in each class
val_folder = folder + '/val_images'
if not os.path.isdir(val_folder):
print(val_folder + ' not found, making a validation set')
os.mkdir(val_folder)
for dirs in os.listdir(train_folder):
if dirs.startswith('000'):
os.mkdir(val_folder + '/' + dirs)
for f in os.listdir(train_folder + '/' + dirs):
if f.startswith('00000') or f.startswith('00001') or f.startswith('00002'):
# move file to validation folder
os.rename(train_folder + '/' + dirs + '/' + f, val_folder + '/' + dirs + '/' + f)