|
- import network.mynn as mynn
- import mindspore.nn as nn
- import mindspore
- from mindspore.common.initializer import HeNormal, Constant
- import numpy as np
- import math
- from mindspore.common.tensor import Tensor
-
- def conv3x3(in_planes, out_planes, stride=1):
- """3x3 convolution with padding"""
- return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
- has_bias=False, weight_init="HeNormal")
-
- def calculate_gain(nonlinearity, param=None):
- """calculate_gain"""
- linear_fns = ['linear', 'conv1d', 'conv2d', 'conv3d', 'conv_transpose1d',
- 'conv_transpose2d', 'conv_transpose3d']
- res = 0
- if nonlinearity in linear_fns or nonlinearity == 'sigmoid':
- res = 1
- elif nonlinearity == 'tanh':
- res = 5.0 / 3
- elif nonlinearity == 'relu':
- res = math.sqrt(2.0)
- elif nonlinearity == 'leaky_relu':
- if param is None:
- negative_slope = 0.01
- elif not isinstance(param, bool) and isinstance(param, int) or isinstance(param, float):
- # True/False are instances of int, hence check above
- negative_slope = param
- else:
- raise ValueError("negative_slope {} not a valid number".format(param))
- res = math.sqrt(2.0 / (1 + negative_slope ** 2))
- else:
- raise ValueError("Unsupported nonlinearity {}".format(nonlinearity))
- return res
-
-
- def _calculate_fan_in_and_fan_out(tensor):
- """
- _calculate_fan_in_and_fan_out
- """
- dimensions = len(tensor)
- if dimensions < 2:
- raise ValueError("Fan in and fan out can not be computed for tensor"
- " with fewer than 2 dimensions")
- if dimensions == 2: # Linear
- fan_in = tensor[1]
- fan_out = tensor[0]
- else:
- num_input_fmaps = tensor[1]
- num_output_fmaps = tensor[0]
- receptive_field_size = 1
- if dimensions > 2:
- receptive_field_size = tensor[2] * tensor[3]
- fan_in = num_input_fmaps * receptive_field_size
- fan_out = num_output_fmaps * receptive_field_size
- return fan_in, fan_out
-
-
- def _calculate_correct_fan(tensor, mode):
- """
- for pylint.
- """
- mode = mode.lower()
- valid_modes = ['fan_in', 'fan_out']
- if mode not in valid_modes:
- raise ValueError("Mode {} not supported, please use one of {}".format(mode, valid_modes))
- fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor)
- return fan_in if mode == 'fan_in' else fan_out
-
-
- def kaiming_normal(inputs_shape, a=0, mode='fan_in', nonlinearity='leaky_relu'):
- """
- for pylint.
- """
- fan = _calculate_correct_fan(inputs_shape, mode)
- gain = calculate_gain(nonlinearity, a)
- std = gain / math.sqrt(fan)
- return np.random.normal(0, std, size=inputs_shape).astype(np.float32)
-
-
- def kaiming_uniform(inputs_shape, a=0., mode='fan_in', nonlinearity='leaky_relu'):
- """
- for pylint.
- """
- fan = _calculate_correct_fan(inputs_shape, mode)
- gain = calculate_gain(nonlinearity, a)
- std = gain / math.sqrt(fan)
- bound = math.sqrt(3.0) * std # Calculate uniform bounds from standard deviation
- return np.random.uniform(-bound, bound, size=inputs_shape).astype(np.float32)
-
- def _conv3x3(in_channel, out_channel, stride=1):
-
- weight_shape = (out_channel, in_channel, 3, 3)
- weight = Tensor(kaiming_normal(weight_shape, mode="fan_out", nonlinearity='relu'))
-
- return nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=stride,
- padding=0, pad_mode='same', weight_init=weight)
-
-
- class BasicBlock(nn.Cell):
- expansion = 1
-
- def __init__(self, inplanes, planes, stride=1, downsample=None):
- super(BasicBlock, self).__init__()
- self.conv1 = _conv3x3(inplanes, planes, stride)
- self.bn1 = mynn.Norm2d(planes)
- self.relu = nn.ReLU()
- self.conv2 = _conv3x3(planes, planes)
- self.bn2 = mynn.Norm2d(planes)
- #self.downsample = nn.SequentialCell()
- if downsample is not None:
- self.downsample = downsample
- else :
- self.downsample = nn.SequentialCell()
- self.stride = stride
- # for m in self.cells():
- # if isinstance(m, nn.Conv2d):
- # HeNormal(m.weight, mode='fan_out', nonlinearity='relu')
- # """batchnorm需不需要权重初始化"""
- # elif isinstance(m, nn.BatchNorm2d):
- # Constant(m.weight, 1)
- # Constant(m.bias, 0)
-
-
- def construct(self, x):
- residual = x
-
- out = self.conv1(x)
- out = self.bn1(out)
- out = self.relu(out)
-
- out = self.conv2(out)
- out = self.bn2(out)
-
-
- residual = self.downsample(residual)
-
- out += residual
- out = self.relu(out)
-
- return out
-
-
- # inputs = mindspore.Tensor(np.ones((2, 64, 24, 24)).astype("float32"))
- # resnet = BasicBlock(64, 64, stride=1, downsample=None)
- # #print(resnet)
- # outputs = resnet(inputs)
- #print(outputs.shape)
|