-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMNISTreader.py
More file actions
75 lines (62 loc) · 2.5 KB
/
Copy pathMNISTreader.py
File metadata and controls
75 lines (62 loc) · 2.5 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
import struct
import csv
import matplotlib.pyplot as plt
class MnistRead:
def __init__(self, trainImages, trainLables, testImages, testLabels):
'''
:param trainImages: filename
:param trainLables: filename
:param testImages: filename
:param testLabels: filename
'''
self.trainInFile = trainImages
self.trainOutFile = trainLables
self.testInFile = testImages
self.testOutFile = testLabels
self.process()
def process(self):
with open(self.trainInFile, 'rb') as f:
magic, size = struct.unpack(">II", f.read(8))
nrows, ncols = struct.unpack(">II", f.read(8))
data = np.fromfile(f, dtype=np.dtype(np.uint8).newbyteorder('>'))
data = data.reshape((size, nrows, ncols))
self.trainIn = data
with open(self.testInFile, 'rb') as f:
magic, size = struct.unpack(">II", f.read(8))
nrows, ncols = struct.unpack(">II", f.read(8))
data = np.fromfile(f, dtype=np.dtype(np.uint8).newbyteorder('>'))
data = data.reshape((size, nrows, ncols))
self.testIn = data
with open(self.trainOutFile, 'rb') as f:
f.read(8)
data = list(f.read())
self.trainOut = data
with open(self.testOutFile, 'rb') as f:
f.read(8)
data = list(f.read())
self.testOut = data
# trainInputsWriter = csv.writer(open("mnist/train/images.csv", 'w'))
self.trainingDataIn = []
for inp in self.trainIn:
whole = []
for row in inp:
whole.extend(row)
self.trainingDataIn.append(whole)
# trainInputsWriter.writerow(whole)
# testInputsWriter = csv.writer(open("mnist/test/images.csv", 'w'))
self.testDataIn = []
for inp in self.testIn:
whole = []
for row in inp:
whole.extend(row)
self.testDataIn.append(whole)
# testInputsWriter.writerow(whole)
self.trainingDataOut = []
for out in self.trainOut:
self.trainingDataOut.append([0 for i in range(out)] + [1] + [0 for i in range(9 - out)])
self.testingDataOut = []
for out in self.testOut:
self.testingDataOut.append([0 for i in range(out)] + [1] + [0 for i in range(9 - out)])
self.train = zip(self.trainingDataIn, self.trainingDataOut)
self.test = zip(self.testDataIn, self.testingDataOut)