Ejemplo n.º 1
0
    def export_weights(self):
        """
        Function to store TensorFlow weights back to into a dict in CoreML format to be used
        by the C++ implementation

        Returns
        -------
        tf_export_params: Dictionary
            Dictionary of weights from TensorFlow stored as {weight_name: weight_value}
        """
        tf_export_params = {}
        with self.ac_graph.as_default():
            tvars = _tf.trainable_variables()
            tvars_vals = self.sess.run(tvars)

        for var, val in zip(tvars, tvars_vals):
            if 'weight' in var.name:
                if var.name.startswith('conv'):

                    tf_export_params[var.name.split(
                        ':')[0]] = _utils.convert_conv1d_tf_to_coreml(val)
                elif var.name.startswith('dense'):
                    tf_export_params[var.name.split(
                        ':')[0]] = _utils.convert_dense_tf_to_coreml(val)
            elif var.name.startswith('rnn/lstm_cell/kernel'):
                i2h_i, i2h_c, i2h_f, i2h_o, h2h_i, h2h_c, h2h_f, h2h_o = _utils.convert_lstm_weight_tf_to_coreml(
                    val, CONV_H)
                tf_export_params['lstm_i2h_i_weight'] = i2h_i
                tf_export_params['lstm_i2h_c_weight'] = i2h_c
                tf_export_params['lstm_i2h_f_weight'] = i2h_f
                tf_export_params['lstm_i2h_o_weight'] = i2h_o
                tf_export_params['lstm_h2h_i_weight'] = h2h_i
                tf_export_params['lstm_h2h_c_weight'] = h2h_c
                tf_export_params['lstm_h2h_f_weight'] = h2h_f
                tf_export_params['lstm_h2h_o_weight'] = h2h_o
            elif var.name.startswith('rnn/lstm_cell/bias'):
                h2h_i_bias, h2h_c_bias, h2h_f_bias, h2h_o_bias = _utils.convert_lstm_bias_tf_to_coreml(
                    val)
                tf_export_params['lstm_h2h_i_bias'] = h2h_i_bias
                tf_export_params['lstm_h2h_c_bias'] = h2h_c_bias
                tf_export_params['lstm_h2h_f_bias'] = h2h_f_bias
                tf_export_params['lstm_h2h_o_bias'] = h2h_o_bias
            elif var.name.startswith('batch_normalization'):
                tf_export_params[
                    'bn_' + var.name.split('/')[-1][0:-2]] = _np.array(val)
            else:
                tf_export_params[var.name.split(':')[0]] = _np.array(val)

        tvars = _tf.global_variables()
        tvars_vals = self.sess.run(tvars)
        for var, val in zip(tvars, tvars_vals):
            if 'moving_mean' in var.name:
                tf_export_params['bn_running_mean'] = _np.array(val)
            if 'moving_variance' in var.name:
                tf_export_params['bn_running_var'] = _np.array(val)
        for layer_name in tf_export_params.keys():
            tf_export_params[layer_name] = _np.ascontiguousarray(
                tf_export_params[layer_name])
        return tf_export_params
Ejemplo n.º 2
0
 def export_weights(self):
     tf_export_params = {}
     tvars = _tf.trainable_variables()
     tvars_vals = self.sess.run(tvars)
     
     for var, val in zip(tvars, tvars_vals):
         if 'weight' in var.name:
             if 'conv' in var.name:
                 tf_export_params[var.name.split(':')[0]] = _utils.convert_conv2d_tf_to_coreml(val)
             else:
                 tf_export_params[var.name.split(':')[0]] =  _utils.convert_dense_tf_to_coreml(val)
         else:
             tf_export_params[var.name.split(':')[0]] = _np.array(val)
     for layer_name in tf_export_params.keys():
         tf_export_params[layer_name] = _np.ascontiguousarray(tf_export_params[layer_name])
     return tf_export_params
Ejemplo n.º 3
0
    def export_weights(self):
        _tf = _lazy_import_tensorflow()

        tf_export_params = {}

        with self.st_graph.as_default():
            tvars = _tf.trainable_variables()
            tvars_vals = self.sess.run(tvars)

        for var, val in zip(tvars, tvars_vals):
            if "weight" in var.name:
                if "conv" in var.name:
                    tf_export_params[var.name.split(
                        ":")[0]] = _utils.convert_conv2d_tf_to_coreml(val)
                else:
                    tf_export_params[var.name.split(
                        ":")[0]] = _utils.convert_dense_tf_to_coreml(val)
            else:
                tf_export_params[var.name.split(":")[0]] = _np.array(val)
        for layer_name in tf_export_params.keys():
            tf_export_params[layer_name] = _np.ascontiguousarray(
                tf_export_params[layer_name])
        return tf_export_params
Ejemplo n.º 4
0
    def export_weights(self):
        """
        Function to store TensorFlow weights back to into a dict in CoreML format to be used
        by the C++ implementation

        Returns
        -------
        tf_export_params: Dictionary
            Dictionary of weights from TensorFlow stored as {weight_name: weight_value}
        """
        _tf = _lazy_import_tensorflow()
        tf_export_params = {}
        with self.ac_graph.as_default():
            tvars = _tf.trainable_variables()
            tvars_vals = self.sess.run(tvars)

        for var, val in zip(tvars, tvars_vals):
            if "weight" in var.name:
                if var.name.startswith("conv"):

                    tf_export_params[var.name.split(
                        ":")[0]] = _utils.convert_conv1d_tf_to_coreml(val)
                elif var.name.startswith("dense"):
                    tf_export_params[var.name.split(
                        ":")[0]] = _utils.convert_dense_tf_to_coreml(val)
            elif var.name.startswith("rnn/lstm_cell/kernel"):
                (
                    i2h_i,
                    i2h_c,
                    i2h_f,
                    i2h_o,
                    h2h_i,
                    h2h_c,
                    h2h_f,
                    h2h_o,
                ) = _utils.convert_lstm_weight_tf_to_coreml(val, CONV_H)
                tf_export_params["lstm_i2h_i_weight"] = i2h_i
                tf_export_params["lstm_i2h_c_weight"] = i2h_c
                tf_export_params["lstm_i2h_f_weight"] = i2h_f
                tf_export_params["lstm_i2h_o_weight"] = i2h_o
                tf_export_params["lstm_h2h_i_weight"] = h2h_i
                tf_export_params["lstm_h2h_c_weight"] = h2h_c
                tf_export_params["lstm_h2h_f_weight"] = h2h_f
                tf_export_params["lstm_h2h_o_weight"] = h2h_o
            elif var.name.startswith("rnn/lstm_cell/bias"):
                (
                    h2h_i_bias,
                    h2h_c_bias,
                    h2h_f_bias,
                    h2h_o_bias,
                ) = _utils.convert_lstm_bias_tf_to_coreml(val)
                tf_export_params["lstm_h2h_i_bias"] = h2h_i_bias
                tf_export_params["lstm_h2h_c_bias"] = h2h_c_bias
                tf_export_params["lstm_h2h_f_bias"] = h2h_f_bias
                tf_export_params["lstm_h2h_o_bias"] = h2h_o_bias
            elif var.name.startswith("batch_normalization"):
                tf_export_params[
                    "bn_" + var.name.split("/")[-1][0:-2]] = _np.array(val)
            else:
                tf_export_params[var.name.split(":")[0]] = _np.array(val)

        tvars = _tf.global_variables()
        tvars_vals = self.sess.run(tvars)
        for var, val in zip(tvars, tvars_vals):
            if "moving_mean" in var.name:
                tf_export_params["bn_running_mean"] = _np.array(val)
            if "moving_variance" in var.name:
                tf_export_params["bn_running_var"] = _np.array(val)
        for layer_name in tf_export_params.keys():
            tf_export_params[layer_name] = _np.ascontiguousarray(
                tf_export_params[layer_name])
        return tf_export_params