예제 #1
0
    def scale_process(self, scale_im):
        h, w = scale_im.shape[:2]
        im_size_min = min(h, w)
        im_size_max = max(h, w)

        if im_size_max <= self.crop_size:
            input_im = T.padding_im(scale_im,
                                    target_size=(self.crop_size,
                                                 self.crop_size),
                                    borderType=cv2.BORDER_CONSTANT)
            output = self.inference(np.asarray([input_im], dtype=np.float32))
            score = output[0].transpose(1, 2, 0)[:h, :w, :]
        else:
            stride = np.ceil(self.crop_size * self.stride_rate)
            pad_im = scale_im
            if im_size_min < self.crop_size:
                pad_im = T.padding_im(scale_im,
                                      target_size=(self.crop_size,
                                                   self.crop_size),
                                      borderType=cv2.BORDER_CONSTANT)

            ph, pw = pad_im.shape[:2]
            h_grid = int(np.ceil(float(ph - self.crop_size) / stride)) + 1
            w_grid = int(np.ceil(float(pw - self.crop_size) / stride)) + 1
            data_scale = np.zeros((ph, pw, self.class_num), dtype=np.float32)
            count_scale = np.zeros((ph, pw, self.class_num), dtype=np.float32)
            for grid_yidx in xrange(0, h_grid):
                for grid_xidx in xrange(0, w_grid):
                    s_x = int(grid_xidx * stride)
                    s_y = int(grid_yidx * stride)
                    e_x = min(s_x + self.crop_size, pw)
                    e_y = min(s_y + self.crop_size, ph)
                    s_x = int(e_x - self.crop_size)
                    s_y = int(e_y - self.crop_size)
                    sub_im = pad_im[s_y:e_y, s_x:e_x, :]
                    count_scale[s_y:e_y, s_x:e_x, :] += 1.0
                    input_im = T.padding_im(sub_im,
                                            target_size=(self.crop_size,
                                                         self.crop_size),
                                            borderType=cv2.BORDER_CONSTANT)
                    output = self.inference(
                        np.asarray([input_im], dtype=np.float32))
                    data_scale[s_y:e_y,
                               s_x:e_x, :] += output[0].transpose(1, 2, 0)
            score = data_scale / count_scale
            score = score[:h, :w, :]

        return score
예제 #2
0
    def seg_im(self, im):
        """ Ignore self.scales; """
        im = im.astype(np.float32, copy=True)
        h, w = im.shape[:2]
        normalized_im = T.normalize(im, mean=self.mean, std=self.std)
        scale_im, scale_ratio = T.scale_by_max(normalized_im, long_size=self.crop_size)
        input_im = T.padding_im(scale_im, target_size=(self.crop_size, self.crop_size),
                                borderType=cv2.BORDER_CONSTANT)
        output = self.inference(np.asarray([input_im], dtype=np.float32))
        score = output[0].transpose(1, 2, 0)
        score_map = cv2.resize(score, None, None, fx=1. / scale_ratio, fy=1. / scale_ratio)[:h, :w, :]

        return score_map.argmax(2)
예제 #3
0
    def seg_im(self, im):
        """ Ignore self.scales; """
        im = im.astype(np.float32, copy=True)
        h, w = im.shape[:2]
        normalized_im = T.normalize(im, mean=self.mean, std=self.std)
        scale_im, scale_ratio = T.scale_by_max(normalized_im,
                                               long_size=self.crop_size)
        input_im = T.padding_im(scale_im,
                                target_size=(self.crop_size, self.crop_size),
                                borderType=cv2.BORDER_CONSTANT,
                                borderValue=(0.0, 0.0, 0.0))
        score = self.caffe_process(input_im)
        score_map = cv2.resize(score,
                               None,
                               None,
                               fx=1. / scale_ratio,
                               fy=1. / scale_ratio)[:h, :w, :]

        return score_map.argmax(2)