示例#1
0
    def __get_paf_and_heatmap(self, img_raw):
        multiplier = [scale * self.configer.get('data', 'input_size')[0] / img_raw.shape[1]
                      for scale in self.configer.get('data', 'scale_search')]

        heatmap_avg = np.zeros((img_raw.shape[0], img_raw.shape[1], self.configer.get('network', 'heatmap_out')))
        paf_avg = np.zeros((img_raw.shape[0], img_raw.shape[1], self.configer.get('network', 'paf_out')))

        for i, scale in enumerate(multiplier):
            img_test = cv2.resize(img_raw, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
            img_test_pad, pad = PadImage(self.configer.get('network', 'stride'))(img_test)
            img_test_pad = ToTensor()(img_test_pad)
            img_test_pad = Normalize(mean=self.configer.get('trans_params', 'mean'),
                                     std=self.configer.get('trans_params', 'std'))(img_test_pad)
            with torch.no_grad():
                img_test_pad = img_test_pad.unsqueeze(0).to(self.device)
                paf_out, heatmap_out = self.pose_net(img_test_pad)

            # extract outputs, resize, and remove padding
            heatmap = heatmap_out.data.squeeze().cpu().numpy().transpose(1, 2, 0)
            heatmap = cv2.resize(heatmap,  (0, 0), fx=self.configer.get('network', 'stride'),
                                 fy=self.configer.get('network', 'stride'), interpolation=cv2.INTER_CUBIC)
            heatmap = heatmap[:img_test_pad.size(2) - pad[3], :img_test_pad.size(3) - pad[2], :]
            heatmap = cv2.resize(heatmap, (img_raw.shape[1], img_raw.shape[0]), interpolation=cv2.INTER_CUBIC)

            paf = paf_out.data.squeeze().cpu().numpy().transpose(1, 2, 0)
            paf = cv2.resize(paf, (0, 0), fx=self.configer.get('network', 'stride'),
                                 fy=self.configer.get('network', 'stride'), interpolation=cv2.INTER_CUBIC)
            paf = paf[:img_test_pad.size(2) - pad[3], :img_test_pad.size(3) - pad[2], :]
            paf = cv2.resize(paf, (img_raw.shape[1], img_raw.shape[0]), interpolation=cv2.INTER_CUBIC)

            heatmap_avg = heatmap_avg + heatmap / len(multiplier)
            paf_avg = paf_avg + paf / len(multiplier)

        return paf_avg, heatmap_avg
示例#2
0
    def __get_paf_and_heatmap(self, img_raw):
        multiplier = [
            scale * self.configer.get('data', 'input_size')[0] /
            img_raw.shape[1]
            for scale in self.configer.get('data', 'scale_search')
        ]

        heatmap_avg = np.zeros((img_raw.shape[0], img_raw.shape[1],
                                self.configer.get('data', 'num_keypoints')))
        paf_avg = np.zeros((img_raw.shape[0], img_raw.shape[1],
                            self.configer.get('network', 'paf_out')))
        partmap_avg = np.zeros((img_raw.shape[0], img_raw.shape[1],
                                self.configer.get('network', 'heatmap_out')))

        for i, scale in enumerate(multiplier):
            img_test = cv2.resize(img_raw, (0, 0),
                                  fx=scale,
                                  fy=scale,
                                  interpolation=cv2.INTER_CUBIC)
            img_test_pad, pad = PadImage(self.configer.get(
                'network', 'stride'))(img_test)
            pad_right = pad[2]
            pad_down = pad[3]
            img_test_pad = ToTensor()(img_test_pad)
            img_test_pad = Normalize(
                mean=self.configer.get('trans_params', 'mean'),
                std=self.configer.get('trans_params', 'std'))(img_test_pad)
            with torch.no_grad():
                img_test_pad = img_test_pad.unsqueeze(0).to(self.device)
                paf_out_list, partmap_out_list = self.pose_net(img_test_pad)

            paf_out = paf_out_list[-1]
            partmap_out = partmap_out_list[-1]
            partmap = partmap_out.data.squeeze().cpu().numpy().transpose(
                1, 2, 0)
            paf = paf_out.data.squeeze().cpu().numpy().transpose(1, 2, 0)
            # self.pose_visualizer.vis_tensor(heatmap_out)
            heatmap = np.zeros((partmap.shape[0], partmap.shape[1],
                                self.configer.get('data', 'num_keypoints')))
            part_num = np.zeros((self.configer.get('data', 'num_keypoints'), ))

            for index in range(len(self.configer.get('details', 'limb_seq'))):
                a = self.configer.get('details', 'limb_seq')[index][0] - 1
                b = self.configer.get('details', 'limb_seq')[index][1] - 1
                heatmap_a = partmap[:, :, index * 4:index * 4 + 2]**2
                heatmap_a = np.sqrt(np.sum(heatmap_a, axis=2).squeeze())
                heatmap[:, :, a] = (heatmap[:, :, a] * part_num[a] +
                                    heatmap_a) / (part_num[a] + 1)
                part_num[a] += 1
                heatmap_b = partmap[:, :, index * 4 + 2:index * 4 + 4]**2
                heatmap_b = np.sqrt(np.sum(heatmap_b, axis=2).squeeze())
                heatmap[:, :, b] = (heatmap[:, :, b] * part_num[b] +
                                    heatmap_b) / (part_num[b] + 1)
                part_num[b] += 1

            heatmap = cv2.resize(heatmap, (0, 0),
                                 fx=self.configer.get('network', 'stride'),
                                 fy=self.configer.get('network', 'stride'),
                                 interpolation=cv2.INTER_CUBIC)
            heatmap = heatmap[:img_test_pad.size(2) -
                              pad_down, :img_test_pad.size(3) - pad_right, :]
            heatmap = cv2.resize(heatmap, (img_raw.shape[1], img_raw.shape[0]),
                                 interpolation=cv2.INTER_CUBIC)

            partmap = cv2.resize(partmap, (0, 0),
                                 fx=self.configer.get('network', 'stride'),
                                 fy=self.configer.get('network', 'stride'),
                                 interpolation=cv2.INTER_CUBIC)
            partmap = partmap[:img_test_pad.size(2) -
                              pad_down, :img_test_pad.size(3) - pad_right, :]
            partmap = cv2.resize(partmap, (img_raw.shape[1], img_raw.shape[0]),
                                 interpolation=cv2.INTER_CUBIC)

            paf = cv2.resize(paf, (0, 0),
                             fx=self.configer.get('network', 'stride'),
                             fy=self.configer.get('network', 'stride'),
                             interpolation=cv2.INTER_CUBIC)
            paf = paf[:img_test_pad.size(2) - pad_down, :img_test_pad.size(3) -
                      pad_right, :]
            paf = cv2.resize(paf, (img_raw.shape[1], img_raw.shape[0]),
                             interpolation=cv2.INTER_CUBIC)

            partmap_avg = partmap_avg + partmap / len(multiplier)
            heatmap_avg = heatmap_avg + heatmap / len(multiplier)
            paf_avg = paf_avg + paf / len(multiplier)

        return paf_avg, heatmap_avg, partmap_avg