-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataloader.py
182 lines (149 loc) · 5.29 KB
/
dataloader.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
175
176
177
178
179
180
181
182
"""Implementation of data loader for training and testing
Haddadpour, F., Kamani, M.M., Mahdavi, M., & Cadambe, V.
"Local SGD with Periodic Averaging: Tighter Analysis and Adaptive Synchronization"
Advances in Neural Information Processing. 2019.
"""
import os
import numpy as np
import sklearn.decomposition
import tensorflow as tf
"""
__author__ = "Mohammad Mahdi Kamani"
__copyright__ = "Copyright 2019, Mohammad Mahdi Kamani"
__license__ = "MIT"
__version__ = "0.0.1"
__maintainer__ = "Mohammad Madhi Kamani"
__status__ = "Prototype"
"""
class Dataset():
def __init__(self, data_dir, num_shards, subset='train'):
self.data_dir = data_dir
self.num_shards = num_shards
self.subset = subset
def get_filenames(self):
if self.subset in ['train', 'validation', 'test']:
return [os.path.join(self.data_dir, self.subset + '.tfrecords')]
else:
raise ValueError('Invalid data subset "%s"' % self.subset)
def parser(self):
return
def make_batch(self, batch_size):
feature_shards = [[] for i in range(self.num_shards)]
label_shards = [[] for i in range(self.num_shards)]
filenames = self.get_filenames()
dataset = tf.data.TFRecordDataset(filenames)
for device_id in range(self.num_shards):
if self.subset == 'train':
dataset = tf.data.TFRecordDataset(filenames)
d0 = dataset.repeat()
# Parse records.
d0 = d0.map(
self.parser, num_parallel_calls=int(batch_size / self.num_shards))
# Potentially shuffle records.
min_queue_examples = int(
self.num_examples_per_epoch(self.subset) * 0.4 / self.num_shards)
# Ensure that the capacity is sufficiently large to provide good random
# shuffling.
d0 = d0.shuffle(buffer_size=min_queue_examples + int(3 * batch_size / self.num_shards))
# Batch it up.
d0 = d0.batch(int(batch_size / self.num_shards))
iterator0 = d0.make_one_shot_iterator()
X_batch, y_batch = iterator0.get_next()
elif self.subset == 'test':
d = dataset.repeat()
d = d.map(self.parser, num_parallel_calls=batch_size)
d = d.batch(batch_size)
iterator = d.make_one_shot_iterator()
X_batch, y_batch = iterator.get_next()
feature_shards[device_id] = X_batch
label_shards[device_id] = y_batch
return feature_shards, label_shards
class MnistDataset(Dataset):
def __init__(self, data_dir, num_shards, subset='train', redundancy=0.0):
super(MnistDataset, self).__init__(
data_dir,
num_shards,
subset,
redundancy
)
self.HEIGHT = 28
self.WIDTH = 28
self.NUM_CLASSES = 10
def parser(self, serialized_example):
"""Parses a single tf.Example into image and label tensors."""
features = tf.parse_single_example(
serialized_example,
features={
'data': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
})
image = tf.decode_raw(features['data'], tf.uint8)
image = tf.cast(image, tf.float32) / 128.0 - 1
image.set_shape([self.HEIGHT * self.WIDTH ])
label = tf.one_hot(tf.cast(features['label'], tf.int32), self.NUM_CLASSES)
return image, label
@staticmethod
def num_examples_per_epoch(subset='train'):
if subset == 'train':
return 60000
elif subset == 'eval':
return 10000
else:
raise ValueError('Invalid data subset "%s"' % subset)
class AdultDataset(Dataset):
def __init__(self, data_dir, num_shards, subset='train', redundancy=0.0):
super(AdultDataset, self).__init__(
data_dir,
num_shards,
subset,
redundancy
)
def parser(self, serialized_example):
"""Parses a single tf.Example into image and label tensors."""
features = tf.parse_single_example(
serialized_example,
features={
'data': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
})
data = tf.decode_raw(features['data'], tf.int64)
data = tf.cast(tf.reshape(data,[14]), tf.float32)
label = tf.cast(features['label'], tf.int32)
return data, label
@staticmethod
def num_examples_per_epoch(subset='train'):
if subset == 'train':
return 30162
elif subset == 'eval':
return 15060
else:
raise ValueError('Invalid data subset "%s"' % subset)
class EpsilonDataset(Dataset):
def __init__(self, data_dir, num_shards, subset='train', redundancy=0.0):
super(EpsilonDataset, self).__init__(
data_dir,
num_shards,
subset,
redundancy
)
def parser(self, serialized_example):
"""Parses a single tf.Example into image and label tensors."""
features = tf.parse_single_example(
serialized_example,
features={
'feature': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.float32),
})
data = tf.decode_raw(features['feature'], tf.float64)
data = tf.cast(tf.reshape(data,[2000]), tf.float64)
# data=features['feature']
label = tf.cast(features['label'], tf.int32)
return data, label
@staticmethod
def num_examples_per_epoch(subset='train'):
if subset == 'train':
return 400000
elif subset == 'eval':
return 100000
else:
raise ValueError('Invalid data subset "%s"' % subset)