-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
133 lines (103 loc) · 3.48 KB
/
dataset.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
import scipy.io as sio
import numpy as np
class DataSet(object):
"""
Class to manage your data
If you need to use batch for your algorithm, there is a next_batch implementation
"""
def __init__(self, images, labels):
assert images.shape[0] == labels.shape[0], ('images.shape: %s labels.shape: %s' % (images.shape,labels.shape))
self._num_examples = images.shape[0]
self._images = images
self._labels = labels
self._index_in_epoch = 0
@property
def images(self):
return self._images
@property
def labels(self):
return self._labels
@property
def num_examples(self):
return self._num_examples
"""
# Next batch operation
def next_batch(self, batch_size):
# Batch size should be smaller than sample size
assert batch_size <= self._num_examples
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
# Shuffle the data
perm = np.arange(self._num_examples)
np.random.shuffle(perm)
self._images = self._images[perm]
self._labels = self._labels[perm]
start = 0
self._index_in_epoch = batch_size
end = self._index_in_epoch
return self._images[start:end], self._labels[start:end]
"""
def read_mnist(datapath):
"""
@datapath : path to the input data
Read data
"""
data=sio.loadmat(datapath)
train=DataSet(data['train'],data['trainLabel'])
tune=DataSet(data['tune'],data['tuneLabel'])
test=data['test']
return train, tune, test
class DataSet_Twoview(object):
"""
Class to manage your data
If you need to use batch for your algorithm, there is a next_batch implementation
"""
def __init__(self, images, images2, labels):
assert images.shape[0] == labels.shape[0], ('images.shape: %s labels.shape: %s' % (images.shape,labels.shape))
self._num_examples = images.shape[0]
self._images = images
self._images2 = images2
self._labels = labels
self._index_in_epoch = 0
@property
def images(self):
return self._images
@property
def images2(self):
return self._images2
@property
def labels(self):
return self._labels
@property
def num_examples(self):
return self._num_examples
def read_mnist_twoview(datapath):
"""
@datapath : path to the input data
Read data
"""
data=sio.loadmat(datapath)
train=DataSet_Twoview(data['train'],data['train2'],data['trainLabel'])
tune=DataSet_Twoview(data['tune'],data['tune2'],data['tuneLabel'])
test1=data['test']
test2=data['test2']
return train, tune, test1, test2
"""
# Next batch operation
def next_batch(self, batch_size):
# Batch size should be smaller than sample size
assert batch_size <= self._num_examples
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
# Shuffle the data
perm = np.arange(self._num_examples)
np.random.shuffle(perm)
self._images = self._images[perm]
self._labels = self._labels[perm]
start = 0
self._index_in_epoch = batch_size
end = self._index_in_epoch
return self._images[start:end], self._labels[start:end]
"""