xref: /aosp_15_r20/external/libopus/dnn/training_tf2/rdovae_exchange.py (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1"""
2/* Copyright (c) 2022 Amazon
3   Written by Jan Buethe */
4/*
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   - Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11
12   - Redistributions in binary form must reproduce the above copyright
13   notice, this list of conditions and the following disclaimer in the
14   documentation and/or other materials provided with the distribution.
15
16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28"""
29
30
31import argparse
32import os
33import sys
34
35os.environ['CUDA_VISIBLE_DEVICES'] = ""
36
37parser = argparse.ArgumentParser()
38
39parser.add_argument('weights', metavar="<weight file>", type=str, help='model weight file in hdf5 format')
40parser.add_argument('output', metavar="<output folder>", type=str, help='output exchange folder')
41parser.add_argument('--cond-size', type=int, help="conditioning size (default: 256)", default=256)
42parser.add_argument('--latent-dim', type=int, help="dimension of latent space (default: 80)", default=80)
43parser.add_argument('--quant-levels', type=int, help="number of quantization steps (default: 16)", default=16)
44
45args = parser.parse_args()
46
47# now import the heavy stuff
48from rdovae import new_rdovae_model
49from wexchange.tf import dump_tf_weights, load_tf_weights
50
51
52exchange_name = {
53    'enc_dense1'    : 'encoder_stack_layer1_dense',
54    'enc_dense3'    : 'encoder_stack_layer3_dense',
55    'enc_dense5'    : 'encoder_stack_layer5_dense',
56    'enc_dense7'    : 'encoder_stack_layer7_dense',
57    'enc_dense8'    : 'encoder_stack_layer8_dense',
58    'gdense1'       : 'encoder_state_layer1_dense',
59    'gdense2'       : 'encoder_state_layer2_dense',
60    'enc_dense2'    : 'encoder_stack_layer2_gru',
61    'enc_dense4'    : 'encoder_stack_layer4_gru',
62    'enc_dense6'    : 'encoder_stack_layer6_gru',
63    'bits_dense'    : 'encoder_stack_layer9_conv',
64    'qembedding'    : 'statistical_model_embedding',
65    'state1'        : 'decoder_state1_dense',
66    'state2'        : 'decoder_state2_dense',
67    'state3'        : 'decoder_state3_dense',
68    'dec_dense1'    : 'decoder_stack_layer1_dense',
69    'dec_dense3'    : 'decoder_stack_layer3_dense',
70    'dec_dense5'    : 'decoder_stack_layer5_dense',
71    'dec_dense7'    : 'decoder_stack_layer7_dense',
72    'dec_dense8'    : 'decoder_stack_layer8_dense',
73    'dec_final'     : 'decoder_stack_layer9_dense',
74    'dec_dense2'    : 'decoder_stack_layer2_gru',
75    'dec_dense4'    : 'decoder_stack_layer4_gru',
76    'dec_dense6'    : 'decoder_stack_layer6_gru'
77}
78
79
80if __name__ == "__main__":
81
82    model, encoder, decoder, qembedding = new_rdovae_model(20, args.latent_dim, cond_size=args.cond_size, nb_quant=args.quant_levels)
83    model.load_weights(args.weights)
84
85    os.makedirs(args.output, exist_ok=True)
86
87    # encoder
88    encoder_dense_names = [
89        'enc_dense1',
90        'enc_dense3',
91        'enc_dense5',
92        'enc_dense7',
93        'enc_dense8',
94        'gdense1',
95        'gdense2'
96    ]
97
98    encoder_gru_names = [
99        'enc_dense2',
100        'enc_dense4',
101        'enc_dense6'
102    ]
103
104    encoder_conv1d_names = [
105        'bits_dense'
106    ]
107
108
109    for name in encoder_dense_names + encoder_gru_names + encoder_conv1d_names:
110        print(f"writing layer {exchange_name[name]}...")
111        dump_tf_weights(os.path.join(args.output, exchange_name[name]), encoder.get_layer(name))
112
113    # qembedding
114    print(f"writing layer {exchange_name['qembedding']}...")
115    dump_tf_weights(os.path.join(args.output, exchange_name['qembedding']), qembedding)
116
117    # decoder
118    decoder_dense_names = [
119        'state1',
120        'state2',
121        'state3',
122        'dec_dense1',
123        'dec_dense3',
124        'dec_dense5',
125        'dec_dense7',
126        'dec_dense8',
127        'dec_final'
128    ]
129
130    decoder_gru_names = [
131        'dec_dense2',
132        'dec_dense4',
133        'dec_dense6'
134    ]
135
136    for name in decoder_dense_names + decoder_gru_names:
137        print(f"writing layer {exchange_name[name]}...")
138        dump_tf_weights(os.path.join(args.output, exchange_name[name]), decoder.get_layer(name))
139