Пример #1
0
def load_data(pair_infos, discrim, train=True):

    img_path1 = pair_infos[0][0]
    img_path2 = pair_infos[1][0]

    bs1 = pair_infos[0][1]  # xmin xmax ymin ymax
    bs2 = pair_infos[1][1]

    gt1 = Rectangle(bs1[0], bs1[2], bs1[1] - bs1[0], bs1[3] - bs1[2])
    gt2 = Rectangle(bs2[0], bs2[2], bs2[1] - bs2[0], bs2[3] - bs2[2])

    gt1 = convert_bbox_format(gt1, to='center-based')
    gt2 = convert_bbox_format(gt2, to='center-based')

    img1 = Image.open(img_path1).convert('RGB')
    img2 = Image.open(img_path2).convert('RGB')

    zbox1 = get_zbox(gt1, 0.25)
    zbox2 = get_zbox(gt2, 0.25)

    scales_w = 1.04**(random.random() * 6 - 3)
    scales_h = 1.04**(random.random() * 6 - 3)

    zbox2_scaled = Rectangle(zbox2.x, zbox2.y, zbox2.width * scales_w,
                             zbox2.height * scales_h)

    dx = 0
    dy = 0

    xbox2 = get_xbox(zbox2_scaled, dx,
                     dy)  # we assume second is the search region

    z = gen_xz(img1, zbox1, to='z')
    x = gen_xz(img2, xbox2, to='x')

    info = [
        dx, dy, gt2.width / scales_w / zbox2.width,
        gt2.height / scales_h / zbox2.height
    ]

    gt_box = np.array([np.log(info[2] * 2), np.log(info[3] * 2)])

    gt = np.zeros((1, 17, 17))
    gt[:, :, :] = -1
    gt[0, 8, 8] = 1.

    gt[0, 7:10, 7:10] = 1.
    gt[0, 8:9, 6:11] = 1.
    gt[0, 6:11, 8:9] = 1.

    return z, x, gt, gt_box
Пример #2
0
def load_data(pair_infos, discrim, train=True):

    anchors = generate_anchor(8, [
        8,
    ], [0.33, 0.5, 1, 2, 3], 17)

    gt = np.zeros((1, 17, 17))
    gt[:, :, :] = -1
    gt[0, 8, 8] = 1.

    #gt[0,7:10,7:10] = 1.
    #gt[0,8:9,6:11] = 1.
    #gt[0,6:11,8:9] = 1.

    #gt = np.zeros((1,28,28))

    #gt[0,13:15,13:15] = 1.

    if random.random > 0.0:

        img_path1 = pair_infos[0][0]
        img_path2 = pair_infos[1][0]

        bs1 = pair_infos[0][1]  #xmin xmax ymin ymax
        bs2 = pair_infos[1][1]

        gt1 = Rectangle(bs1[0], bs1[2], bs1[1] - bs1[0], bs1[3] - bs1[2])
        gt2 = Rectangle(bs2[0], bs2[2], bs2[1] - bs2[0], bs2[3] - bs2[2])

        gt1 = convert_bbox_format(gt1, to='center-based')
        gt2 = convert_bbox_format(gt2, to='center-based')

        img1 = Image.open(img_path1).convert('RGB')
        img2 = Image.open(img_path2).convert('RGB')

        zbox1 = get_zbox(gt1, 0.25)
        zbox2 = get_zbox(gt2, 0.25)

    else:
        cats = coco.loadCats(coco.getCatIds())
        nms = [cat['name'] for cat in cats]
        # catIds = coco.getCatIds(nms[0])
        # catIds = coco.getCatIds('bicycle')
        catIds = coco.getCatIds(nms[np.random.randint(0, len(nms))])
        imgIds = coco.getImgIds(catIds=catIds)
        img = coco.loadImgs(imgIds[np.random.randint(0, len(imgIds))])[0]
        annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
        anns = coco.loadAnns(annIds)
        for ann in anns:
            if i['category_id'] == catIds:
                break
        gt_org = convert_array_to_rec(ann['bbox'])
        gt = convert_bbox_format(gt_org, to='center-based')
        zbox = get_zbox(gt, 0.25)
        xbox = get_xbox(zbox)
        img1 = Image.open(dataDir + dataType + '/%012d.jpg' % img['id'])

        imgIds = coco.getImgIds(catIds=catIds)
        img = coco.loadImgs(imgIds[np.random.randint(0, len(imgIds))])[0]
        annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
        anns = coco.loadAnns(annIds)
        for ann in anns:
            if i['category_id'] == catIds:
                break
        gt_org = convert_array_to_rec(ann['bbox'])
        gt = convert_bbox_format(gt_org, to='center-based')
        zbox = get_zbox(gt, 0.25)
        xbox = get_xbox(zbox)
        img2 = Image.open(dataDir + dataType + '/%012d.jpg' % img['id'])
        gt[:, :, :] = -1

    scales_w = 1.04**(random.random() * 6 - 3)
    scales_h = 1.04**(random.random() * 6 - 3)

    #scales_w = 2
    #scales_h = 2

    zbox2_scaled = Rectangle(zbox2.x, zbox2.y, zbox2.width * scales_w,
                             zbox2.height * scales_h)

    #dx = 1-2*random.random()

    #dy = 1-2*random.random()

    dx = 0

    dy = 0
    #dx = dx/2
    #dy = dy/2

    # dx = 0
    # dy = 0

    xbox2 = get_xbox(zbox2_scaled, dx,
                     dy)  # we assume second is the search region
    #print zbox2,zbox2_scaled

    z = gen_xz(img1, zbox1, to='z')
    x = gen_xz(img2, xbox2, to='x')

    # dw = 256*(random.random())*0.5
    # dh = 256*(random.random())*0.5
    # x = x.crop((0+dw,0+dh,128+dw,128+dh))

    #segz = gen_xz( img2, zbox2,to = 'z')

    #gt = np.ones((1,1,1))
    #gt = gt*-1

    #if dw + dh < 50:

    #    gt[0,0,0] = 1

    info = [
        dx, dy, gt2.width / scales_w / zbox2.width,
        gt2.height / scales_h / zbox2.height
    ]

    gt_box = np.array(
        [-info[0] * 64, -info[1] * 64, info[2] * 128, info[3] * 128])

    anchor_xctr = anchors[:, :1]
    anchor_yctr = anchors[:, 1:2]
    anchor_w = anchors[:, 2:3]
    anchor_h = anchors[:, 3:]
    gt_cx, gt_cy, gt_w, gt_h = gt_box

    target_x = (gt_cx - anchor_xctr) / anchor_w
    target_y = (gt_cy - anchor_yctr) / anchor_h
    target_w = np.log(gt_w / anchor_w)
    target_h = np.log(gt_h / anchor_h)
    regression_target = np.hstack((target_x, target_y, target_w, target_h))

    iou = compute_iou(anchors, gt_box).flatten()
    # print(np.max(iou))

    pos_index = np.where(iou > 0.4)[0]
    neg_index = np.where(iou < 0.3)[0]

    label = np.ones_like(iou) * -1
    label[pos_index] = 1
    label[neg_index] = 0

    #mask = np.zeros((6,17,17))

    #mask[:,:,:] = 0

    #mask[:,p[0],p[1]] = 1

    #gt = gt*mask

    return z, x, gt_box, regression_target, label
Пример #3
0
def load_data(pair_infos, discrim, train=True):

    if random.random > 0.0:

        img_path1 = pair_infos[0][0]
        img_path2 = pair_infos[1][0]

        bs1 = pair_infos[0][1]  #xmin xmax ymin ymax
        bs2 = pair_infos[1][1]

        gt1 = Rectangle(bs1[0], bs1[2], bs1[1] - bs1[0], bs1[3] - bs1[2])
        gt2 = Rectangle(bs2[0], bs2[2], bs2[1] - bs2[0], bs2[3] - bs2[2])

        gt1 = convert_bbox_format(gt1, to='center-based')
        gt2 = convert_bbox_format(gt2, to='center-based')

        img1 = Image.open(img_path1).convert('RGB')
        img2 = Image.open(img_path2).convert('RGB')

        zbox1 = get_zbox(gt1, 0.25)
        zbox2 = get_zbox(gt2, 0.25)

    else:
        cats = coco.loadCats(coco.getCatIds())
        nms = [cat['name'] for cat in cats]
        # catIds = coco.getCatIds(nms[0])
        # catIds = coco.getCatIds('bicycle')
        catIds = coco.getCatIds(nms[np.random.randint(0, len(nms))])
        imgIds = coco.getImgIds(catIds=catIds)
        img = coco.loadImgs(imgIds[np.random.randint(0, len(imgIds))])[0]
        annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
        anns = coco.loadAnns(annIds)
        for ann in anns:
            if i['category_id'] == catIds:
                break
        gt_org = convert_array_to_rec(ann['bbox'])
        gt = convert_bbox_format(gt_org, to='center-based')
        zbox = get_zbox(gt, 0.25)
        xbox = get_xbox(zbox)
        img1 = Image.open(dataDir + dataType + '/%012d.jpg' % img['id'])

        imgIds = coco.getImgIds(catIds=catIds)
        img = coco.loadImgs(imgIds[np.random.randint(0, len(imgIds))])[0]
        annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
        anns = coco.loadAnns(annIds)
        for ann in anns:
            if i['category_id'] == catIds:
                break
        gt_org = convert_array_to_rec(ann['bbox'])
        gt = convert_bbox_format(gt_org, to='center-based')
        zbox = get_zbox(gt, 0.25)
        xbox = get_xbox(zbox)
        img2 = Image.open(dataDir + dataType + '/%012d.jpg' % img['id'])
        gt[:, :, :] = -1

    scales_w = 1.04**(random.random() * 6 - 3)
    scales_h = 1.04**(random.random() * 6 - 3)

    #scales_w = 2
    #scales_h = 2

    zbox2_scaled = Rectangle(zbox2.x, zbox2.y, zbox2.width * scales_w,
                             zbox2.height * scales_h)

    #dx = 1-2*random.random()

    #dy = 1-2*random.random()

    #dx = 1

    #dy = 1

    #dx = dx/2
    #dy = dy/2

    dx = 0
    dy = 0

    xbox2 = get_xbox(zbox2_scaled, dx,
                     dy)  # we assume second is the search region
    #print zbox2,zbox2_scaled

    z = gen_xz(img1, zbox1, to='z')
    x = gen_xz(img2, xbox2, to='x')

    info = [
        dx, dy, gt2.width / scales_w / zbox2.width,
        gt2.height / scales_h / zbox2.height
    ]

    #gt_box = np.array([-info[0]*64,-info[1]*64,info[2]*128,info[3]*128])
    gt_box = np.array([np.log(info[2] * 2), np.log(info[3] * 2)])

    gt = np.zeros((1, 17, 17))
    gt[:, :, :] = -1
    gt[0, 8, 8] = 1.

    gt[0, 7:10, 7:10] = 1.
    gt[0, 8:9, 6:11] = 1.
    gt[0, 6:11, 8:9] = 1.

    return z, x, gt, gt_box
Пример #4
0
def load_data_rpn(pair_infos, discrim, train=True):
    anchors = generate_anchor(8, [
        8,
    ], [0.33, 0.5, 1, 2, 3], 17)

    gt = np.zeros((1, 17, 17))
    gt[:, :, :] = -1
    gt[0, 8, 8] = 1.

    img_path1 = pair_infos[0][0]
    img_path2 = pair_infos[1][0]

    bs1 = pair_infos[0][1]  # xmin xmax ymin ymax
    bs2 = pair_infos[1][1]

    gt1 = Rectangle(bs1[0], bs1[2], bs1[1] - bs1[0], bs1[3] - bs1[2])
    gt2 = Rectangle(bs2[0], bs2[2], bs2[1] - bs2[0], bs2[3] - bs2[2])

    gt1 = convert_bbox_format(gt1, to='center-based')
    gt2 = convert_bbox_format(gt2, to='center-based')

    img1 = Image.open(img_path1).convert('RGB')
    img2 = Image.open(img_path2).convert('RGB')

    zbox1 = get_zbox(gt1, 0.25)
    zbox2 = get_zbox(gt2, 0.25)

    scales_w = 1.04**(random.random() * 6 - 3)
    scales_h = 1.04**(random.random() * 6 - 3)

    zbox2_scaled = Rectangle(zbox2.x, zbox2.y, zbox2.width * scales_w,
                             zbox2.height * scales_h)

    dx = 0

    dy = 0

    xbox2 = get_xbox(zbox2_scaled, dx,
                     dy)  # we assume second is the search region

    z = gen_xz(img1, zbox1, to='z')
    x = gen_xz(img2, xbox2, to='x')

    info = [
        dx, dy, gt2.width / scales_w / zbox2.width,
        gt2.height / scales_h / zbox2.height
    ]

    gt_box = np.array(
        [-info[0] * 64, -info[1] * 64, info[2] * 128, info[3] * 128])

    anchor_xctr = anchors[:, :1]
    anchor_yctr = anchors[:, 1:2]
    anchor_w = anchors[:, 2:3]
    anchor_h = anchors[:, 3:]
    gt_cx, gt_cy, gt_w, gt_h = gt_box

    target_x = (gt_cx - anchor_xctr) / anchor_w
    target_y = (gt_cy - anchor_yctr) / anchor_h
    target_w = np.log(gt_w / anchor_w)
    target_h = np.log(gt_h / anchor_h)
    regression_target = np.hstack((target_x, target_y, target_w, target_h))

    iou = compute_iou(anchors, gt_box).flatten()
    # print(np.max(iou))

    pos_index = np.where(iou > 0.4)[0]
    neg_index = np.where(iou < 0.3)[0]

    label = np.ones_like(iou) * -1
    label[pos_index] = 1
    label[neg_index] = 0

    return z, x, gt_box, regression_target, label