-
Notifications
You must be signed in to change notification settings - Fork 6
/
idx2nparray_py3.py
134 lines (118 loc) · 4.95 KB
/
idx2nparray_py3.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import numpy as np
import struct as st
import time, math
trainingfilenames = {'images' : '/content/train-images-idx3-ubyte' ,'labels' : '/content/train-labels-idx1-ubyte'}
testfilenames = {'images' : '/content/t10k-images-idx3-ubyte' ,'labels' : '/content/t10k-labels-idx1-ubyte'}
data_types = {
0x08: ('ubyte', 'B', 1),
0x09: ('byte', 'b', 1),
0x0B: ('>i2', 'h', 2),
0x0C: ('>i4', 'i', 4),
0x0D: ('>f4', 'f', 4),
0x0E: ('>f8', 'd', 8)}
#..........................................................For training dataset..............................................................
print("Training Dataset.......")
stime = time.time()
for name in trainingfilenames.keys():
if name == 'images':
train_imagesfile = open(trainingfilenames[name],'rb')
if name == 'labels':
train_labelsfile = open(trainingfilenames[name],'rb')#,encoding='latin-1')
train_imagesfile.seek(0)
magic = st.unpack('>4B',train_imagesfile.read(4))
if(magic[0] and magic[1])or(magic[2] not in data_types):
raise ValueError("File Format not correct")
#Information
nDim = magic[3]
print("Data is "+str(nDim)+"-D")
dataType = data_types[magic[2]][0]
print("Data Type :: ",dataType)
dataFormat = data_types[magic[2]][1]
print("Data Format :: ",dataFormat)
dataSize = data_types[magic[2]][2]
print("Data Size :: "+str(dataSize)+" byte\n")
#offset = 0004 for number of images
#offset = 0008 for number of rows
#offset = 0012 for number of columns
#32-bit integer (32 bits = 4 bytes)
train_imagesfile.seek(4)
nImg = st.unpack('>I',train_imagesfile.read(4))[0] #num of images/labels
nR = st.unpack('>I',train_imagesfile.read(4))[0] #num of rows
nC = st.unpack('>I',train_imagesfile.read(4))[0] #num of columns
train_labelsfile.seek(8) #Since no. of items = no. of images and is already read
print("no. of images :: ",nImg)
print("no. of rows :: ",nR)
print("no. of columns :: ",nC)
print
#Training set
#Reading the labels
train_labels_array = np.asarray(st.unpack('>'+dataFormat*nImg,train_labelsfile.read(nImg*dataSize))).reshape((nImg,1))
#Reading the Image data
nBatch = 10000
nIter = int(math.ceil(nImg/nBatch))
nBytes = nBatch*nR*nC*dataSize
nBytesTot = nImg*nR*nC*dataSize
train_images_array = np.array([])
for i in range(0,nIter):
temp_images_array = np.asarray(st.unpack('>'+dataFormat*nBytes,train_imagesfile.read(nBytes))).reshape((nBatch,nR,nC))
#Stacking each nBatch block to form a larger block
if train_images_array.size == 0:
train_images_array = temp_images_array
else:
train_images_array = np.vstack((train_images_array,temp_images_array))
temp_images_array = np.array([])
print("Time taken :: "+str(time.time()-stime)+" seconds\n")
print(str((float(i+1)/nIter)*100)+"% complete...\n")
print("Training Set Labels shape ::",train_labels_array.shape)
print("Training Set Image shape ::",train_images_array.shape)
print("Time of execution :: "+str(time.time()-stime)+" seconds\n")
#..........................................................For test dataset..................................................................
print("Test Dataset.......")
stime = time.time()
for name in testfilenames.keys():
if name == 'images':
test_imagesfile = open(testfilenames[name],'rb')
if name == 'labels':
test_labelsfile = open(testfilenames[name],'rb')
test_imagesfile.seek(0)
magic = st.unpack('>4B',test_imagesfile.read(4))
if(magic[0] and magic[1])or(magic[2] not in data_types):
raise ValueError("File Format not correct")
nDim = magic[3]
print("Data is ",nDim,"-D")
print
#offset = 0004 for number of images
#offset = 0008 for number of rows
#offset = 0012 for number of columns
#32-bit integer (32 bits = 4 bytes)
test_imagesfile.seek(4)
nImg = st.unpack('>I',test_imagesfile.read(4))[0] #num of images/labels
nR = st.unpack('>I',test_imagesfile.read(4))[0] #num of rows
nC = st.unpack('>I',test_imagesfile.read(4))[0] #num of columns
test_labelsfile.seek(8) #Since no. of items = no. of images and is already read
print("no. of images :: ",nImg)
print("no. of rows :: ",nR)
print("no. of columns :: ",nC)
print
#Test set
#Reading the labels
test_labels_array = np.asarray(st.unpack('>'+dataFormat*nImg,test_labelsfile.read(nImg*dataSize))).reshape((nImg,1))
#Reading the Image data
nBatch = 10000
nIter = int(math.ceil(nImg/nBatch))
nBytes = nBatch*nR*nC*dataSize
nBytesTot = nImg*nR*nC*dataSize
test_images_array = np.array([])
for i in range(0,nIter):
temp_images_array = np.asarray(st.unpack('>'+dataFormat*nBytes,test_imagesfile.read(nBytes))).reshape((nBatch,nR,nC))
#Stacking each nBatch block to form a larger block
if test_images_array.size == 0:
test_images_array = temp_images_array
else:
test_images_array = np.vstack((test_images_array,temp_images_array))
temp_images_array = np.array([])
print("Time taken :: "+str(time.time()-stime)+" seconds\n")
print(str((float(i+1)/nIter)*100)+"% complete...\n")
print("Test Set Labels shape ::",test_labels_array.shape)
print("Test Set Image shape ::",test_images_array.shape)
print("Time of execution : %s seconds" % str(time.time()-stime))