forked from mnielsen/neural-networks-and-deep-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize_images_to_json.py
46 lines (34 loc) · 1.1 KB
/
serialize_images_to_json.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
"""
serialize_images_to_json
~~~~~~~~~~~~~~~~~~~~~~~~
Utility to serialize parts of the training and validation data to JSON,
for use with Javascript. """
#### Libraries
# Standard library
import json
import sys
# My library
sys.path.append('../src/')
import mnist_loader
# Third-party libraries
import numpy as np
# Number of training and validation data images to serialize
NTD = 1000
NVD = 100
training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
def make_data_integer(td):
# This will be slow, due to the loop. It'd be better if numpy did
# this directly. But numpy.rint followed by tolist() doesn't
# convert to a standard Python int.
return [int(x) for x in (td*256).reshape(784).tolist()]
data = {"training": [
{"x": [x[0] for x in training_data[j][0].tolist()],
"y": [y[0] for y in training_data[j][1].tolist()]}
for j in xrange(NTD)],
"validation": [
{"x": [x[0] for x in validation_data[j][0].tolist()],
"y": validation_data[j][1]}
for j in xrange(NVD)]}
f = open("data_1000.json", "w")
json.dump(data, f)
f.close()