Python mxnet.autograd.is_training() Examples
The following are 22
code examples of mxnet.autograd.is_training().
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.autograd
, or try the search function
.
Example #1
Source File: center_net.py From gluon-cv with Apache License 2.0 | 6 votes |
def hybrid_forward(self, F, x): # pylint: disable=arguments-differ """Hybrid forward of center net""" y = self.base_network(x) out = [head(y) for head in self.heads] out[0] = F.sigmoid(out[0]) if autograd.is_training(): out[0] = F.clip(out[0], 1e-4, 1 - 1e-4) return tuple(out) if self.flip_test: y_flip = self.base_network(x.flip(axis=3)) out_flip = [head(y_flip) for head in self.heads] out_flip[0] = F.sigmoid(out_flip[0]) out[0] = (out[0] + out_flip[0].flip(axis=3)) * 0.5 out[1] = (out[1] + out_flip[1].flip(axis=3)) * 0.5 heatmap = out[0] keep = F.broadcast_equal(self.heatmap_nms(heatmap), heatmap) results = self.decoder(keep * heatmap, out[1], out[2]) return results
Example #2
Source File: syncbn.py From cascade_rcnn_gluon with Apache License 2.0 | 6 votes |
def hybrid_forward(self, F, x, gamma, beta, running_mean, running_var): """Hybrid forward""" if not autograd.is_training(): return F.BatchNorm(x, gamma, beta, running_mean, running_var, name='fwd', **self._kwargs) isum, isqu = F.SumSquare(x) #isum = x.sum(axis=1, exclude=True) #isqu = (x**2).sum(axis=1, exclude=True) N = self.ndevices * x.shape[0] * x.shape[2] * x.shape[3] allreduce = AllReduce(self._prefix) osum, osqu = allreduce(isum, isqu) # calc mean and std mean = osum / N sumvar = osqu - osum * osum / N bias_var = sumvar / N std = F.sqrt(F.maximum(bias_var, self.eps)) # update running mean and var with autograd.pause(): unbias_var = sumvar / (N - 1) self.updater(self.running_mean, self.running_var, mean, unbias_var, self.momentum, x.context) # update running mean and var output = F.DecoupleBatchNorm(x, gamma, beta, mean, std) return output
Example #3
Source File: oth_centernet.py From imgclsmob with MIT License | 6 votes |
def hybrid_forward(self, F, x): # pylint: disable=arguments-differ """Hybrid forward of center net""" y = self.base_network(x) out = [head(y) for head in self.heads] out[0] = F.sigmoid(out[0]) if autograd.is_training(): out[0] = F.clip(out[0], 1e-4, 1 - 1e-4) return tuple(out) if self.flip_test: y_flip = self.base_network(x.flip(axis=3)) out_flip = [head(y_flip) for head in self.heads] out_flip[0] = F.sigmoid(out_flip[0]) out[0] = (out[0] + out_flip[0].flip(axis=3)) * 0.5 out[1] = (out[1] + out_flip[1].flip(axis=3)) * 0.5 heatmap = out[0] keep = F.broadcast_equal(self.heatmap_nms(heatmap), heatmap) results = self.decoder(keep * heatmap, out[1], out[2]) a, b, c = results a = a.expand_dims(2) b = b.expand_dims(2) results = F.concat(c, a, b, dim=2) return results
Example #4
Source File: oth_centernet2.py From imgclsmob with MIT License | 6 votes |
def hybrid_forward(self, F, x): y = self.backbone(x) out = [head(y) for head in self.heads] out[0] = F.sigmoid(out[0]) if autograd.is_training(): out[0] = F.clip(out[0], 1e-4, 1 - 1e-4) return tuple(out) heatmap = out[0] keep = F.broadcast_equal(self.heatmap_nms(heatmap), heatmap) results = self.decoder(keep * heatmap, out[1], out[2]) a, b, c = results a = a.expand_dims(2) b = b.expand_dims(2) results = F.concat(c, a, b, dim=2) return results
Example #5
Source File: rpn.py From cascade_rcnn_gluon with Apache License 2.0 | 6 votes |
def hybrid_forward(self, F, x, img): """Forward RPN. The behavior during traing and inference is different. Parameters ---------- x : mxnet.nd.NDArray or mxnet.symbol Feature tensor. img : mxnet.nd.NDArray or mxnet.symbol The original input image. Returns ------- (rpn_score, rpn_box) Returns predicted scores and regions which are candidates of objects. """ anchors = self.anchor_generator(x) x = self.conv1(x) raw_rpn_scores = self.score(x).transpose(axes=(0, 2, 3, 1)).reshape((0, -1, 1)) rpn_scores = F.sigmoid(F.stop_gradient(raw_rpn_scores)) rpn_box_pred = self.loc(x).transpose(axes=(0, 2, 3, 1)).reshape((0, -1, 4)) rpn_score, rpn_box = self.region_proposaler( anchors, rpn_scores, F.stop_gradient(rpn_box_pred), img) if autograd.is_training(): # return raw predictions as well in training for bp return rpn_score, rpn_box, raw_rpn_scores, rpn_box_pred, anchors return rpn_score, rpn_box
Example #6
Source File: ssd.py From gluon-cv with Apache License 2.0 | 5 votes |
def hybrid_forward(self, F, x): """Hybrid forward""" features = self.features(x) cls_preds = [F.flatten(F.transpose(cp(feat), (0, 2, 3, 1))) for feat, cp in zip(features, self.class_predictors)] box_preds = [F.flatten(F.transpose(bp(feat), (0, 2, 3, 1))) for feat, bp in zip(features, self.box_predictors)] anchors = [F.reshape(ag(feat), shape=(1, -1)) for feat, ag in zip(features, self.anchor_generators)] cls_preds = F.concat(*cls_preds, dim=1).reshape((0, -1, self.num_classes + 1)) box_preds = F.concat(*box_preds, dim=1).reshape((0, -1, 4)) anchors = F.concat(*anchors, dim=1).reshape((1, -1, 4)) if autograd.is_training(): return [cls_preds, box_preds, anchors] bboxes = self.bbox_decoder(box_preds, anchors) cls_ids, scores = self.cls_decoder(F.softmax(cls_preds, axis=-1)) results = [] for i in range(self.num_classes): cls_id = cls_ids.slice_axis(axis=-1, begin=i, end=i+1) score = scores.slice_axis(axis=-1, begin=i, end=i+1) # per class results per_result = F.concat(*[cls_id, score, bboxes], dim=-1) results.append(per_result) result = F.concat(*results, dim=1) if self.nms_thresh > 0 and self.nms_thresh < 1: result = F.contrib.box_nms( result, overlap_thresh=self.nms_thresh, topk=self.nms_topk, valid_thresh=0.01, id_index=0, score_index=1, coord_start=2, force_suppress=False) if self.post_nms > 0: result = result.slice_axis(axis=1, begin=0, end=self.post_nms) ids = F.slice_axis(result, axis=2, begin=0, end=1) scores = F.slice_axis(result, axis=2, begin=1, end=2) bboxes = F.slice_axis(result, axis=2, begin=2, end=6) return ids, scores, bboxes
Example #7
Source File: panoptic_fpn.py From panoptic-fpn-gluon with Apache License 2.0 | 5 votes |
def hybrid_forward(self, F, x, gtbox=None): # Stuff predict if autograd.is_training(): cls_pred, box_pred, mask_pred, rpn_box, samples, matches, \ raw_rpn_score, raw_rpn_box, anchors = \ super(PanopticFPN, self).hybrid_forward(F, x, gtbox) seg_pred = self.seg_head(x) return cls_pred, box_pred, mask_pred, seg_pred, rpn_box, samples, \ matches, raw_rpn_score, raw_rpn_box, anchors else: cls_ids, cls_scores, boxes, masks = \ super(PanopticFPN, self).hybrid_forward(F, x) segms = self.seg_head(x) return cls_ids, cls_scores, boxes, masks, segms
Example #8
Source File: ssd.py From panoptic-fpn-gluon with Apache License 2.0 | 5 votes |
def hybrid_forward(self, F, x): """Hybrid forward""" features = self.features(x) cls_preds = [F.flatten(F.transpose(cp(feat), (0, 2, 3, 1))) for feat, cp in zip(features, self.class_predictors)] box_preds = [F.flatten(F.transpose(bp(feat), (0, 2, 3, 1))) for feat, bp in zip(features, self.box_predictors)] anchors = [F.reshape(ag(feat), shape=(1, -1)) for feat, ag in zip(features, self.anchor_generators)] cls_preds = F.concat(*cls_preds, dim=1).reshape((0, -1, self.num_classes + 1)) box_preds = F.concat(*box_preds, dim=1).reshape((0, -1, 4)) anchors = F.concat(*anchors, dim=1).reshape((1, -1, 4)) if autograd.is_training(): return [cls_preds, box_preds, anchors] bboxes = self.bbox_decoder(box_preds, anchors) cls_ids, scores = self.cls_decoder(F.softmax(cls_preds, axis=-1)) results = [] for i in range(self.num_classes): cls_id = cls_ids.slice_axis(axis=-1, begin=i, end=i+1) score = scores.slice_axis(axis=-1, begin=i, end=i+1) # per class results per_result = F.concat(*[cls_id, score, bboxes], dim=-1) results.append(per_result) result = F.concat(*results, dim=1) if self.nms_thresh > 0 and self.nms_thresh < 1: result = F.contrib.box_nms( result, overlap_thresh=self.nms_thresh, topk=self.nms_topk, valid_thresh=0.01, id_index=0, score_index=1, coord_start=2, force_suppress=False) if self.post_nms > 0: result = result.slice_axis(axis=1, begin=0, end=self.post_nms) ids = F.slice_axis(result, axis=2, begin=0, end=1) scores = F.slice_axis(result, axis=2, begin=1, end=2) bboxes = F.slice_axis(result, axis=2, begin=2, end=6) return ids, scores, bboxes
Example #9
Source File: ssd.py From cascade_rcnn_gluon with Apache License 2.0 | 5 votes |
def hybrid_forward(self, F, x): """Hybrid forward""" features = self.features(x) cls_preds = [F.flatten(F.transpose(cp(feat), (0, 2, 3, 1))) for feat, cp in zip(features, self.class_predictors)] box_preds = [F.flatten(F.transpose(bp(feat), (0, 2, 3, 1))) for feat, bp in zip(features, self.box_predictors)] anchors = [F.reshape(ag(feat), shape=(1, -1)) for feat, ag in zip(features, self.anchor_generators)] cls_preds = F.concat(*cls_preds, dim=1).reshape((0, -1, self.num_classes)) box_preds = F.concat(*box_preds, dim=1).reshape((0, -1, 4)) anchors = F.concat(*anchors, dim=1).reshape((1, -1, 4)) if autograd.is_training(): return [cls_preds, box_preds, anchors] bboxes = self.bbox_decoder(box_preds, anchors) cls_ids, scores = self.cls_decoder(F.softmax(cls_preds, axis=-1)) results = [] for i in range(self.num_classes - 1): cls_id = cls_ids.slice_axis(axis=-1, begin=i, end=i+1) score = scores.slice_axis(axis=-1, begin=i, end=i+1) # per class results per_result = F.concat(*[cls_id, score, bboxes], dim=-1) results.append(per_result) result = F.concat(*results, dim=1) if self.nms_thresh > 0 and self.nms_thresh < 1: result = F.contrib.box_nms( result, overlap_thresh=self.nms_thresh, topk=self.nms_topk, valid_thresh=0.01, id_index=0, score_index=1, coord_start=2, force_suppress=False) if self.post_nms > 0: result = result.slice_axis(axis=1, begin=0, end=self.post_nms) ids = F.slice_axis(result, axis=2, begin=0, end=1) scores = F.slice_axis(result, axis=2, begin=1, end=2) bboxes = F.slice_axis(result, axis=2, begin=2, end=6) return ids, scores, bboxes
Example #10
Source File: cascade_rcnn.py From cascade_rcnn_gluon with Apache License 2.0 | 5 votes |
def add_batchid(self, F, bbox): num_roi = self._num_sample if autograd.is_training() else self._rpn_test_post_nms with autograd.pause(): roi_batchid = F.arange(0, self._max_batch, repeat=num_roi) # remove batch dim because ROIPooling require 2d input roi = F.concat(*[roi_batchid.reshape((-1, 1)), bbox.reshape((-1, 4))], dim=-1) roi = F.stop_gradient(roi) return roi
Example #11
Source File: cascade_rfcn.py From cascade_rcnn_gluon with Apache License 2.0 | 5 votes |
def add_batchid(self, F, bbox): num_roi = self._num_sample if autograd.is_training() else self._rpn_test_post_nms with autograd.pause(): roi_batchid = F.arange(0, self._max_batch, repeat=num_roi) # remove batch dim because ROIPooling require 2d input roi = F.concat(*[roi_batchid.reshape((-1, 1)), bbox.reshape((-1, 4))], dim=-1) roi = F.stop_gradient(roi) return roi
Example #12
Source File: resnet_symbol_plus_decoder.py From mxnet-centernet with MIT License | 5 votes |
def hybrid_forward(self, F, x): print('F: ', F) print('x: ', x) x = self.features(x) x = self.deconv_layers(x) ret = [] # 2dpose task -> 0: hm, 1: wh, 2: hps, 3: reg, 4: hm_hp, 5:hp_offset for head in self.heads: ret.append(self.__getattribute__(head)(x)) if autograd.is_training(): # during training, just need to return the tensor for computing loss print("training mode") #return [ret] return ret else: # during inference, need to decode the output tensor into actual detections # detections is composed of several things: detections = nd.concat(bboxes, scores, kps, clses, dim=2) # detections = decode_centernet_pose(heat, wh, kps, reg, hm_hp, hp_offset, K=100) print("inference mode") #detections = symbolic_decode_centernet_pose(F, ret[0].sigmoid(), ret[1], ret[2], ret[3], ret[4].sigmoid(), ret[5], K=10) detections = symbolic_decode_centernet_pose(F, ret[0].sigmoid(), ret[1], ret[2], K=10) print("decode finished!") detections.save("symbol-detections.json") return detections # Specification
Example #13
Source File: parallel.py From cascade_rcnn_gluon with Apache License 2.0 | 4 votes |
def criterion_parallel_apply(module, inputs, targets, kwargs_tup=None, sync=False): """Data Parallel Criterion""" if kwargs_tup: assert len(inputs) == len(kwargs_tup) else: kwargs_tup = ({},) * len(inputs) lock = threading.Lock() results = {} def _worker(i, module, input, target, kwargs, results, is_recording, is_training, lock): try: if is_recording: with autograd.record(is_training): output = module(*(input + target), **kwargs) output.wait_to_read() else: output = module(*(input + target), **kwargs) output.wait_to_read() with lock: results[i] = output except Exception as e: with lock: results[i] = e is_training = bool(autograd.is_training()) is_recording = autograd.is_recording() threads = [threading.Thread(target=_worker, args=(i, module, input, target, kwargs, results, is_recording, is_training, lock), ) for i, (input, target, kwargs) in enumerate(zip(inputs, targets, kwargs_tup))] if sync: for thread in threads: thread.start() for thread in threads: thread.join() outputs = [] for i in range(len(inputs)): output = results[i] if isinstance(output, Exception): raise output outputs.append(output) return tuple(outputs) else: outputs = [module(*(input + target), **kwargs) \ for (input, target, kwargs) in zip(inputs, targets, kwargs_tup)] return tuple(outputs)
Example #14
Source File: parallel.py From cascade_rcnn_gluon with Apache License 2.0 | 4 votes |
def parallel_apply(module, inputs, kwargs_tup=None, sync=False): """Parallel applying model forward""" if kwargs_tup is not None: assert len(inputs) == len(kwargs_tup) else: kwargs_tup = ({},) * len(inputs) lock = threading.Lock() results = {} def _worker(i, module, input, kwargs, results, is_recording, is_training, lock): try: if is_recording: with autograd.record(is_training): output = tuple_map(module(*input, **kwargs)) for out in output: out.wait_to_read() else: output = tuple_map(module(*input, **kwargs)) for out in output: out.wait_to_read() with lock: results[i] = output except Exception as e: with lock: results[i] = e is_training = autograd.is_training() is_recording = autograd.is_recording() threads = [threading.Thread(target=_worker, args=(i, module, input, kwargs, results, is_recording, is_training, lock), ) for i, (input, kwargs) in enumerate(zip(inputs, kwargs_tup))] if sync: for thread in threads: thread.start() for thread in threads: thread.join() outputs = [] for i in range(len(inputs)): output = results[i] if isinstance(output, Exception): raise output outputs.append(output) return tuple(outputs) else: outputs = [tuple_map(module(*input, **kwargs)) for (input, kwargs) in zip(inputs, kwargs_tup)] return tuple(outputs)
Example #15
Source File: yolo3.py From cascade_rcnn_gluon with Apache License 2.0 | 4 votes |
def hybrid_forward(self, F, x, anchors, offsets): """Hybrid Foward of YOLOV3Output layer. Parameters ---------- F : mxnet.nd or mxnet.sym `F` is mxnet.sym if hybridized or mxnet.nd if not. x : mxnet.nd.NDArray Input feature map. anchors : mxnet.nd.NDArray Anchors loaded from self, no need to supply. offsets : mxnet.nd.NDArray Offsets loaded from self, no need to supply. Returns ------- (tuple of) mxnet.nd.NDArray During training, return (bbox, raw_box_centers, raw_box_scales, objness, class_pred, anchors, offsets). During inference, return detections. """ # prediction flat to (batch, pred per pixel, height * width) pred = self.prediction(x).reshape((0, self._num_anchors * self._num_pred, -1)) # transpose to (batch, height * width, num_anchor, num_pred) pred = pred.transpose(axes=(0, 2, 1)).reshape((0, -1, self._num_anchors, self._num_pred)) # components raw_box_centers = pred.slice_axis(axis=-1, begin=0, end=2) raw_box_scales = pred.slice_axis(axis=-1, begin=2, end=4) objness = pred.slice_axis(axis=-1, begin=4, end=5) class_pred = pred.slice_axis(axis=-1, begin=5, end=None) # valid offsets, (1, 1, height, width, 2) offsets = F.slice_like(offsets, x * 0, axes=(2, 3)) # reshape to (1, height*width, 1, 2) offsets = offsets.reshape((1, -1, 1, 2)) box_centers = F.broadcast_add(F.sigmoid(raw_box_centers), offsets) * self._stride box_scales = F.broadcast_mul(F.exp(raw_box_scales), anchors) confidence = F.sigmoid(objness) class_score = F.broadcast_mul(F.sigmoid(class_pred), confidence) wh = box_scales / 2.0 bbox = F.concat(box_centers - wh, box_centers + wh, dim=-1) if autograd.is_training(): # during training, we don't need to convert whole bunch of info to detection results return (bbox.reshape((0, -1, 4)), raw_box_centers, raw_box_scales, objness, class_pred, anchors, offsets) # prediction per class bboxes = F.tile(bbox, reps=(self._classes, 1, 1, 1, 1)) scores = F.transpose(class_score, axes=(3, 0, 1, 2)).expand_dims(axis=-1) ids = F.broadcast_add(scores * 0, F.arange(0, self._classes).reshape((0, 1, 1, 1, 1))) detections = F.concat(ids, scores, bboxes, dim=-1) # reshape to (B, xx, 6) detections = F.reshape(detections.transpose(axes=(1, 0, 2, 3, 4)), (0, -1, 6)) return detections
Example #16
Source File: parallel.py From panoptic-fpn-gluon with Apache License 2.0 | 4 votes |
def criterion_parallel_apply(module, inputs, targets, kwargs_tup=None, sync=False): """Data Parallel Criterion""" if kwargs_tup: assert len(inputs) == len(kwargs_tup) else: kwargs_tup = ({},) * len(inputs) lock = threading.Lock() results = {} def _worker(i, module, input, target, kwargs, results, is_recording, is_training, lock): try: if is_recording: with autograd.record(is_training): output = module(*(input + target), **kwargs) output.wait_to_read() else: output = module(*(input + target), **kwargs) output.wait_to_read() with lock: results[i] = output except Exception as e: with lock: results[i] = e is_training = bool(autograd.is_training()) is_recording = autograd.is_recording() threads = [threading.Thread(target=_worker, args=(i, module, input, target, kwargs, results, is_recording, is_training, lock), ) for i, (input, target, kwargs) in enumerate(zip(inputs, targets, kwargs_tup))] if sync: for thread in threads: thread.start() for thread in threads: thread.join() outputs = [] for i in range(len(inputs)): output = results[i] if isinstance(output, Exception): raise output outputs.append(output) return tuple(outputs) else: outputs = [module(*(input + target), **kwargs) \ for (input, target, kwargs) in zip(inputs, targets, kwargs_tup)] return tuple(outputs)
Example #17
Source File: parallel.py From panoptic-fpn-gluon with Apache License 2.0 | 4 votes |
def parallel_apply(module, inputs, kwargs_tup=None, sync=False): """Parallel applying model forward""" if kwargs_tup is not None: assert len(inputs) == len(kwargs_tup) else: kwargs_tup = ({},) * len(inputs) lock = threading.Lock() results = {} def _worker(i, module, input, kwargs, results, is_recording, is_training, lock): try: if is_recording: with autograd.record(is_training): output = tuple_map(module(*input, **kwargs)) for out in output: out.wait_to_read() else: output = tuple_map(module(*input, **kwargs)) for out in output: out.wait_to_read() with lock: results[i] = output except Exception as e: with lock: results[i] = e is_training = autograd.is_training() is_recording = autograd.is_recording() threads = [threading.Thread(target=_worker, args=(i, module, input, kwargs, results, is_recording, is_training, lock), ) for i, (input, kwargs) in enumerate(zip(inputs, kwargs_tup))] if sync: for thread in threads: thread.start() for thread in threads: thread.join() outputs = [] for i in range(len(inputs)): output = results[i] if isinstance(output, Exception): raise output outputs.append(output) return tuple(outputs) else: outputs = [tuple_map(module(*input, **kwargs)) for (input, kwargs) in zip(inputs, kwargs_tup)] return tuple(outputs)
Example #18
Source File: yolo3.py From panoptic-fpn-gluon with Apache License 2.0 | 4 votes |
def hybrid_forward(self, F, x, anchors, offsets): """Hybrid Forward of YOLOV3Output layer. Parameters ---------- F : mxnet.nd or mxnet.sym `F` is mxnet.sym if hybridized or mxnet.nd if not. x : mxnet.nd.NDArray Input feature map. anchors : mxnet.nd.NDArray Anchors loaded from self, no need to supply. offsets : mxnet.nd.NDArray Offsets loaded from self, no need to supply. Returns ------- (tuple of) mxnet.nd.NDArray During training, return (bbox, raw_box_centers, raw_box_scales, objness, class_pred, anchors, offsets). During inference, return detections. """ # prediction flat to (batch, pred per pixel, height * width) pred = self.prediction(x).reshape((0, self._num_anchors * self._num_pred, -1)) # transpose to (batch, height * width, num_anchor, num_pred) pred = pred.transpose(axes=(0, 2, 1)).reshape((0, -1, self._num_anchors, self._num_pred)) # components raw_box_centers = pred.slice_axis(axis=-1, begin=0, end=2) raw_box_scales = pred.slice_axis(axis=-1, begin=2, end=4) objness = pred.slice_axis(axis=-1, begin=4, end=5) class_pred = pred.slice_axis(axis=-1, begin=5, end=None) # valid offsets, (1, 1, height, width, 2) offsets = F.slice_like(offsets, x * 0, axes=(2, 3)) # reshape to (1, height*width, 1, 2) offsets = offsets.reshape((1, -1, 1, 2)) box_centers = F.broadcast_add(F.sigmoid(raw_box_centers), offsets) * self._stride box_scales = F.broadcast_mul(F.exp(raw_box_scales), anchors) confidence = F.sigmoid(objness) class_score = F.broadcast_mul(F.sigmoid(class_pred), confidence) wh = box_scales / 2.0 bbox = F.concat(box_centers - wh, box_centers + wh, dim=-1) if autograd.is_training(): # during training, we don't need to convert whole bunch of info to detection results return (bbox.reshape((0, -1, 4)), raw_box_centers, raw_box_scales, objness, class_pred, anchors, offsets) # prediction per class bboxes = F.tile(bbox, reps=(self._classes, 1, 1, 1, 1)) scores = F.transpose(class_score, axes=(3, 0, 1, 2)).expand_dims(axis=-1) ids = F.broadcast_add(scores * 0, F.arange(0, self._classes).reshape((0, 1, 1, 1, 1))) detections = F.concat(ids, scores, bboxes, dim=-1) # reshape to (B, xx, 6) detections = F.reshape(detections.transpose(axes=(1, 0, 2, 3, 4)), (0, -1, 6)) return detections
Example #19
Source File: yolo3.py From MobileFace with MIT License | 4 votes |
def hybrid_forward(self, F, x, anchors, offsets): """Hybrid Forward of YOLOV3Output layer. Parameters ---------- F : mxnet.nd or mxnet.sym `F` is mxnet.sym if hybridized or mxnet.nd if not. x : mxnet.nd.NDArray Input feature map. anchors : mxnet.nd.NDArray Anchors loaded from self, no need to supply. offsets : mxnet.nd.NDArray Offsets loaded from self, no need to supply. Returns ------- (tuple of) mxnet.nd.NDArray During training, return (bbox, raw_box_centers, raw_box_scales, objness, class_pred, anchors, offsets). During inference, return detections. """ # prediction flat to (batch, pred per pixel, height * width) pred = self.prediction(x).reshape((0, self._num_anchors * self._num_pred, -1)) # transpose to (batch, height * width, num_anchor, num_pred) pred = pred.transpose(axes=(0, 2, 1)).reshape((0, -1, self._num_anchors, self._num_pred)) # components raw_box_centers = pred.slice_axis(axis=-1, begin=0, end=2) raw_box_scales = pred.slice_axis(axis=-1, begin=2, end=4) objness = pred.slice_axis(axis=-1, begin=4, end=5) class_pred = pred.slice_axis(axis=-1, begin=5, end=None) # valid offsets, (1, 1, height, width, 2) offsets = F.slice_like(offsets, x * 0, axes=(2, 3)) # reshape to (1, height*width, 1, 2) offsets = offsets.reshape((1, -1, 1, 2)) box_centers = F.broadcast_add(F.sigmoid(raw_box_centers), offsets) * self._stride box_scales = F.broadcast_mul(F.exp(raw_box_scales), anchors) confidence = F.sigmoid(objness) class_score = F.broadcast_mul(F.sigmoid(class_pred), confidence) wh = box_scales / 2.0 bbox = F.concat(box_centers - wh, box_centers + wh, dim=-1) if autograd.is_training(): # during training, we don't need to convert whole bunch of info to detection results return (bbox.reshape((0, -1, 4)), raw_box_centers, raw_box_scales, objness, class_pred, anchors, offsets) # prediction per class bboxes = F.tile(bbox, reps=(self._classes, 1, 1, 1, 1)) scores = F.transpose(class_score, axes=(3, 0, 1, 2)).expand_dims(axis=-1) ids = F.broadcast_add(scores * 0, F.arange(0, self._classes).reshape((0, 1, 1, 1, 1))) detections = F.concat(ids, scores, bboxes, dim=-1) # reshape to (B, xx, 6) detections = F.reshape(detections.transpose(axes=(1, 0, 2, 3, 4)), (0, -1, 6)) return detections
Example #20
Source File: parallel.py From gluon-cv with Apache License 2.0 | 4 votes |
def criterion_parallel_apply(module, inputs, targets, kwargs_tup=None, sync=False): """Data Parallel Criterion""" if kwargs_tup: assert len(inputs) == len(kwargs_tup) else: kwargs_tup = ({},) * len(inputs) lock = threading.Lock() results = {} def _worker(i, module, input, target, kwargs, results, is_recording, is_training, lock): try: if is_recording: with autograd.record(is_training): output = module(*(input + target), **kwargs) output.wait_to_read() else: output = module(*(input + target), **kwargs) output.wait_to_read() with lock: results[i] = output except Exception as e: with lock: results[i] = e is_training = bool(autograd.is_training()) is_recording = autograd.is_recording() threads = [threading.Thread(target=_worker, args=(i, module, input, target, kwargs, results, is_recording, is_training, lock),) for i, (input, target, kwargs) in enumerate(zip(inputs, targets, kwargs_tup))] if sync: for thread in threads: thread.start() for thread in threads: thread.join() outputs = [] for i in range(len(inputs)): output = results[i] if isinstance(output, Exception): raise output outputs.append(output) return tuple(outputs) else: outputs = [module(*(input + target), **kwargs) \ for (input, target, kwargs) in zip(inputs, targets, kwargs_tup)] return tuple(outputs)
Example #21
Source File: parallel.py From gluon-cv with Apache License 2.0 | 4 votes |
def parallel_apply(module, inputs, kwargs_tup=None, sync=False): """Parallel applying model forward""" if kwargs_tup is not None: assert len(inputs) == len(kwargs_tup) else: kwargs_tup = ({},) * len(inputs) lock = threading.Lock() results = {} def _worker(i, module, input, kwargs, results, is_recording, is_training, lock): try: if is_recording: with autograd.record(is_training): output = tuple_map(module(*input, **kwargs)) for out in output: out.wait_to_read() else: output = tuple_map(module(*input, **kwargs)) for out in output: out.wait_to_read() with lock: results[i] = output except Exception as e: with lock: results[i] = e is_training = autograd.is_training() is_recording = autograd.is_recording() threads = [threading.Thread(target=_worker, args=(i, module, input, kwargs, results, is_recording, is_training, lock),) for i, (input, kwargs) in enumerate(zip(inputs, kwargs_tup))] if sync: for thread in threads: thread.start() for thread in threads: thread.join() outputs = [] for i in range(len(inputs)): output = results[i] if isinstance(output, Exception): raise output outputs.append(output) return tuple(outputs) else: outputs = [tuple_map(module(*input, **kwargs)) for (input, kwargs) in zip(inputs, kwargs_tup)] return tuple(outputs)
Example #22
Source File: yolo3.py From gluon-cv with Apache License 2.0 | 4 votes |
def hybrid_forward(self, F, x, anchors, offsets): """Hybrid Forward of YOLOV3Output layer. Parameters ---------- F : mxnet.nd or mxnet.sym `F` is mxnet.sym if hybridized or mxnet.nd if not. x : mxnet.nd.NDArray Input feature map. anchors : mxnet.nd.NDArray Anchors loaded from self, no need to supply. offsets : mxnet.nd.NDArray Offsets loaded from self, no need to supply. Returns ------- (tuple of) mxnet.nd.NDArray During training, return (bbox, raw_box_centers, raw_box_scales, objness, class_pred, anchors, offsets). During inference, return detections. """ # prediction flat to (batch, pred per pixel, height * width) pred = self.prediction(x).reshape((0, self._num_anchors * self._num_pred, -1)) # transpose to (batch, height * width, num_anchor, num_pred) pred = pred.transpose(axes=(0, 2, 1)).reshape((0, -1, self._num_anchors, self._num_pred)) # components raw_box_centers = pred.slice_axis(axis=-1, begin=0, end=2) raw_box_scales = pred.slice_axis(axis=-1, begin=2, end=4) objness = pred.slice_axis(axis=-1, begin=4, end=5) class_pred = pred.slice_axis(axis=-1, begin=5, end=None) # valid offsets, (1, 1, height, width, 2) offsets = F.slice_like(offsets, x * 0, axes=(2, 3)) # reshape to (1, height*width, 1, 2) offsets = offsets.reshape((1, -1, 1, 2)) box_centers = F.broadcast_add(F.sigmoid(raw_box_centers), offsets) * self._stride box_scales = F.broadcast_mul(F.exp(raw_box_scales), anchors) confidence = F.sigmoid(objness) class_score = F.broadcast_mul(F.sigmoid(class_pred), confidence) wh = box_scales / 2.0 bbox = F.concat(box_centers - wh, box_centers + wh, dim=-1) if autograd.is_training(): # during training, we don't need to convert whole bunch of info to detection results return (bbox.reshape((0, -1, 4)), raw_box_centers, raw_box_scales, objness, class_pred, anchors, offsets) # prediction per class bboxes = F.tile(bbox, reps=(self._classes, 1, 1, 1, 1)) scores = F.transpose(class_score, axes=(3, 0, 1, 2)).expand_dims(axis=-1) ids = F.broadcast_add(scores * 0, F.arange(0, self._classes).reshape((0, 1, 1, 1, 1))) detections = F.concat(ids, scores, bboxes, dim=-1) # reshape to (B, xx, 6) detections = F.reshape(detections.transpose(axes=(1, 0, 2, 3, 4)), (0, -1, 6)) return detections