Exemplo n.º 1
0
def predict(img, net):
    # X = image.imread(img)
    X = nd.array(img)
    X = normalize_image(X).as_in_context(CTX[0])
    # X = (X.astype('float32') / 255).as_in_context(CTX[0])
    X = X.transpose((2, 0, 1)).expand_dims(axis=0)
    pred = nd.argmin(net(X), axis=1)
    # pred = net(X) > 0.01
    return pred.reshape((X.shape[2], X.shape[3]))
Exemplo n.º 2
0
def getclass(rgb, cls_items):
    result = nd.zeros((cls_items.shape[0], ), ctx)
    for i in range(cls_items.shape[0]):
        delta = (rgb -
                 cfg.rgb_dic[str(int(cls_items[i]))]).abs().mean().asscalar()
        result[i] = delta

    get_cls = nd.argmin(result, axis=0).asscalar()
    cls_return = int(cls_items[int(get_cls)])

    return cls_return
Exemplo n.º 3
0
def knn_search(feature_matrix, features):
    """
    Given features and feature matrix, using KNN method to find the nearest class.
    给定特征矩阵和待搜索的特征之后,使用KNN搜索查询和特征最近的类别。
    :param feature_matrix: feature matrix 特征矩阵
    :param features: class uncertained features 待确定类别的特征
    :return predictions: class predictions 预测类别
    """
    predictions = []
    for feature in features:
        diff = feature - feature_matrix
        diff = nd.square(diff)
        diff = nd.sum(diff, axis=1)
        prediction = nd.argmin(diff, axis=0)
        predictions.append(prediction.asscalar())
    return nd.array(predictions)
Exemplo n.º 4
0
def bulyan(epoch, gradients, net, lr, byz, f=0):

    param_list = [
        nd.concat(*[xx.reshape((-1, 1)) for xx in x], dim=0) for x in gradients
    ]
    param_list = byz(epoch, param_list, net, f, lr, np.arange(len(param_list)))

    k = len(param_list) - f - 2
    dist = mx.nd.zeros((len(param_list), len(param_list)))
    for i in range(0, len(param_list)):
        for j in range(0, i):
            dist[i][j] = nd.norm(param_list[i] - param_list[j])
            dist[j][i] = dist[i][j]

    sorted_dist = mx.nd.sort(dist)
    sum_dist = mx.nd.sum(sorted_dist[:, :k + 1], axis=1)
    bulyan_list = []
    bul_client_list = np.ones(len(param_list)) * (-1)
    for i in range(len(param_list) - 2 * f):
        chosen = int(nd.argmin(sum_dist).asscalar())
        sum_dist[chosen] = 10**8
        bul_client_list[i] = chosen
        bulyan_list.append(param_list[chosen])
        for j in range(len(sum_dist)):
            sum_dist[j] = sum_dist[j] - dist[j][chosen]
    sorted_array = nd.sort(nd.concat(*bulyan_list, dim=1), axis=-1)
    trim_nd = nd.mean(sorted_array[:, f:(len(bulyan_list) - f)],
                      axis=-1,
                      keepdims=1)

    idx = 0
    for j, (param) in enumerate(net.collect_params().values()):
        if param.grad_req == 'null':
            continue
        param.set_data(
            param.data() - lr *
            trim_nd[idx:(idx + param.data().size)].reshape(param.data().shape))
        idx += param.data().size
    return trim_nd, bul_client_list
Exemplo n.º 5
0
embedd = nd.stack(*[embedd_d[key] for key in sorted(embedd_d)], axis=0)
router_mat = nd.stack(*[router_mat_d[key] for key in sorted(router_mat_d)],
                      axis=1)

where = nd.argmax(nd.maximum(0, 1 / (router + 0.5)), axis=1)

nd.maximum(0, 1 / (router + 0.5))

where

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)
Exemplo n.º 6
0
def decode_centernet_pose(heat, wh, kps, reg=None, hm_hp=None, hp_offset=None, K=100):
    batch, cat, height, width = heat.shape
    num_joints = kps.shape[1] // 2
    # perform nms on heatmaps
    heat = _nms(heat)
    scores, inds, clses, ys, xs = _topk(heat, K=K)

    kps = _tranpose_and_gather_feat(kps, inds)
    kps = nd.reshape(kps, (batch, K, num_joints * 2))

    kps[:, :, ::2] += nd.reshape(xs, (batch, K, 1)).broadcast_to((batch, K, num_joints))
    kps[:, :, 1::2] += nd.reshape(ys, (batch, K, 1)).broadcast_to((batch, K, num_joints))

    if reg is not None:
        reg = _tranpose_and_gather_feat(reg, inds)
        reg = nd.reshape(reg,(batch, K, 2))
        xs = xs.reshape((batch, K, 1)) + reg[:, :, 0:1]
        ys = ys.reshape((batch, K, 1)) + reg[:, :, 1:2]
    else:
        xs = xs.reshape((batch, K, 1)) + 0.5
        ys = ys.reshape((batch, K, 1)) + 0.5

    wh = _tranpose_and_gather_feat(wh, inds)
    wh = wh.reshape((batch, K, 2))
    clses  = clses.reshape((batch, K, 1)).astype('float32')
    scores = scores.reshape((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 hm_hp is not None:
        hm_hp = _nms(hm_hp)
        thresh = 0.1
        kps = kps.reshape((batch, K, num_joints, 2))
        kps = nd.swapaxes(kps, 1, 2) # b x J x K x 2

        reg_kps = nd.expand_dims(kps, axis=3).broadcast_to((batch, num_joints, K, K, 2))
        hm_score, hm_inds, hm_ys, hm_xs = _topk_channel(hm_hp, K=K) # b x J x K

        if hp_offset is not None:
            hp_offset = _tranpose_and_gather_feat(hp_offset, hm_inds.reshape((batch, -1)))
            hp_offset = hp_offset.reshape((batch, num_joints, K, 2))
            hm_xs = hm_xs + hp_offset[:, :, :, 0]
            hm_ys = hm_ys + hp_offset[:, :, :, 1]
        else:
            hm_xs = hm_xs + 0.5
            hm_ys = hm_ys + 0.5

        mask = (hm_score > thresh).astype('float32')
        hm_score = (1 - mask) * -1 + mask * hm_score
        hm_ys = (1 - mask) * (-10000) + mask * hm_ys
        hm_xs = (1 - mask) * (-10000) + mask * hm_xs

        hm_kps = nd.stack(hm_xs, hm_ys, axis=-1).expand_dims(axis=2).broadcast_to((batch, num_joints, K, K, 2))
        dist = (((reg_kps - hm_kps) ** 2).sum(axis=4) ** 0.5)
        min_dist = dist.min(axis=3) # b x J x K
        min_ind = nd.argmin(dist, axis=3) # b x J x K

        M, N, K = hm_score.shape[0:3]
        for i in range(M):
            for j in range(N):
                for k in range(K):
                    hm_score[i, j, k] = hm_score[i, j, min_ind[i, j, k]]
        hm_score = hm_score.expand_dims(axis=-1)
        min_dist = min_dist.expand_dims(-1)

        hm_kps = hm_kps.reshape((batch, num_joints, K, 2))
        for i in range(M):
            for j in range(N):
                for k in range(K):
                        hm_kps[i, j, k, 0] = hm_kps[i, j, min_ind[i, j, k], 0]
                        hm_kps[i, j, k, 1] = hm_kps[i, j, min_ind[i, j, k], 1]

        l = bboxes[:, :, 0].reshape((batch, 1, K, 1)).broadcast_to((batch, num_joints, K, 1))
        t = bboxes[:, :, 1].reshape((batch, 1, K, 1)).broadcast_to((batch, num_joints, K, 1))
        r = bboxes[:, :, 2].reshape((batch, 1, K, 1)).broadcast_to((batch, num_joints, K, 1))
        b = bboxes[:, :, 3].reshape((batch, 1, K, 1)).broadcast_to((batch, num_joints, K, 1))

        mask = (hm_kps[:, :, 0:1] < l) + (hm_kps[:, :, 0:1] > r)
        mask += (hm_kps[:, :, 1:2] < t) + (hm_kps[:, :, 1:2] > b)
        mask += (hm_score < thresh)
        mask += (min_dist > (nd.maximum(b - t, r - l) * 0.3))
        mask = (mask > 0).astype('float32').broadcast_to((batch, num_joints, K, 2))

        kps = (1 - mask) * hm_kps + mask * kps
        kps = nd.swapaxes(kps, 1, 2).reshape((batch, K, num_joints * 2))

    detections = nd.concat(bboxes, scores, kps, clses, dim=2)
    return detections
Exemplo n.º 7
0
 def argmin(data=None, axis=None):
     res = nd.argmin(data, axis)
     if res.shape == (1, ):
         return res.astype('int32').asscalar()
     else:
         return res