Python mxnet.NDArray() Examples
The following are 7
code examples of mxnet.NDArray().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
mxnet
, or try the search function
.
Example #1
Source File: det_metrics.py From imgclsmob with MIT License | 6 votes |
def update(self, labels, preds): """ Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. """ det_bboxes = [] det_ids = [] det_scores = [] for x_rr, y in zip(preds, labels): bboxes = x_rr.slice_axis(axis=-1, begin=0, end=4) ids = x_rr.slice_axis(axis=-1, begin=4, end=5).squeeze(axis=2) scores = x_rr.slice_axis(axis=-1, begin=5, end=6).squeeze(axis=2) det_ids.append(ids) det_scores.append(scores) # clip to image size det_bboxes.append(bboxes.clip(0, self.img_height)) self.update2(det_bboxes, det_ids, det_scores)
Example #2
Source File: appnpconv.py From dgl with Apache License 2.0 | 5 votes |
def forward(self, graph, feat): r"""Compute APPNP layer. Parameters ---------- graph : DGLGraph The graph. feat : mx.NDArray The input feature of shape :math:`(N, *)` :math:`N` is the number of nodes, and :math:`*` could be of any shape. Returns ------- mx.NDArray The output feature of shape :math:`(N, *)` where :math:`*` should be the same as input shape. """ with graph.local_scope(): norm = mx.nd.power(mx.nd.clip( graph.in_degrees().astype(feat.dtype), a_min=1, a_max=float("inf")), -0.5) shp = norm.shape + (1,) * (feat.ndim - 1) norm = norm.reshape(shp).as_in_context(feat.context) feat_0 = feat for _ in range(self._k): # normalization by src node feat = feat * norm graph.ndata['h'] = feat graph.edata['w'] = self.edge_drop( nd.ones((graph.number_of_edges(), 1), ctx=feat.context)) graph.update_all(fn.u_mul_e('h', 'w', 'm'), fn.sum('m', 'h')) feat = graph.ndata.pop('h') # normalization by dst node feat = feat * norm feat = (1 - self._alpha) * feat + self._alpha * feat_0 return feat
Example #3
Source File: det_metrics.py From imgclsmob with MIT License | 5 votes |
def update(self, labels, preds): """ Updates the internal evaluation result. Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. """ for x_rr, label in zip(preds, labels): outputs = [] for output in x_rr: outputs.append(output.asnumpy()) label_split = label.split("/") resize_scale = float(label_split[2]) image_size = (int(label_split[3]), int(label_split[4])) bboxes, _ = self.predict(outputs, resize_scale, image_size) event_name = label_split[0] event_dir_name = os.path.join(self.output_dir_path, event_name) if not os.path.exists(event_dir_name): os.makedirs(event_dir_name) file_stem = label_split[1] fout = open(os.path.join(event_dir_name, file_stem + ".txt"), "w") fout.write(file_stem + "\n") fout.write(str(len(bboxes)) + "\n") for bbox in bboxes: fout.write("%d %d %d %d %.03f" % (math.floor(bbox[0]), math.floor(bbox[1]), math.ceil(bbox[2] - bbox[0]), math.ceil(bbox[3] - bbox[1]), bbox[4] if bbox[4] <= 1 else 1) + "\n") fout.close()
Example #4
Source File: mxnet.py From training_results_v0.6 with Apache License 2.0 | 4 votes |
def from_mxnet(symbol, arg_params=None, aux_params=None): """Convert from MXNet's model into compatible NNVM format. Parameters ---------- symbol : mxnet.Symbol or mxnet.gluon.HybridBlock MXNet symbol arg_params : dict of str to mx.NDArray The argument parameters in mxnet aux_params : dict of str to mx.NDArray The auxiliary parameters in mxnet Returns ------- sym : nnvm.Symbol Compatible nnvm symbol params : dict of str to tvm.NDArray The parameter dict to be used by nnvm """ try: import mxnet as mx except ImportError as e: raise ImportError('{}. MXNet is required to parse symbols.'.format(e)) if isinstance(symbol, mx.sym.Symbol): sym = _from_mxnet_impl(symbol, {}) params = {} arg_params = arg_params if arg_params else {} aux_params = aux_params if aux_params else {} for k, v in arg_params.items(): params[k] = tvm.nd.array(v.asnumpy()) for k, v in aux_params.items(): params[k] = tvm.nd.array(v.asnumpy()) elif isinstance(symbol, mx.gluon.HybridBlock): data = mx.sym.Variable('data') sym = symbol(data) sym = _from_mxnet_impl(sym, {}) params = {} for k, v in symbol.collect_params().items(): params[k] = tvm.nd.array(v.data().asnumpy()) elif isinstance(symbol, mx.gluon.Block): raise NotImplementedError("Only Hybrid Blocks are supported now.") else: msg = "mxnet.Symbol or gluon.HybridBlock expected, got {}".format(type(symbol)) raise ValueError(msg) if isinstance(sym, list): sym = _sym.Group(sym) return sym, params
Example #5
Source File: coco_instance.py From panoptic-fpn-gluon with Apache License 2.0 | 4 votes |
def update(self, pred_bboxes, pred_labels, pred_scores, pred_masks, *args, **kwargs): """Update internal buffer with latest predictions. Note that the statistics are not available until you call self.get() to return the metrics. Parameters ---------- pred_bboxes : mxnet.NDArray or numpy.ndarray Prediction bounding boxes with shape `B, N, 4`. Where B is the size of mini-batch, N is the number of bboxes. pred_labels : mxnet.NDArray or numpy.ndarray Prediction bounding boxes labels with shape `B, N`. pred_scores : mxnet.NDArray or numpy.ndarray Prediction bounding boxes scores with shape `B, N`. pred_masks: mxnet.NDArray or numpy.ndarray Prediction masks with *original* shape `H, W`. """ def as_numpy(a): """Convert a (list of) mx.NDArray into numpy.ndarray""" if isinstance(a, mx.nd.NDArray): a = a.asnumpy() return a # mask must be the same as image shape, so no batch dimension is supported pred_bbox, pred_label, pred_score, pred_mask = [ as_numpy(x) for x in [pred_bboxes, pred_labels, pred_scores, pred_masks]] # filter out padded detection & low confidence detections valid_pred = np.where((pred_label >= 0) & (pred_score >= self._score_thresh))[0] pred_bbox = pred_bbox[valid_pred].astype('float32') pred_label = pred_label.flat[valid_pred].astype('int32') pred_score = pred_score.flat[valid_pred].astype('float32') pred_mask = pred_mask[valid_pred].astype('uint8') imgid = self._img_ids[self._current_id] self._current_id += 1 # for each bbox detection in each image for bbox, label, score, mask in zip(pred_bbox, pred_label, pred_score, pred_mask): if label not in self.dataset.contiguous_id_to_json: # ignore non-exist class continue if score < self._score_thresh: continue category_id = self.dataset.contiguous_id_to_json[label] # convert [xmin, ymin, xmax, ymax] to [xmin, ymin, w, h] bbox[2:4] -= bbox[:2] # coco format full image mask to rle rle = self._encode_mask(mask) self._results.append({'image_id': imgid, 'category_id': category_id, 'bbox': list(map(lambda x: float(round(x, 2)), bbox[:4])), 'score': float(round(score, 3)), 'segmentation': rle})
Example #6
Source File: coco_detection.py From panoptic-fpn-gluon with Apache License 2.0 | 4 votes |
def update(self, pred_bboxes, pred_labels, pred_scores, *args, **kwargs): """Update internal buffer with latest predictions. Note that the statistics are not available until you call self.get() to return the metrics. Parameters ---------- pred_bboxes : mxnet.NDArray or numpy.ndarray Prediction bounding boxes with shape `B, N, 4`. Where B is the size of mini-batch, N is the number of bboxes. pred_labels : mxnet.NDArray or numpy.ndarray Prediction bounding boxes labels with shape `B, N`. pred_scores : mxnet.NDArray or numpy.ndarray Prediction bounding boxes scores with shape `B, N`. """ def as_numpy(a): """Convert a (list of) mx.NDArray into numpy.ndarray""" if isinstance(a, (list, tuple)): out = [x.asnumpy() if isinstance(x, mx.nd.NDArray) else x for x in a] return np.concatenate(out, axis=0) elif isinstance(a, mx.nd.NDArray): a = a.asnumpy() return a for pred_bbox, pred_label, pred_score in zip( *[as_numpy(x) for x in [pred_bboxes, pred_labels, pred_scores]]): valid_pred = np.where(pred_label.flat >= 0)[0] pred_bbox = pred_bbox[valid_pred, :].astype(np.float) pred_label = pred_label.flat[valid_pred].astype(int) pred_score = pred_score.flat[valid_pred].astype(np.float) imgid = self._img_ids[self._current_id] self._current_id += 1 if self._data_shape is not None: entry = self.dataset.coco.loadImgs(imgid)[0] orig_height = entry['height'] orig_width = entry['width'] height_scale = float(orig_height) / self._data_shape[0] width_scale = float(orig_width) / self._data_shape[1] else: height_scale, width_scale = (1., 1.) # for each bbox detection in each image for bbox, label, score in zip(pred_bbox, pred_label, pred_score): if label not in self.dataset.contiguous_id_to_json: # ignore non-exist class continue if score < self._score_thresh: continue category_id = self.dataset.contiguous_id_to_json[label] # rescale bboxes bbox[[0, 2]] *= width_scale bbox[[1, 3]] *= height_scale # convert [xmin, ymin, xmax, ymax] to [xmin, ymin, w, h] bbox[2:4] -= (bbox[:2] - 1) self._results.append({'image_id': imgid, 'category_id': category_id, 'bbox': bbox[:4].tolist(), 'score': score})
Example #7
Source File: coco_detection.py From cascade_rcnn_gluon with Apache License 2.0 | 4 votes |
def update(self, pred_bboxes, pred_labels, pred_scores, *args, **kwargs): """Update internal buffer with latest predictions. Note that the statistics are not available until you call self.get() to return the metrics. Parameters ---------- pred_bboxes : mxnet.NDArray or numpy.ndarray Prediction bounding boxes with shape `B, N, 4`. Where B is the size of mini-batch, N is the number of bboxes. pred_labels : mxnet.NDArray or numpy.ndarray Prediction bounding boxes labels with shape `B, N`. pred_scores : mxnet.NDArray or numpy.ndarray Prediction bounding boxes scores with shape `B, N`. """ def as_numpy(a): """Convert a (list of) mx.NDArray into numpy.ndarray""" if isinstance(a, (list, tuple)): out = [x.asnumpy() if isinstance(x, mx.nd.NDArray) else x for x in a] return np.concatenate(out, axis=0) elif isinstance(a, mx.nd.NDArray): a = a.asnumpy() return a for pred_bbox, pred_label, pred_score in zip( *[as_numpy(x) for x in [pred_bboxes, pred_labels, pred_scores]]): valid_pred = np.where(pred_label.flat >= 0)[0] pred_bbox = pred_bbox[valid_pred, :].astype(np.float) pred_label = pred_label.flat[valid_pred].astype(int) pred_score = pred_score.flat[valid_pred].astype(np.float) imgid = self._img_ids[self._current_id] self._current_id += 1 if self._data_shape is not None: entry = self.dataset.coco.loadImgs(imgid)[0] orig_height = entry['height'] orig_width = entry['width'] height_scale = orig_height / self._data_shape[0] width_scale = orig_width / self._data_shape[1] else: height_scale, width_scale = (1., 1.) # for each bbox detection in each image for bbox, label, score in zip(pred_bbox, pred_label, pred_score): if label not in self.dataset.contiguous_id_to_json: # ignore non-exist class continue if score < self._score_thresh: continue category_id = self.dataset.contiguous_id_to_json[label] # rescale bboxes bbox[[0, 2]] *= width_scale bbox[[1, 3]] *= height_scale # convert [xmin, ymin, xmax, ymax] to [xmin, ymin, w, h] bbox[2:4] -= (bbox[:2] - 1) self._results.append({'image_id': imgid, 'category_id': category_id, 'bbox': bbox[:4].tolist(), 'score': score})