TrainingExtensions/tensorflow/src/python/aimet_tensorflow/keras/batch_norm_fold.py
def _get_weight_tensor_transpose_reshape(conv_linear: LAYER_TYPE) -> libpymo.TensorParams():
    """
    Get weight tensor from conv layer.

    Converts to right format - performs transpose and reshape.
    Packs it to the format required for BN fold (libpymo.TensorParams()).
    :param conv_linear: conv layer
    :return: return weight tensor in libpymo.TensorParams() format.
    """

    # Weight tensor libpymo format
    weight_tensor = libpymo.TensorParams()

    # linear array to be sent for bn fold
    weight = conv_linear.kernel.numpy()
    shape = conv_linear.kernel.shape

    if isinstance(conv_linear, tf.keras.layers.DepthwiseConv2D):
        # Depthwise conv layers in TF have outputs(Noc) set to 1.
        # we will use format [Nic, Noc, kh, kw] -
        # to be compatible with cpp backend.
        weight = np.transpose(weight, (2, 3, 0, 1))
        # [Nic, Noc, kh, kw]
        shape = np.array([shape[2], shape[3], shape[0], shape[1]])
    elif isinstance(conv_linear, tf.keras.layers.Dense):
        shape = np.concatenate((np.array([1, 1]), shape))
        weight = np.transpose(weight, (1, 0))
        # [Noc, Nic, kh, kw]
        shape = np.array([shape[3], shape[2], shape[0], shape[1]])
    elif isinstance(conv_linear, tf.keras.layers.Conv2D):
        weight = np.transpose(weight, (3, 2, 0, 1))
        # [Noc, Nic, kh, kw]
        shape = np.array([shape[3], shape[2], shape[0], shape[1]])
    else:
        logger.error("_get_weight_tensor_transpose_reshape(): Operation type unsupported")

    weight_tensor.data = weight.reshape(-1)
    weight_tensor.shape = shape

    return weight_tensor

    Get weight tensor from conv layer.

    Converts to right format - performs transpose and reshape.
    Packs it to the format required for BN fold (libpymo.TensorParams()).
    :param conv_linear: conv layer
    :return: return weight tensor in libpymo.TensorParams() format.
    
Get weight tensor from conv layer.
Converts to right format - performs transpose and reshape.
Packs it to the format required for BN fold (libpymo.TensorParams()).
Get weight tensor from conv layer . Converts to right format - performs transpose and reshape . Packs it to the format required for BN fold ( libpymo . TensorParams () ) .
def _get_weight_tensor_transpose_reshape(conv_linear: LAYER_TYPE) -> libpymo.TensorParams():
    weight_tensor = libpymo.TensorParams()
    weight = conv_linear.kernel.numpy()
    shape = conv_linear.kernel.shape
    if isinstance(conv_linear, tf.keras.layers.DepthwiseConv2D):
        weight = np.transpose(weight, (2, 3, 0, 1))
        shape = np.array([shape[2], shape[3], shape[0], shape[1]])
    elif isinstance(conv_linear, tf.keras.layers.Dense):
        shape = np.concatenate((np.array([1, 1]), shape))
        weight = np.transpose(weight, (1, 0))
        shape = np.array([shape[3], shape[2], shape[0], shape[1]])
    elif isinstance(conv_linear, tf.keras.layers.Conv2D):
        weight = np.transpose(weight, (3, 2, 0, 1))
        shape = np.array([shape[3], shape[2], shape[0], shape[1]])
    else:
        logger.error("_get_weight_tensor_transpose_reshape(): Operation type unsupported")
    weight_tensor.data = weight.reshape(-1)
    weight_tensor.shape = shape
    return weight_tensor
def _get_weight_tensor_transpose_reshape ( conv_linear : LAYER_TYPE ) -> libpymo . TensorParams ( ) : weight_tensor = libpymo . TensorParams ( ) weight = conv_linear . kernel . numpy ( ) shape = conv_linear . kernel . shape if isinstance ( conv_linear , tf . keras . layers . DepthwiseConv2D ) : weight = np . transpose ( weight , ( 2 , 3 , 0 , 1 ) ) shape = np . array ( [ shape [ 2 ] , shape [ 3 ] , shape [ 0 ] , shape [ 1 ] ] ) elif isinstance ( conv_linear , tf . keras . layers . Dense ) : shape = np . concatenate ( ( np . array ( [ 1 , 1 ] ) , shape ) ) weight = np . transpose ( weight , ( 1 , 0 ) ) shape = np . array ( [ shape [ 3 ] , shape [ 2 ] , shape [ 0 ] , shape [ 1 ] ] ) elif isinstance ( conv_linear , tf . keras . layers . Conv2D ) : weight = np . transpose ( weight , ( 3 , 2 , 0 , 1 ) ) shape = np . array ( [ shape [ 3 ] , shape [ 2 ] , shape [ 0 ] , shape [ 1 ] ] ) else : logger . error ( "_get_weight_tensor_transpose_reshape(): Operation type unsupported" ) weight_tensor . data = weight . reshape ( - 1 ) weight_tensor . shape = shape return weight_tensor
Get weight tensor from conv layer.
Get weight tensor from conv layer .
"""
    Get weight tensor from conv layer.

    Converts to right format - performs transpose and reshape.
    Packs it to the format required for BN fold (libpymo.TensorParams()).
    :param conv_linear: conv layer
    :return: return weight tensor in libpymo.TensorParams() format.
    """ # Weight tensor libpymo format # linear array to be sent for bn fold # Depthwise conv layers in TF have outputs(Noc) set to 1. # we will use format [Nic, Noc, kh, kw] - # to be compatible with cpp backend. # [Nic, Noc, kh, kw] # [Noc, Nic, kh, kw] # [Noc, Nic, kh, kw]
return weight tensor in libpymo.TensorParams() format.
return weight tensor in libpymo . TensorParams () format .
conv_linear
LAYER_TYPE
None