예제 #1
0
    :param targets: [[x, y, v], ...]
    :param widths: [[w1], [w2], ...]
    :return: 
    '''
    dist = preds[:, :2] - targets[:, :2]
    dist = np.sqrt(dist[:, 0]**2 + dist[:, 1]**2)
    targets = np.copy(targets)
    targets[targets<0] = 0
    if np.sum(targets[:, 2]) == 0:
        return 0
    ne = np.sum(dist/widths * targets[:, 2]) / np.sum(targets[:, 2])
    return ne

if __name__ == '__main__':
    from kpda_parser import KPDA
    from src.config import Config
    import cv2
    config = Config('trousers')
    db_path = '/home/storage/lsy/fashion/FashionAI_Keypoint_Detection/'
    kpda = KPDA(config, db_path, 'train')
    for idx in range(kpda.size()):
        img = cv2.imread(kpda.get_image_path(idx))  # BGR
        # bboxes = [kpda.get_bbox(idx)]
        # probs = [1.]
        # draw_bbox(img, bboxes, probs, '/home/storage/lsy/fashion/tmp/%d.png' % idx)
        keypoints = kpda.get_keypoints(idx)
        if np.all(keypoints[:, 2]>0):
            print(keypoints)
            draw = draw_keypoints(img, keypoints)
            cv2.imwrite('/home/storage/lsy/fashion/tmp/%d.png' % idx, draw)
            break
                y2, x2 = np.unravel_index(si[:, -2], [h, w])
                x = (3 * x1 + x2) / 4.
                y = (3 * y1 + y2) / 4.
            var = np.var(heatmap_th, axis=1)
            x[var < 1] = dfx
            y[var < 1] = dfy
        x = x * stride / scale
        y = y * stride / scale
        return np.rint(x + 2), np.rint(y + 2)


if __name__ == '__main__':
    from kpda_parser import KPDA
    import cv2
    from src.config import Config
    import numpy as np
    config = Config()
    db_path = '/home/storage/lsy/fashion/FashionAI_Keypoint_Detection/train/'
    kpda = KPDA(db_path, 'train')
    img_path = kpda.get_image_path(2)
    kpts = kpda.get_keypoints(2)
    kpts = torch.from_numpy(kpts)
    img = cv2.imread(img_path)
    image = np.zeros([512, 512, 3])
    image[:512, :504, :] = img
    cv2.imwrite('/home/storage/lsy/fashion/tmp/img.jpg', image)
    ke = KeypointEncoder()
    heatmaps, _ = ke.encode(kpts, image.shape[:2], config.hm_stride)
    for i, heatmap in enumerate(heatmaps):
        heatmap = np.expand_dims(heatmap.numpy() * 255, 2)
        cv2.imwrite('/home/storage/lsy/fashion/tmp/map%d.jpg' % i, heatmap)
    net = net.cuda()
    net = DataParallel(net)
    net.eval()
    net2 = CascadePyramidNetV9(config)
    checkpoint = torch.load(
        root_path +
        'checkpoints/dress_030_senet_posneu_lgtrain.ckpt')  # must before cuda
    net2.load_state_dict(checkpoint['state_dict'])
    net2 = net2.cuda()
    net2 = DataParallel(net2)
    net2.eval()
    encoder = KeypointEncoder()
    nes = []
    for idx in tqdm(range(val_kpda.size())):
        img_path = val_kpda.get_image_path(idx)
        kpts = val_kpda.get_keypoints(idx)
        img0 = cv2.imread(img_path)  # BGR
        img0_flip = cv2.flip(img0, 1)
        img_h, img_w, _ = img0.shape

        # ----------------------------------------------------------------------------------------------------------------------
        scale = config.img_max_size / max(img_w, img_h)
        hm_pred = compute_keypoints(config, img0, net, encoder)
        hm_pred2 = compute_keypoints(config,
                                     img0_flip,
                                     net,
                                     encoder,
                                     doflip=True)
        hm_pred3 = compute_keypoints(config, img0, net2, encoder)
        hm_pred4 = compute_keypoints(config,
                                     img0_flip,