forked from ramarlina/DenoisingAutoEncoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
47 lines (38 loc) · 1.33 KB
/
utils.py
File metadata and controls
47 lines (38 loc) · 1.33 KB
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
import gzip
import cPickle
from PIL import Image
import numpy
import sys
def makeMultiClass(y):
u = numpy.unique(y)
coords = {}
for idx in range(len(u)):
coords[str(u[idx])] = idx
V = numpy.zeros((len(y), len(u)))
for idx in range(len(y)):
V[idx, coords[str(y[idx])]] = 1
return V
def load_mnist():
f = gzip.open("mnist/mnist.pkl.gz", 'rb')
train, valid, test = cPickle.load(f)
f.close()
return train
def scale(X, eps=1e-8):
return (X - X.min())/ (X.max() + eps)
def saveTiles(X, img_shape, tile_shape, tile_spacing=(0, 0), filename="Results/res_DA.png"):
out_shape = [(ishp + tsp) * tshp - tsp for ishp, tshp, tsp
in zip(img_shape, tile_shape, tile_spacing)]
H, W = img_shape
Hs, Ws = tile_spacing
out_array = numpy.zeros(out_shape, dtype='uint8')
for tile_row in xrange(tile_shape[0]):
for tile_col in xrange(tile_shape[1]):
if tile_row * tile_shape[1] + tile_col < X.shape[0]:
img = scale(X[tile_row * tile_shape[1] + tile_col].reshape(img_shape))
out_array[
tile_row * (H+Hs): tile_row * (H + Hs) + H,
tile_col * (W+Ws): tile_col * (W + Ws) + W
] \
= img * 255
img = Image.fromarray(out_array)
img.save(filename)