def __init__(self, in_planes, kernel_size, stride, pad_mode, pad, channel_multiplier=1, has_bias=False): super(DepthwiseConv, self).__init__() self.has_bias = has_bias self.in_channels = in_planes self.channel_multiplier = channel_multiplier self.out_channels = in_planes * channel_multiplier self.kernel_size = (kernel_size, kernel_size) self.depthwise_conv = P.DepthwiseConv2dNative( channel_multiplier=channel_multiplier, kernel_size=self.kernel_size, stride=stride, pad_mode=pad_mode, pad=pad) self.bias_add = P.BiasAdd() weight_shape = [channel_multiplier, in_planes, *self.kernel_size] self.weight = Parameter(initializer('ones', weight_shape), name='weight') if has_bias: bias_shape = [channel_multiplier * in_planes] self.bias = Parameter(initializer('zeros', bias_shape), name='bias') else: self.bias = None
def __init__(self, in_planes, kernel_size, stride): super(DepthWiseConv, self).__init__() platform = context.get_context("device_target") weight_shape = [1, kernel_size, in_planes] weight_init = _initialize_weight_goog(shape=weight_shape) if platform == "GPU": self.depthwise_conv = P.Conv2D(out_channel=in_planes*1, kernel_size=kernel_size, stride=stride, pad=int(kernel_size/2), pad_mode="pad", group=in_planes) self.weight = Parameter(initializer(weight_init, [in_planes*1, 1, kernel_size, kernel_size]), name='depthwise_weight') else: self.depthwise_conv = P.DepthwiseConv2dNative(channel_multiplier=1, kernel_size=kernel_size, stride=stride, pad_mode='pad', pad=int(kernel_size/2)) self.weight = Parameter(initializer(weight_init, [1, in_planes, kernel_size, kernel_size]), name='depthwise_weight')
def __init__(self, input_channel, kernel_size): super(DepthwiseConv2dAndReLU6, self).__init__() weight_shape = [1, input_channel, kernel_size, kernel_size] from mindspore.common.initializer import initializer self.weight = Parameter(initializer('ones', weight_shape), name='weight') self.depthwise_conv = P.DepthwiseConv2dNative(channel_multiplier=1, kernel_size=(kernel_size, kernel_size)) self.relu6 = nn.ReLU6()
def __init__(self, in_planes, kernel_size, stride, pad_mode, pad, channel_multiplier=1, has_bias=False): super(DepthWiseConv, self).__init__() self.has_bias = has_bias self.depthwise_conv = P.DepthwiseConv2dNative( channel_multiplier=channel_multiplier, kernel_size=kernel_size, stride=stride, pad_mode=pad_mode, pad=pad) self.bias_add = P.BiasAdd() weight_shape = [ channel_multiplier, in_planes, kernel_size[0], kernel_size[1] ] self.weight = Parameter(initializer('ones', weight_shape)) if has_bias: bias_shape = [channel_multiplier * in_planes] self.bias = Parameter(initializer('zeros', bias_shape)) else: self.bias = None
def __init__(self, in_channels, channel_multiplier, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, weight_init='normal'): kernel_size = twice(kernel_size) super(DepthwiseConv2dNative, self).__init__( in_channels, channel_multiplier, kernel_size, stride, pad_mode, padding, dilation, group, weight_init) self.depthwise_conv2d_native = P.DepthwiseConv2dNative(channel_multiplier=self.channel_multiplier, kernel_size=self.kernel_size, mode=3, pad_mode=self.pad_mode, pad=self.padding, stride=self.stride, dilation=self.dilation, group=self.group)
def __init__(self): super(Conv2dNativeNet, self).__init__() self.conv = P.DepthwiseConv2dNative(channel_multiplier=3, kernel_size=(3, 3)) self.flatten = P.Flatten() channel_multipliers = 1 in_channels = 3 kernel_size = (3, 3) self.weight = Parameter(initializer( Tensor(np.ones([channel_multipliers, in_channels, *kernel_size], dtype=np.float32)), [channel_multipliers, in_channels, *kernel_size]), name='weight')
def __init__(self, max_val=1.0, filter_size=11, filter_sigma=1.5, k1=0.01, k2=0.03): super(SSIM, self).__init__() validator.check_value_type('max_val', max_val, [int, float], self.cls_name) validator.check_number('max_val', max_val, 0.0, Rel.GT, self.cls_name) self.max_val = max_val self.filter_size = validator.check_integer('filter_size', filter_size, 1, Rel.GE, self.cls_name) self.filter_sigma = validator.check_float_positive('filter_sigma', filter_sigma, self.cls_name) validator.check_value_type('k1', k1, [float], self.cls_name) self.k1 = validator.check_number_range('k1', k1, 0.0, 1.0, Rel.INC_NEITHER, self.cls_name) validator.check_value_type('k2', k2, [float], self.cls_name) self.k2 = validator.check_number_range('k2', k2, 0.0, 1.0, Rel.INC_NEITHER, self.cls_name) self.mean = P.DepthwiseConv2dNative(channel_multiplier=1, kernel_size=filter_size)
def __init__(self, in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, weight_init='normal', bias_init='zeros'): super(DepthwiseConv2d, self).__init__() self.kernel_size = twice(kernel_size) self.stride = twice(stride) self.dilation = twice(dilation) self.in_channels = check_int_positive(in_channels) self.out_channels = check_int_positive(out_channels) validator.check_integer('group', group, in_channels, Rel.EQ) validator.check_integer('group', group, out_channels, Rel.EQ) validator.check_integer('group', group, 1, Rel.GE) self.pad_mode = pad_mode self.dilation = dilation self.group = group self.has_bias = has_bias self.weight_init = weight_init self.bias_init = bias_init Validator.check_value_type('padding', padding, (int, tuple), self.cls_name) if isinstance(padding, tuple): Validator.check_integer('padding size', len(padding), 4, Rel.EQ, self.cls_name) self.padding = padding self.conv = P.DepthwiseConv2dNative(channel_multiplier=1, kernel_size=self.kernel_size, pad_mode=self.pad_mode, pad=self.padding, stride=self.stride, dilation=self.dilation) self.bias_add = P.BiasAdd() weight_shape = [1, in_channels, *self.kernel_size] self.weight = Parameter(initializer(weight_init, weight_shape), name='weight') if check_bool(has_bias): self.bias = Parameter(initializer(bias_init, [out_channels]), name='bias') else: if bias_init != 'zeros': logger.warning( "value of `has_bias` is False, value of `bias_init` will be ignore." ) self.bias = None
def _init_depthwise_conv2d(self): """Init depthwise conv2d op""" if context.get_context("device_target") == "Ascend" and self.group > 1: self.dilation = self._dilation validator.check_integer('group', self.group, self.in_channels, Rel.EQ) validator.check_integer('group', self.group, self.out_channels, Rel.EQ) self.conv2d = P.DepthwiseConv2dNative(channel_multiplier=1, kernel_size=self.kernel_size, pad_mode=self.pad_mode, pad=self.padding, stride=self.stride, dilation=self.dilation) weight_shape = [1, self.in_channels, *self.kernel_size] self.weight = Parameter(initializer(self.weight_init, weight_shape), name='weight')
def _init_depthwise_conv2d(self): """Initialize depthwise conv2d op""" if context.get_context("device_target") == "Ascend" and self.group > 1: self.dilation = self._dilation Validator.check_equal_int(self.group, self.in_channels, 'group') Validator.check_equal_int(self.group, self.out_channels, 'group') self.conv2d = P.DepthwiseConv2dNative(channel_multiplier=1, kernel_size=self.kernel_size, pad_mode=self.pad_mode, pad=self.padding, stride=self.stride, dilation=self.dilation) weight_shape = [1, self.in_channels, *self.kernel_size] if isinstance(self.weight_init, Tensor): self.weight_init = Tensor(self.weight_init.asnumpy().swapaxes(0, 1), self.weight_init.dtype) if isinstance(self.weight_init, Initializer): self.weight_init.shape = weight_shape self.weight = Parameter(initializer(self.weight_init, weight_shape), name='weight')
def __init__(self, in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, has_bias=False, weight_init='normal', bias_init='zeros'): super(DepthwiseConv2d, self).__init__() self.kernel_size = twice(kernel_size) self.stride = twice(stride) self.dilation = twice(dilation) self.in_channels = check_int_positive(in_channels) self.out_channels = check_int_positive(out_channels) self.pad_mode = pad_mode self.padding = padding self.dilation = dilation self.has_bias = has_bias self.weight_init = weight_init self.bias_init = bias_init self.conv = P.DepthwiseConv2dNative(channel_multiplier=1, kernel_size=self.kernel_size, pad_mode=self.pad_mode, pad=self.padding, stride=self.stride, dilation=self.dilation) self.bias_add = P.BiasAdd() weight_shape = [1, in_channels, *self.kernel_size] self.weight = Parameter(initializer(weight_init, weight_shape), name='weight') if check_bool(has_bias): self.bias = Parameter(initializer(bias_init, [out_channels]), name='bias') else: if bias_init != 'zeros': logger.warning("value of `has_bias` is False, value of `bias_init` will be ignore.") self.bias = None
tensorToScalar = TensorToScalar() def get_tensor_to_scalar(logits, labels): return tensorToScalar(logits, labels) conv2d = P.Conv2D(64, (3, 3), pad_mode="pad", pad=1, stride=2) def get_conv2d(x, w): return conv2d(x, w) conv2dNative = P.DepthwiseConv2dNative(3, (3, 3), pad_mode="pad", pad=1, stride=2) def get_conv2d_native(x, w): return conv2dNative(x, w) biasAdd = P.BiasAdd() def get_bias_add(x, b): return biasAdd(x, b) def test_conv2d(out_channel, kernel_size, pad, stride, dilation):
('OnesLike', { 'block': P.OnesLike(), 'desc_inputs': [Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))], 'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))] }), ('ZerosLike', { 'block': P.ZerosLike(), 'desc_inputs': [Tensor(np.array([[0, 1], [2, 1]]).astype(np.int32))], 'desc_bprop': [Tensor(np.array([[1, 1], [1, 1]]).astype(np.int32))] }), ('Softmax', { 'block': P.Softmax(), 'desc_inputs': [[5, 5]], 'desc_bprop': [[5, 5]]}), ('DepthwiseConv2dNative_1', { 'block': P.DepthwiseConv2dNative(3, (3, 3), pad_mode="pad", pad=1, stride=2), 'desc_inputs': [[10, 32, 32, 32], [1, 32, 3, 3]], 'desc_bprop': [[10, 32, 16, 16]]}), ('DepthwiseConv2dNative_2', { 'block': P.DepthwiseConv2dNative(1, (3, 3), pad_mode="same", pad=0, stride=1), 'desc_inputs': [[2592, 2048, 4, 4], [1, 2048, 3, 3]], 'desc_bprop': [[2592, 2048, 4, 4]]}), ('SigmoidCrossEntropyWithLogits', { 'block': P.SigmoidCrossEntropyWithLogits(), 'desc_inputs': [[128, 10], [128, 10]], 'desc_bprop': [[128, 10]]}), ('Pad', { 'block': P.Pad(((1, 2), (2, 3))), 'desc_inputs': [[7, 7]], 'desc_bprop': [[10, 12]]}), ('BinaryCrossEntropy', {
# kernel_size != w_shape[2:4] ('Conv2D7', { 'block': (P.Conv2D(2, (5, 5)), { 'exception': ValueError, 'error_keywords': ['Conv2D'] }), 'desc_inputs': [ Tensor(np.ones([1, 1, 9, 9]).astype(np.float32)), Tensor(np.ones([2, 1, 5, 6]).astype(np.float32)) ], 'skip': ['backward'] }), # input is scalar ('DepthwiseConv2dNative0', { 'block': (P.DepthwiseConv2dNative(2, (5, 5)), { 'exception': TypeError, 'error_keywords': ['DepthwiseConv2dNative'] }), 'desc_inputs': [5.0, 5.0], 'skip': ['backward'] }), # input is Tensor(bool) ('DepthwiseConv2dNative1', { 'block': (P.DepthwiseConv2dNative(2, (5, 5)), { 'exception': TypeError, 'error_keywords': ['DepthwiseConv2dNative'] }), 'desc_inputs': [ Tensor(np.ones([5]).astype(np.bool_)), Tensor(np.ones([5]).astype(np.bool_))
def __init__(self, in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, eps=1e-5, momentum=0.997, weight_init=None, beta_init=None, gamma_init=None, mean_init=None, var_init=None, quant_delay=0, freeze_bn=100000, fake=True, num_bits=8, per_channel=False, symmetric=False, narrow_range=False): """init Conv2dBatchNormQuant layer""" super(Conv2dBatchNormQuant, self).__init__() self.in_channels = in_channels self.out_channels = out_channels self.kernel_size = twice(kernel_size) self.stride = twice(stride) self.pad_mode = pad_mode self.padding = padding self.dilation = twice(dilation) self.group = group self.eps = eps self.momentum = momentum self.quant_delay = quant_delay self.freeze_bn = freeze_bn self.fake = fake self.num_bits = num_bits self.per_channel = per_channel self.symmetric = symmetric self.narrow_range = narrow_range self.is_gpu = context.get_context('device_target') == "GPU" # initialize convolution op and Parameter if context.get_context('device_target') == "Ascend" and group > 1: validator.check_integer('group', group, in_channels, Rel.EQ, 'Conv2dBatchNormQuant') validator.check_integer('group', group, out_channels, Rel.EQ, 'Conv2dBatchNormQuant') self.conv = P.DepthwiseConv2dNative(channel_multiplier=1, kernel_size=self.kernel_size, pad_mode=pad_mode, pad=padding, stride=self.stride, dilation=self.dilation) if weight_init is None: weight_init = initializer('normal', [1, in_channels, *self.kernel_size]) channel_axis = 1 else: self.conv = P.Conv2D(out_channel=out_channels, kernel_size=self.kernel_size, pad_mode=pad_mode, pad=padding, stride=self.stride, dilation=self.dilation, group=group) if weight_init is None: weight_init = initializer( 'normal', [out_channels, in_channels // group, *self.kernel_size]) channel_axis = 0 self.weight = Parameter(weight_init, name='weight') # initialize batchnorm Parameter if gamma_init is None: gamma_init = initializer('ones', [out_channels]) self.gamma = Parameter(gamma_init, name='gamma') if beta_init is None: beta_init = initializer('zeros', [out_channels]) self.beta = Parameter(beta_init, name='beta') if mean_init is None: mean_init = initializer('zeros', [out_channels]) self.moving_mean = Parameter(mean_init, name='moving_mean', requires_grad=False) if var_init is None: var_init = initializer('ones', [out_channels]) self.moving_variance = Parameter(var_init, name='moving_variance', requires_grad=False) # initialize fake ops self.fake_quant_weight = FakeQuantWithMinMax(min_init=-6, max_init=6, ema=False, num_bits=num_bits, quant_delay=quant_delay, per_channel=per_channel, out_channels=out_channels, symmetric=symmetric, narrow_range=narrow_range) self.batchnorm_fold = BatchNormFoldCell(epsilon=eps, momentum=momentum, freeze_bn=freeze_bn) self.correct_mul = P.CorrectionMul(channel_axis) if context.get_context('device_target') == "Ascend": self.batchnorm_fold2_train = P.BatchNormFold2_D( freeze_bn=freeze_bn) self.batchnorm_fold2_infer = P.BatchNormFold2_D(freeze_bn=0) elif context.get_context('device_target') == "GPU": self.batchnorm_fold2_train = P.BatchNormFold2(freeze_bn=freeze_bn) self.batchnorm_fold2_infer = P.BatchNormFold2(freeze_bn=0) else: raise ValueError("Unsupported platform: {}".format( context.get_context('device_target'))) self.step = Parameter(initializer('normal', [1], dtype=mstype.int32), name='step', requires_grad=False) self.one = Tensor(1, mstype.int32) self.assignadd = P.AssignAdd()