def get_anchor_patches(self):
     # set seed here if want same patches
     # Regardless of whether use or not, should
     # be 0 if not dense compute
     '''Sample n_anchor patches from original image'''
     self.anchor_count = 0
     if self.use_patches:
         # pass existing patches
         return util.process_im(self.patches)
     # Sample n_anchor patches randomly and normalize them to [-1.0,1.0]
     return util.process_im(
         np.array([self.rand_patch() for i in range(self.n_anchors)],
                  dtype=np.float32))
예제 #2
0
    def get_anchor_patches(self):
        # set seed here if want same patches
        # Regardless of whether use or not, should
        # be 0 if not dense compute
        self.anchor_count = 0
        if self.dense_compute:
            self.anchor_inds = self.indices.copy()
            return util.process_im(
                np.array([
                    self.get_patch(self.anchor_inds[i][0],
                                   self.anchor_inds[i][1])
                    for i in range(
                        self.anchor_count,
                        min(self.anchor_count +
                            self.n_anchors, self.anchor_inds.shape[0]))
                ]))

        if self.use_patches:
            # pass existing patches
            return util.process_im(self.patches)
        return util.process_im(
            np.array([self.rand_patch() for i in range(self.n_anchors)],
                     dtype=np.float32))
    def data_fn(self, hind, wind):
        n_anchors = self.n_anchors
        y_ind, x_ind = hind * self.stride, wind * self.stride
        _, batch_indexes = np.mgrid[0:hind.shape[0], 0:self.patch_size]
        patches = self.image[y_ind + batch_indexes, x_ind + batch_indexes, :]

        # anchor_inds = np.arange(self.anchor_count, self.anchor_count + n_anchors)
        h_inds = hind
        w_inds = wind
        # batch_a = self.anchor_patches
        # batch_b is the selected patch copied n_anchor times
        batch_b = util.process_im(patches)

        return h_inds, w_inds, batch_b
예제 #4
0
 def dense_argless(self):
     assert False, "Deprecated"
     if self.count >= self.indices.shape[0]:
         self.count = 0
         self.anchor_count += self.n_anchors
         if self.anchor_count >= self.anchor_inds.shape[0]:
             raise StopIteration()
         inds2 = self.anchor_inds[self.anchor_count]
         self.anchor_patches = util.process_im(
             np.array([
                 self.get_patch(self.anchor_inds[i][0],
                                self.anchor_inds[i][1])
                 for i in range(
                     self.anchor_count,
                     min(self.anchor_count +
                         self.n_anchors, self.anchor_inds.shape[0]))
             ]))
         self.n_anchors = self.anchor_patches.shape[0]
     inds = self.indices[self.count]
     self.count += 1
     d = self.data_fn(inds[0], inds[1])
     return d
예제 #5
0
    def data_fn(self, hind, wind):
        n_anchors = self.anchor_patches.shape[0]
        y_ind, x_ind = hind * self.stride, wind * self.stride

        patch = self.image[y_ind:y_ind + self.patch_size,
                           x_ind:x_ind + self.patch_size, :]

        anchor_inds = np.arange(self.anchor_count,
                                self.anchor_count + n_anchors)
        h_inds = np.array([hind] * n_anchors, dtype=np.int64)
        w_inds = np.array([wind] * n_anchors, dtype=np.int64)
        batch_a = self.anchor_patches
        batch_b = util.process_im(
            np.array([patch] * n_anchors, dtype=np.float32))
        # anc, y, x, bat_a, bat_b
        if self.mirror_pred:
            anchor_inds = np.vstack([anchor_inds] * 2)
            h_inds = np.vstack([h_inds] * 2)
            w_inds = np.vstack([w_inds] * 2)
            batch_a, batch_b = np.vstack([batch_a, batch_b
                                          ]), np.vstack([batch_b, batch_a])

        return anchor_inds, h_inds, w_inds, batch_a, batch_b
예제 #6
0
def run_vote_no_threads(image, solver, exif_to_use, n_anchors=1, num_per_dim=None,
             patch_size=None, batch_size=None, sample_ratio=3.0, override_anchor=False):
    """
    solver: exif_solver module. Must be initialized and have a network connected.
    exif_to_use: exif to extract responses from. A list. If exif_to_use is None
                 extract result from classification output cls_pred
    n_anchors: number of anchors to use.
    num_per_dim: number of patches to use along the largest dimension.
    patch_size: size of the patch. If None, uses the one specified in solver.net
    batch_size: size of the batch. If None, uses the one specified in solver.net
    sample_ratio: The ratio of overlap between patches. num_per_dim must be None
                  to be useful.
    """

    h, w = np.shape(image)[:2]

    if patch_size is None:
        patch_size = solver.net.im_size

    if batch_size is None:
        batch_size = solver.net.batch_size

    if num_per_dim is None:
        num_per_dim = int(np.ceil(sample_ratio * (max(h,w)/float(patch_size))))

    if exif_to_use is None:
        not_exif = True
        exif_to_use = ['out']
    else:
        not_exif = False
        exif_map = {e: np.squeeze(np.argwhere(np.array(solver.net.train_runner.tags) == e)) for e in exif_to_use}

    responses   = {e:np.zeros((n_anchors, h, w)) for e in exif_to_use}
    vote_counts = {e:1e-6 * np.ones((n_anchors, h, w)) for e in exif_to_use}

    if np.min(image) < 0.0:
        # already preprocessed
        processed_image = image
    else:
        processed_image = util.process_im(image)
    ones = np.ones((patch_size, patch_size))

    anchor_indices = []
    # select n anchors
    for anchor_idx in range(n_anchors):
        if override_anchor is False:
            _h, _w  = np.random.randint(0, h - patch_size), np.random.randint(0, w - patch_size)
        else:
            assert len(override_anchor) == 2, override_anchor
            _h, _w = override_anchor

        anchor_indices.append((_h, _w))
        anchor_patch = processed_image[_h:_h+patch_size, _w:_w+patch_size, :]

        batch_a = np.tile([anchor_patch], [batch_size, 1, 1, 1])
        batch_b, batch_b_coord = [], []

        prev_batch = None
        for i in np.linspace(0, h - patch_size, num_per_dim).astype(int):
            for j in np.linspace(0, w - patch_size, num_per_dim).astype(int):
                compare_patch = processed_image[i:i+patch_size, j:j+patch_size]
                batch_b.append(compare_patch)
                batch_b_coord.append((i,j))

                if len(batch_b) == batch_size:
                    if not_exif:
                        pred = solver.sess.run(solver.net.cls_pred,
                                 feed_dict={solver.net.im_a:batch_a,
                                            solver.net.im_b:batch_b,
                                            solver.net.is_training:False})
                    else:
                        pred = solver.sess.run(solver.net.pred,
                                 feed_dict={solver.net.im_a:batch_a,
                                            solver.net.im_b:batch_b,
                                            solver.net.is_training:False})

                    for p_vec, (_i, _j) in zip(pred, batch_b_coord):
                        for e in exif_to_use:
                            if not_exif:
                                p = p_vec[0]
                            else:
                                p = p_vec[int(exif_map[e])]
                            responses[e][anchor_idx, _i:_i+patch_size, _j:_j+patch_size] += (p * ones)
                            vote_counts[e][anchor_idx, _i:_i+patch_size, _j:_j+patch_size] += ones
                    prev_batch = batch_b
                    batch_b, batch_b_coord = [], []

        if len(batch_b) > 0:
            batch_b_len = len(batch_b)
            to_pad = np.array(prev_batch)[:batch_size - batch_b_len]
            batch_b = np.concatenate([batch_b, to_pad], axis=0)

            if not_exif:
                pred = solver.sess.run(solver.net.cls_pred,
                                     feed_dict={solver.net.im_a:batch_a,
                                                solver.net.im_b:batch_b,
                                                solver.net.is_training:False})
            else:
                pred = solver.sess.run(solver.net.pred,
                                     feed_dict={solver.net.im_a:batch_a,
                                                solver.net.im_b:batch_b,
                                                solver.net.is_training:False})

            for p_vec, (_i, _j) in zip(pred, batch_b_coord):
                for e in exif_to_use:
                    if not_exif:
                        p = p_vec[0]
                    else:
                        p = p_vec[int(exif_map[e])]
                    responses[e][anchor_idx, _i:_i+patch_size, _j:_j+patch_size] += (p * ones)
                    vote_counts[e][anchor_idx, _i:_i+patch_size, _j:_j+patch_size] += ones

    return {e: {'responses':(responses[e] / vote_counts[e]), 'anchors':anchor_indices} for e in exif_to_use}