From: @lijiaqi0612 Reviewed-by: @kingxian,@kisnwang Signed-off-by: @kingxianpull/13669/MERGE
| @@ -436,7 +436,7 @@ class DiceLoss(_Loss): | |||||
| >>> y = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mstype.float32) | >>> y = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mstype.float32) | ||||
| >>> output = loss(y_pred, y) | >>> output = loss(y_pred, y) | ||||
| >>> print(output) | >>> print(output) | ||||
| [0.38596618] | |||||
| 0.38596618 | |||||
| """ | """ | ||||
| def __init__(self, smooth=1e-5): | def __init__(self, smooth=1e-5): | ||||
| super(DiceLoss, self).__init__() | super(DiceLoss, self).__init__() | ||||
| @@ -1031,7 +1031,11 @@ class FocalLoss(_Loss): | |||||
| r""" | r""" | ||||
| The loss function proposed by Kaiming team in their paper ``Focal Loss for Dense Object Detection`` improves the | The loss function proposed by Kaiming team in their paper ``Focal Loss for Dense Object Detection`` improves the | ||||
| effect of image object detection. It is a loss function to solve the imbalance of categories and the difference of | effect of image object detection. It is a loss function to solve the imbalance of categories and the difference of | ||||
| classification difficulty. | |||||
| classification difficulty. If you want to learn more, please refer to the paper. | |||||
| `https://arxiv.org/pdf/1708.02002.pdf`. The function is shown as follows: | |||||
| .. math:: | |||||
| FL(p_t) = -(1-p_t)^\gamma log(p_t) | |||||
| Args: | Args: | ||||
| gamma (float): Gamma is used to adjust the steepness of weight curve in focal loss. Default: 2.0. | gamma (float): Gamma is used to adjust the steepness of weight curve in focal loss. Default: 2.0. | ||||
| @@ -1042,25 +1046,25 @@ class FocalLoss(_Loss): | |||||
| Inputs: | Inputs: | ||||
| - **predict** (Tensor) - Tensor of shape should be (B, C) or (B, C, H) or (B, C, H, W). Where C is the number | - **predict** (Tensor) - Tensor of shape should be (B, C) or (B, C, H) or (B, C, H, W). Where C is the number | ||||
| of classes. Its value is greater than 1. If the shape is (B, C, H, W) or (B, C, H), the H or product of H | |||||
| and W should be the same as target. | |||||
| of classes. Its value is greater than 1. If the shape is (B, C, H, W) or (B, C, H), the H or product of H | |||||
| and W should be the same as target. | |||||
| - **target** (Tensor) - Tensor of shape should be (B, C) or (B, C, H) or (B, C, H, W). The value of C is 1 or | - **target** (Tensor) - Tensor of shape should be (B, C) or (B, C, H) or (B, C, H, W). The value of C is 1 or | ||||
| it needs to be the same as predict's C. If C is not 1, the shape of target should be the same as that of | |||||
| predict, where C is the number of classes. If the shape is (B, C, H, W) or (B, C, H), the H or product of H | |||||
| and W should be the same as predict. | |||||
| it needs to be the same as predict's C. If C is not 1, the shape of target should be the same as that of | |||||
| predict, where C is the number of classes. If the shape is (B, C, H, W) or (B, C, H), the H or product of H | |||||
| and W should be the same as predict. | |||||
| Outputs: | Outputs: | ||||
| Tensor, it's a tensor with the same shape and type as input `predict`. | Tensor, it's a tensor with the same shape and type as input `predict`. | ||||
| Raises: | Raises: | ||||
| TypeError: If the data type of ``gamma`` is not float.. | |||||
| TypeError: If ``weight`` is not a Parameter. | |||||
| TypeError: If the data type of ``gamma`` is not float. | |||||
| TypeError: If ``weight`` is not a Tensor. | |||||
| ValueError: If ``target`` dim different from ``predict``. | ValueError: If ``target`` dim different from ``predict``. | ||||
| ValueError: If ``target`` channel is not 1 and ``target`` shape is different from ``predict``. | ValueError: If ``target`` channel is not 1 and ``target`` shape is different from ``predict``. | ||||
| ValueError: If ``reduction`` is not one of 'none', 'mean', 'sum'. | ValueError: If ``reduction`` is not one of 'none', 'mean', 'sum'. | ||||
| Supported Platforms: | Supported Platforms: | ||||
| ``Ascend`` ``GPU`` | |||||
| ``Ascend`` | |||||
| Example: | Example: | ||||
| >>> predict = Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], mstype.float32) | >>> predict = Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], mstype.float32) | ||||
| @@ -32,14 +32,18 @@ def auc(x, y, reorder=False): | |||||
| Returns: | Returns: | ||||
| area (float): Compute result. | area (float): Compute result. | ||||
| Supported Platforms: | |||||
| ``Ascend`` ``GPU`` ``CPU`` | |||||
| Examples: | Examples: | ||||
| >>> y_pred = np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]]) | >>> y_pred = np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]]) | ||||
| >>> y = np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]]) | >>> y = np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]]) | ||||
| >>> metric = ROC(pos_label=2) | |||||
| >>> metric = nn.ROC(pos_label=2) | |||||
| >>> metric.clear() | >>> metric.clear() | ||||
| >>> metric.update(y_pred, y) | >>> metric.update(y_pred, y) | ||||
| >>> fpr, tpr, thre = metric.eval() | >>> fpr, tpr, thre = metric.eval() | ||||
| >>> output = auc(fpr, tpr) | >>> output = auc(fpr, tpr) | ||||
| >>> print(output) | |||||
| 0.5357142857142857 | 0.5357142857142857 | ||||
| """ | """ | ||||
| if not isinstance(x, np.ndarray) or not isinstance(y, np.ndarray): | if not isinstance(x, np.ndarray) or not isinstance(y, np.ndarray): | ||||
| @@ -27,6 +27,9 @@ class BleuScore(Metric): | |||||
| n_gram (int): The n_gram value ranged from 1 to 4. Default: 4 | n_gram (int): The n_gram value ranged from 1 to 4. Default: 4 | ||||
| smooth (bool): Whether or not to apply smoothing. Default: False | smooth (bool): Whether or not to apply smoothing. Default: False | ||||
| Supported Platforms: | |||||
| ``Ascend`` ``GPU`` ``CPU`` | |||||
| Example: | Example: | ||||
| >>> candidate_corpus = [['i', 'have', 'a', 'pen', 'on', 'my', 'desk']] | >>> candidate_corpus = [['i', 'have', 'a', 'pen', 'on', 'my', 'desk']] | ||||
| >>> reference_corpus = [[['i', 'have', 'a', 'pen', 'in', 'my', 'desk'], | >>> reference_corpus = [[['i', 'have', 'a', 'pen', 'in', 'my', 'desk'], | ||||
| @@ -35,6 +38,7 @@ class BleuScore(Metric): | |||||
| >>> metric.clear() | >>> metric.clear() | ||||
| >>> metric.update(candidate_corpus, reference_corpus) | >>> metric.update(candidate_corpus, reference_corpus) | ||||
| >>> bleu_score = metric.eval() | >>> bleu_score = metric.eval() | ||||
| >>> print(output) | |||||
| 0.5946035575013605 | 0.5946035575013605 | ||||
| """ | """ | ||||
| def __init__(self, n_gram=4, smooth=False): | def __init__(self, n_gram=4, smooth=False): | ||||
| @@ -39,10 +39,13 @@ class ConfusionMatrix(Metric): | |||||
| threshold (float): A threshold, which is used to compare with the input tensor. Default: 0.5. | threshold (float): A threshold, which is used to compare with the input tensor. Default: 0.5. | ||||
| Supported Platforms: | |||||
| ``Ascend`` ``GPU`` ``CPU`` | |||||
| Examples: | Examples: | ||||
| >>> x = Tensor(np.array([1, 0, 1, 0])) | >>> x = Tensor(np.array([1, 0, 1, 0])) | ||||
| >>> y = Tensor(np.array([1, 0, 0, 1])) | >>> y = Tensor(np.array([1, 0, 0, 1])) | ||||
| >>> metric = nn.ConfusionMatrix(num_classes=2, normalize=NO_NORM, threshold=0.5) | |||||
| >>> metric = nn.ConfusionMatrix(num_classes=2, normalize='no_norm', threshold=0.5) | |||||
| >>> metric.clear() | >>> metric.clear() | ||||
| >>> metric.update(x, y) | >>> metric.update(x, y) | ||||
| >>> output = metric.eval() | >>> output = metric.eval() | ||||
| @@ -165,9 +168,12 @@ class ConfusionMatrixMetric(Metric): | |||||
| calculation_method is True. Default: "mean". Choose from: | calculation_method is True. Default: "mean". Choose from: | ||||
| ["none", "mean", "sum", "mean_batch", "sum_batch", "mean_channel", "sum_channel"]. | ["none", "mean", "sum", "mean_batch", "sum_batch", "mean_channel", "sum_channel"]. | ||||
| Supported Platforms: | |||||
| ``Ascend`` ``GPU`` ``CPU`` | |||||
| Examples: | Examples: | ||||
| >>> metric = ConfusionMatrixMetric(skip_channel=True, metric_name="tpr", | >>> metric = ConfusionMatrixMetric(skip_channel=True, metric_name="tpr", | ||||
| >>> calculation_method=False, decrease="mean") | |||||
| ... calculation_method=False, decrease="mean") | |||||
| >>> metric.clear() | >>> metric.clear() | ||||
| >>> x = Tensor(np.array([[[0], [1]], [[1], [0]]])) | >>> x = Tensor(np.array([[[0], [1]], [[1], [0]]])) | ||||
| >>> y = Tensor(np.array([[[0], [1]], [[0], [1]]])) | >>> y = Tensor(np.array([[[0], [1]], [[0], [1]]])) | ||||
| @@ -29,17 +29,21 @@ class CosineSimilarity(Metric): | |||||
| Return: | Return: | ||||
| A square matrix (input1, input1) with the similarity scores between all elements. | A square matrix (input1, input1) with the similarity scores between all elements. | ||||
| If sum or mean are used, then returns (b, 1) with the reduced value for each row | |||||
| If sum or mean are used, then returns (b, 1) with the reduced value for each row. | |||||
| Supported Platforms: | |||||
| ``Ascend`` ``GPU`` ``CPU`` | |||||
| Example: | Example: | ||||
| >>> test_data = np.random.randn(4, 8) | |||||
| >>> test_data = np.array([[1, 3, 4, 7], [2, 4, 2, 5], [3, 1, 5, 8]]) | |||||
| >>> metric = CosineSimilarity() | >>> metric = CosineSimilarity() | ||||
| >>> metric.clear() | >>> metric.clear() | ||||
| >>> metric.update(test_data) | >>> metric.update(test_data) | ||||
| >>> square_matrix = metric.eval() | >>> square_matrix = metric.eval() | ||||
| [[0. -0.14682831 0.19102288 -0.36204537] | |||||
| ... | |||||
| ] | |||||
| >>> print(square_matrix) | |||||
| [[0. 0.94025615 0.95162452] | |||||
| [0.94025615 0. 0.86146098] | |||||
| [0.95162452 0.86146098 0.]] | |||||
| """ | """ | ||||
| def __init__(self, similarity='cosine', reduction='none', zero_diagonal=True): | def __init__(self, similarity='cosine', reduction='none', zero_diagonal=True): | ||||
| super().__init__() | super().__init__() | ||||
| @@ -32,6 +32,9 @@ class Dice(Metric): | |||||
| smooth (float): A term added to the denominator to improve numerical stability. Should be greater than 0. | smooth (float): A term added to the denominator to improve numerical stability. Should be greater than 0. | ||||
| Default: 1e-5. | Default: 1e-5. | ||||
| Supported Platforms: | |||||
| ``Ascend`` ``GPU`` ``CPU`` | |||||
| Examples: | Examples: | ||||
| >>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]])) | >>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]])) | ||||
| >>> y = Tensor(np.array([[0, 1], [1, 0], [0, 1]])) | >>> y = Tensor(np.array([[0, 1], [1, 0], [0, 1]])) | ||||
| @@ -86,10 +86,13 @@ class HausdorffDistance(Metric): | |||||
| here the bounding box is achieved by (y_pred | y) which represents the union set of two images. | here the bounding box is achieved by (y_pred | y) which represents the union set of two images. | ||||
| Default: True. | Default: True. | ||||
| Supported Platforms: | |||||
| ``Ascend`` ``GPU`` | |||||
| Examples: | Examples: | ||||
| >>> x = Tensor(np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]])) | >>> x = Tensor(np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]])) | ||||
| >>> y = Tensor(np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]])) | >>> y = Tensor(np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]])) | ||||
| >>> metric = nn.HausdorffDistance | |||||
| >>> metric = nn.HausdorffDistance() | |||||
| >>> metric.clear() | >>> metric.clear() | ||||
| >>> metric.update(x, y, 0) | >>> metric.update(x, y, 0) | ||||
| >>> mean_average_distance = metric.eval() | >>> mean_average_distance = metric.eval() | ||||
| @@ -32,6 +32,9 @@ class MeanSurfaceDistance(Metric): | |||||
| if sets ``symmetric = True``, the average symmetric surface distance between these two inputs | if sets ``symmetric = True``, the average symmetric surface distance between these two inputs | ||||
| will be returned. Defaults: False. | will be returned. Defaults: False. | ||||
| Supported Platforms: | |||||
| ``Ascend`` ``GPU`` ``CPU`` | |||||
| Examples: | Examples: | ||||
| >>> x = Tensor(np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]])) | >>> x = Tensor(np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]])) | ||||
| >>> y = Tensor(np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]])) | >>> y = Tensor(np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]])) | ||||
| @@ -13,7 +13,6 @@ | |||||
| # limitations under the License. | # limitations under the License. | ||||
| # ============================================================================ | # ============================================================================ | ||||
| """OcclusionSensitivity.""" | """OcclusionSensitivity.""" | ||||
| from collections.abc import Sequence | |||||
| import numpy as np | import numpy as np | ||||
| from mindspore import nn | from mindspore import nn | ||||
| from mindspore.common.tensor import Tensor | from mindspore.common.tensor import Tensor | ||||
| @@ -47,16 +46,19 @@ class OcclusionSensitivity(Metric): | |||||
| as the input image. If a bounding box is used, the output image will be cropped to this size. | as the input image. If a bounding box is used, the output image will be cropped to this size. | ||||
| Default: None. | Default: None. | ||||
| Supported Platforms: | |||||
| ``Ascend`` ``GPU`` | |||||
| Example: | Example: | ||||
| >>> class DenseNet(nn.Cell): | >>> class DenseNet(nn.Cell): | ||||
| >>> def init(self): | |||||
| >>> super(DenseNet, self).init() | |||||
| >>> w = np.array([[0.1, 0.8, 0.1, 0.1],[1, 1, 1, 1]]).astype(np.float32) | |||||
| >>> b = np.array([0.3, 0.6]).astype(np.float32) | |||||
| >>> self.dense = nn.Dense(4, 2, weight_init=Tensor(w), bias_init=Tensor(b)) | |||||
| >>> | |||||
| >>> def construct(self, x): | |||||
| >>> return self.dense(x) | |||||
| ... def init(self): | |||||
| ... super(DenseNet, self).init() | |||||
| ... w = np.array([[0.1, 0.8, 0.1, 0.1],[1, 1, 1, 1]]).astype(np.float32) | |||||
| ... b = np.array([0.3, 0.6]).astype(np.float32) | |||||
| ... self.dense = nn.Dense(4, 2, weight_init=Tensor(w), bias_init=Tensor(b)) | |||||
| ... | |||||
| ... def construct(self, x): | |||||
| ... return self.dense(x) | |||||
| >>> | >>> | ||||
| >>> model = DenseNet() | >>> model = DenseNet() | ||||
| >>> test_data = np.array([[0.1, 0.2, 0.3, 0.4]]).astype(np.float32) | >>> test_data = np.array([[0.1, 0.2, 0.3, 0.4]]).astype(np.float32) | ||||
| @@ -65,6 +67,7 @@ class OcclusionSensitivity(Metric): | |||||
| >>> metric.clear() | >>> metric.clear() | ||||
| >>> metric.update(model, test_data, label) | >>> metric.update(model, test_data, label) | ||||
| >>> score = metric.eval() | >>> score = metric.eval() | ||||
| >>> print(score) | |||||
| [0.29999995 0.6 1 0.9] | [0.29999995 0.6 1 0.9] | ||||
| """ | """ | ||||
| def __init__(self, pad_val=0.0, margin=2, n_batch=128, b_box=None): | def __init__(self, pad_val=0.0, margin=2, n_batch=128, b_box=None): | ||||
| @@ -32,6 +32,9 @@ class Perplexity(Metric): | |||||
| ignore_label (int): Index of an invalid label to be ignored when counting. If set to `None`, it will include all | ignore_label (int): Index of an invalid label to be ignored when counting. If set to `None`, it will include all | ||||
| entries. Default: -1. | entries. Default: -1. | ||||
| Supported Platforms: | |||||
| ``Ascend`` ``GPU`` ``CPU`` | |||||
| Examples: | Examples: | ||||
| >>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]])) | >>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]])) | ||||
| >>> y = Tensor(np.array([1, 0, 1])) | >>> y = Tensor(np.array([1, 0, 1])) | ||||
| @@ -39,6 +42,7 @@ class Perplexity(Metric): | |||||
| >>> metric.clear() | >>> metric.clear() | ||||
| >>> metric.update(x, y) | >>> metric.update(x, y) | ||||
| >>> perplexity = metric.eval() | >>> perplexity = metric.eval() | ||||
| >>> print(perplexity) | |||||
| 2.231443166940565 | 2.231443166940565 | ||||
| """ | """ | ||||
| @@ -30,6 +30,9 @@ class ROC(Metric): | |||||
| to 1. For multiclass problems, this argument should not be set, as it is iteratively changed in the | to 1. For multiclass problems, this argument should not be set, as it is iteratively changed in the | ||||
| range [0,num_classes-1]. Default: None. | range [0,num_classes-1]. Default: None. | ||||
| Supported Platforms: | |||||
| ``Ascend`` ``GPU`` ``CPU`` | |||||
| Examples: | Examples: | ||||
| >>> # 1) binary classification example | >>> # 1) binary classification example | ||||
| >>> x = Tensor(np.array([3, 1, 4, 2])) | >>> x = Tensor(np.array([3, 1, 4, 2])) | ||||
| @@ -38,8 +41,11 @@ class ROC(Metric): | |||||
| >>> metric.clear() | >>> metric.clear() | ||||
| >>> metric.update(x, y) | >>> metric.update(x, y) | ||||
| >>> fpr, tpr, thresholds = metric.eval() | >>> fpr, tpr, thresholds = metric.eval() | ||||
| >>> print(fpr) | |||||
| [0., 0., 0.33333333, 0.6666667, 1.] | [0., 0., 0.33333333, 0.6666667, 1.] | ||||
| >>> print(tpr) | |||||
| [0., 1, 1., 1., 1.] | [0., 1, 1., 1., 1.] | ||||
| >>> print(thresholds) | |||||
| [5, 4, 3, 2, 1] | [5, 4, 3, 2, 1] | ||||
| >>> | >>> | ||||
| >>> # 2) multiclass classification example | >>> # 2) multiclass classification example | ||||
| @@ -50,9 +56,12 @@ class ROC(Metric): | |||||
| >>> metric.clear() | >>> metric.clear() | ||||
| >>> metric.update(x, y) | >>> metric.update(x, y) | ||||
| >>> fpr, tpr, thresholds = metric.eval() | >>> fpr, tpr, thresholds = metric.eval() | ||||
| >>> print(fpr) | |||||
| [array([0., 0., 0.33333333, 0.66666667, 1.]), array([0., 0.33333333, 0.33333333, 1.]), | [array([0., 0., 0.33333333, 0.66666667, 1.]), array([0., 0.33333333, 0.33333333, 1.]), | ||||
| array([0., 0.33333333, 1.]), array([0., 0., 1.])] | array([0., 0.33333333, 1.]), array([0., 0., 1.])] | ||||
| >>> print(tpr) | |||||
| [array([0., 1., 1., 1., 1.]), array([0., 0., 1., 1.]), array([0., 1., 1.]), array([0., 1., 1.])] | [array([0., 1., 1., 1., 1.]), array([0., 0., 1., 1.]), array([0., 1., 1.]), array([0., 1., 1.])] | ||||
| >>> print(thresholds) | |||||
| [array([1.28, 0.28, 0.2, 0.1, 0.05]), array([1.55, 0.55, 0.2, 0.05]), array([1.15, 0.15, 0.05]), | [array([1.28, 0.28, 0.2, 0.1, 0.05]), array([1.55, 0.55, 0.2, 0.05]), array([1.15, 0.15, 0.05]), | ||||
| array([1.75, 0.75, 0.05])] | array([1.75, 0.75, 0.05])] | ||||
| """ | """ | ||||
| @@ -33,6 +33,9 @@ class RootMeanSquareDistance(Metric): | |||||
| if sets ``symmetric = True``, the average symmetric surface distance between these two inputs | if sets ``symmetric = True``, the average symmetric surface distance between these two inputs | ||||
| will be returned. Defaults: False. | will be returned. Defaults: False. | ||||
| Supported Platforms: | |||||
| ``Ascend`` ``GPU`` ``CPU`` | |||||
| Examples: | Examples: | ||||
| >>> x = Tensor(np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]])) | >>> x = Tensor(np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]])) | ||||
| >>> y = Tensor(np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]])) | >>> y = Tensor(np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]])) | ||||