コード例 #1
0
def write_weight_Conv2D_cpp(weight, bias, file_name, weight_array_name, bias_array_name, type_name, isFixed=False, fractal_width=0, array_type=None):
    # Weight generated by Keras has 4D array with (height, width, input_depth, output_depth)
    # Transpose weight axis from (height, width, input_depth, output_depth) to (output_depth, input_depth ,height, width)

    with open(file_name, 'w') as f:

        # reshape weight array
        weight = weight.transpose(3, 2, 0, 1)
        if isFixed is True:
            weight = float2fixed.float2fixed_array(array_type, fractal_width, weight)
            bias = float2fixed.float2fixed_array(array_type, fractal_width, bias)

        # headers
        todaytime = str(datetime.datetime.today())
        f.write("/*\n")
        f.write(" * author : shtsno24\n")
        f.write(" * Date : " + todaytime + "\n")
        if isFixed is True:
            f.write(str(" * array_type : {}\n").format(weight.dtype))
            f.write(str(" * fractal_width : {} bit\n").format(fractal_width))
            f.write(str(" * bit_width : {} bit\n").format(str(8 * np.dtype(array_type).itemsize)))
        f.write(" *\n */\n")

        # include <cstdint> and <vector>
        f.write("#pragma once\n")
        f.write("#include <cstdint>\n")
        f.write("#include <vector>\n\n")
        f.write("using namespace std;\n\n")

        # define data_width
        if isFixed is True:
            f.write(str("#define data_width_{} {}\n").format(str(weight_array_name[:-2]), str(8 * np.dtype(array_type).itemsize)))
            f.write(str("#define fractal_width_{} {}\n\n").format(str(weight_array_name[:-2]), str(fractal_width)))

        # weights
        f.write(str("const vector< uint16_t> shape_{}_w = ").format(str(weight_array_name[:-2])))
        f.write("{%d, %d, %d, %d};\n" % (weight.shape[0], weight.shape[1], weight.shape[2], weight.shape[3]))

        f.write("const vector< vector< vector< vector< " + type_name + "> > > > " + weight_array_name + " =\n")

        write_array_ND(weight, f)
        f.write(";")
        f.write("\n\n")

        # bias
        f.write(str("const uint16_t shape_{}_b = {};\n").format(str(weight_array_name[:-2]), bias.shape[0]))

        f.write("const vector< " + type_name + "> " + bias_array_name + " = ")
        # f.write(" = " % bias.shape)

        write_array_ND(bias, f)
        f.write(";")
        f.write("\n")
コード例 #2
0
ファイル: tsv2array.py プロジェクト: shtsno24/DeepCAE
import numpy as np
from keras_mnist_DCAE import float2fixed
import keras_weight_generator
import os

if os.path.isdir("test_data") is False:
    os.mkdir("test_data")

tsv_file = input("file name : ")
tsv_array = np.loadtxt(tsv_file + ".tsv", dtype=np.float32, delimiter='\t')
tsv_array = tsv_array.reshape((1,) + tsv_array.shape)
print(tsv_array.shape, tsv_array.dtype)

tsv_array_fix16 = float2fixed.float2fixed_array(np.int16, 16-3, tsv_array)
print(tsv_array_fix16.shape, tsv_array_fix16.dtype, tsv_array_fix16)

with open("test_data/test_data.h", "w") as f:
    f.write("#include <stdint.h>\n\n")
    f.write(str("int16_t test_input_fix16[{}][{}][{}] = ").format(tsv_array_fix16.shape[0], tsv_array_fix16.shape[1], tsv_array_fix16.shape[2]))
    keras_weight_generator.write_array_ND(tsv_array_fix16, f)
    f.write(";\n\n")

    f.write(str("float test_input_float32[{}][{}][{}] = ").format(tsv_array.shape[0], tsv_array.shape[1], tsv_array.shape[2]))
    keras_weight_generator.write_array_ND(tsv_array, f)
    f.write(";\n\n")
コード例 #3
0
def write_weight_SeparableConv2D_c(weight_depthwise, weight_pointwise, bias, file_name, weight_array_name, bias_array_name, type_name, isFixed=False, fractal_width=0, array_type=None):
    # Weight generated by Keras has 4D array with (height, width, input_depth, output_depth)
    # Transpose weight axis from (height, width, input_depth, output_depth) to (output_depth, input_depth ,height, width)

    with open(file_name, 'w') as f:

        # reshape weight array
        weight_depthwise = weight_depthwise.transpose(3, 2, 0, 1)
        weight_pointwise = weight_pointwise.transpose(3, 2, 0, 1)
        bias_pointwise = np.copy(bias)

        if isFixed is True:
            weight_depthwise = float2fixed.float2fixed_array(array_type, fractal_width, weight_depthwise)
            weight_pointwise = float2fixed.float2fixed_array(array_type, fractal_width, weight_pointwise)
            bias_pointwise = float2fixed.float2fixed_array(array_type, fractal_width, bias_pointwise)

        # bias_depthwise = np.zeros((bias.shape), dtype=bias_pointwise.dtype)
        bias_depthwise = np.zeros((weight_pointwise.shape[1],), dtype=bias_pointwise.dtype)

        # headers
        todaytime = str(datetime.datetime.today())
        f.write("/*\n")
        f.write(" * author : shtsno24\n")
        f.write(" * Date : " + todaytime + "\n")
        if isFixed is True:
            f.write(str(" * array_type : {}\n").format(weight_depthwise.dtype))
            f.write(str(" * fractal_width : {} bit\n").format(fractal_width))
            f.write(str(" * bit_width : {} bit\n").format(str(8 * np.dtype(array_type).itemsize)))
        f.write(" *\n */\n")

        # include <stdint.h>
        f.write(str("#pragma once\n"))
        f.write(str("#include <stdint.h>\n\n"))

        # define data_width
        if isFixed is True:
            f.write(str("#define data_width_{} {}\n").format(str(weight_array_name[:-2]), str(8 * np.dtype(array_type).itemsize)))
            f.write(str("#define fractal_width_{} {}\n\n").format(str(weight_array_name[:-2]), str(fractal_width)))

        # weights(depthwise)
        f.write(str("const uint16_t shape_{}_w_d[] = ").format(str(weight_array_name[:-2])))
        f.write("{%d, %d, %d, %d};\n" % (weight_depthwise.shape[0], weight_depthwise.shape[1], weight_depthwise.shape[2], weight_depthwise.shape[3]))

        f.write("const " + type_name + " " + weight_array_name + "_d")
        f.write(str("[{}][{}][{}][{}] =\n").format(weight_depthwise.shape[0], weight_depthwise.shape[1], weight_depthwise.shape[2], weight_depthwise.shape[3]))

        write_array_ND(weight_depthwise, f)
        f.write(";")
        f.write("\n\n")

        # weights(pointwise)
        f.write(str("const uint16_t shape_{}_w_p[] = ").format(str(weight_array_name[:-2])))
        f.write("{%d, %d, %d, %d};\n" % (weight_pointwise.shape[0], weight_pointwise.shape[1], weight_pointwise.shape[2], weight_pointwise.shape[3]))

        f.write("const " + type_name + " " + weight_array_name + "_p")
        # f.write(str("[{}][{}][{}][{}] =\n").format(weight_pointwise.shape[0], weight_pointwise.shape[1], weight_pointwise.shape[2], weight_pointwise.shape[3]))
        f.write(str("[{}] =\n").format(weight_pointwise.shape[0] * weight_pointwise.shape[1] * weight_pointwise.shape[2] * weight_pointwise.shape[3]))

        write_array_ND(weight_pointwise.reshape(-1), f)
        f.write(";")
        f.write("\n\n")

        # bias(depthwise)
        f.write(str("const uint16_t shape_{}_b_d = {};\n").format(str(weight_array_name[:-2]), bias_depthwise.shape[0]))

        f.write("const " + type_name + " " + bias_array_name + "_d")
        f.write("[%d] = " % bias_depthwise.shape)

        write_array_ND(bias_depthwise, f)
        f.write(";")
        f.write("\n")

        # bias(pointwise)
        f.write(str("const uint16_t shape_{}_b_p = {};\n").format(str(weight_array_name[:-2]), bias.shape[0]))

        f.write("const " + type_name + " " + bias_array_name + "_p")
        f.write("[%d] = " % bias_pointwise.shape)

        write_array_ND(bias_pointwise, f)
        f.write(";")
        f.write("\n")