-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_config_misc.py
395 lines (308 loc) · 12.7 KB
/
tf_config_misc.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# 12/5/23 DH: Refactor TFConfig for:
"""
API
---
mnist
getMNISTexamples()
pruneDigitDict()
checkMNISTexamples()
bitwise
bitwiseAND()
getImgCheckTotals()
printDigitArrayValues()
saveDigitArrayValues()
convertDigitDict()
"""
from tf_config import *
# 6/5/23 DH:
import pickle
import time
import numpy
import sys
class TFConfigMisc(TFConfig):
def __init__(self) -> None:
super().__init__()
self.mnistFilename = "digitDictionary.pkl"
self.mnistFilenameINT = "digitDictionaryINTEGER.pkl"
self.digitFilename = "digitOutput"
with open(self.mnistFilenameINT, 'rb') as fp:
#with open(self.mnistFilename, 'rb') as fp:
self.digitDict = pickle.load(fp)
# =========================== API =================================
# 5/5/23 DH: http://yann.lecun.com/exdb/mnist/
"""
"SD-3 is much cleaner and easier to recognize than SD-1"
"Therefore it was necessary to build a new database by mixing NIST's datasets."
"The MNIST training set is composed of 30,000 patterns from SD-3 and 30,000 patterns from SD-1.
Our test set was composed of 5,000 patterns from SD-3 and 5,000 patterns from SD-1. "
"""
def getMNISTexamples(self):
print("Getting MNIST examples...")
#digitDict = {0:None, 1:None, 2:None, 3:None, 4:None, 5:None, 6:None, 7:None, 8:None, 9:None}
self.digitDict = {0:[], 1:[], 2:[], 3:[], 4:[], 5:[], 6:[], 7:[], 8:[], 9:[]}
imgs = self.x_test
imgValues = self.y_test
imgNum = imgs.shape[0]
#print(digitDict.keys())
#print(digitDict.values())
# 5/5/23 DH: Need to find way to select a good image for each digit
# (via a shortlist array for each digit which then gets selected in 'displayImgList()')
"""
self.digitDict:
0 ..........
1 ..........
2 ..........
...
"""
for elem in range(imgNum):
#digitDict[imgValues[elem]] = imgs[elem]
self.digitDict[imgValues[elem]].append(imgs[elem])
# 6/5/23 DH: Change this from 'not None' to a 'list of 10' per elem
#if not any(x is None for x in digitDict.values()):
if not any(len(list) < 10 for list in self.digitDict.values()):
print("Got full set at", elem)
break
self.pruneDigitDict(printOut=True)
# "pickle rick"...https://www.youtube.com/watch?v=_gRnvDRFYN4
with open(self.mnistFilename, 'wb') as fp:
pickle.dump(self.digitDict, fp)
for key in self.digitDict.keys():
#self.displayDictImg(digitDict, key)
self.displayImgList(self.digitDict[key])
def checkMNISTexamples(self, digit=None):
print("Checking MNIST examples in", self.mnistFilename)
changed = False
if digit:
#self.displayDictImg(digitDict, int(digit))
changed, self.digitDict[int(digit)] = self.displayImgList(self.digitDict[int(digit)])
else:
for key in self.digitDict.keys():
#self.displayDictImg(digitDict, key)
chgFlag, self.digitDict[key] = self.displayImgList(self.digitDict[key])
# Prevent mid list changes being forgotten for repickling
if chgFlag:
changed = True
if changed:
print("List shortened so repickling")
with open(self.mnistFilename, 'wb') as fp:
pickle.dump(self.digitDict, fp)
# --------------------------------------------------------------------------------------------
# 7/5/23 DH: Test bitwise-AND with example images for reinforcement learning
# (which is an example of "Expert system" and shows power of AI for image variation)
# --------------------------------------------------------------------------------------------
def bitwiseAND(self, check=False):
print("Correlating 'y_test[index]' with return of highest bitwise-AND\n")
# Get first example digit ie 0
#testImg = self.digitDict[0][0]
#self.printDigitArrayValues(testImg)
self.check = check
# 7/5/23 DH: 'x_test' is converted to float above (for NN accuracy reasons)
if check:
# 'digitDict.values()' is array of 1 elem arrays
valuesList = list(self.digitDict.values())
# imgs = numpy.asarray(valuesList[0])
# Need to loop through the first element in each of the digit array
#imgs = np.ndarray(0)
imgList = []
for key in self.digitDict.keys():
#imgs = np.append(imgs, valuesList[key])
imgList.append(valuesList[key][0])
imgs = numpy.asarray(imgList)
imgValues = list(self.digitDict.keys())
else:
# 12/5/23 DH: By default (unless 'integer=True' specified) the images are float
imgs = self.x_test
imgValues = self.y_test
totalsErrors = 0
errorNum = 0
# 'shape' returns a tuple so access first value
imgNum = imgs.shape[0]
print("imgNum:",imgNum)
#imgNum = 1
for elem in range(imgNum):
img = imgs[elem]
self.displayImg(img)
totalsDict = self.getImgCheckTotals(img)
totals = list(totalsDict.values())
digit = np.argmax(totals)
if int(digit) != int(imgValues[elem]):
totalsErrors += 1
#self.displayImg(img, timeout=2)
if totalsErrors < 3:
print("Check:", digit, "y_test:",imgValues[elem])
for k in sorted(totalsDict, key=totalsDict.get, reverse=True):
if k == imgValues[elem]:
print("*",k,"-",totalsDict[k])
else:
print(k,"-",totalsDict[k])
print()
"""
else:
print("Breaking after 3 errors for debug")
break
"""
if errorNum != totalsErrors and totalsErrors % 100 == 0:
print(totalsErrors, "errors at element",elem)
errorNum = totalsErrors
# ----- END: 'for elem in range(imgNum)' -----
print("Total errors:", totalsErrors)
# 7/5/23 DH:
def convertDigitDict(self):
print("Converting example digits from float to integer")
for key in self.digitDict.keys():
# The example digits only contain 1 element per digit list
img = self.digitDict[key][0]
# 6/5/23 DH: Reverse the float conversion after 'mnist.load_data()'
img = img * 255
img = np.asarray(img, dtype = 'uint8')
self.digitDict[key][0] = img
with open(self.mnistFilenameINT, 'wb') as fp:
pickle.dump(self.digitDict, fp)
# ================================ Internal =================================
def pruneDigitDict(self, printOut=False):
for key in self.digitDict.keys():
if printOut:
print(key,":",len(self.digitDict[key]))
# 6/5/23 DH: Now limit size to 10 elements
self.digitDict[key] = self.digitDict[key][:10]
def printDigitArrayValues(self, testImg):
# Get the image for dictionary key '0'
#testImg = self.digitDict[0][0]
print("Test img:",type(testImg), testImg.shape, type(testImg[0][0]))
xOffset = 15
y = 15
print(xOffset,",",y,"of",testImg.shape)
for xIdx in range(7):
xIdx += xOffset
# 6/5/23 DH: When remove "Convert the sample data from integers to floating-point numbers"
# "{:.2f}" becomes "{:3}"
print("{:3}".format(testImg[xIdx][y]),
"{:3}".format(testImg[xIdx][y+1]),
"{:3}".format(testImg[xIdx][y+2]),
"{:3}".format(testImg[xIdx][y+3]),
"{:3}".format(testImg[xIdx][y+4]),
"{:3}".format(testImg[xIdx][y+5]),
"{:3}".format(testImg[xIdx][y+6]),
"{:3}".format(testImg[xIdx][y+7]))
"""
print(testImg[xIdx][y],
testImg[xIdx][y+1],
testImg[xIdx][y+2],
testImg[xIdx][y+3],
testImg[xIdx][y+4],
testImg[xIdx][y+5],
testImg[xIdx][y+6],
testImg[xIdx][y+7])
"""
print()
def saveDigitArrayValues(self, testImg,fileNum=1):
filename = self.digitFilename + str(fileNum)
with open(filename, 'w') as fp:
fp.write("Test img:" + str(type(testImg)) + " " + str(testImg.shape) + " " +
str(type(testImg[0][0])) + "\n\n")
# 15/5/23 DH: https://docs.python.org/3.6/library/string.html#format-examples
"""
"The FIELD_NAME is optionally followed by a CONVERSION FIELD, which is preceded by an
exclamation point '!', and a FORMAT_SPEC, which is preceded by a colon ':'.
----------------- -----
These specify a non-default format for the replacement value."
"Three CONVERSION FLAGS are currently supported: '!s' which calls str() on the value,
'!r' which calls repr() and '!a' which calls ascii()."
"FORMAT_SPEC ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type]"
"""
if isinstance(testImg[0][0], numpy.float32):
formatStr = "{:5.1f}"
else:
# "xyz "
formatStr = "{:4}"
# ----------------- Write selected region --------------------
xOffset = 15
y = 15
fp.write(str(xOffset) + "," + str(y) + " of " + str(testImg.shape) + "\n")
for xIdx in range(7):
xIdx += xOffset
# 6/5/23 DH: When remove "Convert the sample data from integers to floating-point numbers"
# "{:.2f}" becomes "{:3}" for integers
# This a row printout (despite looking like a column in code)
fp.write(formatStr.format(testImg[xIdx][y]) +
formatStr.format(testImg[xIdx][y+1]) +
formatStr.format(testImg[xIdx][y+2]) +
formatStr.format(testImg[xIdx][y+3]) +
formatStr.format(testImg[xIdx][y+4]) +
formatStr.format(testImg[xIdx][y+5]) +
formatStr.format(testImg[xIdx][y+6]) +
formatStr.format(testImg[xIdx][y+7]) + "\n")
fp.write("\n")
# ------------------------------------------------------------
# --------------------- Write whole image --------------------
xSize, ySize = testImg.shape
fp.write("\n")
for x in range(xSize):
for y in range(ySize):
fp.write(formatStr.format(testImg[x][y]))
fp.write("\n")
# ------------------------------------------------------------
sortedPixVals = sorted(testImg.reshape(784))
fp.write("\nSorted pixel values:\n")
fp.write(str(sortedPixVals))
def getImgCheckTotals(self, img):
#totals = []
totalsDict = {}
# 12/5/23 DH: By default (unless 'integer=True' specified) the images 'x_train' and 'x_test' are float
# Convert image from float to integer array
img = img * 255
img = np.asarray(img, dtype = 'uint8')
for key in self.digitDict.keys():
# First (and only) element of list for each digit 'key'
testImg = self.digitDict[key][0]
#self.displayImg(img)
# Convert image from float to integer array
# ('testImg' is likely to already be an integer so commented out for reference)
#testImg = testImg * 255
#testImg = np.asarray(testImg, dtype = 'uint8')
bitwiseAndRes = numpy.bitwise_and(testImg, img)
imgX, imgY = bitwiseAndRes.shape
#print("\n",key,"- Shape:",imgX, imgY)
iTotal = 0
iPixValChg = 0
for x in range(imgX):
for y in range(imgY):
#iTotal += bitwiseAndRes[x][y]
# 9/5/23 DH: appears to be a "hooked" test function that selectively alters the image
# (giving image a mottled effect)
if bitwiseAndRes[x][y] > 0:
iTotal += 1
"""
print("X:",x,"Y:",y,"=",bitwiseAndRes[x][y])
print("testImg:",testImg[x][y],"img:",img[x][y])
print("testImg & img:",testImg[x][y] & img[x][y])
print("{:>10}".format(bin(testImg[x][y])))
print("{:>10}".format(bin(img[x][y])))
print("{:>10}".format(bin(bitwiseAndRes[x][y])))
print("{:>10}".format(bin(testImg[x][y] & img[x][y])))
print()
"""
if self.check:
# 9/5/23 DH: Make whole image binary (rather than grey_scale)
pixVal = int(key+1) * 100
img[x][y] = pixVal
iPixValChg += 1
"""
print("********************")
print(key,"total:", iTotal)
print("********************")
"""
if self.check:
if key < 3:
print("Key:",key,"PixVal:",pixVal,"iPixValChg:",iPixValChg)
# Print out binary image to compare with grey_scale image at start of function
self.printDigitArrayValues(img)
self.saveDigitArrayValues(img,key)
self.displayImg(img,timeout=3)
else:
sys.exit()
#totals.append(iTotal)
totalsDict[key] = iTotal
# END: ------------ 'for key in self.digitDict.keys()' --------------
return totalsDict
# --------------------------------------------------------------------------------------------