pull/5958/head
lilei 4 years ago
parent af48c17798
commit 71adabd944

@ -124,7 +124,6 @@ constexpr char CLONED_INDEX[] = "cloned_index";
constexpr char BE_CLONED_INDEX[] = "be_cloned_index"; constexpr char BE_CLONED_INDEX[] = "be_cloned_index";
constexpr char GROUP_RANKS[] = "group_ranks"; constexpr char GROUP_RANKS[] = "group_ranks";
constexpr char IS_IN_FORWARD[] = "is_in_forward"; constexpr char IS_IN_FORWARD[] = "is_in_forward";
constexpr char DEFAULT_INPUT[] = "default_input";
constexpr char DTYPE[] = "DType"; constexpr char DTYPE[] = "DType";
constexpr char DEV_NUM[] = "dev_num"; constexpr char DEV_NUM[] = "dev_num";
constexpr char MEAN_FLAG[] = "mean_flag"; constexpr char MEAN_FLAG[] = "mean_flag";

@ -154,17 +154,17 @@ void ConvertObjectToTensors(const py::dict &dict, TensorOrderMap *const tensors)
} }
std::shared_ptr<Tensor> tensor; std::shared_ptr<Tensor> tensor;
std::string name = py::cast<std::string>(item.first); std::string name = py::cast<std::string>(item.first);
if (py::isinstance<py::float_>(item.second.attr("default_input"))) { if (py::isinstance<py::float_>(item.second.attr("data"))) {
// convert float to tensor with shape([1]) // convert float to tensor with shape([1])
tensor = std::make_shared<Tensor>(kNumberTypeFloat32, std::vector<int>({1})); tensor = std::make_shared<Tensor>(kNumberTypeFloat32, std::vector<int>({1}));
*(static_cast<float *>(tensor->data_c())) = py::cast<float>(item.second.attr("default_input")); *(static_cast<float *>(tensor->data_c())) = py::cast<float>(item.second.attr("data"));
} else if (py::isinstance<py::int_>(item.second.attr("default_input"))) { } else if (py::isinstance<py::int_>(item.second.attr("data"))) {
// convert int to tensor with shape([1]) // convert int to tensor with shape([1])
tensor = std::make_shared<Tensor>(kNumberTypeInt32, std::vector<int>({1})); tensor = std::make_shared<Tensor>(kNumberTypeInt32, std::vector<int>({1}));
*(static_cast<float *>(tensor->data_c())) = py::cast<float>(item.second.attr("default_input")); *(static_cast<float *>(tensor->data_c())) = py::cast<float>(item.second.attr("data"));
} else if (py::isinstance<Tensor>(item.second.attr("default_input"))) { } else if (py::isinstance<Tensor>(item.second.attr("data"))) {
// cast tensor // cast tensor
tensor = py::cast<std::shared_ptr<Tensor>>(item.second.attr("default_input")); tensor = py::cast<std::shared_ptr<Tensor>>(item.second.attr("data"));
} }
if (tensor == nullptr) { if (tensor == nullptr) {

@ -49,7 +49,7 @@ class Parameter(MetaTensor):
Each parameter of Cell is represented by Parameter class. Each parameter of Cell is represented by Parameter class.
Args: Args:
default_input (Union[Tensor, Initializer, Number]): Parameter data, to be set initialized. set_data (Union[Tensor, Initializer, Number]): Parameter data, to be set initialized.
name (str): Name of the child parameter. name (str): Name of the child parameter.
requires_grad (bool): True if the parameter requires gradient. Default: True. requires_grad (bool): True if the parameter requires gradient. Default: True.
layerwise_parallel (bool): A kind of model parallel mode. When layerwise_parallel is true in parallel mode, layerwise_parallel (bool): A kind of model parallel mode. When layerwise_parallel is true in parallel mode,
@ -78,7 +78,7 @@ class Parameter(MetaTensor):
>>> x = Tensor(np.ones((2,1))) >>> x = Tensor(np.ones((2,1)))
>>> net(x) >>> net(x)
[[2.]] [[2.]]
>>> net.weight.set_parameter_data(Tensor(np.zeros((1,2)))) >>> net.weight.set_data(Tensor(np.zeros((1,2))))
>>> net(x) >>> net(x)
[[0.]] [[0.]]
""" """
@ -136,7 +136,7 @@ class Parameter(MetaTensor):
@staticmethod @staticmethod
def _get_parameter_new_args(data): def _get_parameter_new_args(data):
"""Set `default_input` of current `Parameter`.""" """Set `set_data` of current `Parameter`."""
if isinstance(data, bool): if isinstance(data, bool):
raise ValueError('Parameter data can not be `bool`') raise ValueError('Parameter data can not be `bool`')
if isinstance(data, Initializer): if isinstance(data, Initializer):
@ -266,7 +266,7 @@ class Parameter(MetaTensor):
if init != 'same': if init != 'same':
shape = self.shape shape = self.shape
dtype = self.dtype dtype = self.dtype
x.default_input = initializer(init, shape=shape, dtype=dtype) x.set_data(initializer(init, shape=shape, dtype=dtype))
return x return x
@property @property
@ -292,16 +292,8 @@ class Parameter(MetaTensor):
@property @property
def data(self): def data(self):
return self.default_input
@property
def default_input(self):
return self return self
@default_input.setter
def default_input(self, data):
self.set_parameter_data(data)
def _update_tensor_data(self, data): def _update_tensor_data(self, data):
"Update the parameter by a Tensor." "Update the parameter by a Tensor."
if isinstance(self, Tensor): if isinstance(self, Tensor):
@ -311,9 +303,9 @@ class Parameter(MetaTensor):
# create a new tensor # create a new tensor
return Parameter(data, self.name, self.requires_grad) return Parameter(data, self.name, self.requires_grad)
def set_parameter_data(self, data, slice_shape=False): def set_data(self, data, slice_shape=False):
""" """
Set `default_input` of current `Parameter`. Set `set_data` of current `Parameter`.
Args: Args:
data (Union[Tensor, Initializer, int, float]): new data. data (Union[Tensor, Initializer, int, float]): new data.
@ -339,7 +331,7 @@ class Parameter(MetaTensor):
is_current_tensor = isinstance(self, Tensor) is_current_tensor = isinstance(self, Tensor)
if is_incoming_tensor and not is_current_tensor: if is_incoming_tensor and not is_current_tensor:
raise TypeError("Parameter is a `MetaTensor` and not initializered, `data` for `set_parameter_data`" raise TypeError("Parameter is a `MetaTensor` and not initializered, `data` for `set_data`"
"should be a Initializer. If you want to update it by Tensor, call method" "should be a Initializer. If you want to update it by Tensor, call method"
"`init_parameters_data` of `Cell` to init and replace all the Parameter of" "`init_parameters_data` of `Cell` to init and replace all the Parameter of"
"network, then call this method.") "network, then call this method.")
@ -360,7 +352,7 @@ class Parameter(MetaTensor):
else: else:
# also update the related inited parameter data # also update the related inited parameter data
if self.inited_param is not None: if self.inited_param is not None:
self.inited_param.set_parameter_data(data) self.inited_param.set_data(data)
self.init_mode = data self.init_mode = data
elif is_incoming_tensor or is_current_tensor: elif is_incoming_tensor or is_current_tensor:
self._update_tensor_data(data) self._update_tensor_data(data)

@ -364,7 +364,7 @@ class Cell(Cell_):
cells[name] = value cells[name] = value
elif params and name in params: elif params and name in params:
if isinstance(value, Tensor) and self._params[name] is not None: if isinstance(value, Tensor) and self._params[name] is not None:
self._params[name].set_parameter_data(value) self._params[name].set_data(value)
elif value is not None: elif value is not None:
raise TypeError("Expected type in (Parameter, ParameterTuple), but got {}.".format(type(value))) raise TypeError("Expected type in (Parameter, ParameterTuple), but got {}.".format(type(value)))
else: else:
@ -425,7 +425,7 @@ class Cell(Cell_):
continue continue
layout = self.parameter_layout_dict[key] layout = self.parameter_layout_dict[key]
new_tensor = _load_tensor_by_layout(tensor, layout) new_tensor = _load_tensor_by_layout(tensor, layout)
params[key].set_parameter_data(new_tensor, True) params[key].set_data(new_tensor, True)
else: else:
raise TypeError('Parameters need OrderedDict type, but got {}'. raise TypeError('Parameters need OrderedDict type, but got {}'.
format(type(params))) format(type(params)))

@ -138,7 +138,7 @@ def calc_broadcast_shape_from_param(params):
if value is None: if value is None:
return None return None
if isinstance(value, Parameter): if isinstance(value, Parameter):
value_t = value.default_input value_t = value.data
else: else:
value_t = cast_to_tensor(value, mstype.float32) value_t = cast_to_tensor(value, mstype.float32)
broadcast_shape = utils.get_broadcast_shape( broadcast_shape = utils.get_broadcast_shape(
@ -159,9 +159,9 @@ def check_greater_equal_zero(value, name):
""" """
if isinstance(value, Parameter): if isinstance(value, Parameter):
if not isinstance(value.default_input, Tensor): if not isinstance(value.data, Tensor):
return return
value = value.default_input value = value.data
comp = np.less(value.asnumpy(), np.zeros(value.shape)) comp = np.less(value.asnumpy(), np.zeros(value.shape))
if comp.any(): if comp.any():
raise ValueError(f'{name} should be greater than ot equal to zero.') raise ValueError(f'{name} should be greater than ot equal to zero.')
@ -182,9 +182,9 @@ def check_greater_zero(value, name):
if value is None: if value is None:
raise ValueError(f'input value cannot be None in check_greater_zero') raise ValueError(f'input value cannot be None in check_greater_zero')
if isinstance(value, Parameter): if isinstance(value, Parameter):
if not isinstance(value.default_input, Tensor): if not isinstance(value.data, Tensor):
return return
value = value.default_input value = value.data
comp = np.less(np.zeros(value.shape), value.asnumpy()) comp = np.less(np.zeros(value.shape), value.asnumpy())
if not comp.all(): if not comp.all():
raise ValueError(f'{name} should be greater than zero.') raise ValueError(f'{name} should be greater than zero.')
@ -225,9 +225,9 @@ def check_prob(p):
if p is None: if p is None:
raise ValueError(f'input value cannot be None in check_greater_zero') raise ValueError(f'input value cannot be None in check_greater_zero')
if isinstance(p, Parameter): if isinstance(p, Parameter):
if not isinstance(p.default_input, Tensor): if not isinstance(p.data, Tensor):
return return
p = p.default_input p = p.data
comp = np.less(np.zeros(p.shape), p.asnumpy()) comp = np.less(np.zeros(p.shape), p.asnumpy())
if not comp.all(): if not comp.all():
raise ValueError('Probabilities should be greater than zero') raise ValueError('Probabilities should be greater than zero')

@ -251,7 +251,7 @@ class Cast(PrimitiveWithInfer):
if isinstance(x, numbers.Number): if isinstance(x, numbers.Number):
return (True, Tensor(x, dtype=dtype)) return (True, Tensor(x, dtype=dtype))
if isinstance(x, Parameter): if isinstance(x, Parameter):
data = x.default_input data = x.data
if data.dtype == dtype: if data.dtype == dtype:
return (True, x) return (True, x)
return (False, None) return (False, None)

@ -283,5 +283,5 @@ def load_nonquant_param_into_quant_net(quant_model, params_dict, quant_new_param
raise ValueError(f"Can't find match parameter in ckpt,param name = {name}") raise ValueError(f"Can't find match parameter in ckpt,param name = {name}")
value_param = next(iterable_dict[key_name], None) value_param = next(iterable_dict[key_name], None)
if value_param is not None: if value_param is not None:
param.set_parameter_data(value_param[1].data) param.set_data(value_param[1].data)
print(f'init model param {name} with checkpoint param {value_param[0]}') print(f'init model param {name} with checkpoint param {value_param[0]}')

@ -63,7 +63,7 @@ def _special_process_par(par, new_par):
if delta_i == delta_len - 1: if delta_i == delta_len - 1:
new_val = new_par.data.asnumpy() new_val = new_par.data.asnumpy()
new_val = new_val.reshape(par.data.shape) new_val = new_val.reshape(par.data.shape)
par.set_parameter_data(Tensor(new_val, par.data.dtype)) par.set_data(Tensor(new_val, par.data.dtype))
return True return True
return False return False
@ -86,7 +86,7 @@ def _update_param(param, new_param):
raise RuntimeError(msg) raise RuntimeError(msg)
return return
param.set_parameter_data(new_param.data) param.set_data(new_param.data)
return return
if isinstance(param.data, Tensor) and not isinstance(new_param.data, Tensor): if isinstance(param.data, Tensor) and not isinstance(new_param.data, Tensor):
@ -95,7 +95,7 @@ def _update_param(param, new_param):
msg = ("Net parameters {} shape({}) is not (1,), inconsitent with parameter_dict's(scalar)." msg = ("Net parameters {} shape({}) is not (1,), inconsitent with parameter_dict's(scalar)."
.format(param.name, param.data.shape)) .format(param.name, param.data.shape))
raise RuntimeError(msg) raise RuntimeError(msg)
param.set_parameter_data(initializer(new_param.data, param.data.shape, param.data.dtype)) param.set_data(initializer(new_param.data, param.data.shape, param.data.dtype))
elif isinstance(new_param.data, Tensor) and not isinstance(param.data, Tensor): elif isinstance(new_param.data, Tensor) and not isinstance(param.data, Tensor):
logger.error("Failed to combine the net and the parameters for param %s.", param.name) logger.error("Failed to combine the net and the parameters for param %s.", param.name)
@ -104,7 +104,7 @@ def _update_param(param, new_param):
raise RuntimeError(msg) raise RuntimeError(msg)
else: else:
param.set_parameter_data(type(param.data)(new_param.data)) param.set_data(type(param.data)(new_param.data))
def _exec_save(ckpt_file_name, data_list): def _exec_save(ckpt_file_name, data_list):

@ -90,7 +90,7 @@ if __name__ == '__main__':
if args_opt.platform == "Ascend": if args_opt.platform == "Ascend":
for param in net.trainable_params(): for param in net.trainable_params():
if 'beta' not in param.name and 'gamma' not in param.name and 'bias' not in param.name: if 'beta' not in param.name and 'gamma' not in param.name and 'bias' not in param.name:
param.set_parameter_data(initializer(XavierUniform(), param.data.shape, param.data.dtype)) param.set_data(initializer(XavierUniform(), param.data.shape, param.data.dtype))
group_params = [{'params': decayed_params, 'weight_decay': cfg.weight_decay}, group_params = [{'params': decayed_params, 'weight_decay': cfg.weight_decay},
{'params': no_decayed_params}, {'params': no_decayed_params},
{'order_params': net.trainable_params()}] {'order_params': net.trainable_params()}]

@ -260,15 +260,15 @@ class MobileNetV2Backbone(nn.Cell):
for _, m in self.cells_and_names(): for _, m in self.cells_and_names():
if isinstance(m, (nn.Conv2d, DepthwiseConv)): if isinstance(m, (nn.Conv2d, DepthwiseConv)):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.set_parameter_data(Tensor(np.random.normal(0, np.sqrt(2. / n), m.weight.set_data(Tensor(np.random.normal(0, np.sqrt(2. / n),
m.weight.data.shape).astype("float32"))) m.weight.data.shape).astype("float32")))
if m.bias is not None: if m.bias is not None:
m.bias.set_parameter_data( m.bias.set_data(
Tensor(np.zeros(m.bias.data.shape, dtype="float32"))) Tensor(np.zeros(m.bias.data.shape, dtype="float32")))
elif isinstance(m, nn.BatchNorm2d): elif isinstance(m, nn.BatchNorm2d):
m.gamma.set_parameter_data( m.gamma.set_data(
Tensor(np.ones(m.gamma.data.shape, dtype="float32"))) Tensor(np.ones(m.gamma.data.shape, dtype="float32")))
m.beta.set_parameter_data( m.beta.set_data(
Tensor(np.zeros(m.beta.data.shape, dtype="float32"))) Tensor(np.zeros(m.beta.data.shape, dtype="float32")))
@property @property
@ -316,10 +316,10 @@ class MobileNetV2Head(nn.Cell):
self.init_parameters_data() self.init_parameters_data()
for _, m in self.cells_and_names(): for _, m in self.cells_and_names():
if isinstance(m, nn.Dense): if isinstance(m, nn.Dense):
m.weight.set_parameter_data(Tensor(np.random.normal( m.weight.set_data(Tensor(np.random.normal(
0, 0.01, m.weight.data.shape).astype("float32"))) 0, 0.01, m.weight.data.shape).astype("float32")))
if m.bias is not None: if m.bias is not None:
m.bias.set_parameter_data( m.bias.set_data(
Tensor(np.zeros(m.bias.data.shape, dtype="float32"))) Tensor(np.zeros(m.bias.data.shape, dtype="float32")))
@property @property
def get_head(self): def get_head(self):

@ -216,24 +216,24 @@ class mobilenetV2(nn.Cell):
if isinstance(m, nn.Conv2d): if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
w = Tensor(np.random.normal(0, np.sqrt(2. / n), m.weight.data.shape).astype("float32")) w = Tensor(np.random.normal(0, np.sqrt(2. / n), m.weight.data.shape).astype("float32"))
m.weight.set_parameter_data(w) m.weight.set_data(w)
if m.bias is not None: if m.bias is not None:
m.bias.set_parameter_data(Tensor(np.zeros(m.bias.data.shape, dtype="float32"))) m.bias.set_data(Tensor(np.zeros(m.bias.data.shape, dtype="float32")))
elif isinstance(m, nn.Conv2dBnAct): elif isinstance(m, nn.Conv2dBnAct):
n = m.conv.kernel_size[0] * m.conv.kernel_size[1] * m.conv.out_channels n = m.conv.kernel_size[0] * m.conv.kernel_size[1] * m.conv.out_channels
w = Tensor(np.random.normal(0, np.sqrt(2. / n), m.conv.weight.data.shape).astype("float32")) w = Tensor(np.random.normal(0, np.sqrt(2. / n), m.conv.weight.data.shape).astype("float32"))
m.conv.weight.set_parameter_data(w) m.conv.weight.set_data(w)
if m.conv.bias is not None: if m.conv.bias is not None:
m.conv.bias.set_parameter_data(Tensor(np.zeros(m.conv.bias.data.shape, dtype="float32"))) m.conv.bias.set_data(Tensor(np.zeros(m.conv.bias.data.shape, dtype="float32")))
elif isinstance(m, nn.BatchNorm2d): elif isinstance(m, nn.BatchNorm2d):
m.gamma.set_parameter_data(Tensor(np.ones(m.gamma.data.shape, dtype="float32"))) m.gamma.set_data(Tensor(np.ones(m.gamma.data.shape, dtype="float32")))
m.beta.set_parameter_data(Tensor(np.zeros(m.beta.data.shape, dtype="float32"))) m.beta.set_data(Tensor(np.zeros(m.beta.data.shape, dtype="float32")))
elif isinstance(m, nn.Dense): elif isinstance(m, nn.Dense):
m.weight.set_parameter_data(Tensor(np.random.normal(0, 0.01, m.weight.data.shape).astype("float32"))) m.weight.set_data(Tensor(np.random.normal(0, 0.01, m.weight.data.shape).astype("float32")))
if m.bias is not None: if m.bias is not None:
m.bias.set_parameter_data(Tensor(np.zeros(m.bias.data.shape, dtype="float32"))) m.bias.set_data(Tensor(np.zeros(m.bias.data.shape, dtype="float32")))
elif isinstance(m, nn.DenseBnAct): elif isinstance(m, nn.DenseBnAct):
m.dense.weight.set_parameter_data( m.dense.weight.set_data(
Tensor(np.random.normal(0, 0.01, m.dense.weight.data.shape).astype("float32"))) Tensor(np.random.normal(0, 0.01, m.dense.weight.data.shape).astype("float32")))
if m.dense.bias is not None: if m.dense.bias is not None:
m.dense.bias.set_parameter_data(Tensor(np.zeros(m.dense.bias.data.shape, dtype="float32"))) m.dense.bias.set_data(Tensor(np.zeros(m.dense.bias.data.shape, dtype="float32")))

@ -221,24 +221,24 @@ class mobilenetV2(nn.Cell):
if isinstance(m, nn.Conv2d): if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
w = Tensor(np.random.normal(0, np.sqrt(2. / n), m.weight.data.shape).astype("float32")) w = Tensor(np.random.normal(0, np.sqrt(2. / n), m.weight.data.shape).astype("float32"))
m.weight.set_parameter_data(w) m.weight.set_data(w)
if m.bias is not None: if m.bias is not None:
m.bias.set_parameter_data(Tensor(np.zeros(m.bias.data.shape, dtype="float32"))) m.bias.set_data(Tensor(np.zeros(m.bias.data.shape, dtype="float32")))
elif isinstance(m, nn.Conv2dBnAct): elif isinstance(m, nn.Conv2dBnAct):
n = m.conv.kernel_size[0] * m.conv.kernel_size[1] * m.conv.out_channels n = m.conv.kernel_size[0] * m.conv.kernel_size[1] * m.conv.out_channels
w = Tensor(np.random.normal(0, np.sqrt(2. / n), m.conv.weight.data.shape).astype("float32")) w = Tensor(np.random.normal(0, np.sqrt(2. / n), m.conv.weight.data.shape).astype("float32"))
m.conv.weight.set_parameter_data(w) m.conv.weight.set_data(w)
if m.conv.bias is not None: if m.conv.bias is not None:
m.conv.bias.set_parameter_data(Tensor(np.zeros(m.conv.bias.data.shape, dtype="float32"))) m.conv.bias.set_data(Tensor(np.zeros(m.conv.bias.data.shape, dtype="float32")))
elif isinstance(m, nn.BatchNorm2d): elif isinstance(m, nn.BatchNorm2d):
m.gamma.set_parameter_data(Tensor(np.ones(m.gamma.data.shape, dtype="float32"))) m.gamma.set_data(Tensor(np.ones(m.gamma.data.shape, dtype="float32")))
m.beta.set_parameter_data(Tensor(np.zeros(m.beta.data.shape, dtype="float32"))) m.beta.set_data(Tensor(np.zeros(m.beta.data.shape, dtype="float32")))
elif isinstance(m, nn.Dense): elif isinstance(m, nn.Dense):
m.weight.set_parameter_data(Tensor(np.random.normal(0, 0.01, m.weight.data.shape).astype("float32"))) m.weight.set_data(Tensor(np.random.normal(0, 0.01, m.weight.data.shape).astype("float32")))
if m.bias is not None: if m.bias is not None:
m.bias.set_parameter_data(Tensor(np.zeros(m.bias.data.shape, dtype="float32"))) m.bias.set_data(Tensor(np.zeros(m.bias.data.shape, dtype="float32")))
elif isinstance(m, nn.DenseBnAct): elif isinstance(m, nn.DenseBnAct):
m.dense.weight.set_parameter_data( m.dense.weight.set_data(
Tensor(np.random.normal(0, 0.01, m.dense.weight.data.shape).astype("float32"))) Tensor(np.random.normal(0, 0.01, m.dense.weight.data.shape).astype("float32")))
if m.dense.bias is not None: if m.dense.bias is not None:
m.dense.bias.set_parameter_data(Tensor(np.zeros(m.dense.bias.data.shape, dtype="float32"))) m.dense.bias.set_data(Tensor(np.zeros(m.dense.bias.data.shape, dtype="float32")))

@ -323,21 +323,21 @@ class MobileNetV3(nn.Cell):
for _, m in self.cells_and_names(): for _, m in self.cells_and_names():
if isinstance(m, (nn.Conv2d)): if isinstance(m, (nn.Conv2d)):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.set_parameter_data(Tensor(np.random.normal(0, np.sqrt(2. / n), m.weight.set_data(Tensor(np.random.normal(0, np.sqrt(2. / n),
m.weight.data.shape).astype("float32"))) m.weight.data.shape).astype("float32")))
if m.bias is not None: if m.bias is not None:
m.bias.set_parameter_data( m.bias.set_data(
Tensor(np.zeros(m.bias.data.shape, dtype="float32"))) Tensor(np.zeros(m.bias.data.shape, dtype="float32")))
elif isinstance(m, nn.BatchNorm2d): elif isinstance(m, nn.BatchNorm2d):
m.gamma.set_parameter_data( m.gamma.set_data(
Tensor(np.ones(m.gamma.data.shape, dtype="float32"))) Tensor(np.ones(m.gamma.data.shape, dtype="float32")))
m.beta.set_parameter_data( m.beta.set_data(
Tensor(np.zeros(m.beta.data.shape, dtype="float32"))) Tensor(np.zeros(m.beta.data.shape, dtype="float32")))
elif isinstance(m, nn.Dense): elif isinstance(m, nn.Dense):
m.weight.set_parameter_data(Tensor(np.random.normal( m.weight.set_data(Tensor(np.random.normal(
0, 0.01, m.weight.data.shape).astype("float32"))) 0, 0.01, m.weight.data.shape).astype("float32")))
if m.bias is not None: if m.bias is not None:
m.bias.set_parameter_data( m.bias.set_data(
Tensor(np.zeros(m.bias.data.shape, dtype="float32"))) Tensor(np.zeros(m.bias.data.shape, dtype="float32")))

@ -821,21 +821,21 @@ class NASNetAMobile(nn.Cell):
for _, m in self.cells_and_names(): for _, m in self.cells_and_names():
if isinstance(m, nn.Conv2d): if isinstance(m, nn.Conv2d):
n = m.kernel_size[0]*m.kernel_size[1]*m.out_channels n = m.kernel_size[0]*m.kernel_size[1]*m.out_channels
m.weight.set_parameter_data(Tensor(np.random.normal(0, np.sqrt(2./n), m.weight.set_data(Tensor(np.random.normal(0, np.sqrt(2./n),
m.weight.data.shape).astype("float32"))) m.weight.data.shape).astype("float32")))
if m.bias is not None: if m.bias is not None:
m.bias.set_parameter_data( m.bias.set_data(
Tensor(np.zeros(m.bias.data.shape, dtype="float32"))) Tensor(np.zeros(m.bias.data.shape, dtype="float32")))
elif isinstance(m, nn.BatchNorm2d): elif isinstance(m, nn.BatchNorm2d):
m.gamma.set_parameter_data( m.gamma.set_data(
Tensor(np.ones(m.gamma.data.shape, dtype="float32"))) Tensor(np.ones(m.gamma.data.shape, dtype="float32")))
m.beta.set_parameter_data( m.beta.set_data(
Tensor(np.zeros(m.beta.data.shape, dtype="float32"))) Tensor(np.zeros(m.beta.data.shape, dtype="float32")))
elif isinstance(m, nn.Dense): elif isinstance(m, nn.Dense):
m.weight.set_parameter_data(Tensor(np.random.normal( m.weight.set_data(Tensor(np.random.normal(
0, 0.01, m.weight.data.shape).astype("float32"))) 0, 0.01, m.weight.data.shape).astype("float32")))
if m.bias is not None: if m.bias is not None:
m.bias.set_parameter_data( m.bias.set_data(
Tensor(np.zeros(m.bias.data.shape, dtype="float32"))) Tensor(np.zeros(m.bias.data.shape, dtype="float32")))
def construct(self, x): def construct(self, x):

@ -108,13 +108,13 @@ if __name__ == '__main__':
else: else:
for _, cell in net.cells_and_names(): for _, cell in net.cells_and_names():
if isinstance(cell, nn.Conv2d): if isinstance(cell, nn.Conv2d):
cell.weight.default_input = weight_init.initializer(weight_init.XavierUniform(), cell.weight.set_data(weight_init.initializer(weight_init.XavierUniform(),
cell.weight.shape, cell.weight.shape,
cell.weight.dtype) cell.weight.dtype))
if isinstance(cell, nn.Dense): if isinstance(cell, nn.Dense):
cell.weight.default_input = weight_init.initializer(weight_init.TruncatedNormal(), cell.weight.set_data(weight_init.initializer(weight_init.TruncatedNormal(),
cell.weight.shape, cell.weight.shape,
cell.weight.dtype) cell.weight.dtype))
# init lr # init lr
if args_opt.net == "resnet50" or args_opt.net == "se-resnet50": if args_opt.net == "resnet50" or args_opt.net == "se-resnet50":

@ -93,13 +93,13 @@ if __name__ == '__main__':
else: else:
for _, cell in net.cells_and_names(): for _, cell in net.cells_and_names():
if isinstance(cell, nn.Conv2d): if isinstance(cell, nn.Conv2d):
cell.weight.default_input = weight_init.initializer(weight_init.XavierUniform(), cell.weight.set_data(weight_init.initializer(weight_init.XavierUniform(),
cell.weight.shape, cell.weight.shape,
cell.weight.dtype) cell.weight.dtype))
if isinstance(cell, nn.Dense): if isinstance(cell, nn.Dense):
cell.weight.default_input = weight_init.initializer(weight_init.TruncatedNormal(), cell.weight.set_data(weight_init.initializer(weight_init.TruncatedNormal(),
cell.weight.shape, cell.weight.shape,
cell.weight.dtype) cell.weight.dtype))
if not config.use_label_smooth: if not config.use_label_smooth:
config.label_smooth_factor = 0.0 config.label_smooth_factor = 0.0
loss = CrossEntropy(smooth_factor=config.label_smooth_factor, num_classes=config.class_num) loss = CrossEntropy(smooth_factor=config.label_smooth_factor, num_classes=config.class_num)

@ -61,21 +61,21 @@ class Resnet(ImageClassificationNetwork):
for cell in self.cells_and_names(): for cell in self.cells_and_names():
if isinstance(cell, nn.Conv2d): if isinstance(cell, nn.Conv2d):
cell.weight.default_input = init.initializer( cell.weight.set_data(init.initializer(
KaimingNormal(a=math.sqrt(5), mode='fan_out', nonlinearity='relu'), KaimingNormal(a=math.sqrt(5), mode='fan_out', nonlinearity='relu'),
cell.weight.shape, cell.weight.dtype) cell.weight.shape, cell.weight.dtype))
elif isinstance(cell, nn.BatchNorm2d): elif isinstance(cell, nn.BatchNorm2d):
cell.gamma.default_input = init.initializer('ones', cell.gamma.shape) cell.gamma.set_data(init.initializer('ones', cell.gamma.shape))
cell.beta.default_input = init.initializer('zeros', cell.beta.shape) cell.beta.set_data(init.initializer('zeros', cell.beta.shape))
# Zero-initialize the last BN in each residual branch, # Zero-initialize the last BN in each residual branch,
# so that the residual branch starts with zeros, and each residual block behaves like an identity. # so that the residual branch starts with zeros, and each residual block behaves like an identity.
# This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677 # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
for cell in self.cells_and_names(): for cell in self.cells_and_names():
if isinstance(cell, backbones.resnet.Bottleneck): if isinstance(cell, backbones.resnet.Bottleneck):
cell.bn3.gamma.default_input = init.initializer('zeros', cell.bn3.gamma.shape) cell.bn3.gamma.set_data(init.initializer('zeros', cell.bn3.gamma.shape))
elif isinstance(cell, backbones.resnet.BasicBlock): elif isinstance(cell, backbones.resnet.BasicBlock):
cell.bn2.gamma.default_input = init.initializer('zeros', cell.bn2.gamma.shape) cell.bn2.gamma.set_data(init.initializer('zeros', cell.bn2.gamma.shape))

@ -187,24 +187,24 @@ def default_recurisive_init(custom_cell):
"""default_recurisive_init""" """default_recurisive_init"""
for _, cell in custom_cell.cells_and_names(): for _, cell in custom_cell.cells_and_names():
if isinstance(cell, nn.Conv2d): if isinstance(cell, nn.Conv2d):
cell.weight.default_input = init.initializer(KaimingUniform(a=math.sqrt(5)), cell.weight.set_data(init.initializer(KaimingUniform(a=math.sqrt(5)),
cell.weight.shape, cell.weight.shape,
cell.weight.dtype) cell.weight.dtype))
if cell.bias is not None: if cell.bias is not None:
fan_in, _ = _calculate_in_and_out(cell.weight) fan_in, _ = _calculate_in_and_out(cell.weight)
bound = 1 / math.sqrt(fan_in) bound = 1 / math.sqrt(fan_in)
cell.bias.default_input = init.initializer(init.Uniform(bound), cell.bias.set_data(init.initializer(init.Uniform(bound),
cell.bias.shape, cell.bias.shape,
cell.bias.dtype) cell.bias.dtype))
elif isinstance(cell, nn.Dense): elif isinstance(cell, nn.Dense):
cell.weight.default_input = init.initializer(KaimingUniform(a=math.sqrt(5)), cell.weight.set_data(init.initializer(KaimingUniform(a=math.sqrt(5)),
cell.weight.shape, cell.weight.shape,
cell.weight.dtype) cell.weight.dtype))
if cell.bias is not None: if cell.bias is not None:
fan_in, _ = _calculate_in_and_out(cell.weight) fan_in, _ = _calculate_in_and_out(cell.weight)
bound = 1 / math.sqrt(fan_in) bound = 1 / math.sqrt(fan_in)
cell.bias.default_input = init.initializer(init.Uniform(bound), cell.bias.set_data(init.initializer(init.Uniform(bound),
cell.bias.shape, cell.bias.shape,
cell.bias.dtype) cell.bias.dtype))
elif isinstance(cell, (nn.BatchNorm2d, nn.BatchNorm1d)): elif isinstance(cell, (nn.BatchNorm2d, nn.BatchNorm1d)):
pass pass

@ -163,11 +163,11 @@ class ShuffleNetV2(nn.Cell):
for name, m in self.cells_and_names(): for name, m in self.cells_and_names():
if isinstance(m, nn.Conv2d): if isinstance(m, nn.Conv2d):
if 'first' in name: if 'first' in name:
m.weight.set_parameter_data(Tensor(np.random.normal(0, 0.01, m.weight.set_data(Tensor(np.random.normal(0, 0.01,
m.weight.data.shape).astype("float32"))) m.weight.data.shape).astype("float32")))
else: else:
m.weight.set_parameter_data(Tensor(np.random.normal(0, 1.0/m.weight.data.shape[1], m.weight.set_data(Tensor(np.random.normal(0, 1.0/m.weight.data.shape[1],
m.weight.data.shape).astype("float32"))) m.weight.data.shape).astype("float32")))
if isinstance(m, nn.Dense): if isinstance(m, nn.Dense):
m.weight.set_parameter_data(Tensor(np.random.normal(0, 0.01, m.weight.data.shape).astype("float32"))) m.weight.set_data(Tensor(np.random.normal(0, 0.01, m.weight.data.shape).astype("float32")))

@ -22,9 +22,9 @@ def init_net_param(network, initialize_mode='TruncatedNormal'):
for p in params: for p in params:
if 'beta' not in p.name and 'gamma' not in p.name and 'bias' not in p.name: if 'beta' not in p.name and 'gamma' not in p.name and 'bias' not in p.name:
if initialize_mode == 'TruncatedNormal': if initialize_mode == 'TruncatedNormal':
p.set_parameter_data(initializer(TruncatedNormal(), p.data.shape, p.data.dtype)) p.set_data(initializer(TruncatedNormal(), p.data.shape, p.data.dtype))
else: else:
p.set_parameter_data(initialize_mode, p.data.shape, p.data.dtype) p.set_data(initialize_mode, p.data.shape, p.data.dtype)
def load_backbone_params(network, param_dict): def load_backbone_params(network, param_dict):
@ -37,7 +37,7 @@ def load_backbone_params(network, param_dict):
if 'features_2' in param_name: if 'features_2' in param_name:
param_name = '.'.join(['features', str(int(name_split[1]) + 14)] + name_split[2:]) param_name = '.'.join(['features', str(int(name_split[1]) + 14)] + name_split[2:])
if param_name in param_dict: if param_name in param_dict:
param.set_parameter_data(param_dict[param_name].data) param.set_data(param_dict[param_name].data)
def filter_checkpoint_parameter(param_dict): def filter_checkpoint_parameter(param_dict):
"""remove useless parameters""" """remove useless parameters"""

@ -187,24 +187,24 @@ def default_recurisive_init(custom_cell):
"""default_recurisive_init""" """default_recurisive_init"""
for _, cell in custom_cell.cells_and_names(): for _, cell in custom_cell.cells_and_names():
if isinstance(cell, nn.Conv2d): if isinstance(cell, nn.Conv2d):
cell.weight.default_input = init.initializer(KaimingUniform(a=math.sqrt(5)), cell.weight.set_data(init.initializer(KaimingUniform(a=math.sqrt(5)),
cell.weight.shape, cell.weight.shape,
cell.weight.dtype) cell.weight.dtype))
if cell.bias is not None: if cell.bias is not None:
fan_in, _ = _calculate_in_and_out(cell.weight) fan_in, _ = _calculate_in_and_out(cell.weight)
bound = 1 / math.sqrt(fan_in) bound = 1 / math.sqrt(fan_in)
cell.bias.default_input = init.initializer(init.Uniform(bound), cell.bias.set_data(init.initializer(init.Uniform(bound),
cell.bias.shape, cell.bias.shape,
cell.bias.dtype) cell.bias.dtype))
elif isinstance(cell, nn.Dense): elif isinstance(cell, nn.Dense):
cell.weight.default_input = init.initializer(KaimingUniform(a=math.sqrt(5)), cell.weight.set_data(init.initializer(KaimingUniform(a=math.sqrt(5)),
cell.weight.shape, cell.weight.shape,
cell.weight.dtype) cell.weight.dtype))
if cell.bias is not None: if cell.bias is not None:
fan_in, _ = _calculate_in_and_out(cell.weight) fan_in, _ = _calculate_in_and_out(cell.weight)
bound = 1 / math.sqrt(fan_in) bound = 1 / math.sqrt(fan_in)
cell.bias.default_input = init.initializer(init.Uniform(bound), cell.bias.set_data(init.initializer(init.Uniform(bound),
cell.bias.shape, cell.bias.shape,
cell.bias.dtype) cell.bias.dtype))
elif isinstance(cell, (nn.BatchNorm2d, nn.BatchNorm1d)): elif isinstance(cell, (nn.BatchNorm2d, nn.BatchNorm1d)):
pass pass

@ -101,18 +101,18 @@ class Vgg(nn.Cell):
""" """
for _, cell in self.cells_and_names(): for _, cell in self.cells_and_names():
if isinstance(cell, nn.Conv2d): if isinstance(cell, nn.Conv2d):
cell.weight.default_input = init.initializer( cell.weight.set_data(init.initializer(
KaimingNormal(a=math.sqrt(5), mode='fan_out', nonlinearity='relu'), KaimingNormal(a=math.sqrt(5), mode='fan_out', nonlinearity='relu'),
cell.weight.shape, cell.weight.dtype) cell.weight.shape, cell.weight.dtype))
if cell.bias is not None: if cell.bias is not None:
cell.bias.default_input = init.initializer( cell.bias.set_data(init.initializer(
'zeros', cell.bias.shape, cell.bias.dtype) 'zeros', cell.bias.shape, cell.bias.dtype))
elif isinstance(cell, nn.Dense): elif isinstance(cell, nn.Dense):
cell.weight.default_input = init.initializer( cell.weight.set_data(init.initializer(
init.Normal(0.01), cell.weight.shape, cell.weight.dtype) init.Normal(0.01), cell.weight.shape, cell.weight.dtype))
if cell.bias is not None: if cell.bias is not None:
cell.bias.default_input = init.initializer( cell.bias.set_data(init.initializer(
'zeros', cell.bias.shape, cell.bias.dtype) 'zeros', cell.bias.shape, cell.bias.dtype))
cfg = { cfg = {

@ -155,24 +155,24 @@ def default_recurisive_init(custom_cell):
"""Initialize parameter.""" """Initialize parameter."""
for _, cell in custom_cell.cells_and_names(): for _, cell in custom_cell.cells_and_names():
if isinstance(cell, nn.Conv2d): if isinstance(cell, nn.Conv2d):
cell.weight.default_input = init.initializer(KaimingUniform(a=math.sqrt(5)), cell.weight.set_data(init.initializer(KaimingUniform(a=math.sqrt(5)),
cell.weight.shape, cell.weight.shape,
cell.weight.dtype) cell.weight.dtype))
if cell.bias is not None: if cell.bias is not None:
fan_in, _ = _calculate_fan_in_and_fan_out(cell.weight) fan_in, _ = _calculate_fan_in_and_fan_out(cell.weight)
bound = 1 / math.sqrt(fan_in) bound = 1 / math.sqrt(fan_in)
cell.bias.default_input = init.initializer(init.Uniform(bound), cell.bias.set_data(init.initializer(init.Uniform(bound),
cell.bias.shape, cell.bias.shape,
cell.bias.dtype) cell.bias.dtype))
elif isinstance(cell, nn.Dense): elif isinstance(cell, nn.Dense):
cell.weight.default_input = init.initializer(KaimingUniform(a=math.sqrt(5)), cell.weight.set_data(init.initializer(KaimingUniform(a=math.sqrt(5)),
cell.weight.shape, cell.weight.shape,
cell.weight.dtype) cell.weight.dtype))
if cell.bias is not None: if cell.bias is not None:
fan_in, _ = _calculate_fan_in_and_fan_out(cell.weight) fan_in, _ = _calculate_fan_in_and_fan_out(cell.weight)
bound = 1 / math.sqrt(fan_in) bound = 1 / math.sqrt(fan_in)
cell.bias.default_input = init.initializer(init.Uniform(bound), cell.bias.set_data(init.initializer(init.Uniform(bound),
cell.bias.shape, cell.bias.shape,
cell.bias.dtype) cell.bias.dtype))
elif isinstance(cell, (nn.BatchNorm2d, nn.BatchNorm1d)): elif isinstance(cell, (nn.BatchNorm2d, nn.BatchNorm1d)):
pass pass

@ -69,12 +69,12 @@ def load_backbone(net, ckpt_path, args):
darknet_weight = '{}.weight'.format(name) darknet_weight = '{}.weight'.format(name)
darknet_bias = '{}.bias'.format(name) darknet_bias = '{}.bias'.format(name)
if darknet_weight in param_dict: if darknet_weight in param_dict:
cell.weight.default_input = param_dict[darknet_weight].data cell.weight.set_data(param_dict[darknet_weight].data)
find_param.append(darknet_weight) find_param.append(darknet_weight)
else: else:
not_found_param.append(darknet_weight) not_found_param.append(darknet_weight)
if darknet_bias in param_dict: if darknet_bias in param_dict:
cell.bias.default_input = param_dict[darknet_bias].data cell.bias.set_data(param_dict[darknet_bias].data)
find_param.append(darknet_bias) find_param.append(darknet_bias)
else: else:
not_found_param.append(darknet_bias) not_found_param.append(darknet_bias)
@ -84,22 +84,22 @@ def load_backbone(net, ckpt_path, args):
darknet_gamma = '{}.gamma'.format(name) darknet_gamma = '{}.gamma'.format(name)
darknet_beta = '{}.beta'.format(name) darknet_beta = '{}.beta'.format(name)
if darknet_moving_mean in param_dict: if darknet_moving_mean in param_dict:
cell.moving_mean.default_input = param_dict[darknet_moving_mean].data cell.moving_mean.set_data(param_dict[darknet_moving_mean].data)
find_param.append(darknet_moving_mean) find_param.append(darknet_moving_mean)
else: else:
not_found_param.append(darknet_moving_mean) not_found_param.append(darknet_moving_mean)
if darknet_moving_variance in param_dict: if darknet_moving_variance in param_dict:
cell.moving_variance.default_input = param_dict[darknet_moving_variance].data cell.moving_variance.set_data(param_dict[darknet_moving_variance].data)
find_param.append(darknet_moving_variance) find_param.append(darknet_moving_variance)
else: else:
not_found_param.append(darknet_moving_variance) not_found_param.append(darknet_moving_variance)
if darknet_gamma in param_dict: if darknet_gamma in param_dict:
cell.gamma.default_input = param_dict[darknet_gamma].data cell.gamma.set_data(param_dict[darknet_gamma].data)
find_param.append(darknet_gamma) find_param.append(darknet_gamma)
else: else:
not_found_param.append(darknet_gamma) not_found_param.append(darknet_gamma)
if darknet_beta in param_dict: if darknet_beta in param_dict:
cell.beta.default_input = param_dict[darknet_beta].data cell.beta.set_data(param_dict[darknet_beta].data)
find_param.append(darknet_beta) find_param.append(darknet_beta)
else: else:
not_found_param.append(darknet_beta) not_found_param.append(darknet_beta)

@ -155,22 +155,22 @@ def default_recurisive_init(custom_cell):
"""Initialize parameter.""" """Initialize parameter."""
for _, cell in custom_cell.cells_and_names(): for _, cell in custom_cell.cells_and_names():
if isinstance(cell, nn.Conv2d): if isinstance(cell, nn.Conv2d):
cell.weight.default_input = init.initializer(KaimingUniform(a=math.sqrt(5)), cell.weight.set_data(init.initializer(KaimingUniform(a=math.sqrt(5)),
cell.weight.default_input.shape, cell.weight.data.shape,
cell.weight.default_input.dtype).to_tensor() cell.weight.data.dtype).to_tensor())
if cell.bias is not None: if cell.bias is not None:
fan_in, _ = _calculate_fan_in_and_fan_out(cell.weight.default_input.asnumpy()) fan_in, _ = _calculate_fan_in_and_fan_out(cell.weight.data.asnumpy())
bound = 1 / math.sqrt(fan_in) bound = 1 / math.sqrt(fan_in)
cell.bias.default_input = Tensor(np.random.uniform(-bound, bound, cell.bias.default_input.shape), cell.bias.set_data(Tensor(np.random.uniform(-bound, bound, cell.bias.data.shape),
cell.bias.default_input.dtype) cell.bias.data.dtype))
elif isinstance(cell, nn.Dense): elif isinstance(cell, nn.Dense):
cell.weight.default_input = init.initializer(KaimingUniform(a=math.sqrt(5)), cell.weight.set_data(init.initializer(KaimingUniform(a=math.sqrt(5)),
cell.weight.default_input.shape, cell.weight.data.shape,
cell.weight.default_input.dtype).to_tensor() cell.weight.data.dtype).to_tensor())
if cell.bias is not None: if cell.bias is not None:
fan_in, _ = _calculate_fan_in_and_fan_out(cell.weight.default_input.asnumpy()) fan_in, _ = _calculate_fan_in_and_fan_out(cell.weight.data.asnumpy())
bound = 1 / math.sqrt(fan_in) bound = 1 / math.sqrt(fan_in)
cell.bias.default_input = Tensor(np.random.uniform(-bound, bound, cell.bias.default_input.shape), cell.bias.set_data(Tensor(np.random.uniform(-bound, bound, cell.bias.data.shape),
cell.bias.default_input.dtype) cell.bias.data.dtype))
elif isinstance(cell, (nn.BatchNorm2d, nn.BatchNorm1d)): elif isinstance(cell, (nn.BatchNorm2d, nn.BatchNorm1d)):
pass pass

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save