def extract_flatten_output(): conv = model.get_layer(index=0) maxPool = model.get_layer(index=1) flatten = model.get_layer(index=2) input_img = x_test[:5] #7 2 1 0 4 conv_output = conv(input_img) maxPool_output = maxPool(conv_output) flatten_output = flatten(maxPool_output) flatten_output_7 = flatten_output[0] tool.write_to_file(flatten_output_7, path + "flatten_output_7.csv", 1)
def extract_conv_weights(): # extract conv bias conv_bias = conv.weights[1] tool.write_to_file(conv_bias.numpy(), path + "conv_bias.csv", 1) # extract conv weights conv_weights = conv.weights[0] conv0_weights = np.empty((5, 5), dtype=float) conv1_weights = np.empty((5, 5), dtype=float) conv2_weights = np.empty((5, 5), dtype=float) for i in range(5): for j in range(5): conv0_weights[i][j] = conv_weights[i][j][0][0] conv1_weights[i][j] = conv_weights[i][j][0][1] conv2_weights[i][j] = conv_weights[i][j][0][2] tool.write_to_file(conv0_weights, path + "conv0_weights.csv", 2) tool.write_to_file(conv1_weights, path + "conv1_weights.csv", 2) tool.write_to_file(conv2_weights, path + "conv2_weights.csv", 2)
def extract_conv_output(): #extract conv output # get 5 images from test and print label input_img = x_test[:5] #7 2 1 0 4 conv = model.get_layer(index=0) conv_output = conv(input_img) conv_output_7 = conv_output[0] conv0_output_7 = np.empty((24, 24), dtype=float) conv1_output_7 = np.empty((24, 24), dtype=float) conv2_output_7 = np.empty((24, 24), dtype=float) for i in range(24): for j in range(24): conv0_output_7[i][j] = conv_output_7[i][j][0] conv1_output_7[i][j] = conv_output_7[i][j][1] conv2_output_7[i][j] = conv_output_7[i][j][2] tool.write_to_file(conv0_output_7, path + "conv0_output_7.csv", 2) tool.write_to_file(conv1_output_7, path + "conv1_output_7.csv", 2) tool.write_to_file(conv2_output_7, path + "conv2_output_7.csv", 2)
def extract_maxPool_output(): #extract maxPool output import numpy as np input_img = x_test[:5] #7 2 1 0 4 conv = model.get_layer(index=0) conv_output = conv(input_img) maxPool = model.get_layer(index=1) maxPool_output = maxPool(conv_output) maxPool_output_7 = maxPool_output[0] maxPool0_output_7 = np.empty((12, 12), dtype=float) maxPool1_output_7 = np.empty((12, 12), dtype=float) maxPool2_output_7 = np.empty((12, 12), dtype=float) for i in range(12): for j in range(12): maxPool0_output_7[i][j] = maxPool_output_7[i][j][0] maxPool1_output_7[i][j] = maxPool_output_7[i][j][1] maxPool2_output_7[i][j] = maxPool_output_7[i][j][2] tool.write_to_file(maxPool0_output_7, path + "maxPool0_output_7.csv", 2) tool.write_to_file(maxPool1_output_7, path + "maxPool1_output_7.csv", 2) tool.write_to_file(maxPool2_output_7, path + "maxPool2_output_7.csv", 2)
if neuron_result > 0: return neuron_result else: return 0.0 def conv(matrix, bias, filter): print("now is filter " + str(filter) + " with bias: " + str(bias)) conv_result = np.empty((24, 24), dtype=float) conv_weights = np.empty((24, 24), dtype=float) if filter == 0: print("conv0") conv_weights = conv0_weights elif filter == 1: print("conv1") conv_weights = conv1_weights else: print("conv2") conv_weights = conv2_weights for i in range(24): for j in range(24): conv_matrix = getFilter(matrix, i, j, 5, 5) conv_result[i][j] = neuron(conv_matrix, conv_weights, 5, 5, bias) return conv_result tool.write_to_file(conv(x_test[0], bias[0], 0), path + "conv0_manual_output_7.csv", 2) tool.write_to_file(conv(x_test[0], bias[1], 1), path + "conv1_manual_output_7.csv", 2) tool.write_to_file(conv(x_test[0], bias[2], 2), path + "conv2_manual_output_7.csv", 2)
# this file is to extract model parameter and write it into csv file import tensorflow as tf import extract_tools as tool path = tool.ann_path # load model from h5 file model = tf.keras.models.load_model('./tensorflow/resources/chisel_ann.h5') # get layers flatten = model.get_layer(index=0) dense = model.get_layer(index=1) dense1 = model.get_layer(index=2) # write layers bias and weights to csv file(flatten doesn't need) tool.write_to_file(dense.weights[1].numpy(), path + 'dense_weights_bias.csv', 1) tool.write_to_file(dense.weights[0].numpy(), path + 'dense_weights.csv', 2) tool.write_to_file(dense1.weights[1].numpy(), path + 'dense1_weights_bias.csv', 1) tool.write_to_file(dense1.weights[0].numpy(), path + 'dense1_weights.csv', 2)
def extract_dense_weights(): dense_weights = dense.weights[0] dense_bias = dense.weights[1] tool.write_to_file(dense_bias.numpy(), path + "dense_bias.csv", 1) tool.write_to_file(dense_weights.numpy(), path + "dense_weights.csv", 2)
model = tf.keras.models.load_model(path + 'chisel_ann.h5') print(model.summary()) # get layers flatten = model.get_layer(index=0) dense = model.get_layer(index=1) dense1 = model.get_layer(index=2) # get 5 images from test and print label input_img = tf.convert_to_tensor(x_test[:5]) for i in range(0, 5): print(y_test[i]) # get flatten layer output flatten_output = flatten(input_img) tool.write_to_file(flatten_output[0].numpy(), path + 'flatten_output_7.csv', 1) tool.write_to_file(flatten_output[1].numpy(), path + 'flatten_output_2.csv', 1) tool.write_to_file(flatten_output[2].numpy(), path + 'flatten_output_1.csv', 1) tool.write_to_file(flatten_output[3].numpy(), path + 'flatten_output_0.csv', 1) tool.write_to_file(flatten_output[4].numpy(), path + 'flatten_output_4.csv', 1) # get dense layer output dense_output = dense(flatten_output) tool.write_to_file(dense_output[0].numpy(), path + 'dense_output_7.csv', 1) tool.write_to_file(dense_output[1].numpy(), path + 'dense_output_2.csv', 1) tool.write_to_file(dense_output[2].numpy(), path + 'dense_output_1.csv', 1) tool.write_to_file(dense_output[3].numpy(), path + 'dense_output_0.csv', 1) tool.write_to_file(dense_output[4].numpy(), path + 'dense_output_4.csv', 1) # get dense1 layer output dense1_output = dense1(dense_output)