forked from mindspore-Ecosystem/mindspore
fix some network built-in function problem.
This commit is contained in:
parent
22d62f20ff
commit
07ee4054c5
|
@ -15,15 +15,66 @@
|
|||
"""
|
||||
define loss function for network.
|
||||
"""
|
||||
from mindspore.nn.loss.loss import _Loss
|
||||
from mindspore.nn.cell import Cell
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore.ops import functional as F
|
||||
from mindspore import Tensor
|
||||
from mindspore.common import dtype as mstype
|
||||
import mindspore.nn as nn
|
||||
|
||||
class MyLoss(Cell):
|
||||
"""
|
||||
Base class for other losses.
|
||||
"""
|
||||
def __init__(self, reduction='mean'):
|
||||
super(MyLoss, self).__init__()
|
||||
if reduction is None:
|
||||
reduction = 'none'
|
||||
|
||||
class CrossEntropy(_Loss):
|
||||
if reduction not in ('mean', 'sum', 'none'):
|
||||
raise ValueError(f"reduction method for {reduction.lower()} is not supported")
|
||||
|
||||
self.average = True
|
||||
self.reduce = True
|
||||
if reduction == 'sum':
|
||||
self.average = False
|
||||
if reduction == 'none':
|
||||
self.reduce = False
|
||||
|
||||
self.reduce_mean = P.ReduceMean()
|
||||
self.reduce_sum = P.ReduceSum()
|
||||
self.mul = P.Mul()
|
||||
self.cast = P.Cast()
|
||||
|
||||
def get_axis(self, x):
|
||||
shape = F.shape(x)
|
||||
length = F.tuple_len(shape)
|
||||
perm = F.make_range(0, length)
|
||||
return perm
|
||||
|
||||
def get_loss(self, x, weights=1.0):
|
||||
"""
|
||||
Computes the weighted loss
|
||||
Args:
|
||||
weights: Optional `Tensor` whose rank is either 0, or the same rank as inputs, and must be broadcastable to
|
||||
inputs (i.e., all dimensions must be either `1`, or the same as the corresponding inputs dimension).
|
||||
"""
|
||||
input_dtype = x.dtype
|
||||
x = self.cast(x, mstype.float32)
|
||||
weights = self.cast(weights, mstype.float32)
|
||||
x = self.mul(weights, x)
|
||||
if self.reduce and self.average:
|
||||
x = self.reduce_mean(x, self.get_axis(x))
|
||||
if self.reduce and not self.average:
|
||||
x = self.reduce_sum(x, self.get_axis(x))
|
||||
x = self.cast(x, input_dtype)
|
||||
return x
|
||||
|
||||
def construct(self, base, target):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class CrossEntropy(MyLoss):
|
||||
"""
|
||||
the redefined loss function with SoftmaxCrossEntropyWithLogits.
|
||||
"""
|
||||
|
|
|
@ -13,13 +13,14 @@
|
|||
# limitations under the License.
|
||||
# ============================================================================
|
||||
import mindspore.nn as nn
|
||||
import mindspore.common.dtype as mstype
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore.nn.loss.loss import _Loss
|
||||
from mindspore.nn.cell import Cell
|
||||
from mindspore.ops import functional as F
|
||||
from mindspore.ops import composite as C
|
||||
from mindspore.context import ParallelMode
|
||||
from mindspore.context import ParallelMode, get_auto_parallel_context
|
||||
from mindspore.communication.management import get_group_size
|
||||
from mindspore import context
|
||||
from mindspore.parallel._utils import _get_device_num, _get_parallel_mode, _get_gradients_mean
|
||||
from mindspore.nn.wrap.grad_reducer import DistributedGradReducer
|
||||
|
||||
from src.config import params
|
||||
|
@ -59,7 +60,58 @@ def _clip_grad(clip_type, clip_value, grad):
|
|||
new_grad = nn.ClipByNorm()(grad, F.cast(F.tuple_to_array((clip_value,)), dt))
|
||||
return new_grad
|
||||
|
||||
class openpose_loss(_Loss):
|
||||
class MyLoss(Cell):
|
||||
"""
|
||||
Base class for other losses.
|
||||
"""
|
||||
def __init__(self, reduction='mean'):
|
||||
super(MyLoss, self).__init__()
|
||||
if reduction is None:
|
||||
reduction = 'none'
|
||||
|
||||
if reduction not in ('mean', 'sum', 'none'):
|
||||
raise ValueError(f"reduction method for {reduction.lower()} is not supported")
|
||||
|
||||
self.average = True
|
||||
self.reduce = True
|
||||
if reduction == 'sum':
|
||||
self.average = False
|
||||
if reduction == 'none':
|
||||
self.reduce = False
|
||||
|
||||
self.reduce_mean = P.ReduceMean()
|
||||
self.reduce_sum = P.ReduceSum()
|
||||
self.mul = P.Mul()
|
||||
self.cast = P.Cast()
|
||||
|
||||
def get_axis(self, x):
|
||||
shape = F.shape(x)
|
||||
length = F.tuple_len(shape)
|
||||
perm = F.make_range(0, length)
|
||||
return perm
|
||||
|
||||
def get_loss(self, x, weights=1.0):
|
||||
"""
|
||||
Computes the weighted loss
|
||||
Args:
|
||||
weights: Optional `Tensor` whose rank is either 0, or the same rank as inputs, and must be broadcastable to
|
||||
inputs (i.e., all dimensions must be either `1`, or the same as the corresponding inputs dimension).
|
||||
"""
|
||||
input_dtype = x.dtype
|
||||
x = self.cast(x, mstype.float32)
|
||||
weights = self.cast(weights, mstype.float32)
|
||||
x = self.mul(weights, x)
|
||||
if self.reduce and self.average:
|
||||
x = self.reduce_mean(x, self.get_axis(x))
|
||||
if self.reduce and not self.average:
|
||||
x = self.reduce_sum(x, self.get_axis(x))
|
||||
x = self.cast(x, input_dtype)
|
||||
return x
|
||||
|
||||
def construct(self, base, target):
|
||||
raise NotImplementedError
|
||||
|
||||
class openpose_loss(MyLoss):
|
||||
def __init__(self):
|
||||
super(openpose_loss, self).__init__()
|
||||
self.expand_dims = P.ExpandDims()
|
||||
|
@ -130,12 +182,12 @@ class TrainOneStepWithClipGradientCell(nn.Cell):
|
|||
self.sens = sens
|
||||
self.reducer_flag = False
|
||||
self.grad_reducer = None
|
||||
parallel_mode = _get_parallel_mode()
|
||||
parallel_mode = get_auto_parallel_context("parallel_mode")
|
||||
if parallel_mode in (ParallelMode.DATA_PARALLEL, ParallelMode.HYBRID_PARALLEL):
|
||||
self.reducer_flag = True
|
||||
if self.reducer_flag:
|
||||
mean = _get_gradients_mean()
|
||||
degree = _get_device_num()
|
||||
mean = get_auto_parallel_context("gradients_mean")
|
||||
degree = get_group_size()
|
||||
self.grad_reducer = DistributedGradReducer(optimizer.parameters, mean, degree)
|
||||
|
||||
def construct(self, *inputs):
|
||||
|
|
|
@ -16,12 +16,62 @@
|
|||
import mindspore.nn as nn
|
||||
from mindspore import Tensor
|
||||
from mindspore.common import dtype as mstype
|
||||
from mindspore.nn.loss.loss import _Loss
|
||||
from mindspore.nn.cell import Cell
|
||||
from mindspore.ops import functional as F
|
||||
from mindspore.ops import operations as P
|
||||
|
||||
class MyLoss(Cell):
|
||||
"""
|
||||
Base class for other losses.
|
||||
"""
|
||||
def __init__(self, reduction='mean'):
|
||||
super(MyLoss, self).__init__()
|
||||
if reduction is None:
|
||||
reduction = 'none'
|
||||
|
||||
class CrossEntropySmooth(_Loss):
|
||||
if reduction not in ('mean', 'sum', 'none'):
|
||||
raise ValueError(f"reduction method for {reduction.lower()} is not supported")
|
||||
|
||||
self.average = True
|
||||
self.reduce = True
|
||||
if reduction == 'sum':
|
||||
self.average = False
|
||||
if reduction == 'none':
|
||||
self.reduce = False
|
||||
|
||||
self.reduce_mean = P.ReduceMean()
|
||||
self.reduce_sum = P.ReduceSum()
|
||||
self.mul = P.Mul()
|
||||
self.cast = P.Cast()
|
||||
|
||||
def get_axis(self, x):
|
||||
shape = F.shape(x)
|
||||
length = F.tuple_len(shape)
|
||||
perm = F.make_range(0, length)
|
||||
return perm
|
||||
|
||||
def get_loss(self, x, weights=1.0):
|
||||
"""
|
||||
Computes the weighted loss
|
||||
Args:
|
||||
weights: Optional `Tensor` whose rank is either 0, or the same rank as inputs, and must be broadcastable to
|
||||
inputs (i.e., all dimensions must be either `1`, or the same as the corresponding inputs dimension).
|
||||
"""
|
||||
input_dtype = x.dtype
|
||||
x = self.cast(x, mstype.float32)
|
||||
weights = self.cast(weights, mstype.float32)
|
||||
x = self.mul(weights, x)
|
||||
if self.reduce and self.average:
|
||||
x = self.reduce_mean(x, self.get_axis(x))
|
||||
if self.reduce and not self.average:
|
||||
x = self.reduce_sum(x, self.get_axis(x))
|
||||
x = self.cast(x, input_dtype)
|
||||
return x
|
||||
|
||||
def construct(self, base, target):
|
||||
raise NotImplementedError
|
||||
|
||||
class CrossEntropySmooth(MyLoss):
|
||||
"""CrossEntropy"""
|
||||
def __init__(self, sparse=True, reduction='mean', smooth_factor=0., num_classes=1000):
|
||||
super(CrossEntropySmooth, self).__init__()
|
||||
|
|
|
@ -16,12 +16,64 @@
|
|||
import mindspore.nn as nn
|
||||
from mindspore import Tensor
|
||||
from mindspore.common import dtype as mstype
|
||||
from mindspore.nn.loss.loss import _Loss
|
||||
from mindspore.nn.cell import Cell
|
||||
from mindspore.ops import functional as F
|
||||
from mindspore.ops import operations as P
|
||||
|
||||
|
||||
class CrossEntropySmooth(_Loss):
|
||||
class MyLoss(Cell):
|
||||
"""
|
||||
Base class for other losses.
|
||||
"""
|
||||
def __init__(self, reduction='mean'):
|
||||
super(MyLoss, self).__init__()
|
||||
if reduction is None:
|
||||
reduction = 'none'
|
||||
|
||||
if reduction not in ('mean', 'sum', 'none'):
|
||||
raise ValueError(f"reduction method for {reduction.lower()} is not supported")
|
||||
|
||||
self.average = True
|
||||
self.reduce = True
|
||||
if reduction == 'sum':
|
||||
self.average = False
|
||||
if reduction == 'none':
|
||||
self.reduce = False
|
||||
|
||||
self.reduce_mean = P.ReduceMean()
|
||||
self.reduce_sum = P.ReduceSum()
|
||||
self.mul = P.Mul()
|
||||
self.cast = P.Cast()
|
||||
|
||||
def get_axis(self, x):
|
||||
shape = F.shape(x)
|
||||
length = F.tuple_len(shape)
|
||||
perm = F.make_range(0, length)
|
||||
return perm
|
||||
|
||||
def get_loss(self, x, weights=1.0):
|
||||
"""
|
||||
Computes the weighted loss
|
||||
Args:
|
||||
weights: Optional `Tensor` whose rank is either 0, or the same rank as inputs, and must be broadcastable to
|
||||
inputs (i.e., all dimensions must be either `1`, or the same as the corresponding inputs dimension).
|
||||
"""
|
||||
input_dtype = x.dtype
|
||||
x = self.cast(x, mstype.float32)
|
||||
weights = self.cast(weights, mstype.float32)
|
||||
x = self.mul(weights, x)
|
||||
if self.reduce and self.average:
|
||||
x = self.reduce_mean(x, self.get_axis(x))
|
||||
if self.reduce and not self.average:
|
||||
x = self.reduce_sum(x, self.get_axis(x))
|
||||
x = self.cast(x, input_dtype)
|
||||
return x
|
||||
|
||||
def construct(self, base, target):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class CrossEntropySmooth(MyLoss):
|
||||
"""CrossEntropy"""
|
||||
def __init__(self, sparse=True, reduction='mean', smooth_factor=0., num_classes=1000):
|
||||
super(CrossEntropySmooth, self).__init__()
|
||||
|
|
|
@ -15,11 +15,65 @@
|
|||
|
||||
import mindspore
|
||||
import mindspore.nn as nn
|
||||
import mindspore.common.dtype as mstype
|
||||
import mindspore.ops.operations as F
|
||||
from mindspore.nn.loss.loss import _Loss
|
||||
from mindspore.ops import functional as F2
|
||||
from mindspore.nn.cell import Cell
|
||||
|
||||
|
||||
class CrossEntropyWithLogits(_Loss):
|
||||
class MyLoss(Cell):
|
||||
"""
|
||||
Base class for other losses.
|
||||
"""
|
||||
def __init__(self, reduction='mean'):
|
||||
super(MyLoss, self).__init__()
|
||||
if reduction is None:
|
||||
reduction = 'none'
|
||||
|
||||
if reduction not in ('mean', 'sum', 'none'):
|
||||
raise ValueError(f"reduction method for {reduction.lower()} is not supported")
|
||||
|
||||
self.average = True
|
||||
self.reduce = True
|
||||
if reduction == 'sum':
|
||||
self.average = False
|
||||
if reduction == 'none':
|
||||
self.reduce = False
|
||||
|
||||
self.reduce_mean = F.ReduceMean()
|
||||
self.reduce_sum = F.ReduceSum()
|
||||
self.mul = F.Mul()
|
||||
self.cast = F.Cast()
|
||||
|
||||
def get_axis(self, x):
|
||||
shape = F2.shape(x)
|
||||
length = F2.tuple_len(shape)
|
||||
perm = F2.make_range(0, length)
|
||||
return perm
|
||||
|
||||
def get_loss(self, x, weights=1.0):
|
||||
"""
|
||||
Computes the weighted loss
|
||||
Args:
|
||||
weights: Optional `Tensor` whose rank is either 0, or the same rank as inputs, and must be broadcastable to
|
||||
inputs (i.e., all dimensions must be either `1`, or the same as the corresponding inputs dimension).
|
||||
"""
|
||||
input_dtype = x.dtype
|
||||
x = self.cast(x, mstype.float32)
|
||||
weights = self.cast(weights, mstype.float32)
|
||||
x = self.mul(weights, x)
|
||||
if self.reduce and self.average:
|
||||
x = self.reduce_mean(x, self.get_axis(x))
|
||||
if self.reduce and not self.average:
|
||||
x = self.reduce_sum(x, self.get_axis(x))
|
||||
x = self.cast(x, input_dtype)
|
||||
return x
|
||||
|
||||
def construct(self, base, target):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class CrossEntropyWithLogits(MyLoss):
|
||||
def __init__(self):
|
||||
super(CrossEntropyWithLogits, self).__init__()
|
||||
self.transpose_fn = F.Transpose()
|
||||
|
|
|
@ -16,12 +16,62 @@
|
|||
import mindspore.nn as nn
|
||||
from mindspore import Tensor
|
||||
from mindspore.common import dtype as mstype
|
||||
from mindspore.nn.loss.loss import _Loss
|
||||
from mindspore.nn.cell import Cell
|
||||
from mindspore.ops import functional as F
|
||||
from mindspore.ops import operations as P
|
||||
|
||||
class MyLoss(Cell):
|
||||
"""
|
||||
Base class for other losses.
|
||||
"""
|
||||
def __init__(self, reduction='mean'):
|
||||
super(MyLoss, self).__init__()
|
||||
if reduction is None:
|
||||
reduction = 'none'
|
||||
|
||||
class CrossEntropySmooth(_Loss):
|
||||
if reduction not in ('mean', 'sum', 'none'):
|
||||
raise ValueError(f"reduction method for {reduction.lower()} is not supported")
|
||||
|
||||
self.average = True
|
||||
self.reduce = True
|
||||
if reduction == 'sum':
|
||||
self.average = False
|
||||
if reduction == 'none':
|
||||
self.reduce = False
|
||||
|
||||
self.reduce_mean = P.ReduceMean()
|
||||
self.reduce_sum = P.ReduceSum()
|
||||
self.mul = P.Mul()
|
||||
self.cast = P.Cast()
|
||||
|
||||
def get_axis(self, x):
|
||||
shape = F.shape(x)
|
||||
length = F.tuple_len(shape)
|
||||
perm = F.make_range(0, length)
|
||||
return perm
|
||||
|
||||
def get_loss(self, x, weights=1.0):
|
||||
"""
|
||||
Computes the weighted loss
|
||||
Args:
|
||||
weights: Optional `Tensor` whose rank is either 0, or the same rank as inputs, and must be broadcastable to
|
||||
inputs (i.e., all dimensions must be either `1`, or the same as the corresponding inputs dimension).
|
||||
"""
|
||||
input_dtype = x.dtype
|
||||
x = self.cast(x, mstype.float32)
|
||||
weights = self.cast(weights, mstype.float32)
|
||||
x = self.mul(weights, x)
|
||||
if self.reduce and self.average:
|
||||
x = self.reduce_mean(x, self.get_axis(x))
|
||||
if self.reduce and not self.average:
|
||||
x = self.reduce_sum(x, self.get_axis(x))
|
||||
x = self.cast(x, input_dtype)
|
||||
return x
|
||||
|
||||
def construct(self, base, target):
|
||||
raise NotImplementedError
|
||||
|
||||
class CrossEntropySmooth(MyLoss):
|
||||
"""CrossEntropy"""
|
||||
def __init__(self, sparse=True, reduction='mean', smooth_factor=0., num_classes=1000):
|
||||
super(CrossEntropySmooth, self).__init__()
|
||||
|
|
|
@ -24,7 +24,7 @@ import mindspore.nn as nn
|
|||
from mindspore import context
|
||||
from mindspore.communication.management import init, get_rank, get_group_size
|
||||
from mindspore.train.callback import ModelCheckpoint, RunContext
|
||||
from mindspore.train.callback import _InternalCallbackParam, CheckpointConfig
|
||||
from mindspore.train.callback import CheckpointConfig
|
||||
import mindspore as ms
|
||||
from mindspore import amp
|
||||
from mindspore.train.loss_scale_manager import FixedLossScaleManager
|
||||
|
@ -97,6 +97,16 @@ def convert_training_shape(args_training_shape):
|
|||
return training_shape
|
||||
|
||||
|
||||
class InternalCallbackParam(dict):
|
||||
"""Internal callback object's parameters."""
|
||||
|
||||
def __getattr__(self, key):
|
||||
return self[key]
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
self[key] = value
|
||||
|
||||
|
||||
class BuildTrainNetwork(nn.Cell):
|
||||
def __init__(self, network_, criterion):
|
||||
super(BuildTrainNetwork, self).__init__()
|
||||
|
@ -224,7 +234,7 @@ def run_train():
|
|||
network.set_train(True)
|
||||
|
||||
if config.rank_save_ckpt_flag or config.run_eval:
|
||||
cb_params = _InternalCallbackParam()
|
||||
cb_params = InternalCallbackParam()
|
||||
cb_params.train_network = network
|
||||
cb_params.epoch_num = config.max_epoch * config.steps_per_epoch // config.ckpt_interval
|
||||
cb_params.cur_epoch_num = 1
|
||||
|
|
|
@ -26,7 +26,7 @@ from mindspore.nn.optim import Momentum
|
|||
from mindspore.communication.management import get_group_size, init, get_rank
|
||||
from mindspore.nn import TrainOneStepCell
|
||||
from mindspore.context import ParallelMode
|
||||
from mindspore.train.callback import ModelCheckpoint, RunContext, _InternalCallbackParam, CheckpointConfig
|
||||
from mindspore.train.callback import ModelCheckpoint, RunContext, CheckpointConfig
|
||||
from mindspore.train.serialization import load_checkpoint, load_param_into_net
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore.common import dtype as mstype
|
||||
|
@ -42,6 +42,15 @@ devid = int(os.getenv('DEVICE_ID'))
|
|||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=False, device_id=devid)
|
||||
|
||||
|
||||
class InternalCallbackParam(dict):
|
||||
"""Internal callback object's parameters."""
|
||||
|
||||
def __getattr__(self, _key):
|
||||
return self[_key]
|
||||
|
||||
def __setattr__(self, _key, _value):
|
||||
self[_key] = _value
|
||||
|
||||
class BuildTrainNetwork(nn.Cell):
|
||||
'''Build train network.'''
|
||||
def __init__(self, my_network, my_criterion):
|
||||
|
@ -171,7 +180,7 @@ if __name__ == "__main__":
|
|||
ckpt_max_num = args.max_epoch
|
||||
train_config = CheckpointConfig(save_checkpoint_steps=args.steps_per_epoch, keep_checkpoint_max=ckpt_max_num)
|
||||
ckpt_cb = ModelCheckpoint(config=train_config, directory=args.outputs_dir, prefix='{}'.format(args.local_rank))
|
||||
cb_params = _InternalCallbackParam()
|
||||
cb_params = InternalCallbackParam()
|
||||
cb_params.train_network = train_net
|
||||
cb_params.epoch_num = ckpt_max_num
|
||||
cb_params.cur_epoch_num = 0
|
||||
|
|
|
@ -17,11 +17,61 @@ import numpy as np
|
|||
|
||||
import mindspore.nn as nn
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore.nn.loss.loss import _Loss
|
||||
from mindspore.ops import functional as F
|
||||
from mindspore.nn import Cell
|
||||
from mindspore import Tensor
|
||||
from mindspore.common import dtype as mstype
|
||||
|
||||
class MyLoss(Cell):
|
||||
"""
|
||||
Base class for other losses.
|
||||
"""
|
||||
def __init__(self, reduction='mean'):
|
||||
super(MyLoss, self).__init__()
|
||||
if reduction is None:
|
||||
reduction = 'none'
|
||||
|
||||
if reduction not in ('mean', 'sum', 'none'):
|
||||
raise ValueError(f"reduction method for {reduction.lower()} is not supported")
|
||||
|
||||
self.average = True
|
||||
self.reduce = True
|
||||
if reduction == 'sum':
|
||||
self.average = False
|
||||
if reduction == 'none':
|
||||
self.reduce = False
|
||||
|
||||
self.reduce_mean = P.ReduceMean()
|
||||
self.reduce_sum = P.ReduceSum()
|
||||
self.mul = P.Mul()
|
||||
self.cast = P.Cast()
|
||||
|
||||
def get_axis(self, x):
|
||||
shape = F.shape(x)
|
||||
length = F.tuple_len(shape)
|
||||
perm = F.make_range(0, length)
|
||||
return perm
|
||||
|
||||
def get_loss(self, x, weights=1.0):
|
||||
"""
|
||||
Computes the weighted loss
|
||||
Args:
|
||||
weights: Optional `Tensor` whose rank is either 0, or the same rank as inputs, and must be broadcastable to
|
||||
inputs (i.e., all dimensions must be either `1`, or the same as the corresponding inputs dimension).
|
||||
"""
|
||||
input_dtype = x.dtype
|
||||
x = self.cast(x, mstype.float32)
|
||||
weights = self.cast(weights, mstype.float32)
|
||||
x = self.mul(weights, x)
|
||||
if self.reduce and self.average:
|
||||
x = self.reduce_mean(x, self.get_axis(x))
|
||||
if self.reduce and not self.average:
|
||||
x = self.reduce_sum(x, self.get_axis(x))
|
||||
x = self.cast(x, input_dtype)
|
||||
return x
|
||||
|
||||
def construct(self, base, target):
|
||||
raise NotImplementedError
|
||||
|
||||
class PtLinspace(Cell):
|
||||
'''PtLinspace'''
|
||||
|
@ -38,7 +88,7 @@ class PtLinspace(Cell):
|
|||
return lin_x
|
||||
|
||||
|
||||
class MSELoss(_Loss):
|
||||
class MSELoss(MyLoss):
|
||||
'''MSELoss'''
|
||||
def __init__(self):
|
||||
super(MSELoss, self).__init__()
|
||||
|
@ -156,9 +206,7 @@ class YoloLoss(Cell):
|
|||
self.iou = P.IOU()
|
||||
|
||||
def construct(self, output, coord_mask, conf_pos_mask, conf_neg_mask, cls_mask, t_coord, t_conf, t_cls, gt_list):
|
||||
"""
|
||||
Compute Yolo loss.
|
||||
"""
|
||||
"""Compute Yolo loss."""
|
||||
output_d = self.shape(output)
|
||||
num_batch = output_d[0]
|
||||
num_anchors = self.num_anchors
|
||||
|
@ -241,7 +289,6 @@ class YoloLoss(Cell):
|
|||
t_coord_wh = t_coord[:, :, 2:]
|
||||
|
||||
one_hot_label = None
|
||||
shape_cls_mask = None
|
||||
if num_classes > 1:
|
||||
shape_t_cls = self.shape(t_cls)
|
||||
t_cls = self.reshape(t_cls, (shape_t_cls[0] * shape_t_cls[1] * shape_t_cls[2],))
|
||||
|
@ -263,7 +310,6 @@ class YoloLoss(Cell):
|
|||
|
||||
loss_conf = loss_conf_pos + loss_conf_neg
|
||||
|
||||
loss_cls = None
|
||||
if num_classes > 1:
|
||||
loss_cls = self.class_scale * 1.0 * self.sum(cls_mask * self.ce(cls, one_hot_label)[0], ())
|
||||
else:
|
||||
|
|
|
@ -19,10 +19,10 @@ import numpy as np
|
|||
import mindspore.nn as nn
|
||||
from mindspore.ops.operations import NPUGetFloatStatus, NPUAllocFloatStatus, NPUClearFloatStatus, ReduceSum, \
|
||||
LessEqual
|
||||
from mindspore.parallel._utils import _get_device_num, _get_parallel_mode, _get_gradients_mean
|
||||
from mindspore.nn.wrap.grad_reducer import DistributedGradReducer
|
||||
from mindspore import Tensor
|
||||
from mindspore.context import ParallelMode
|
||||
from mindspore.context import ParallelMode, get_auto_parallel_context
|
||||
from mindspore.communication.management import get_group_size
|
||||
from mindspore.ops import composite as C
|
||||
from mindspore.ops import functional as F
|
||||
from mindspore.ops import operations as P
|
||||
|
@ -74,14 +74,14 @@ class TrainOneStepWithLossScaleCell(nn.Cell):
|
|||
self.reducer_flag = False
|
||||
self.less_equal = LessEqual()
|
||||
self.allreduce = P.AllReduce()
|
||||
self.parallel_mode = _get_parallel_mode()
|
||||
self.parallel_mode = get_auto_parallel_context("parallel_mode")
|
||||
self.grad_reducer = None
|
||||
parallel_mode = _get_parallel_mode()
|
||||
parallel_mode = self.parallel_mode
|
||||
if parallel_mode in (ParallelMode.DATA_PARALLEL, ParallelMode.HYBRID_PARALLEL):
|
||||
self.reducer_flag = True
|
||||
if self.reducer_flag:
|
||||
mean = _get_gradients_mean()
|
||||
degree = _get_device_num()
|
||||
mean = get_auto_parallel_context("gradients_mean")
|
||||
degree = get_group_size()
|
||||
self.grad_reducer = DistributedGradReducer(optimizer.parameters, mean, degree)
|
||||
self.is_distributed = self.parallel_mode != ParallelMode.STAND_ALONE
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ from mindspore import Tensor
|
|||
from mindspore.communication.management import init
|
||||
from mindspore.context import ParallelMode
|
||||
from mindspore.train.callback import ModelCheckpoint, RunContext
|
||||
from mindspore.train.callback import _InternalCallbackParam, CheckpointConfig
|
||||
from mindspore.train.callback import CheckpointConfig
|
||||
from mindspore.common import dtype as mstype
|
||||
|
||||
from src.logging import get_logger
|
||||
|
@ -36,6 +36,15 @@ from model_utils.moxing_adapter import moxing_wrapper
|
|||
from model_utils.device_adapter import get_device_id, get_device_num, get_rank_id
|
||||
|
||||
|
||||
class InternalCallbackParam(dict):
|
||||
"""Internal callback object's parameters."""
|
||||
|
||||
def __getattr__(self, key):
|
||||
return self[key]
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
self[key] = value
|
||||
|
||||
def modelarts_pre_process():
|
||||
'''modelarts pre process function.'''
|
||||
def unzip(zip_file, save_dir):
|
||||
|
@ -127,7 +136,7 @@ def run_train():
|
|||
ckpt_max_num = config.max_epoch * config.steps_per_epoch // config.ckpt_interval
|
||||
train_config = CheckpointConfig(save_checkpoint_steps=config.ckpt_interval, keep_checkpoint_max=ckpt_max_num)
|
||||
ckpt_cb = ModelCheckpoint(config=train_config, directory=config.outputs_dir, prefix='{}'.format(config.local_rank))
|
||||
cb_params = _InternalCallbackParam()
|
||||
cb_params = InternalCallbackParam()
|
||||
cb_params.train_network = train_net
|
||||
cb_params.epoch_num = ckpt_max_num
|
||||
cb_params.cur_epoch_num = 1
|
||||
|
|
|
@ -14,8 +14,7 @@
|
|||
# ============================================================================
|
||||
"""Face Recognition loss."""
|
||||
from mindspore import Tensor
|
||||
from mindspore.nn import Cell
|
||||
from mindspore.nn.loss.loss import _Loss
|
||||
from mindspore.nn.cell import Cell
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore.ops import functional as F
|
||||
from mindspore.common import dtype as mstype
|
||||
|
@ -24,7 +23,58 @@ __all__ = ['get_loss']
|
|||
|
||||
eps = 1e-24
|
||||
|
||||
class CrossEntropy(_Loss):
|
||||
class MyLoss(Cell):
|
||||
"""
|
||||
Base class for other losses.
|
||||
"""
|
||||
def __init__(self, reduction='mean'):
|
||||
super(MyLoss, self).__init__()
|
||||
if reduction is None:
|
||||
reduction = 'none'
|
||||
|
||||
if reduction not in ('mean', 'sum', 'none'):
|
||||
raise ValueError(f"reduction method for {reduction.lower()} is not supported")
|
||||
|
||||
self.average = True
|
||||
self.reduce = True
|
||||
if reduction == 'sum':
|
||||
self.average = False
|
||||
if reduction == 'none':
|
||||
self.reduce = False
|
||||
|
||||
self.reduce_mean = P.ReduceMean()
|
||||
self.reduce_sum = P.ReduceSum()
|
||||
self.mul = P.Mul()
|
||||
self.cast = P.Cast()
|
||||
|
||||
def get_axis(self, x):
|
||||
shape = F.shape(x)
|
||||
length = F.tuple_len(shape)
|
||||
perm = F.make_range(0, length)
|
||||
return perm
|
||||
|
||||
def get_loss(self, x, weights=1.0):
|
||||
"""
|
||||
Computes the weighted loss
|
||||
Args:
|
||||
weights: Optional `Tensor` whose rank is either 0, or the same rank as inputs, and must be broadcastable to
|
||||
inputs (i.e., all dimensions must be either `1`, or the same as the corresponding inputs dimension).
|
||||
"""
|
||||
input_dtype = x.dtype
|
||||
x = self.cast(x, mstype.float32)
|
||||
weights = self.cast(weights, mstype.float32)
|
||||
x = self.mul(weights, x)
|
||||
if self.reduce and self.average:
|
||||
x = self.reduce_mean(x, self.get_axis(x))
|
||||
if self.reduce and not self.average:
|
||||
x = self.reduce_sum(x, self.get_axis(x))
|
||||
x = self.cast(x, input_dtype)
|
||||
return x
|
||||
|
||||
def construct(self, base, target):
|
||||
raise NotImplementedError
|
||||
|
||||
class CrossEntropy(MyLoss):
|
||||
'''CrossEntropy'''
|
||||
def __init__(self, args):
|
||||
super(CrossEntropy, self).__init__()
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
import math
|
||||
import numpy as np
|
||||
from mindspore.common import initializer as init
|
||||
from mindspore.common.initializer import _assignment
|
||||
|
||||
def calculate_gain(nonlinearity, param=None):
|
||||
r"""Return the recommended gain value for the given nonlinearity function.
|
||||
|
@ -172,6 +171,19 @@ def xavier_uniform_(arr, gain=1.):
|
|||
|
||||
return np.random.uniform(-a, a, arr.shape)
|
||||
|
||||
def assignment(arr, num):
|
||||
"""Assign the value of `num` to `arr`."""
|
||||
if arr.shape == ():
|
||||
arr = arr.reshape((1))
|
||||
arr[:] = num
|
||||
arr = arr.reshape(())
|
||||
else:
|
||||
if isinstance(num, np.ndarray):
|
||||
arr[:] = num[:]
|
||||
else:
|
||||
arr[:] = num
|
||||
return arr
|
||||
|
||||
|
||||
class ReidXavierUniform(init.Initializer):
|
||||
def __init__(self, gain=1.):
|
||||
|
@ -180,7 +192,7 @@ class ReidXavierUniform(init.Initializer):
|
|||
|
||||
def _initialize(self, arr):
|
||||
tmp = xavier_uniform_(arr, self.gain)
|
||||
_assignment(arr, tmp)
|
||||
assignment(arr, tmp)
|
||||
|
||||
|
||||
class ReidKaimingUniform(init.Initializer):
|
||||
|
@ -192,7 +204,7 @@ class ReidKaimingUniform(init.Initializer):
|
|||
|
||||
def _initialize(self, arr):
|
||||
tmp = kaiming_uniform_(arr, self.a, self.mode, self.nonlinearity)
|
||||
_assignment(arr, tmp)
|
||||
assignment(arr, tmp)
|
||||
|
||||
|
||||
class ReidKaimingNormal(init.Initializer):
|
||||
|
@ -204,4 +216,4 @@ class ReidKaimingNormal(init.Initializer):
|
|||
|
||||
def _initialize(self, arr):
|
||||
tmp = kaiming_normal_(arr, self.a, self.mode, self.nonlinearity)
|
||||
_assignment(arr, tmp)
|
||||
assignment(arr, tmp)
|
||||
|
|
|
@ -271,13 +271,19 @@ def run_train():
|
|||
keep_checkpoint_max = config.max_ckpts
|
||||
config.logger.info('keep_checkpoint_max: %d', keep_checkpoint_max)
|
||||
|
||||
ckpt_config = CheckpointConfig(save_checkpoint_steps=save_checkpoint_steps, keep_checkpoint_max=keep_checkpoint_max)
|
||||
config.logger.info('max_epoch_train: %d', config.max_epoch)
|
||||
ckpt_cb = ModelCheckpoint(config=ckpt_config, directory=config.ckpt_path, prefix='{}'.format(config.local_rank))
|
||||
callback_list = []
|
||||
config.epoch_cnt = 0
|
||||
progress_cb = ProgressMonitor(config)
|
||||
callback_list.append(progress_cb)
|
||||
if config.local_rank % 8 == 0:
|
||||
ckpt_config = CheckpointConfig(save_checkpoint_steps=save_checkpoint_steps,
|
||||
keep_checkpoint_max=keep_checkpoint_max)
|
||||
config.logger.info('max_epoch_train: %d', config.max_epoch)
|
||||
ckpt_cb = ModelCheckpoint(config=ckpt_config, directory=config.ckpt_path, prefix='{}'.format(config.local_rank))
|
||||
callback_list.append(ckpt_cb)
|
||||
|
||||
new_epoch_train = config.max_epoch * steps_per_epoch // config.log_interval
|
||||
model.train(new_epoch_train, de_dataset, callbacks=[progress_cb, ckpt_cb], sink_size=config.log_interval)
|
||||
model.train(new_epoch_train, de_dataset, callbacks=callback_list, sink_size=config.log_interval)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -16,12 +16,62 @@
|
|||
import mindspore.nn as nn
|
||||
from mindspore import Tensor
|
||||
from mindspore.common import dtype as mstype
|
||||
from mindspore.nn.loss.loss import _Loss
|
||||
from mindspore.nn.cell import Cell
|
||||
from mindspore.ops import functional as F
|
||||
from mindspore.ops import operations as P
|
||||
|
||||
class MyLoss(Cell):
|
||||
"""
|
||||
Base class for other losses.
|
||||
"""
|
||||
def __init__(self, reduction='mean'):
|
||||
super(MyLoss, self).__init__()
|
||||
if reduction is None:
|
||||
reduction = 'none'
|
||||
|
||||
class CrossEntropySmooth(_Loss):
|
||||
if reduction not in ('mean', 'sum', 'none'):
|
||||
raise ValueError(f"reduction method for {reduction.lower()} is not supported")
|
||||
|
||||
self.average = True
|
||||
self.reduce = True
|
||||
if reduction == 'sum':
|
||||
self.average = False
|
||||
if reduction == 'none':
|
||||
self.reduce = False
|
||||
|
||||
self.reduce_mean = P.ReduceMean()
|
||||
self.reduce_sum = P.ReduceSum()
|
||||
self.mul = P.Mul()
|
||||
self.cast = P.Cast()
|
||||
|
||||
def get_axis(self, x):
|
||||
shape = F.shape(x)
|
||||
length = F.tuple_len(shape)
|
||||
perm = F.make_range(0, length)
|
||||
return perm
|
||||
|
||||
def get_loss(self, x, weights=1.0):
|
||||
"""
|
||||
Computes the weighted loss
|
||||
Args:
|
||||
weights: Optional `Tensor` whose rank is either 0, or the same rank as inputs, and must be broadcastable to
|
||||
inputs (i.e., all dimensions must be either `1`, or the same as the corresponding inputs dimension).
|
||||
"""
|
||||
input_dtype = x.dtype
|
||||
x = self.cast(x, mstype.float32)
|
||||
weights = self.cast(weights, mstype.float32)
|
||||
x = self.mul(weights, x)
|
||||
if self.reduce and self.average:
|
||||
x = self.reduce_mean(x, self.get_axis(x))
|
||||
if self.reduce and not self.average:
|
||||
x = self.reduce_sum(x, self.get_axis(x))
|
||||
x = self.cast(x, input_dtype)
|
||||
return x
|
||||
|
||||
def construct(self, base, target):
|
||||
raise NotImplementedError
|
||||
|
||||
class CrossEntropySmooth(MyLoss):
|
||||
"""CrossEntropy"""
|
||||
def __init__(self, sparse=True, reduction='mean', smooth_factor=0., num_classes=1000):
|
||||
super(CrossEntropySmooth, self).__init__()
|
||||
|
|
Loading…
Reference in New Issue