public override void Update(NDArray labels, NDArray preds) { CheckLabelShapes(labels, preds, true); NDArray pred_label = null; if (preds.Shape != labels.Shape) { pred_label = preds.Argmax(Axis); } else { pred_label = preds; } var label = labels.Ravel().AsType(DType.Int32); pred_label = pred_label.Ravel().AsType(DType.Int32); var num_correct = nd.Equal(pred_label, label).AsType(DType.Float32).Sum(); sum_metric += num_correct; global_sum_metric += num_correct; num_inst += pred_label.Shape.Size; global_num_inst += pred_label.Shape.Size; }
private NDArrayOrSymbol F(NDArray x) { var match = nd.Contrib.BipartiteMatching(x, threshold: this._threshold, is_ascend: this._is_ascend); // make sure if iou(a, y) == iou(b, y), then b should also be a good match // otherwise positive/negative samples are confusing // potential argmax and max var pargmax = x.Argmax(axis: -1, keepdims: true); var maxs = x.Max(axis: -2, keepdims: true); NDArray mask = null; if (this._share_max) { mask = nd.BroadcastGreaterEqual(x + this._eps, maxs); mask = nd.Sum(mask, axis: -1, keepdims: true); } else { var pmax = nd.Pick(x, pargmax, axis: -1, keepdims: true); mask = nd.BroadcastGreaterEqual(pmax + this._eps, maxs); mask = nd.Pick(mask, pargmax, axis: -1, keepdims: true); } var new_match = nd.Where(mask > 0, pargmax, nd.OnesLike(pargmax) * -1); var result = nd.Where(match < 0, new_match.Squeeze(axis: -1), match); return(result); }
public static float Acc(NDArray output, NDArray label) { // output: (batch, num_output) float32 ndarray // label: (batch, ) int32 ndarray return(nd.Equal(output.Argmax(axis: 1), label.AsType(DType.Float32)).Mean()); }