1 |
2 |
qaztronic |
#
|
2 |
|
|
import numpy as np
|
3 |
|
|
import tensorflow as tf
|
4 |
|
|
from tensorflow.keras.datasets import cifar10
|
5 |
|
|
import matplotlib.pyplot as plt
|
6 |
|
|
import matplotlib.gridspec as gridspec
|
7 |
|
|
import shutil
|
8 |
|
|
import struct
|
9 |
|
|
import os
|
10 |
|
|
|
11 |
|
|
# ---------------------------------------------------------------
|
12 |
|
|
def plot_history(history, name):
|
13 |
|
|
# summarize history for accuracy
|
14 |
|
|
plt.plot(history.history['sparse_top_k_categorical_accuracy'])
|
15 |
|
|
plt.title('model accuracy')
|
16 |
|
|
plt.ylabel('accuracy')
|
17 |
|
|
plt.xlabel('epoch')
|
18 |
|
|
plt.savefig(str(name) + '_model_accuracy.png')
|
19 |
|
|
plt.close()
|
20 |
|
|
|
21 |
|
|
# summarize history for loss
|
22 |
|
|
plt.plot(history.history['loss'])
|
23 |
|
|
plt.title('model loss')
|
24 |
|
|
plt.ylabel('loss')
|
25 |
|
|
plt.xlabel('epoch')
|
26 |
|
|
plt.savefig(str(name) + '_model_loss.png')
|
27 |
|
|
plt.close()
|
28 |
|
|
|
29 |
|
|
# ---------------------------------------------------------------
|
30 |
|
|
def show_the_image(image, title=None):
|
31 |
|
|
# plt.imshow(image, interpolation='bilinear')
|
32 |
|
|
plt.imshow(image, cmap=plt.cm.gray)
|
33 |
|
|
if title is not None:
|
34 |
|
|
plt.title(title)
|
35 |
|
|
plt.show()
|
36 |
|
|
|
37 |
|
|
# ---------------------------------------------------------------
|
38 |
|
|
def save_the_image(image, filename, title=None):
|
39 |
|
|
# plt.imshow(image, interpolation='bilinear')
|
40 |
|
|
plt.imshow(image)
|
41 |
|
|
if title is not None:
|
42 |
|
|
plt.title(title)
|
43 |
|
|
plt.savefig(filename)
|
44 |
|
|
plt.close()
|
45 |
|
|
|
46 |
|
|
# -------------------------------------------------------
|
47 |
|
|
def grid_plot(im):
|
48 |
|
|
nrow = im.shape[0]
|
49 |
|
|
ncol = im.shape[1]
|
50 |
|
|
fig = plt.figure(figsize=(ncol+1, nrow+1))
|
51 |
|
|
gs = gridspec.GridSpec(nrow, ncol,
|
52 |
|
|
wspace=0.02, hspace=0.02,
|
53 |
|
|
top=1.-0.5/(nrow+1), bottom=0.5/(nrow+1),
|
54 |
|
|
left=0.5/(ncol+1), right=1-0.5/(ncol+1))
|
55 |
|
|
|
56 |
|
|
for i in range(nrow):
|
57 |
|
|
for j in range(ncol):
|
58 |
|
|
ax= plt.subplot(gs[i,j])
|
59 |
|
|
ax.imshow(im[i][j], cmap=plt.get_cmap('gray'))
|
60 |
|
|
plt.axis('off')
|
61 |
|
|
|
62 |
|
|
plt.show()
|
63 |
|
|
|
64 |
|
|
# -------------------------------------------------------
|
65 |
|
|
def load_mnist():
|
66 |
|
|
mnist = tf.keras.datasets.mnist # mnist is a dataset of 28x28 images of handwritten digits and their labels
|
67 |
|
|
|
68 |
|
|
# input image dimensions
|
69 |
|
|
img_rows, img_cols = 28, 28
|
70 |
|
|
|
71 |
|
|
# the data, split between train and test sets
|
72 |
|
|
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
73 |
|
|
|
74 |
|
|
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
|
75 |
|
|
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
|
76 |
|
|
|
77 |
|
|
x_train = x_train.astype('float32')
|
78 |
|
|
x_test = x_test.astype('float32')
|
79 |
|
|
x_train /= 255
|
80 |
|
|
x_test /= 255
|
81 |
|
|
|
82 |
|
|
print('x_train shape:', x_train.shape)
|
83 |
|
|
print(x_train.shape[0], 'train samples')
|
84 |
|
|
print(x_test.shape[0], 'test samples')
|
85 |
|
|
|
86 |
|
|
return (x_train, y_train),(x_test, y_test)
|
87 |
|
|
|
88 |
|
|
# -------------------------------------------------------
|
89 |
|
|
def load_cifar10():
|
90 |
|
|
|
91 |
|
|
# The data, split between train and test sets:
|
92 |
|
|
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
|
93 |
|
|
# print('x_train shape:', x_train.shape)
|
94 |
|
|
# print(x_train.shape[0], 'train samples')
|
95 |
|
|
# print(x_test.shape[0], 'test samples')
|
96 |
|
|
|
97 |
|
|
return (x_train, y_train),(x_test, y_test)
|
98 |
|
|
|
99 |
|
|
# -------------------------------------------------------
|
100 |
|
|
def float_to_hex(f):
|
101 |
|
|
return format(struct.unpack('<I', struct.pack('<f', f))[0], 'x')
|
102 |
|
|
|
103 |
|
|
# -------------------------------------------------------
|
104 |
|
|
def save_float_to_raw(a, name, index=None):
|
105 |
|
|
if index:
|
106 |
|
|
file_name = name + '_' + str(index) + '.raw'
|
107 |
|
|
else:
|
108 |
|
|
file_name = name + '.raw'
|
109 |
|
|
print('save_float_to_raw() |', file_name)
|
110 |
|
|
a = a.astype('float32')
|
111 |
|
|
with open(file_name, "bw") as fh:
|
112 |
|
|
a.flatten().tofile(fh)
|
113 |
|
|
|
114 |
|
|
# -------------------------------------------------------
|
115 |
|
|
def write_conv2d_kernel(layer, dir='weights'):
|
116 |
|
|
w = layer.get_weights()
|
117 |
|
|
for i in range(layer.input_shape[3]):
|
118 |
|
|
for o in range(layer.output_shape[3]):
|
119 |
|
|
file_name = dir + '/' + layer.name + '-' + str(i) + '_' + str(o) + '.txt'
|
120 |
|
|
print(file_name)
|
121 |
|
|
with open(file_name, "w") as text_file:
|
122 |
|
|
for y in range(0, 3):
|
123 |
|
|
for x in range(0, 3):
|
124 |
|
|
print(float_to_hex(w[0][x,y,i,o]), file=text_file)
|
125 |
|
|
|
126 |
|
|
# -------------------------------------------------------
|
127 |
|
|
def write_dense_weights(layer, dir='weights'):
|
128 |
|
|
w = layer.get_weights()
|
129 |
|
|
for y in range(0, w[0].shape[1]):
|
130 |
|
|
file_name = dir + '/' + layer.name + '_' + str(y) + '.txt'
|
131 |
|
|
print(file_name)
|
132 |
|
|
with open(file_name, "w") as text_file:
|
133 |
|
|
for x in range(0, w[0].shape[0]):
|
134 |
|
|
print(float_to_hex(w[0][x][y]), file=text_file)
|
135 |
|
|
print(float_to_hex(w[1][y]), file=text_file)
|
136 |
|
|
|
137 |
|
|
|