-
Notifications
You must be signed in to change notification settings - Fork 9
/
prepare_data.py
62 lines (46 loc) · 1.46 KB
/
prepare_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
import numpy as np
import os
from PIL import Image
def load(dir, files, reshaped):
"Load .npy or .npz files from disk and return them as numpy arrays. \
Takes in a list of filenames and returns a list of numpy arrays."
data = []
for file in files:
f = np.load(dir + file)
if reshaped:
new_f = []
for i in range(len(f)):
x = np.reshape(f[i], (28, 28))
x = np.expand_dims(x, axis=0)
x = np.reshape(f[i], (28, 28, 1))
new_f.append(x)
f = new_f
data.append(f)
return data
def normalize(data):
"Takes a list or a list of lists and returns its normalized form"
return np.interp(data, [0, 255], [-1, 1])
def denormalize(data):
"Takes a list or a list of lists and returns its denormalized form"
return np.interp(data, [-1, 1], [0, 255])
def visualize(array):
"Visulaze a 2D array as an Image"
img = Image.fromarray(array)
img.show(title="Visulizing array")
def set_limit(arrays, n):
"Limit elements from each array up to n elements and return a single list"
new = []
for array in arrays:
i = 0
for item in array:
if i == n:
break
new.append(item)
i += 1
return new
def make_labels(N1, N2):
"make labels from 0 to N1, each repeated N2 times"
labels = []
for i in range(N1):
labels += [i] * N2
return labels