Exemplo n.º 1
0
    def visu_network_input_pred(self,
                                tag,
                                epoch,
                                data,
                                images,
                                target,
                                cam,
                                max_images=10,
                                store=False,
                                jupyter=False,
                                method='def'):
        num = min(max_images, data.shape[0])
        fig = plt.figure(figsize=(10.5, num * 3.5))

        for i in range(num):
            # real render input
            n_render = f'batch{i}_render.png'
            n_real = f'batch{i}_real.png'
            real = np.transpose(
                data[i, :3, :, :].cpu().numpy().astype(np.uint8), (1, 2, 0))
            render = np.transpose(
                data[i, 3:, :, :].cpu().numpy().astype(np.uint8), (1, 2, 0))
            fig.add_subplot(num, 3, i * 3 + 1)
            plt.imshow(real)
            plt.tight_layout()
            fig.add_subplot(num, 3, i * 3 + 2)
            plt.imshow(render)
            plt.tight_layout()

            # prediction
            masked_idx = backproject_points(target[i],
                                            fx=cam[i, 2],
                                            fy=cam[i, 3],
                                            cx=cam[i, 0],
                                            cy=cam[i, 1])
            for j in range(masked_idx.shape[0]):
                try:
                    images[i,
                           int(masked_idx[j, 0]),
                           int(masked_idx[j, 1]), 0] = 0
                    images[i,
                           int(masked_idx[j, 0]),
                           int(masked_idx[j, 1]), 1] = 255
                    images[i,
                           int(masked_idx[j, 0]),
                           int(masked_idx[j, 1]), 2] = 0
                except:
                    pass
            min1 = torch.min(masked_idx[:, 0])
            max1 = torch.max(masked_idx[:, 0])
            max2 = torch.max(masked_idx[:, 1])
            min2 = torch.min(masked_idx[:, 1])
            bb = BoundingBox(p1=torch.stack([min1, min2]),
                             p2=torch.stack([max1, max2]))
            bb_img = bb.plot(images[i, :, :, :3].cpu().numpy().astype(
                np.uint8))
            fig.add_subplot(num, 3, i * 3 + 3)
            plt.imshow(bb_img)
            # fig.add_subplot(num, 2, i * 2 + 4)
            # real = images[i, :, :, :3].cpu().numpy().astype(np.uint8)
            # plt.imshow(real)
        if method != 'def':
            a = get_img_from_fig(fig).astype(np.uint8)
            plt.close()
            return a

        if store:
            #store_ar = (img_d* 255).round().astype(np.uint8)
            plt.savefig(
                f'{self.p_visu}/{str(epoch)}_{tag}_network_input_and_prediction.png',
                dpi=300)
            #save_image(img_d, tag=str(epoch) + tag, p_store=self.p_visu)
        if jupyter:
            plt.show()
        if self.writer is not None:
            # you can get a high-resolution image as numpy array!!
            plot_img_np = get_img_from_fig(fig)
            self.writer.add_image(tag,
                                  plot_img_np,
                                  global_step=epoch,
                                  dataformats='HWC')
        plt.close()
Exemplo n.º 2
0
    def plot_batch_projection(self,
                              tag,
                              epoch,
                              images,
                              target,
                              cam,
                              max_images=10,
                              store=False,
                              jupyter=False,
                              method='def'):

        num = min(max_images, target.shape[0])
        fig = plt.figure(figsize=(7, num * 3.5))
        for i in range(num):
            masked_idx = backproject_points(target[i],
                                            fx=cam[i, 2],
                                            fy=cam[i, 3],
                                            cx=cam[i, 0],
                                            cy=cam[i, 1])

            for j in range(masked_idx.shape[0]):
                try:
                    images[i,
                           int(masked_idx[j, 0]),
                           int(masked_idx[j, 1]), 0] = 0
                    images[i,
                           int(masked_idx[j, 0]),
                           int(masked_idx[j, 1]), 1] = 255
                    images[i,
                           int(masked_idx[j, 0]),
                           int(masked_idx[j, 1]), 2] = 0
                except:
                    pass

            min1 = torch.min(masked_idx[:, 0])
            max1 = torch.max(masked_idx[:, 0])
            max2 = torch.max(masked_idx[:, 1])
            min2 = torch.min(masked_idx[:, 1])

            bb = BoundingBox(p1=torch.stack([min1, min2]),
                             p2=torch.stack([max1, max2]))

            bb_img = bb.plot(images[i, :, :, :3].cpu().numpy().astype(
                np.uint8))
            fig.add_subplot(num, 2, i * 2 + 1)
            plt.imshow(bb_img)

            fig.add_subplot(num, 2, i * 2 + 2)
            real = images[i, :, :, :3].cpu().numpy().astype(np.uint8)
            plt.imshow(real)

        if method != 'def':
            a = get_img_from_fig(fig).astype(np.uint8)
            plt.close()
            return a

        if store:
            #store_ar = (img_d* 255).round().astype(np.uint8)
            plt.savefig(f'{self.p_visu}/{str(epoch)}_{tag}_project_batch.png',
                        dpi=300)
            #save_image(img_d, tag=str(epoch) + tag, p_store=self.p_visu)
        if jupyter:
            plt.show()
        if self.writer is not None:
            # you can get a high-resolution image as numpy array!!
            plot_img_np = get_img_from_fig(fig)
            self.writer.add_image(tag,
                                  plot_img_np,
                                  global_step=epoch,
                                  dataformats='HWC')