private NDArrayOrSymbol F(NDArray a, NDArray b) { var _tup_1 = this._pre.Call(a); NDArray al = _tup_1[0]; NDArray at = _tup_1[1]; NDArray ar = _tup_1[2]; NDArray ab = _tup_1[3]; var _tup_2 = this._pre.Call(b); NDArray bl = _tup_2[0]; NDArray bt = _tup_2[1]; NDArray br = _tup_2[2]; NDArray bb = _tup_2[3]; // (B, N, M) var left = nd.BroadcastMaximum(al.ExpandDims(-1), bl.ExpandDims(-2)); var right = nd.BroadcastMinimum(ar.ExpandDims(-1), br.ExpandDims(-2)); var top = nd.BroadcastMaximum(at.ExpandDims(-1), bt.ExpandDims(-2)); var bot = nd.BroadcastMinimum(ab.ExpandDims(-1), bb.ExpandDims(-2)); // clip with (0, float16.max) var iw = nd.Clip(right - left + this._offset, a_min: 0, a_max: 65504); var ih = nd.Clip(bot - top + this._offset, a_min: 0, a_max: 65504); var i = iw * ih; // areas var area_a = ((ar - al + this._offset) * (ab - at + this._offset)).ExpandDims(-1); var area_b = ((br - bl + this._offset) * (bb - bt + this._offset)).ExpandDims(-2); var union = nd.BroadcastAdd(area_a, area_b) - i; return(i / (union + this._eps)); }
private NDArrayOrSymbol F(NDArray x, NDArray gamma, NDArray beta) { NDArray y = null; // normalization using (var ag = Autograd.TrainMode()) { y = x.ExpandDims(0).Reshape(0, 0, this.Ngroups, -1); y = y.Reshape(1, -3, -1); var batch = x.Shape[0]; y = nd.BatchNorm(y, nd.Ones(new Shape(batch * this.Ngroups), ctx: x.Context), nd.Zeros(new Shape(batch * this.Ngroups), ctx: x.Context), nd.Zeros(new Shape(batch * this.Ngroups), ctx: x.Context), nd.Ones(new Shape(batch * this.Ngroups), ctx: x.Context), eps: Epsilon, axis: Axis); } // scale and shift y = nd.ReshapeLike(y, x).Reshape(0, 0, -1); y = y * gamma.Reshape(1, -1, 1) + beta.Reshape(1, -1, 1); return(nd.ReshapeLike(y, x)); }
private NDArrayOrSymbol F(NDArray samples, NDArray matches, NDArray anchors, NDArray labels, NDArray refs) { var _tup_2 = this.class_agnostic_encoder.Call(samples, matches, anchors, refs); NDArray targets = _tup_2[0]; NDArray masks = _tup_2[1]; var ref_labels = nd.BroadcastLike(labels.Reshape(0, 1, -1), matches, lhs_axes: new Shape(1), rhs_axes: new Shape(1)); // labels [B, N, M] -> pick from matches [B, N] -> [B, N, 1] ref_labels = nd.Pick(ref_labels, matches, axis: 2).Reshape(0, -1).ExpandDims(2); // boolean array [B, N, C] var same_cids = nd.BroadcastEqual(ref_labels, nd.Reshape(nd.Arange(this._num_class), shape: new Shape(1, 1, -1))); // reduce box targets to positive samples only var indices = nd.SliceAxis(nd.Reshape(nd.Argsort(nd.SliceAxis(masks, axis: -1, begin: 0, end: 1), axis: 1, is_ascend: false), new Shape(this._batch_size, -1)), axis: 1, begin: 0, end: this._max_pos); var targets_tmp = new List <NDArray>(); var masks_tmp = new List <NDArray>(); var same_cids_tmp = new List <NDArray>(); foreach (var i in Enumerable.Range(0, this._batch_size)) { var ind = nd.SliceAxis(indices, axis: 0, begin: i, end: i + 1).Squeeze(axis: 0); var target = nd.SliceAxis(targets, axis: 0, begin: i, end: i + 1).Squeeze(axis: 0); var mask = nd.SliceAxis(masks, axis: 0, begin: i, end: i + 1).Squeeze(axis: 0); var same_cid = nd.SliceAxis(same_cids, axis: 0, begin: i, end: i + 1).Squeeze(axis: 0); targets_tmp.Add(nd.Take(target, ind).ExpandDims(axis: 0)); masks_tmp.Add(nd.Take(mask, ind).ExpandDims(axis: 0)); same_cids_tmp.Add(nd.Take(same_cid, ind).ExpandDims(axis: 0)); } targets = nd.Concat(targets_tmp, dim: 0); masks = nd.Concat(masks_tmp, dim: 0); same_cids = nd.Concat(same_cids_tmp, dim: 0).ExpandDims(3); // targets, masks [B, N_pos, C, 4] var all_targets = nd.BroadcastAxis(targets.ExpandDims(2), axis: new Shape(2), size: new Shape(this._num_class)); var all_masks = nd.BroadcastMul(masks.ExpandDims(2), nd.BroadcastAxis(same_cids, axis: new Shape(3), size: new Shape(4))); return(new NDArrayOrSymbol(all_targets, all_masks, indices)); }