Ejemplo n.º 1
0
def refine_bbox_nd(bbox, bbox_delta, im_info=None, means=None, stds=None):

    xmin, ymin, xmax, ymax = nd.split(data=bbox, num_outputs=4, axis=1)
    bbox_width = xmax - xmin + 1.
    bbox_height = ymax - ymin + 1.
    center_x = 0.5 * (xmin + xmax)
    center_y = 0.5 * (ymin + ymax)

    bbox_delta_reshape = nd.Reshape(data=bbox_delta, shape=(0, -1, 4))
    dx, dy, dw, dh = nd.split(data=bbox_delta_reshape,
                              num_outputs=4,
                              axis=2,
                              squeeze_axis=1)
    if (means is not None) and (stds is not None):
        dx = dx * stds[0] + means[0]
        dy = dy * stds[1] + means[1]
        dw = dw * stds[2] + means[2]
        dh = dh * stds[3] + means[3]

    refine_center_x = nd.broadcast_add(lhs=center_x,
                                       rhs=nd.broadcast_mul(lhs=bbox_width,
                                                            rhs=dx))
    refine_center_y = nd.broadcast_add(lhs=center_y,
                                       rhs=nd.broadcast_mul(lhs=bbox_height,
                                                            rhs=dy))
    refined_width = nd.broadcast_mul(lhs=bbox_width, rhs=nd.exp(dw))
    refined_height = nd.broadcast_mul(lhs=bbox_height, rhs=nd.exp(dh))
    w_offset = 0.5 * (refined_width - 1.)
    h_offset = 0.5 * (refined_height - 1.)
    refined_xmin = nd.expand_dims(refine_center_x - w_offset, axis=1)
    refined_ymin = nd.expand_dims(refine_center_y - h_offset, axis=1)
    refined_xmax = nd.expand_dims(refine_center_x + w_offset, axis=1)
    refined_ymax = nd.expand_dims(refine_center_y + h_offset, axis=1)

    refined_bbox = nd.concat(refined_xmin,
                             refined_ymin,
                             refined_xmax,
                             refined_ymax,
                             dim=1)
    if im_info is not None:
        # assume im_info [[height, width, scale]] with shape (1,3)
        im_hw = nd.slice_axis(im_info, axis=1, begin=0, end=2)
        im_wh = nd.reverse(im_hw, axis=1)
        im_wh = im_wh - 1.
        im_wh = nd.tile(data=im_wh, reps=(1, 2))
        im_wh = nd.Reshape(im_wh, shape=(1, 4, 1))
        refined_bbox = nd.broadcast_minimum(lhs=refined_bbox, rhs=im_wh)
        refined_bbox = nd.broadcast_maximum(lhs=refined_bbox,
                                            rhs=nd.zeros_like(refined_bbox))
    # print refined_bbox.debug_str()
    return refined_bbox
Ejemplo n.º 2
0
    def forward(self, x):
        root = next(iter(self._structure.items()))[0]

        router, router_mat, weight, embedd = self._contextify(x)(root)

        presence = nd.sum(router_mat, axis=2)
        weight_adj = presence * weight
        depth = len(self._weightlayer) - nd.topk(nd.reverse(presence,
                                                            axis=1)) - 1
        depth = depth[:, 0]
        remainder = 1 - nd.sum(weight_adj, axis=1)
        remainder += nd.choose_element_0index(weight_adj, depth)
        weight_adj = nd.fill_element_0index(weight_adj, remainder, depth)

        return (router, weight, weight_adj, router_mat)
Ejemplo n.º 3
0
    def forward(self, inputs, states_forward=None, states_backward=None):
        inputs_reversed = nd.reverse(inputs, axis=2)

        if not (states_forward and states_backward):
            states_forward, states_backward = self.begin_state(batch_size=inputs.shape[1])

        outputs_forward = []
        outputs_backward = []

        for i in range(self.num_layers):
            if i == 0:
                output, states_forward[i] = getattr(self, 'forward_lstm_{}'.format(i))(inputs, states_forward[i])
                outputs_forward.append(output)

                output, states_backward[i] = getattr(self, 'backward_lstm_{}'.format(i))(inputs_reversed, states_backward[i])
                outputs_backward.append(output)
            else:
                output, states_forward[i] = getattr(self, 'forward_lstm_{}'.format(i))(outputs_forward[i-1], states_forward[i])
                outputs_forward.append(output)

                output, states_backward[i] = getattr(self, 'backward_lstm_{}'.format(i))(outputs_backward[i-1], states_backward[i])
                outputs_backward.append(output)
        return outputs_forward, states_forward, outputs_backward, states_backward
Ejemplo n.º 4
0
    def forward(self, x):
        root = next(iter(self._structure.items()))[0]

        if (len(self._routerlayer) > 0):
            router, router_mat, weight, embedd = self._contextify(x)(root)

            presence = nd.sum(router_mat, axis=2)
            weight_adj = presence * weight
            depth = len(self._weightlayer) - nd.topk(
                nd.reverse(presence, axis=1))
            depth -= 1
            depth = depth[:, 0]
            remainder = 1 - nd.sum(weight_adj, axis=1)
            remainder += nd.choose_element_0index(weight_adj, depth)
            weight_adj = nd.fill_element_0index(weight_adj, remainder, depth)

            head = nd.sum(nd.expand_dims(weight_adj, axis=2) * router_mat,
                          axis=1)

            return nd.expand_dims(nd.dot(head, embedd), axis=-1)

        else:
            head = nd.ones_like(nd.slice_axis(x, axis=1, begin=0, end=None))
            return self._contextify(x)(root) * head
Ejemplo n.º 5
0
presence = nd.sum(router_mat, axis=2)
weight_adj = presence * weight

weight
router + 0.5
nd.argmin(nd.abs(router + 0.5), axis=1)

router_mat[1][4]

depth

where = nd.argmin(nd.abs(router + 0.5), axis=1)
nd.concat(*[router_mat[i][k] for i, k in enumerate(where)], dim=0)

depth = len(tree._weightlayer) - nd.topk(nd.reverse(presence, axis=1))
depth -= 1
depth = depth[:, 0]
remainder = 1 - nd.sum(weight_adj, axis=1)
remainder += nd.choose_element_0index(weight_adj, depth)
weight_adj = nd.fill_element_0index(weight_adj, remainder, depth)

head = nd.sum(nd.expand_dims(weight_adj, axis=2) * router_mat, axis=1)

print(head)

nd.expand_dims(nd.dot(head, embedd), axis=-1)

old = weight_adj
remainder
Ejemplo n.º 6
0
tree = Tree()
tree.collect_params().initialize(force_reinit=True)

tree(nd.array([[1, 3], [5, 6], [1, 2], [3, 0], [8, 8], [3.5, 3.5]]))

tree(nd.array([[10, 3], [3.5, 4.5], [0, 2], [2.9, 6], [5, 6]]))

nd.sum(nd.expand_dims(weight_adj, axis=2) * router_mat, axis=1)

weight_adj

router_mat

nd.sum(router_mat, axis=2)

(6 - nd.topk(nd.reverse(nd.sum(router_mat, axis=2), axis=1)))[:, 0]

nd.choose_element_0index(nd.sum(router_mat, axis=2), nd.array([4, 6, 1, 0, 6]))

weight

nd.sum(router_mat, axis=2) * weight
nd.sum(nd.sum(router_mat, axis=2) * weight, axis=1)

tree.collect_params()

[x for x in tree._weightlayer]

[key for key, value in tree._weightlayer._children.items()]
tree._weightlayer(nd.array([[1, 3], [5, 6], [6, 2], [20, 20]]))
tree._routerlayer(nd.array([[1, 3], [5, 6], [6, 2], [2, 2]]))
Ejemplo n.º 7
0
    def _viterbi_decode(self, feats):
        """
        CRF's prediction algorithm, Viterbi algorithm, which finds the best path based on features

        Args:
            feats (NDArray): a representative representation of each symbol representing the entire
                             sequence in a batch, shape is (seq_len, batch_size, tagset_size)

        Returns:
            path_score (NDArray): value of score , shape is (batch_size, )
            best_path_matrix (NDArray): the matrix of best path, shape is (batch_size, seq_len)
        """

        batch_size = feats.shape[1]

        # vvars shape:(batch_size, self.tagset_size)
        vvars = nd.full((batch_size, self.tagset_size), -10000., ctx=self.ctx)
        vvars[:, self.tag2idx[START_TAG]] = 0.0

        def update_decode(data, states):
            feat = data
            vvars_iner = states

            # vvars_t shape: (self.tagset_size, batch_size, self.tagset_size)
            vvars_t = nd.broadcast_axis(nd.expand_dims(vvars_iner, axis=0), axis=0,
                                        size=self.tagset_size)
            # trans shape: (self.tagset_size, 1, self.tagset_size)
            trans = nd.expand_dims(self.transitions.data(), axis=1)
            next_tag_var = vvars_t + trans

            # best_tag_id shape: (self.tagset_size, batch_size)
            best_tag_id = nd.argmax(next_tag_var, axis=-1)

            # bptrs_t, viterbivars_t  shape :(batch_size, tagset_size)
            viterbivars_t = nd.transpose(nd.pick(next_tag_var, best_tag_id, axis=-1), axes=(1, 0))
            bptrs_t = nd.transpose(best_tag_id, axes=(1, 0))

            vvars_iner = viterbivars_t + feat

            return bptrs_t, vvars_iner

        # backpointers shape: (seq_len, batch_size, self.tagset_size)
        backpointers, vvars = nd.contrib.foreach(update_decode, feats, vvars)

        # transform to STOP_TAG
        # terminal_var shape: (batch_size, self.tagset_size)
        terminal_var = vvars + self.transitions.data()[self.tag2idx[STOP_TAG]]
        best_tag_id = nd.argmax(terminal_var, axis=1)
        # path_score shape:(batch_size, )
        path_score = nd.pick(terminal_var, best_tag_id, axis=1)

        # According to the reverse pointer backpointers to decode the best path
        best_path = [best_tag_id]
        for bptrs_t in nd.reverse(backpointers, axis=0):
            # best_tag_id shape: (batch_size, )
            best_tag_id = nd.pick(bptrs_t, best_tag_id, axis=1)
            best_path.append(best_tag_id)

        # remove START_TAG
        # start shape: (batch_size, )
        start = best_path.pop()
        # Check if start is the start symbol
        for i in range(batch_size):
            assert start[i].asscalar() == self.tag2idx[START_TAG]
        best_path.reverse()

        # Build the matrix of the best path, shape: (batch_size, seq_len)
        best_path_matrix = nd.stack(*best_path, axis=1)
        return path_score, best_path_matrix