def _get_vectorised_sift_samples(archetype_config, dataloader):
    (num_batches, batch_sz) = (len(dataloader), archetype_config.dataloader_batch_sz)
    num_imgs_max = (num_batches * batch_sz)
    img_sz = archetype_config.input_sz
    desc_side = int((img_sz / SIFT_STEP))
    print(('img sz %d, desc_side %d' % (img_sz, desc_side)))
    sys.stdout.flush()
    descs_all = np.zeros((num_imgs_max, (desc_side * desc_side), SIFT_DLEN), dtype=np.uint8)
    masks_all = np.zeros((num_imgs_max, (desc_side * desc_side)), dtype=np.bool)
    labels_all = None
    actual_num_imgs = 0
    central_inds_h = ((np.arange(desc_side) * SIFT_STEP) + (SIFT_STEP / 2)).reshape((desc_side, 1)).repeat(desc_side, axis=1)
    central_inds_w = ((np.arange(desc_side) * SIFT_STEP) + (SIFT_STEP / 2)).reshape((1, desc_side)).repeat(desc_side, axis=0)
    (central_inds_h, central_inds_w) = (central_inds_h.reshape((- 1)), central_inds_w.reshape((- 1)))
    for (b_i, batch) in enumerate(dataloader):
        if (len(batch) == 3):
            store_labels = True
            if (labels_all is None):
                labels_all = np.zeros((num_imgs_max, (desc_side * desc_side)), dtype=np.int32)
            (imgs, labels, masks) = batch
            labels = labels.cpu().numpy().astype(np.int32)
        else:
            store_labels = False
            (imgs, _, _, masks) = batch
        imgs = (imgs * 255.0).permute(0, 2, 3, 1).cpu().numpy().astype(np.uint8)
        masks = masks.cpu().numpy().astype(np.bool)
        (curr_batch_sz, h, w, c) = imgs.shape
        assert ((h == archetype_config.input_sz) and (w == archetype_config.input_sz) and (c == archetype_config.in_channels))
        if (b_i < (num_batches - 1)):
            assert (batch_sz == curr_batch_sz)
        start = (b_i * batch_sz)
        for i in range(curr_batch_sz):
            grey_img = cv2.cvtColor(imgs[i, :, :, :], cv2.COLOR_RGB2GRAY)
            (locs, descs) = vlfeat.vl_dsift(grey_img, step=SIFT_STEP)
            descs = descs.transpose((1, 0))
            descs = descs.reshape((- 1), SIFT_DLEN)
            mask = masks[i][(central_inds_h, central_inds_w)]
            offset = (start + i)
            descs_all[offset, :, :] = descs
            masks_all[offset, :] = mask
            if store_labels:
                label = labels[i][(central_inds_h, central_inds_w)]
                labels_all[offset, :] = label
        actual_num_imgs += curr_batch_sz
    descs_all = descs_all[:actual_num_imgs, :, :]
    masks_all = masks_all[:actual_num_imgs, :]
    num_unmasked = masks_all.sum()
    if store_labels:
        labels_all = labels_all[:actual_num_imgs, :]
        samples_labels = labels_all[masks_all].reshape((- 1))
        assert (samples_labels.shape[0] == num_unmasked)
    samples = descs_all[masks_all, :].reshape((- 1), SIFT_DLEN)
    assert (samples.shape[0] == num_unmasked)
    if (not store_labels):
        return samples
    else:
        return (samples, samples_labels)
def _get_vectorised_sift_samples(archetype_config, dataloader):
    (num_batches, batch_sz) = (len(dataloader), archetype_config.dataloader_batch_sz)
    num_imgs_max = (num_batches * batch_sz)
    img_sz = archetype_config.input_sz
    desc_side = int((img_sz / SIFT_STEP))
    print(('img sz %d, desc_side %d' % (img_sz, desc_side)))
    sys.stdout.flush()
    descs_all = np.zeros((num_imgs_max, (desc_side * desc_side), SIFT_DLEN), dtype=np.uint8)
    masks_all = np.zeros((num_imgs_max, (desc_side * desc_side)), dtype=np.bool)
    labels_all = None
    actual_num_imgs = 0
    central_inds_h = ((np.arange(desc_side) * SIFT_STEP) + (SIFT_STEP / 2)).reshape((desc_side, 1)).repeat(desc_side, axis=1)
    central_inds_w = ((np.arange(desc_side) * SIFT_STEP) + (SIFT_STEP / 2)).reshape((1, desc_side)).repeat(desc_side, axis=0)
    (central_inds_h, central_inds_w) = (central_inds_h.reshape((- 1)), central_inds_w.reshape((- 1)))
    for (b_i, batch) in enumerate(dataloader):
        if (len(batch) == 3):
            store_labels = True
            if (labels_all is None):
                labels_all = np.zeros((num_imgs_max, (desc_side * desc_side)), dtype=np.int32)
            (imgs, labels, masks) = batch
            labels = labels.cpu().numpy().astype(np.int32)
        else:
            store_labels = False
            (imgs, _, _, masks) = batch
        imgs = (imgs * 255.0).permute(0, 2, 3, 1).cpu().numpy().astype(np.uint8)
        masks = masks.cpu().numpy().astype(np.bool)
        (curr_batch_sz, h, w, c) = imgs.shape
        assert ((h == archetype_config.input_sz) and (w == archetype_config.input_sz) and (c == archetype_config.in_channels))
        if (b_i < (num_batches - 1)):
            assert (batch_sz == curr_batch_sz)
        start = (b_i * batch_sz)
        for i in range(curr_batch_sz):
            grey_img = cv2.cvtColor(imgs[i, :, :, :], cv2.COLOR_RGB2GRAY)
            (locs, descs) = vlfeat.vl_dsift(grey_img, step=SIFT_STEP)
            descs = descs.transpose((1, 0))
            descs = descs.reshape((- 1), SIFT_DLEN)
            mask = masks[i][(central_inds_h, central_inds_w)]
            offset = (start + i)
            descs_all[offset, :, :] = descs
            masks_all[offset, :] = mask
            if store_labels:
                label = labels[i][(central_inds_h, central_inds_w)]
                labels_all[offset, :] = label
        actual_num_imgs += curr_batch_sz
    descs_all = descs_all[:actual_num_imgs, :, :]
    masks_all = masks_all[:actual_num_imgs, :]
    num_unmasked = masks_all.sum()
    if store_labels:
        labels_all = labels_all[:actual_num_imgs, :]
        samples_labels = labels_all[masks_all].reshape((- 1))
        assert (samples_labels.shape[0] == num_unmasked)
    samples = descs_all[masks_all, :].reshape((- 1), SIFT_DLEN)
    assert (samples.shape[0] == num_unmasked)
    if (not store_labels):
        return samples
    else:
        return (samples, samples_labels)