-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
38 lines (25 loc) · 975 Bytes
/
utils.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
import numpy as np
import random
import scipy
from tensorflow.examples.tutorials.mnist import input_data
def randomize(x, y):
""" Randomizes the order of data samples and their corresponding labels"""
permutation = np.random.permutation(y.shape[0])
shuffled_x = x[permutation, :, :, :]
shuffled_y = y[permutation]
return shuffled_x, shuffled_y
def reformat(x, y):
"""
Reformats the data to the format acceptable for convolutional layers
:param x: input array
:param y: corresponding labels
:return: reshaped input and labels
"""
img_size, num_ch, num_class = int(np.sqrt(x.shape[-1])), 1, len(np.unique(np.argmax(y, 1)))
dataset = x.reshape((-1, img_size, img_size, num_ch)).astype(np.float32)
labels = (np.arange(num_class) == y[:, None]).astype(np.float32)
return dataset, labels
def get_next_batch(x, y, start, end):
x_batch = x[start:end]
y_batch = y[start:end]
return x_batch, y_batch