Python mxnet.nd.reshape() Examples
The following are 28
code examples of mxnet.nd.reshape().
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.nd
, or try the search function
.
Example #1
Source File: basic_structure.py From ST-MetaNet with MIT License | 6 votes |
def forward(self, feature, data): """ Forward process of a MetaDense layer Parameters ---------- feature: NDArray with shape [n, d] data: NDArray with shape [n, b, input_hidden_size] Returns ------- output: NDArray with shape [n, b, output_hidden_size] """ weight = self.w_mlp(feature) # [n, input_hidden_size * output_hidden_size] weight = nd.reshape(weight, (-1, self.input_hidden_size, self.output_hidden_size)) bias = nd.reshape(self.b_mlp(feature), shape=(-1, 1, 1)) # [n, 1, 1] return nd.batch_dot(data, weight) + bias
Example #2
Source File: cell.py From ST-MetaNet with MIT License | 6 votes |
def forward(self, feature, data, begin_state): n, b, length, _ = data.shape # reshape the data and states for rnn unroll data = nd.reshape(data, shape=(n * b, length, -1)) # [n * b, t, d] if begin_state is not None: begin_state = [ nd.reshape(state, shape=(n * b, -1)) for state in begin_state ] # [n * b, d] # unroll the rnn data, state = self.cell.unroll(length, data, begin_state, merge_outputs=True) # reshape the data & states back data = nd.reshape(data, shape=(n, b, length, -1)) state = [nd.reshape(s, shape=(n, b, -1)) for s in state] return data, state
Example #3
Source File: siamrpn_tracker.py From gluon-cv with Apache License 2.0 | 6 votes |
def _convert_bbox(self, delta, anchor): """from loc to predict postion Parameters ---------- delta : ndarray or np.ndarray network output anchor : np.ndarray generate anchor location Returns ------- rejust predict postion though Anchor """ delta = nd.transpose(delta, axes=(1, 2, 3, 0)) delta = nd.reshape(delta, shape=(4, -1)) delta = delta.asnumpy() delta[0, :] = delta[0, :] * anchor[:, 2] + anchor[:, 0] delta[1, :] = delta[1, :] * anchor[:, 3] + anchor[:, 1] delta[2, :] = np.exp(delta[2, :]) * anchor[:, 2] delta[3, :] = np.exp(delta[3, :]) * anchor[:, 3] return delta
Example #4
Source File: siamrpn_tracker.py From gluon-cv with Apache License 2.0 | 6 votes |
def _convert_score(self, score): """from cls to score Parameters ---------- score : ndarray network output Returns ------- get feature map score though softmax """ score = nd.transpose(score, axes=(1, 2, 3, 0)) score = nd.reshape(score, shape=(2, -1)) score = nd.transpose(score, axes=(1, 0)) score = nd.softmax(score, axis=1) score = nd.slice_axis(score, axis=1, begin=1, end=2) score = nd.squeeze(score, axis=1) return score.asnumpy()
Example #5
Source File: model.py From training_results_v0.6 with Apache License 2.0 | 6 votes |
def _spectral_norm(self): """ spectral normalization """ w = self.params.get('weight').data(self.ctx) w_mat = nd.reshape(w, [w.shape[0], -1]) _u = self.u.data(self.ctx) _v = None for _ in range(POWER_ITERATION): _v = nd.L2Normalization(nd.dot(_u, w_mat)) _u = nd.L2Normalization(nd.dot(_v, w_mat.T)) sigma = nd.sum(nd.dot(_u, w_mat) * _v) if sigma == 0.: sigma = EPSILON self.params.setattr('u', _u) return w / sigma
Example #6
Source File: basic_structure.py From ST-MetaNet with MIT License | 6 votes |
def forward(self, feature, data): """ Forward process of a MetaDense layer Parameters ---------- feature: NDArray with shape [n, d] data: NDArray with shape [n, b, input_hidden_size] Returns ------- output: NDArray with shape [n, b, output_hidden_size] """ weight = self.w_mlp(feature) # [n, input_hidden_size * output_hidden_size] weight = nd.reshape(weight, (-1, self.input_hidden_size, self.output_hidden_size)) bias = nd.reshape(self.b_mlp(feature), shape=(-1, 1, 1)) # [n, 1, 1] return nd.batch_dot(data, weight) + bias
Example #7
Source File: cell.py From ST-MetaNet with MIT License | 6 votes |
def forward(self, feature, data, begin_state): n, b, length, _ = data.shape # reshape the data and states for rnn unroll data = nd.reshape(data, shape=(n * b, length, -1)) # [n * b, t, d] if begin_state is not None: begin_state = [ nd.reshape(state, shape=(n * b, -1)) for state in begin_state ] # [n * b, d] # unroll the rnn data, state = self.cell.unroll(length, data, begin_state, merge_outputs=True) # reshape the data & states back data = nd.reshape(data, shape=(n, b, length, -1)) state = [nd.reshape(s, shape=(n, b, -1)) for s in state] return data, state
Example #8
Source File: decoder.py From mxnet-centernet with MIT License | 6 votes |
def _topk(scores, K=40): batch, cat, height, width = scores.shape [topk_scores, topk_inds] = nd.topk(nd.reshape(scores, (batch, cat, -1)), ret_typ='both', k=K) # return both value and indices topk_inds = topk_inds % (height * width) topk_ys = (topk_inds / width).astype('int32').astype('float32') topk_xs = (topk_inds % width).astype('int32').astype('float32') [topk_score, topk_ind] = nd.topk(nd.reshape(topk_scores, (batch, -1)), ret_typ='both', k=K) topk_clses = (topk_ind / K).astype('int32') topk_inds = _gather_feat(nd.reshape(topk_inds, (batch, -1, 1)), topk_ind) topk_inds = nd.reshape(topk_inds, (batch, K)) topk_ys = _gather_feat(nd.reshape(topk_ys, (batch, -1, 1)), topk_ind) topk_ys = nd.reshape(topk_ys, (batch, K)) topk_xs = _gather_feat(nd.reshape(topk_xs, (batch, -1, 1)), topk_ind) topk_xs = nd.reshape(topk_xs, (batch, K)) return topk_score, topk_inds, topk_clses, topk_ys, topk_xs
Example #9
Source File: graph.py From ST-MetaNet with MIT License | 6 votes |
def msg_edge(self, edge): state = nd.concat(edge.src['state'], edge.dst['state'], dim=-1) feature = nd.concat(edge.src['feature'], edge.dst['feature'], edge.data['dist'], dim=-1) # generate weight by meta-learner weight = self.w_mlp(feature) weight = nd.reshape(weight, shape=(-1, self.hidden_size * 2, self.hidden_size)) # reshape state to [n, b * t, d] for batch_dot (currently mxnet only support batch_dot for 3D tensor) shape = state.shape state = nd.reshape(state, shape=(shape[0], -1, shape[-1])) alpha = nd.LeakyReLU(nd.batch_dot(state, weight)) # reshape alpha to [n, b, t, d] alpha = nd.reshape(alpha, shape=shape[:-1] + (self.hidden_size,)) return { 'alpha': alpha, 'state': edge.src['state'] }
Example #10
Source File: decoder.py From mxnet-centernet with MIT License | 6 votes |
def symbolic_topk(F, scores, K=40): batch, cat, height, width = 1, 1, 128.0, 128.0 [topk_scores, topk_inds] = F.topk(scores.reshape((batch, cat, -1)), ret_typ='both', k=K) # return both value and indices topk_inds = topk_inds % (height * width) topk_ys = (topk_inds / width).astype('int32').astype('float32') topk_xs = (topk_inds % width).astype('int32').astype('float32') [topk_score, topk_ind] = F.topk(topk_scores.reshape((batch, -1)), ret_typ='both', k=K) topk_clses = (topk_ind / K).astype('int32') topk_inds = symbolic_gather_feat(F, topk_inds.reshape((batch, -1, 1)), topk_ind, K, attri=1) topk_inds = topk_inds.reshape((batch, K)) topk_ys = symbolic_gather_feat(F, topk_ys.reshape((batch, -1, 1)), topk_ind, K, attri=1) topk_ys = topk_ys.reshape((batch, K)) topk_xs = symbolic_gather_feat(F, topk_xs.reshape((batch, -1, 1)), topk_ind, K, attri=1) topk_xs = topk_xs.reshape((batch, K)) return topk_score, topk_inds, topk_clses, topk_ys, topk_xs
Example #11
Source File: model.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def _spectral_norm(self): """ spectral normalization """ w = self.params.get('weight').data(self.ctx) w_mat = nd.reshape(w, [w.shape[0], -1]) _u = self.u.data(self.ctx) _v = None for _ in range(POWER_ITERATION): _v = nd.L2Normalization(nd.dot(_u, w_mat)) _u = nd.L2Normalization(nd.dot(_v, w_mat.T)) sigma = nd.sum(nd.dot(_u, w_mat) * _v) if sigma == 0.: sigma = EPSILON self.params.setattr('u', _u) return w / sigma
Example #12
Source File: decoder.py From mxnet-centernet with MIT License | 5 votes |
def symbolic_topk_channel(F, scores, K=40): scores_shape = F.shape_array(scores) batch, cat, height, width = 1, 1, 128.0, 128.0 [topk_scores, topk_inds] = F.topk(scores.reshape((batch, cat, -1)), ret_typ = "both", k= K) topk_inds = topk_inds % (height * width) topk_ys = (topk_inds / width).astype('int32').astype('float32') topk_xs = (topk_inds % width).astype('int32').astype('float32') return topk_scores, topk_inds, topk_ys, topk_xs
Example #13
Source File: CapsLayers.py From CapsNet_Mxnet with Apache License 2.0 | 5 votes |
def forward(self, x): #(batch_size, 1, 10, 16, 1) =>(batch_size,10, 16)=> (batch_size, 10, 1) x_shape = x.shape x = x.reshape(shape=(x_shape[0],x_shape[2],x_shape[3])) x_l2norm = nd.sqrt((x.square()).sum(axis=-1)) # prob = nd.softmax(x_l2norm, axis=-1) return x_l2norm
Example #14
Source File: CapsLayers.py From CapsNet_Mxnet with Apache License 2.0 | 5 votes |
def reshape_conv(self,conv_vector): return nd.reshape(conv_vector,shape=(self.batch_size, self.dim_vector,-1))
Example #15
Source File: tensor_utils.py From mxnet-centernet with MIT License | 5 votes |
def symbolic_transpose_and_gather_feat(F, feat, ind, K, batch, cat, attri): #print("In symbolic_transpose_and_gather_feat, feat.shape = ", feat.shape) feat = F.transpose(feat, axes=(0, 2, 3, 1)) feat = F.reshape(feat, shape=(batch, -1, cat)) #print("In symbolic_transpose_and_gather_feat, feat.shape = ", feat.shape) feat = symbolic_gather_feat(F, feat, ind, K, attri) return feat
Example #16
Source File: tensor_utils.py From mxnet-centernet with MIT License | 5 votes |
def flip_lr_off(x, flip_idx): tmp = x.detach().cpu().numpy()[..., ::-1].copy() shape = tmp.shape tmp = tmp.reshape(tmp.shape[0], 17, 2, tmp.shape[2], tmp.shape[3]) tmp[:, :, 0, :, :] *= -1 for e in flip_idx: tmp[:, e[0], ...], tmp[:, e[1], ...] = \ tmp[:, e[1], ...].copy(), tmp[:, e[0], ...].copy() return nd.array(tmp.reshape(shape)).to(x.device)
Example #17
Source File: tensor_utils.py From mxnet-centernet with MIT License | 5 votes |
def flip_lr(x, flip_idx): tmp = x.detach().cpu().numpy()[..., ::-1].copy() shape = tmp.shape for e in flip_idx: tmp[:, e[0], ...], tmp[:, e[1], ...] = \ tmp[:, e[1], ...].copy(), tmp[:, e[0], ...].copy() return nd.array(tmp.reshape(shape)).to(x.device)
Example #18
Source File: tensor_utils.py From mxnet-centernet with MIT License | 5 votes |
def _tranpose_and_gather_feat(feat, ind): feat = nd.transpose(feat, axes=(0, 2, 3, 1)) feat = nd.reshape(feat, shape=(feat.shape[0], -1, feat.shape[3])) feat = _gather_feat(feat, ind) return feat
Example #19
Source File: decoder.py From mxnet-centernet with MIT License | 5 votes |
def decode_centernet_3dod(heat, rot, depth, dim, wh=None, reg=None, K=40): batch, cat, height, width = heat.shape # perform nms on heatmaps heat = _nms(heat) scores, inds, clses, ys, xs = _topk(heat, K=K) if reg is not None: reg = _tranpose_and_gather_feat(reg, inds) reg = nd.reshape(reg, (batch, K, 2)) xs = nd.reshape(xs, (batch, K, 1)) + reg[:, :, 0:1] ys = nd.reshape(ys, (batch, K, 1)) + reg[:, :, 1:2] else: xs = nd.reshape(xs, (batch, K, 1)) + 0.5 ys = nd.reshape(ys, (batch, K, 1)) + 0.5 rot = _tranpose_and_gather_feat(rot, inds) rot = nd.reshape(rot, (batch, K, 8)) depth = _tranpose_and_gather_feat(depth, inds) depth = nd.reshape(depth, (batch, K, 1)) dim = _tranpose_and_gather_feat(dim, inds) dim = nd.reshape(dim, (batch, K, 3)) clses = nd.reshape(clses, (batch, K, 1)).astype('float32') scores = nd.reshape(scores, (batch, K, 1)) xs = nd.reshape(xs, (batch, K, 1)) ys = nd.reshape(ys, (batch, K, 1)) if wh is not None: wh = _tranpose_and_gather_feat(wh, inds) wh = nd.reshape(wh, (batch, K, 2)) detections = nd.concat(xs, ys, scores, rot, depth, dim, wh, clses, dim=2) else: detections = nd.concat(xs, ys, scores, rot, depth, dim, clses, dim=2) return detections
Example #20
Source File: losses_symbol.py From mxnet-centernet with MIT License | 5 votes |
def compute_rot_loss(output, target_bin, target_res, mask): # output: (B, 128, 8) [bin1_cls[0], bin1_cls[1], bin1_sin, bin1_cos, # bin2_cls[0], bin2_cls[1], bin2_sin, bin2_cos] # target_bin: (B, 128, 2) [bin1_cls, bin2_cls] # target_res: (B, 128, 2) [bin1_res, bin2_res] # mask: (B, 128, 1) output = nd.reshape(output, (-1, 8)) target_bin = nd.reshape(target_bin, (-1, 2)) target_res = nd.reshape(target_res, (-1, 2)) mask = nd.reshape(mask, (-1, 1)) loss_bin1 = compute_bin_loss(output[:, 0:2], target_bin[:, 0], mask) loss_bin2 = compute_bin_loss(output[:, 4:6], target_bin[:, 1], mask) loss_res = nd.zeros_like(loss_bin1) mask1 = (target_bin[:, 0] > 0).astype('float32') if mask1.sum() > 0: valid_output1 = output valid_target_res1 = target_res loss_sin1 = compute_res_loss(valid_output1[:, 2], nd.sin(valid_target_res1[:, 0]), mask1) loss_cos1 = compute_res_loss(valid_output1[:, 3], nd.cos(valid_target_res1[:, 0]), mask1) loss_res = loss_res + loss_sin1 + loss_cos1 mask2 = (target_bin[:, 1] > 0).astype('float32') if mask2.sum() > 0: valid_output2 = output valid_target_res2 = target_res loss_sin2 = compute_res_loss(valid_output2[:, 6], nd.sin(valid_target_res2[:, 1]), mask2) loss_cos2 = compute_res_loss(valid_output2[:, 7], nd.cos(valid_target_res2[:, 1]), mask2) loss_res = loss_res + loss_sin2 + loss_cos2 #print("loss_bin1: {}, loss_bin2: {}, loss_sin1: {}, loss_sin2: {}, loss_cos1: {}, loss_cos2: {}".format(loss_bin1, loss_bin2, loss_sin1, loss_sin2, loss_cos1, loss_cos2)) return loss_bin1 + loss_bin2 + loss_res
Example #21
Source File: losses.py From mxnet-centernet with MIT License | 5 votes |
def compute_rot_loss(output, target_bin, target_res, mask): # output: (B, 128, 8) [bin1_cls[0], bin1_cls[1], bin1_sin, bin1_cos, # bin2_cls[0], bin2_cls[1], bin2_sin, bin2_cos] # target_bin: (B, 128, 2) [bin1_cls, bin2_cls] # target_res: (B, 128, 2) [bin1_res, bin2_res] # mask: (B, 128, 1) output = nd.reshape(output, (-1, 8)) target_bin = nd.reshape(target_bin, (-1, 2)) target_res = nd.reshape(target_res, (-1, 2)) mask = nd.reshape(mask, (-1, 1)) loss_bin1 = compute_bin_loss(output[:, 0:2], target_bin[:, 0], mask) loss_bin2 = compute_bin_loss(output[:, 4:6], target_bin[:, 1], mask) loss_res = nd.zeros_like(loss_bin1) mask1 = (target_bin[:, 0] > 0).astype('float32') if mask1.sum() > 0: valid_output1 = output valid_target_res1 = target_res loss_sin1 = compute_res_loss(valid_output1[:, 2], nd.sin(valid_target_res1[:, 0]), mask1) loss_cos1 = compute_res_loss(valid_output1[:, 3], nd.cos(valid_target_res1[:, 0]), mask1) loss_res = loss_res + loss_sin1 + loss_cos1 mask2 = (target_bin[:, 1] > 0).astype('float32') if mask2.sum() > 0: valid_output2 = output valid_target_res2 = target_res loss_sin2 = compute_res_loss(valid_output2[:, 6], nd.sin(valid_target_res2[:, 1]), mask2) loss_cos2 = compute_res_loss(valid_output2[:, 7], nd.cos(valid_target_res2[:, 1]), mask2) loss_res = loss_res + loss_sin2 + loss_cos2 #print("loss_bin1: {}, loss_bin2: {}, loss_sin1: {}, loss_sin2: {}, loss_cos1: {}, loss_cos2: {}".format(loss_bin1, loss_bin2, loss_sin1, loss_sin2, loss_cos1, loss_cos2)) return loss_bin1 + loss_bin2 + loss_res
Example #22
Source File: siamrpn_tracker.py From gluon-cv with Apache License 2.0 | 5 votes |
def generate_anchor(self, score_size): """ generate score map anchors based on predefined configuration Parameters ---------- score_size : int score map size Returns ---------- score map anchor """ anchors = Anchors(self.STRIDE, self.RATIOS, self.SCALES) anchor = anchors.anchors x_min, y_min, x_max, y_max = anchor[:, 0], anchor[:, 1], anchor[:, 2], anchor[:, 3] anchor = np.stack([(x_min+x_max)*0.5, (y_min+y_max)*0.5, x_max-x_min, y_max-y_min], 1) total_stride = anchors.stride anchor_num = anchor.shape[0] anchor = np.tile(anchor, score_size * score_size).reshape((-1, 4)) ori = - (score_size // 2) * total_stride x_x, y_y = np.meshgrid([ori + total_stride * dx for dx in range(score_size)], [ori + total_stride * dy for dy in range(score_size)]) x_x, y_y = np.tile(x_x.flatten(), (anchor_num, 1)).flatten(), \ np.tile(y_y.flatten(), (anchor_num, 1)).flatten() anchor[:, 0], anchor[:, 1] = x_x.astype(np.float32), y_y.astype(np.float32) return anchor
Example #23
Source File: utils.py From YOLO with MIT License | 5 votes |
def deal_output(y: nd.NDArray, s, b, c): """ :param y: :param s: :param b: :param c: :return: """ label = y[:, 0:s * s * c] preds = y[:, s * s * c: s * s * c + s * s * b] location = y[:, s * s * c + s * s * b:] label = nd.reshape(label, shape=(-1, s * s, c)) location = nd.reshape(location, shape=(-1, s * s, b, 4)) return label, preds, location
Example #24
Source File: seq2seq.py From ST-MetaNet with MIT License | 4 votes |
def forward(self, feature, label, begin_states, is_training): ''' Decode the hidden states to a temporal sequence. Parameters ---------- feature: a NDArray with shape [n, d]. label: a NDArray with shape [n, b, t, d]. begin_states: a list of hidden states (list of hidden units with shape [n, b, d]) of RNNs. is_training: bool Returns ------- outputs: the prediction, which is a NDArray with shape [n, b, t, d] ''' ctx = label.context num_nodes, batch_size, seq_len, _ = label.shape aux = label[:,:,:, self.output_dim:] # [n,b,t,d] label = label[:,:,:, :self.output_dim] # [n,b,t,d] go = nd.zeros(shape=(num_nodes, batch_size, self.input_dim), ctx=ctx) output, states = [], begin_states for i in range(seq_len): # get next input if i == 0: data = go else: prev = nd.concat(output[i - 1], aux[:,:,i - 1], dim=-1) truth = nd.concat(label[:,:,i - 1], aux[:,:,i - 1], dim=-1) if is_training and self.use_sampling: value = self.sampling() else: value = 0 data = value * truth + (1 - value) * prev # unroll 1 step for depth, cell in enumerate(self.cells): data, states[depth] = cell.forward_single(feature, data, states[depth]) if self.graphs[depth] is not None: _data = 0 for g in self.graphs[depth]: _data = _data + g(data, feature) data = _data / len(self.graphs[depth]) # append feature to output _feature = nd.expand_dims(feature, axis=1) # [n, 1, d] _feature = nd.broadcast_to(_feature, shape=(0, batch_size, 0)) # [n, b, d] data = nd.concat(data, _feature, dim=-1) # [n, b, t, d] # proj output to prediction data = nd.reshape(data, shape=(num_nodes * batch_size, -1)) data = self.proj(data) data = nd.reshape(data, shape=(num_nodes, batch_size, -1)) output.append(data) output = nd.stack(*output, axis=2) return output
Example #25
Source File: decoder.py From mxnet-centernet with MIT License | 4 votes |
def decode_centernet(heat, wh, reg=None, cat_spec_wh=False, K=100, flag_split=False): batch, cat, height, width = heat.shape # perform nms on heatmaps, find the peaks heat = _nms(heat) scores, inds, clses, ys, xs = _topk(heat, K=K) if reg is not None: reg = _tranpose_and_gather_feat(reg, inds) reg = nd.reshape(reg, (batch, K, 2)) xs = nd.reshape(xs, (batch, K, 1)) + reg[:, :, 0:1] ys = nd.reshape(ys, (batch, K, 1)) + reg[:, :, 1:2] else: xs = nd.reshape(xs, (batch, K, 1)) + 0.5 ys = nd.reshape(ys, (batch, K, 1)) + 0.5 wh = _tranpose_and_gather_feat(wh, inds) if cat_spec_wh: wh = nd.reshape(wh, (batch, K, cat, 2)) clses_ind = nd.reshape(clses, (batch, K, 1, 1)) clses_ind = nd.stack(clses_ind, clses_ind, axis=3) #becomes (batch, K, 1, 2) clses_ind = clses_ind.astype('int64') wh = wh.gather_nd(2, clses_ind) wh = nd.reshape(wh, (batch, K, 2)) else: wh = nd.reshape(wh, (batch, K, 2)) clses = nd.reshape(clses, (batch, K, 1)).astype('float32') scores = nd.reshape(scores, (batch, K, 1)) bboxes = nd.concat(xs - wh[:, :, 0:1] / 2, ys - wh[:, :, 1:2] / 2, xs + wh[:, :, 0:1] / 2, ys + wh[:, :, 1:2] / 2, dim=2) if flag_split is True: return bboxes, scores, clses else: detections = nd.concat(bboxes, scores, clses, dim=2) return detections
Example #26
Source File: seq2seq.py From ST-MetaNet with MIT License | 4 votes |
def forward(self, feature, label, begin_states, is_training): ''' Decode the hidden states to a temporal sequence. Parameters ---------- feature: a NDArray with shape [n, d]. label: a NDArray with shape [n, b, t, d]. begin_states: a list of hidden states (list of hidden units with shape [n, b, d]) of RNNs. is_training: bool Returns ------- outputs: the prediction, which is a NDArray with shape [n, b, t, d] ''' ctx = label.context num_nodes, batch_size, seq_len, _ = label.shape aux = label[:,:,:, self.output_dim:] # [n,b,t,d] label = label[:,:,:, :self.output_dim] # [n,b,t,d] go = nd.zeros(shape=(num_nodes, batch_size, self.input_dim), ctx=ctx) output, states = [], begin_states for i in range(seq_len): # get next input if i == 0: data = go else: prev = nd.concat(output[i - 1], aux[:,:,i - 1], dim=-1) truth = nd.concat(label[:,:,i - 1], aux[:,:,i - 1], dim=-1) if is_training and self.use_sampling: value = self.sampling() else: value = 0 data = value * truth + (1 - value) * prev # unroll 1 step for depth, cell in enumerate(self.cells): data, states[depth] = cell.forward_single(feature, data, states[depth]) if self.graphs[depth] is not None: _data = data for g in self.graphs[depth]: _data = _data + g(data, feature) data = _data # append feature to output _feature = nd.expand_dims(feature, axis=1) # [n, 1, d] _feature = nd.broadcast_to(_feature, shape=(0, batch_size, 0)) # [n, b, d] data = nd.concat(data, _feature, dim=-1) # [n, b, t, d] # proj output to prediction data = nd.reshape(data, shape=(num_nodes * batch_size, -1)) data = self.proj(data) data = nd.reshape(data, shape=(num_nodes, batch_size, -1)) output.append(data) output = nd.stack(*output, axis=2) return output
Example #27
Source File: siamrpn_tracker.py From gluon-cv with Apache License 2.0 | 4 votes |
def generate_all_anchors(self, im_c, size): """ generate all anchors Parameters ---------- im_c: int image center size: image size """ if self.image_center == im_c and self.size == size: return False self.image_center = im_c self.size = size a0x = im_c - size // 2 * self.stride ori = np.array([a0x] * 4, dtype=np.float32) zero_anchors = self.anchors + ori x1 = zero_anchors[:, 0] y1 = zero_anchors[:, 1] x2 = zero_anchors[:, 2] y2 = zero_anchors[:, 3] x1, y1, x2, y2 = map(lambda x: x.reshape(self.anchor_num, 1, 1), [x1, y1, x2, y2]) cx, cy, w, h = corner2center([x1, y1, x2, y2]) disp_x = np.arange(0, size).reshape(1, 1, -1) * self.stride disp_y = np.arange(0, size).reshape(1, -1, 1) * self.stride cx = cx + disp_x cy = cy + disp_y # broadcast zero = np.zeros((self.anchor_num, size, size), dtype=np.float32) cx, cy, w, h = map(lambda x: x + zero, [cx, cy, w, h]) x1, y1, x2, y2 = center2corner([cx, cy, w, h]) self.all_anchors = (np.stack([x1, y1, x2, y2]).astype(np.float32), np.stack([cx, cy, w, h]).astype(np.float32)) return True
Example #28
Source File: utils.py From YOLO with MIT License | 4 votes |
def translate_y(label, s, b, c): """ :param y: :param s: :param b: :param c: :return: """ y_ = label.asnumpy() labels = y_[:, 0] + 1 location = y_[:, 1:] batch = len(label) new_y = np.zeros(shape=[batch, s * s * (b * 5 + c)]) for i, locat in enumerate(location): labels_ = np.zeros(shape=(s * s, c)) preds_ = np.zeros(shape=(s * s, b)) location_ = np.zeros(shape=(s * s, b, 4)) index = find_range(locat, s) if index == -1: labels_[:, 0] = 1 labels_ = labels_.reshape((s * s * c,)) preds_ = preds_.reshape((s * s * b,)) location_ = location_.reshape((s * s * b * 4,)) new_y[i] = (np.concatenate([labels_, preds_, location_], axis=0)) continue for index_ in range(s * s): if index_ != index: labels_[index_][0] = 1 labels_[index][int(labels[i])] = 1 x_min, y_min, x_max, y_max = locat x, y, w, h = translate_locat(x_min, y_min, x_max, y_max) w, h = np.sqrt(w), np.sqrt(h) ceil = 1 / s x, y = round(x % ceil, 4), round(y % ceil, 4) for j, b_ in enumerate(preds_[index]): if b_ != 1: preds_[index][j] = 1 location_[index][j] = [x, y, w, h] break labels_ = labels_.reshape((s * s * c,)) preds_ = preds_.reshape((s * s * b,)) location_ = location_.reshape((s * s * b * 4,)) new_y[i] = (np.concatenate([labels_, preds_, location_], axis=0)) return new_y