xref: /aosp_15_r20/external/libopus/dnn/training_tf2/rdovae_import.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('input', metavar="<input folder>", type=str, help='input exchange folder')
40parser.add_argument('weights', metavar="<weight file>", type=str, help='model weight file in hdf5 format')
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 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
79if __name__ == "__main__":
80
81    model, encoder, decoder, qembedding = new_rdovae_model(20, args.latent_dim, cond_size=args.cond_size, nb_quant=args.quant_levels)
82
83    encoder_layers = [
84        'enc_dense1',
85        'enc_dense3',
86        'enc_dense5',
87        'enc_dense7',
88        'enc_dense8',
89        'gdense1',
90        'gdense2',
91        'enc_dense2',
92        'enc_dense4',
93        'enc_dense6',
94        'bits_dense'
95    ]
96
97    decoder_layers = [
98        'state1',
99        'state2',
100        'state3',
101        'dec_dense1',
102        'dec_dense3',
103        'dec_dense5',
104        'dec_dense7',
105        'dec_dense8',
106        'dec_final',
107        'dec_dense2',
108        'dec_dense4',
109        'dec_dense6'
110    ]
111
112    for name in encoder_layers:
113        print(f"loading weight for layer {name}...")
114        load_tf_weights(os.path.join(args.input, exchange_name[name]), encoder.get_layer(name))
115
116    print(f"loading weight for layer qembedding...")
117    load_tf_weights(os.path.join(args.input, exchange_name['qembedding']), qembedding)
118
119    for name in decoder_layers:
120        print(f"loading weight for layer {name}...")
121        load_tf_weights(os.path.join(args.input, exchange_name[name]), decoder.get_layer(name))
122
123    model.save(args.weights)
124