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)
    def __len__(self):
        return self.data.size()


if __name__ == '__main__':
    from config import Config
    from kpda_parser import KPDA
    from torch.utils.data import DataLoader
    from time import time
    from utils import draw_heatmap

    root_path = '/home/storage/lsy/fashion/'
    db_path = root_path + 'FashionAI_Keypoint_Detection/'

    config = Config('dress')
    train_data = KPDA(config, db_path, 'train')

    data_dataset = DataGenerator(config, train_data, phase='train')
    data_loader = DataLoader(data_dataset,
                             batch_size=1,
                             shuffle=False,
                             num_workers=1,
                             collate_fn=data_dataset.collate_fn,
                             pin_memory=True)
    for i, (imgs, heatmaps, vismaps) in enumerate(data_loader):
        print()
        # for j in range(len(imgs)):
        #     img = imgs[j]
        #     img = np.transpose(img.numpy(), (1, 2, 0))
        #     img = ((img * config.sigma + config.mu) * 255).astype(np.uint8)
        #     hm_pred = heatmaps[j].numpy()
Exemplo n.º 3
0
import cv2
import pandas as pd
import numpy as np

from config import Config
from kpda_parser import KPDA
from utils import draw_keypoint_with_caption

root_path = '/home/storage/lsy/fashion/'
db_path = root_path + 'FashionAI_Keypoint_Detection/'

if __name__ == '__main__':
    config = Config('blouse')
    test_kpda = KPDA(config, db_path, 'test')
    df = pd.read_csv(root_path + 'kp_predictions/' + config.clothes + '.csv')
    for idx in range(3):
        img_path = test_kpda.get_image_path(idx)
        img0 = cv2.imread(img_path)  # BGR
        row = test_kpda.anno_df.iloc[idx]
        row2 = df[df['image_id'] == row['image_id']].T.squeeze()
        hps = []
        for k, v in row2.iteritems():
            if k in ['image_id', 'image_category'] or v.split('_')[2] == '-1':
                continue
            x = int(v.split('_')[0])
            y = int(v.split('_')[1])
            image = draw_keypoint_with_caption(img0, [x, y], k)
            hps.append(image)
        cv2.imwrite('/home/storage/lsy/fashion/tmp/%d.png' % idx,
                    np.concatenate(hps, axis=1))
Exemplo n.º 4
0
                reg_target, cls_target,
                [config.img_max_size, config.img_max_size])
            img = np.transpose(img.numpy(), (1, 2, 0))
            img = ((img * config.sigma + config.mu) * 255).astype(np.uint8)
            draw_bbox(
                img, bboxes.numpy(), scores.numpy(),
                '/home/storage/lsy/fashion/predictions/' + config.clothes +
                '/%d-%d.png' % (i, j), bboxes2.numpy())


if __name__ == '__main__':
    batch_size = 24
    workers = 16
    config = Config('outwear')
    n_gpu = pytorch_utils.setgpu('all')
    test_kpda = KPDA(config, db_path, 'val')
    print('Val sample number: %d' % test_kpda.size())

    net = RetinaNet(config, num_classes=2)
    checkpoint = torch.load(root_path +
                            'checkpoints/rpn_053.ckpt')  # must before cuda
    net.load_state_dict(checkpoint['state_dict'])
    net = net.cuda()
    cudnn.benchmark = True
    net = DataParallel(net)

    test_dataset = DataGenerator(config, test_kpda, phase='test')
    test_loader = DataLoader(test_dataset,
                             batch_size=batch_size,
                             shuffle=False,
                             num_workers=workers,
        a[:, :, :img_w2 // config.hm_stride] = np.flip(
            hm_pred[:, :, :img_w2 // config.hm_stride], 2)
        for conj in config.conjug:
            a[conj] = a[conj[::-1]]
        hm_pred = a
    return hm_pred

    # x, y = encoder.decode_np(hm_pred, scale, config.hm_stride, method='maxoffset')
    # keypoints = np.stack([x, y, np.ones(x.shape)], axis=1).astype(np.int16)
    # return keypoints


if __name__ == '__main__':
    config = Config('trousers')
    n_gpu = pytorch_utils.setgpu('3')
    test_kpda = KPDA(config, db_path, 'test')
    print('Test sample number: %d' % test_kpda.size())
    df = data_frame_template()
    net = CascadePyramidNet(config)
    checkpoint = torch.load(
        root_path +
        'checkpoints/trousers_027_posneu_lgtrain.ckpt')  # must before cuda
    net.load_state_dict(checkpoint['state_dict'])
    net = net.cuda()
    cudnn.benchmark = True
    net = DataParallel(net)
    net.eval()
    encoder = KeypointEncoder()
    for idx in tqdm(range(test_kpda.size())):
        img_path = test_kpda.get_image_path(idx)
        img0 = cv2.imread(img_path)  # BGR
Exemplo n.º 6
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
Exemplo n.º 7
0
    batch_size = 8
    workers = 4
    n_gpu = pytorch_utils.setgpu('all')
    epochs = 100
    base_lr = 1e-3  # SGD L1 loss starts from 1e-2, L2 loss starts from 1e-3
    save_dir = root_path + 'checkpoints/'
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)
    resume = False

    config = Config(args.clothes)
    net = CascadePyramidNetV3(
        config
    )  #UNet(config) #VGG_FCN(config, layer_num=8) #ResidualUNet2D(config)  #
    loss = VisErrorLossV3()
    train_data = KPDA(config, db_path, 'train')
    val_data = KPDA(config, db_path, 'val')
    print('Train sample number: %d' % train_data.size())
    print('Val sample number: %d' % val_data.size())

    start_epoch = 1
    lr = base_lr
    best_val_loss = float('inf')
    log_mode = 'w'
    if resume:
        checkpoint = torch.load(save_dir + 'kpt_outwear_020.ckpt')
        start_epoch = checkpoint['epoch'] + 1
        lr = checkpoint['lr']
        best_val_loss = checkpoint['best_val_loss']
        net.load_state_dict(checkpoint['state_dict'])
        log_mode = 'a'
        a[:, :, :img_w2 // config.hm_stride] = np.flip(
            hm_pred[:, :, :img_w2 // config.hm_stride], 2)
        for conj in config.conjug:
            a[conj] = a[conj[::-1]]
        hm_pred = a
    return hm_pred

    # x, y = encoder.decode_np(hm_pred, scale, config.hm_stride, method='maxoffset')
    # keypoints = np.stack([x, y, np.ones(x.shape)], axis=1).astype(np.int16)
    # return keypoints


if __name__ == '__main__':
    config = Config('dress')
    n_gpu = pytorch_utils.setgpu('0')
    val_kpda = KPDA(config, db_path, 'val')
    print('Validation sample number: %d' % val_kpda.size())
    cudnn.benchmark = True
    net = CascadePyramidNet(config)
    checkpoint = torch.load(
        root_path +
        'checkpoints/dress_043_posneu_lgtrain.ckpt')  # must before cuda
    net.load_state_dict(checkpoint['state_dict'])
    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'])