-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert_cifar10.py
174 lines (146 loc) · 5.79 KB
/
convert_cifar10.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
164
165
166
167
168
169
170
171
172
173
174
import warnings
warnings.filterwarnings('ignore')
import numpy as np
import os
import sys
import math
import time
import random
import datetime
import pickle
from os import listdir
import os.path
import argparse
import glob
import math
import mxnet as mx
from mxnet import nd, gluon, autograd
from mxnet.gluon.utils import download
from mxnet.gluon import nn
from mxnet.gluon.data.vision import transforms
from gluoncv.data import transforms as gcv_transforms
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--nsplit", type=int, help="number of partitions of training set", default=20)
parser.add_argument("--output", type=str, help="dir of output", required=True)
args = parser.parse_args()
output_dir = os.path.join(args.output, 'dataset_split_{}'.format(args.nsplit))
output_train_dir = os.path.join(output_dir, 'train')
output_val_dir = os.path.join(output_dir, 'val')
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if not os.path.exists(output_train_dir):
os.makedirs(output_train_dir)
if not os.path.exists(output_val_dir):
os.makedirs(output_val_dir)
context = [mx.cpu()]
batch_size = 10
dataset_name = 'cifar-10'
# transform_train = transforms.Compose([
# # Randomly crop an area, and then resize it to be 32x32
# transforms.RandomResizedCrop(32),
# # Randomly flip the image horizontally
# transforms.RandomFlipLeftRight(),
# # Randomly jitter the brightness, contrast and saturation of the image
# transforms.RandomColorJitter(brightness=0.1, contrast=0.1, saturation=0.1),
# # Randomly adding noise to the image
# transforms.RandomLighting(0.1),
# # Transpose the image from height*width*num_channels to num_channels*height*width
# # and map values from [0, 255] to [0,1]
# transforms.ToTensor(),
# # Normalize the image with mean and standard deviation calculated across all images
# transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])
# ])
# transform_test = transforms.Compose([
# transforms.Resize(32),
# transforms.ToTensor(),
# transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])
# ])
transform_train = transforms.Compose([
gcv_transforms.RandomCrop(32, pad=4),
transforms.RandomFlipLeftRight(),
transforms.ToTensor(),
transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010])
])
train_data = gluon.data.DataLoader(
gluon.data.vision.CIFAR10(train=True).transform_first(transform_train),
batch_size=batch_size, shuffle=True, last_batch='keep', num_workers=0)
# Set train=False for validation data
test_data = gluon.data.DataLoader(
gluon.data.vision.CIFAR10(train=False).transform_first(transform_test),
batch_size=batch_size, shuffle=False, last_batch='keep', num_workers=0)
x_train_list = []
y_train_list = []
for i, (data, target) in enumerate(train_data):
x_train_list.append(data)
y_train_list.append(target)
x_test_list = []
y_test_list = []
for i, (data, target) in enumerate(test_data):
x_test_list.append(data)
y_test_list.append(target)
# x_train = nd.concat(*x_train_list, dim=0)
# y_train = nd.concat(*y_train_list, dim=0)
# x_test = nd.concat(*x_test_list, dim=0)
# y_test = nd.concat(*y_test_list, dim=0)
nd.waitall()
# print(x_train.shape)
# print(y_train.shape)
# print(x_test.shape)
# print(y_test.shape)
perm = list(range( len(x_train_list) ))
random.shuffle(perm)
x_train_list = [x_train_list[index] for index in perm]
y_train_list = [y_train_list[index] for index in perm]
# zeno validation
n_val = round(len(x_train_list) / 20)
x_val_list = []
y_val_list = []
for i in range(n_val):
x_val_list.append(x_train_list.pop(0))
y_val_list.append(y_train_list.pop(0))
x_val = nd.concat(*x_val_list, dim=0)
y_val = nd.concat(*y_val_list, dim=0)
print(x_val.shape)
print(y_val.shape)
output_val_filename = os.path.join(output_val_dir, "val_data.pkl")
print(output_val_filename, flush=True)
with open(output_val_filename, "wb") as f:
data = pickle.dumps([x_val, y_val])
pickle.dump(data, f)
# training data
x_worker_list = []
y_worker_list = []
for i in range(args.nsplit):
x_worker_list.append([])
y_worker_list.append([])
worker_index = 0
for i in range(len(x_train_list)):
x_worker_list[worker_index].append(x_train_list.pop(0))
y_worker_list[worker_index].append(y_train_list.pop(0))
worker_index = (worker_index + 1) % args.nsplit
for i in range(args.nsplit):
output_train_filename = os.path.join(output_train_dir, "train_data_%03d.pkl" % i)
x_train = nd.concat(*(x_worker_list[i]), dim=0)
y_train = nd.concat(*(y_worker_list[i]), dim=0)
print(x_train.shape)
print(y_train.shape)
print(output_train_filename, flush=True)
with open(output_train_filename, "wb") as f:
data = pickle.dumps([x_train, y_train])
pickle.dump(data, f)
# testing data
n_test = len(x_test_list)
x_test = nd.concat(*x_test_list, dim=0)
y_test = nd.concat(*y_test_list, dim=0)
print(x_test.shape)
print(y_test.shape)
output_test_filename = os.path.join(output_val_dir, "test_data.pkl")
print(output_test_filename, flush=True)
with open(output_test_filename, "wb") as f:
data = pickle.dumps([x_test, y_test])
pickle.dump(data, f)