Пример #1
0
def show_pairs(images, features, pairs):
    dists = np.sqrt(np.sum((features[0] - features[1])**2, axis=1))
    ds_utils.denormalize(images[0])
    ds_utils.denormalize(images[1])
    images[1] = vis.add_error_to_images(images[1],
                                        dists,
                                        size=2.0,
                                        thickness=2,
                                        vmin=0,
                                        vmax=1)
    images[1] = vis.add_id_to_images(images[1],
                                     pairs.numpy(),
                                     size=1.2,
                                     thickness=2,
                                     color=(1, 0, 1))
    thresh = 0.4
    corrects = (dists < thresh) == pairs.cpu().numpy()
    colors = [(0, 1, 0) if c else (1, 0, 0) for c in corrects]
    images[1] = vis.add_cirle_to_images(images[1], colors)
    images[0] = vis._to_disp_images(images[0])
    img_rows = [
        vis.make_grid(imgs,
                      fx=0.75,
                      fy=0.75,
                      nCols=len(dists),
                      normalize=False) for imgs in images
    ]
    vis.vis_square(img_rows, nCols=1, normalize=False)
Пример #2
0
if __name__ == "__main__":
    # extract_main()
    # exit()
    from utils.nn import Batch
    from utils import random

    random.init_random()

    ds = VggFace2(
        train=True,
        deterministic=True,
        use_cache=False,
        align_face_orientation=False,
        return_modified_images=False,
        image_size=256,
    )
    micro_batch_loader = td.DataLoader(ds,
                                       batch_size=10,
                                       shuffle=True,
                                       num_workers=0)

    f = 1.0
    t = time.perf_counter()
    for iter, data in enumerate(micro_batch_loader):
        print("t load:", time.perf_counter() - t)
        t = time.perf_counter()
        batch = Batch(data, gpu=False)
        print("t Batch:", time.perf_counter() - t)
        images = nn.denormalized(batch.images)
        vis.vis_square(images, fx=f, fy=f, normalize=False, nCols=10, wait=0)
Пример #3
0
                result['landmarks'], self.landmark_sigma, self.landmark_ids)

        return result


if __name__ == '__main__':
    from utils.nn import Batch, to_numpy
    import utils.common
    from landmarks import lmconfig as lmcfg

    utils.common.init_random(3)
    lmcfg.config_landmarks('wflw')

    ds = WFLW(train=True, deterministic=True, use_cache=True, daug=0)
    # ds.filter_labels({'pose':0, 'blur':0, 'occlusion':1})
    dl = td.DataLoader(ds, batch_size=1, shuffle=False, num_workers=0)

    cfg.WITH_LANDMARK_LOSS = False

    for data in dl:
        batch = Batch(data, gpu=False)
        images = vis._to_disp_images(batch.images, denorm=True)
        # lms = lmutils.convert_landmarks(to_numpy(batch.landmarks), lmutils.LM98_TO_LM68)
        lms = batch.landmarks
        images = vis.add_landmarks_to_images(images,
                                             lms,
                                             draw_wireframe=False,
                                             color=(0, 255, 0),
                                             radius=3)
        vis.vis_square(images, nCols=1, fx=1., fy=1., normalize=False)
Пример #4
0
        return np.concatenate(sizes)


if __name__ == '__main__':
    import torch
    from utils import vis
    from utils.nn import Batch
    from datasets import ds_utils
    from datasets import affectnet

    torch.manual_seed(0)
    torch.cuda.manual_seed_all(0)

    train = True
    datasets = [
        affectnet.AffectNet(train=train, max_samples=1000),
        # vggface2.VggFace2(train=train, max_samples=1000),
    ]
    multi_ds = MultiFaceDataset(datasets, train=True, max_samples=5000)
    print(multi_ds)
    dl = td.DataLoader(multi_ds, batch_size=40, shuffle=False, num_workers=0)
    for data in dl:
        batch = Batch(data, gpu=False)
        inputs = batch.images.clone()
        ds_utils.denormalize(inputs)
        imgs = vis.add_landmarks_to_images(inputs.numpy(),
                                           batch.landmarks.numpy())
        # imgs = vis.add_pose_to_images(inputs.numpy(), batch.poses.numpy())
        # imgs = vis.add_emotion_to_images(imgs, batch.emotions.numpy())
        vis.vis_square(imgs, nCols=20, fx=0.6, fy=0.6, normalize=False)
Пример #5
0
    parser.add_argument('--nd', default=1, type=int)
    args = parser.parse_args()

    extract_features(st=args.st, nd=args.nd)



if __name__ == '__main__':
    # extract_main()
    # exit()

    import torch
    from utils import vis, face_processing
    from utils.nn import Batch

    torch.manual_seed(0)
    torch.cuda.manual_seed_all(0)

    ds = LFWImages(train=True, deterministic=True, max_samples=40, use_cache=False)
    dl = torch.utils.data.DataLoader(ds, batch_size=10, shuffle=True, num_workers=1)

    data = next(iter(dl))
    inputs = Batch(data).images

    ds_utils.denormalize(inputs)
    # imgs = vis.add_id_to_images(inputs.numpy(), data[1].numpy())
    # imgs = vis.add_pose_to_images(inputs.numpy(), pose.numpy())
    # imgs = vis.add_landmarks_to_images(inputs.numpy(), landmarks.numpy())
    imgs = inputs.detach().cpu().numpy()
    vis.vis_square(imgs, fx=1.0, fy=1.0, normalize=False)
Пример #6
0
    args = parser.parse_args()

    extract_features(st=args.st, nd=args.nd)


if __name__ == '__main__':
    # extract_main()
    # create_annotations(split='dev', num_ids=500)
    # extract_crops()

    # read_openface_csvs()
    # exit()

    from utils import vis, face_processing

    ds = VoxCeleb(train=True, max_samples=50000, use_cache=True)
    print(ds)
    dl = td.DataLoader(ds, batch_size=40, shuffle=False, num_workers=0)
    from utils.nn import Batch

    for data in dl:
        batch = Batch(data)
        print(batch.ids)
        ds_utils.denormalize(batch.images)
        # vis.show_images_in_batch(batch.images.detach().cpu())
        vis.vis_square(batch.images.detach().cpu(),
                       fx=0.7,
                       fy=0.7,
                       normalize=False)
        # print(item)
Пример #7
0
            lm_x, lm_y = lm[0], lm[1]
            cv2.circle(img, (int(lm_x), int(lm_y)), 3, (0, 0, 255), -1)
        cv2.imshow('landmarks', cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        cv2.waitKey(0)



if __name__ == '__main__':

    from utils import vis
    import torch

    torch.manual_seed(0)
    torch.cuda.manual_seed_all(0)

    ds = W300(train=True, deterministic=True, use_cache=True,
              test_split='challenging', daug=0, align_face_orientation=False,
              return_modified_images=False)
    dl = td.DataLoader(ds, batch_size=50, shuffle=False, num_workers=0)
    print(ds)

    cfg.WITH_LANDMARK_LOSS = False

    for data in dl:
        batch = Batch(data, gpu=False)
        inputs = batch.images.clone()
        imgs = vis._to_disp_images(inputs, denorm=True)
        imgs = vis.add_landmarks_to_images(imgs, batch.landmarks, radius=3, color=(0,255,0))
        # imgs = vis.add_landmarks_to_images(imgs, data['landmarks_of'].numpy(), color=(1,0,0))
        vis.vis_square(imgs, nCols=5, fx=1, fy=1, normalize=False)
Пример #8
0
def calc_triplet_loss(outputs,
                      c,
                      return_acc=False,
                      images=None,
                      feature_name=None,
                      wnd_title=None):

    margin = 0.2
    eps = 1e-8

    debug = False
    is_expressions = (not isinstance(
        c, list)) and len(c.shape) > 1 and c.shape[1] == 3

    pos_id, neg_id = make_triplets(outputs, c, debug=debug)

    X, P, N = outputs[:, :], outputs[pos_id, :], outputs[neg_id, :]
    dpos = torch.sqrt(torch.sum((X - P)**2, dim=1) + eps)
    dneg = torch.sqrt(torch.sum((X - N)**2, dim=1) + eps)
    loss = torch.mean(
        torch.clamp(dpos - dneg + margin, min=0.0, max=margin * 2.0))
    # show triplets
    if images is not None:
        from utils import vis
        from datasets import ds_utils
        if debug and is_expressions:
            for i in range(10):
                print(c[:, 0][i].item(), c[pos_id, 0][i].item(),
                      c[neg_id, 0][i].item())
        # ids, vids = c[0], c[1]
        # print(vids[:5])
        # print(vids[pos_id][:5])
        # print(vids[neg_id][:5])
        nimgs = 20
        losses = to_numpy(
            torch.clamp(dpos - dneg + margin, min=0.0, max=margin * 2.0))
        # print("Acc: ", 1 - sum(dpos[:nimgs] >= dneg[:nimgs]).item()/float(len(dpos[:nimgs])))
        # print("L  : ", losses.mean())
        images_ref = ds_utils.denormalized(images[:nimgs].clone())
        images_pos = ds_utils.denormalized(images[pos_id][:nimgs].clone())
        images_neg = ds_utils.denormalized(images[neg_id][:nimgs].clone())
        colors = [(0, 1, 0) if c else (1, 0, 0) for c in dpos < dneg]
        f = 0.75
        images_ref = vis.add_error_to_images(vis.add_cirle_to_images(
            images_ref, colors),
                                             losses,
                                             size=1.0,
                                             vmin=0,
                                             vmax=0.5,
                                             thickness=2,
                                             format_string='{:.2f}')
        images_pos = vis.add_error_to_images(images_pos,
                                             to_numpy(dpos),
                                             size=1.0,
                                             vmin=0.5,
                                             vmax=1.0,
                                             thickness=2,
                                             format_string='{:.2f}')
        images_neg = vis.add_error_to_images(images_neg,
                                             to_numpy(dneg),
                                             size=1.0,
                                             vmin=0.5,
                                             vmax=1.0,
                                             thickness=2,
                                             format_string='{:.2f}')
        if is_expressions:
            emotions = to_numpy(c[:, 0]).astype(int)
            images_ref = vis.add_emotion_to_images(images_ref, emotions)
            images_pos = vis.add_emotion_to_images(images_pos,
                                                   emotions[pos_id])
            images_neg = vis.add_emotion_to_images(images_neg,
                                                   emotions[neg_id])
        elif feature_name == 'id':
            ids = to_numpy(c).astype(int)
            images_ref = vis.add_id_to_images(images_ref, ids, loc='tr')
            images_pos = vis.add_id_to_images(images_pos,
                                              ids[pos_id],
                                              loc='tr')
            images_neg = vis.add_id_to_images(images_neg,
                                              ids[neg_id],
                                              loc='tr')

        img_ref = vis.make_grid(images_ref, nCols=nimgs, padsize=1, fx=f, fy=f)
        img_pos = vis.make_grid(images_pos, nCols=nimgs, padsize=1, fx=f, fy=f)
        img_neg = vis.make_grid(images_neg, nCols=nimgs, padsize=1, fx=f, fy=f)
        title = 'triplets'
        if feature_name is not None:
            title += " " + feature_name
        if wnd_title is not None:
            title += " " + wnd_title
        vis.vis_square([img_ref, img_pos, img_neg],
                       nCols=1,
                       padsize=1,
                       normalize=False,
                       wait=10,
                       title=title)

        # plt.plot(to_numpy((X[:nimgs]-P[:nimgs]).abs()), 'b')
        # plt.plot(to_numpy((X[:nimgs]-N[:nimgs]).abs()), 'r')
        # plt.show()
    if return_acc:
        return loss, sum(dpos >= dneg).item() / float(len(dpos))
    else:
        return loss