shithub: opus

Download patch

ref: ea4d8f54c3f0bd269ebd2ee97214d84d62628d81
parent: 50966eecc5024b3b648889319153d534a147cc71
author: Jan Buethe <jbuethe@amazon.de>
date: Wed Oct 19 13:18:25 EDT 2022

added statistical model to dump_nfec_model

--- a/dnn/training_tf2/dump_nfec_model.py
+++ b/dnn/training_tf2/dump_nfec_model.py
@@ -1,6 +1,7 @@
 import argparse
 import os
 
+
 parser = argparse.ArgumentParser()
 
 parser.add_argument('weights', metavar="<weight file>", type=str, help='model weight file in hdf5 format')
@@ -10,7 +11,8 @@
 args = parser.parse_args()
 
 # now import the heavy stuff
-from keraslayerdump import dump_conv1d_layer, dump_dense_layer, dump_gru_layer
+import tensorflow as tf
+from keraslayerdump import dump_conv1d_layer, dump_dense_layer, dump_gru_layer, printVector
 from rdovae import new_rdovae_model
 
 def start_header(header_fid, header_name):
@@ -47,6 +49,31 @@
     pass
 
 
+def dump_statistical_model(qembedding, f, fh):
+    w = qembedding.weights[0].numpy()
+    levels, dim = w.shape
+    N = dim // 6
+
+    quant_scales    = tf.math.softplus(w[:, : N]).numpy()
+    dead_zone_theta = 0.5 + 0.05 * tf.math.softplus(w[:, N : 2 * N]).numpy()
+    r               = 0.5 + 0.5 * tf.math.sigmoid(w[:, 4 * N : 5 * N]).numpy()
+    theta           = tf.math.sigmoid(w[:, 5 * N : 6 * N]).numpy()
+
+    printVector(f, quant_scales[:], 'nfec_stats_quant_scales')
+    printVector(f, dead_zone_theta[:], 'nfec_stats_dead_zone_theta')
+    printVector(f, r, 'nfec_stats_r')
+    printVector(f, theta, 'nfec_stats_theta')
+
+    fh.write(
+f"""
+extern float nfec_stats_quant_scales;
+extern float nfec_stats_dead_zone_theta;
+extern float nfec_stats_r;
+extern float nfec_stats_theta;
+
+"""
+    )
+
 if __name__ == "__main__":
 
     model, encoder, decoder, qembedding = new_rdovae_model(20, args.latent_dim, cond_size=args.cond_size)
@@ -53,7 +80,7 @@
     model.load_weights(args.weights)
 
 
-    # for the time being only dump encoder
+    # encoder
     encoder_dense_names = [
         'enc_dense1',
         'enc_dense3',
@@ -114,6 +141,29 @@
 
 """
     )
+
+    finish_header(header_fid)
+    finish_source(source_fid)
+
+    header_fid.close()
+    source_fid.close()
+
+    # statistical model
+    source_fid = open("nfec_stats_data.c", 'w')
+    header_fid = open("nfec_stats_data.h", 'w')
+
+    start_header(header_fid, "nfec_stats_data.h")
+    start_source(source_fid, "nfec_stats_data.h", os.path.basename(args.weights))
+
+    num_levels = qembedding.weights[0].shape[0]
+    header_fid.write(
+f"""
+#define NFEC_STATS_NUM_LEVELS {num_levels}
+
+"""
+    )
+
+    dump_statistical_model(qembedding, source_fid, header_fid)
 
     finish_header(header_fid)
     finish_source(source_fid)
--- a/dnn/training_tf2/keraslayerdump.py
+++ b/dnn/training_tf2/keraslayerdump.py
@@ -157,4 +157,4 @@
     hf.write('#define {}_STATE_SIZE ({}*{})\n'.format(name.upper(), weights[0].shape[1], (weights[0].shape[0]-1)))
     hf.write('#define {}_DELAY {}\n'.format(name.upper(), (weights[0].shape[0]-1)//2))
     hf.write('extern const Conv1DLayer {};\n\n'.format(name));
-    return max_conv_inputs
\ No newline at end of file
+    return max_conv_inputs
--