Example #1
0
def extract_detector_negatives(hs, output_dir, batch_extract_kwargs):
    from itertools import product as iprod
    negreg_dir = join(output_dir, 'negatives', 'regions')
    negall_dir = join(output_dir, 'negatives', 'whole')
    negreg_fmt = join(negreg_dir, 'gx%d_wix%d_hix%d_neg.png')
    negall_fmt = join(negall_dir, 'gx%d_all_neg.png')
    helpers.ensuredir(negall_dir)
    helpers.ensuredir(negreg_dir)

    print('[train] extract_negatives')
    gx_list = hs.get_valid_gxs()
    nChips_list = np.array(hs.gx2_nChips(gx_list))
    aif_list = np.array(hs.gx2_aif(gx_list))

    # Find images where there are completely negative. They have no animals.
    #is_negative = np.logical_and(aif_list, nChips_list)
    is_completely_negative = np.logical_and(aif_list, nChips_list == 0)
    negall_gxs = gx_list[np.where(is_completely_negative)[0]]

    gfpath_list = []
    cfpath_list = []
    roi_list = []

    def add_neg_eg(roi, gfpath, cfpath):
        roi_list.append(roi)
        gfpath_list.append(gfpath)
        cfpath_list.append(cfpath)

    width_split = 2
    (uw, uh) = batch_extract_kwargs['uniform_size']

    for gx in negall_gxs:
        gfpath = hs.gx2_gname(gx, full=True)
        # Add whole negative image
        (gw, gh) = hs.gx2_image_size(gx)
        roi = (0, 0, gw, gh)
        add_neg_eg(roi, gfpath, negall_fmt % (gx))
        # Add negative regions
        w_step = gw // width_split
        h_step = int(round(gh * (w_step / gw)))
        nHeights, nWidths  = gh // h_step, gw // w_step
        if nWidths < 2 or nHeights < 1:
            continue
        for wix, hix in iprod(xrange(nWidths), xrange(nHeights)):
            x, y = wix * w_step, hix * h_step
            w, h = w_step, h_step
            roi = (x, y, w, h)
            add_neg_eg(roi, gfpath, negreg_fmt % (gx, wix, hix))

    theta_list = [0] * len(roi_list)

    cc2.batch_extract_chips(gfpath_list, cfpath_list, roi_list, theta_list,
                            **batch_extract_kwargs)
Example #2
0
def extract_detector_positives(hs, output_dir, batch_extract_kwargs):
    print('[train] extract_positives')
    cx_list    = hs.get_valid_cxs()
    gx_list    = hs.tables.cx2_gx[cx_list]
    cid_list   = hs.tables.cx2_cid[cx_list]
    theta_list = hs.tables.cx2_theta[cx_list]
    roi_list   = hs.tables.cx2_roi[cx_list]
    gfpath_list = hs.gx2_gname(gx_list, full=True)

    posoutput_dir = join(output_dir, 'positives')
    helpers.ensuredir(posoutput_dir)
    pos_fmt = join(posoutput_dir, 'cid%d_gx%d_pos.png')
    cfpath_list = [pos_fmt  % (cid, gx) for (cid, gx) in zip(cid_list, gx_list)]

    cc2.batch_extract_chips(gfpath_list, cfpath_list, roi_list, theta_list,
                            **batch_extract_kwargs)