Exemple #1
0
    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True, w=None):
        # ch_in, ch_out, weights, kernel, stride, padding, groups
        super(tf_Conv, self).__init__()
        assert g == 1, "TF v2.2 Conv2D does not support 'groups' argument"
        assert isinstance(
            k, int), "Convolution with multiple kernels are not allowed."
        # TensorFlow convolution padding is inconsistent with PyTorch (e.g. k=3 s=2 'SAME' padding)
        # see https://stackoverflow.com/questions/52975843/comparing-conv2d-with-padding-between-tensorflow-and-pytorch

        conv = keras.layers.Conv2D(
            c2,
            k,
            s,
            'SAME' if s == 1 else 'VALID',
            use_bias=False,
            kernel_initializer=keras.initializers.Constant(
                w.conv.weight.permute(2, 3, 1, 0).numpy()))
        self.conv = conv if s == 1 else keras.Sequential(
            [tf_Pad(autopad(k, p)), conv])
        self.bn = tf_BN(w.bn) if hasattr(w, 'bn') else tf.identity

        # YOLOv5 activations
        if isinstance(w.act, nn.LeakyReLU):
            self.act = (lambda x: keras.activations.relu(x, alpha=0.1)
                        ) if act else tf.identity
        elif isinstance(w.act, nn.Hardswish):
            self.act = (lambda x: x * tf.nn.relu6(x + 3) * 0.166666667
                        ) if act else tf.identity
        elif isinstance(w.act, nn.SiLU):
            self.act = (
                lambda x: keras.activations.swish(x)) if act else tf.identity
 def __init__(
     self: tf_Conv,
     in_channels: int,
     out_channels: int,  # filters
     kernel_size: int = 1,
     strides: int = 1,
     padding: Optional[int] = None,
     groups: int = 1,
     activate: bool = True,
     module: Optional[nn.Module] = None
 ) -> None:
     super().__init__()
     assert module is not None
     # TF v2.2 Conv2D does not support 'groups' argument"
     assert groups == 1
     # Convolute multi kernels are not allowed
     assert isinstance(kernel_size, int)
     # Convolution
     # TensorFlow convolution padding is inconsistent
     # with PyTorch (e.g. k=3 s=2 'SAME' padding)
     # see https://stackoverflow.com/questions/52975843
     # /comparing-conv2d-with-padding-between-tensorflow-and-pytorch
     if strides == 1:
         padding_tf = 'SAME'
     else:
         padding_tf = 'VALID'
     kernel_initializer = Constant(
         module.conv.weight.permute(2, 3, 1, 0).numpy()
     )
     conv = Conv2D(
         filters=out_channels,
         kernel_size=kernel_size,
         strides=strides,
         padding=padding_tf,
         use_bias=False,
         kernel_initializer=kernel_initializer
     )
     if strides == 1:
         self.conv = conv
     else:
         self.conv = Sequential([
             tf_Pad(autopad(kernel_size, padding)),
             conv
         ])
     # batch normalization
     if hasattr(module, 'bn'):
         self.bn = tf_BN(module=module.bn)
     else:
         self.bn = tf.indentity
     # activation
     if not activate:
         self.act = tf.identity
     elif isinstance(module.act, nn.LeakyReLU):
         self.act = (lambda x: relu(x, alpha=0.1))
     elif isinstance(module.act, nn.Hardswish):
         self.act = (lambda x: x * tf.nn.relu6(x + 3) * 0.166666667)
     elif isinstance(module.act, nn.SiLU):
         # self.act = (lambda x: x * keras.activations.sigmoid(x))
         self.act = (lambda x: swish(x))
         return
Exemple #3
0
 def __init__(self, c1, c2, k=1, s=1, p=None, act=True, w=None):
     # ch_in, ch_out, weights, kernel, stride, padding, groups
     super().__init__()
     assert c2 % c1 == 0, f'TFDWConv() output={c2} must be a multiple of input={c1} channels'
     conv = keras.layers.DepthwiseConv2D(
         kernel_size=k,
         depth_multiplier=c2 // c1,
         strides=s,
         padding='SAME' if s == 1 else 'VALID',
         use_bias=not hasattr(w, 'bn'),
         depthwise_initializer=keras.initializers.Constant(w.conv.weight.permute(2, 3, 1, 0).numpy()),
         bias_initializer='zeros' if hasattr(w, 'bn') else keras.initializers.Constant(w.conv.bias.numpy()))
     self.conv = conv if s == 1 else keras.Sequential([TFPad(autopad(k, p)), conv])
     self.bn = TFBN(w.bn) if hasattr(w, 'bn') else tf.identity
     self.act = activations(w.act) if act else tf.identity
Exemple #4
0
 def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True, w=None):
     # ch_in, ch_out, weights, kernel, stride, padding, groups
     super().__init__()
     assert g == 1, "TF v2.2 Conv2D does not support 'groups' argument"
     # TensorFlow convolution padding is inconsistent with PyTorch (e.g. k=3 s=2 'SAME' padding)
     # see https://stackoverflow.com/questions/52975843/comparing-conv2d-with-padding-between-tensorflow-and-pytorch
     conv = keras.layers.Conv2D(
         filters=c2,
         kernel_size=k,
         strides=s,
         padding='SAME' if s == 1 else 'VALID',
         use_bias=not hasattr(w, 'bn'),
         kernel_initializer=keras.initializers.Constant(w.conv.weight.permute(2, 3, 1, 0).numpy()),
         bias_initializer='zeros' if hasattr(w, 'bn') else keras.initializers.Constant(w.conv.bias.numpy()))
     self.conv = conv if s == 1 else keras.Sequential([TFPad(autopad(k, p)), conv])
     self.bn = TFBN(w.bn) if hasattr(w, 'bn') else tf.identity
     self.act = activations(w.act) if act else tf.identity