def extract_multi_position_matrix_nd(bbox): bbox = nd.transpose(bbox, axes=(1, 0, 2)) xmin, ymin, xmax, ymax = nd.split(data=bbox, num_outputs=4, axis=2) # [num_fg_classes, num_boxes, 1] bbox_width = xmax - xmin + 1. bbox_height = ymax - ymin + 1. center_x = 0.5 * (xmin + xmax) center_y = 0.5 * (ymin + ymax) # [num_fg_classes, num_boxes, num_boxes] delta_x = nd.broadcast_minus(lhs=center_x, rhs=nd.transpose(center_x, axes=(0, 2, 1))) delta_x = nd.broadcast_div(delta_x, bbox_width) delta_x = nd.log(nd.maximum(nd.abs(delta_x), 1e-3)) delta_y = nd.broadcast_minus(lhs=center_y, rhs=nd.transpose(center_y, axes=(0, 2, 1))) delta_y = nd.broadcast_div(delta_y, bbox_height) delta_y = nd.log(nd.maximum(nd.abs(delta_y), 1e-3)) delta_width = nd.broadcast_div(lhs=bbox_width, rhs=nd.transpose(bbox_width, axes=(0, 2, 1))) delta_width = nd.log(delta_width) delta_height = nd.broadcast_div(lhs=bbox_height, rhs=nd.transpose(bbox_height, axes=(0, 2, 1))) delta_height = nd.log(delta_height) concat_list = [delta_x, delta_y, delta_width, delta_height] for idx, sym in enumerate(concat_list): concat_list[idx] = nd.expand_dims(sym, axis=3) position_matrix = nd.concat(*concat_list, dim=3) return position_matrix
def do_detect(model, img, conf_thresh, nms_thresh, use_cuda=1): model.eval() t0 = time.time() if type(img) == np.ndarray and len(img.shape) == 3: # cv2 image img = nd.from_numpy(img.transpose(2, 0, 1)).\ img = nd.broadcast_div(img,255.0) img.expend_dims(axis=0) elif type(img) == np.ndarray and len(img.shape) == 4: img = nd.from_numpy(img.transpose(0, 3, 1, 2)) img = nd.broadcast_div(img,255.0) else: print("unknow image type") exit(-1) if use_cuda: img = img.cuda() img = nd.autograd.Variable(img) t1 = time.time() output = model(img) t2 = time.time() print('-----------------------------------') print(' Preprocess : %f' % (t1 - t0)) print(' Model Inference : %f' % (t2 - t1)) print('-----------------------------------') return utils.post_processing(img, conf_thresh, nms_thresh, output)
def extract_pairwise_multi_position_embedding_nd(position_mat, feat_dim, wave_length=1000): """ Extract multi-class position embedding Args: position_mat: [num_fg_classes, num_rois, num_rois, 4] feat_dim: dimension of embedding feature wave_length: Returns: embedding: [num_fg_classes, num_rois, num_rois, feat_dim] """ feat_range = nd.arange(0, feat_dim / 8) dim_mat = nd.broadcast_power(lhs=nd.full((1, ), wave_length), rhs=(8. / feat_dim) * feat_range) dim_mat = nd.Reshape(dim_mat, shape=(1, 1, 1, 1, -1)) position_mat = nd.expand_dims(100.0 * position_mat, axis=4) div_mat = nd.broadcast_div(lhs=position_mat, rhs=dim_mat) sin_mat = nd.sin(data=div_mat) cos_mat = nd.cos(data=div_mat) # embedding, [num_fg_classes, num_rois, num_rois, 4, feat_dim/4] embedding = nd.concat(sin_mat, cos_mat, dim=4) embedding = nd.Reshape(embedding, shape=(0, 0, 0, feat_dim)) return embedding
def extract_rank_embedding_nd(rank_dim, feat_dim, wave_length=1000): rank_range = nd.arange(0, rank_dim) feat_range = nd.arange(0, feat_dim / 2) dim_mat = nd.broadcast_power(lhs=nd.full((1, ), wave_length), rhs=(2. / feat_dim) * feat_range) dim_mat = nd.Reshape(dim_mat, shape=(1, -1)) rank_mat = nd.expand_dims(rank_range, axis=1) div_mat = nd.broadcast_div(lhs=rank_mat, rhs=dim_mat) sin_mat = nd.sin(data=div_mat) cos_mat = nd.cos(data=div_mat) embedding = nd.concat(sin_mat, cos_mat, dim=1) return embedding
def generate_weighted_disp_masks(self, flow, _LARGE_DISP): flow_mag = nd.norm(flow, axis=1, keepdims=True) flow_mag = nd.broadcast_div(flow_mag, _LARGE_DISP) flow_mag = nd.broadcast_minimum(flow_mag, nd.ones((1), ctx=flow_mag.context)) small_disp_masks = 1.0 - flow_mag large_disp_masks = flow_mag stacked_mask = nd.concat(small_disp_masks, large_disp_masks, dim=1) return stacked_mask
def preprocess(self, img): #pylint: disable=E1101 return nd.broadcast_div( nd.broadcast_minus(img, self.color_mean.as_in_context(img.context)), self.color_std.as_in_context(img.context))
def forward(self, words, subwords, wordsmask=None, subwordsmask=None, words_to_unique_subwords_indices=None): """Compute embedding of words in batch. Parameters ---------- words : mx.nd.NDArray Array of token indices. subwords : mx.nd.NDArray The subwords associated with the tokens in `words`. If words_to_unique_subwords_indices is specified may contain the subwords of the unique tokens in `words` with `words_to_unique_subwords_indices` containing the reverse mapping. wordsmask : mx.nd.NDArray, optional Mask for embeddings returned by the word level embedding operator. subwordsmask : mx.nd.NDArray, optional A mask for the subword embeddings looked up from `subwords`. Applied before sum reducing the subword embeddings. words_to_unique_subwords_indices : mx.nd.NDArray, optional Mapping from the position in the `words` array to the position in the words_to_unique_subwords_indices` array. """ #pylint: disable=arguments-differ embeddings = self.embedding(words) if wordsmask is not None: wordsmask = nd.expand_dims(wordsmask, axis=-1) embeddings = nd.broadcast_mul(embeddings, wordsmask) else: wordsmask = 1 if words_to_unique_subwords_indices is None: assert words.shape[0] == subwords.shape[0] if subwordsmask is None: subwordsmask = nd.ones_like(subwords) num_embeddings = \ nd.sum(subwordsmask, axis=-1, keepdims=True) + wordsmask subword_embeddings = self.subword_embedding(subwords, subwordsmask) return nd.broadcast_div(embeddings + subword_embeddings, num_embeddings) else: if subwordsmask is None: subwordsmask = nd.ones_like(subwords) subword_embedding_weights = self.subword_embedding( subwords, subwordsmask) words_to_unique_subwords_indices = \ words_to_unique_subwords_indices.reshape(words.shape) subword_embeddings = nd.Embedding( data=words_to_unique_subwords_indices, weight=subword_embedding_weights, input_dim=subword_embedding_weights.shape[0], output_dim=self.embedding_size) num_embeddings = nd.Embedding( data=words_to_unique_subwords_indices, weight=nd.sum(subwordsmask, axis=-1, keepdims=True), input_dim=subword_embedding_weights.shape[0], output_dim=1).reshape(words.shape).expand_dims(-1) + wordsmask return nd.broadcast_div(embeddings + subword_embeddings, num_embeddings)