def test_image_with_3_channel_batched(self): self.assertTrue( compare_proto( summary.image('dummy', np.random.rand(2, 3, 8, 8).astype(np.float32), dataformats='NCHW'), self)) # noqa E127
def save_img_results(imgs_tcpu, fake_imgs, num_imgs, count, image_dir, summary_writer): num = cfg.TRAIN.VIS_COUNT # The range of real_img (i.e., self.imgs_tcpu[i][0:num]) # is changed to [0, 1] by function vutils.save_image real_img = imgs_tcpu[-1][0:num] vutils.save_image(real_img, '%s/real_samples.png' % (image_dir), normalize=True) real_img_set = vutils.make_grid(real_img).numpy() # print("////////before transpose///////////////\n",real_img_set.shape, len(real_img_set.shape)) real_img_set = np.transpose(real_img_set, (1, 2, 0)) # print("//////////after transpose/////////////\n",real_img_set.shape, len(real_img_set.shape),"\n///////////////////////") real_img_set = real_img_set * 255 real_img_set = real_img_set.astype(np.uint8) #doge #sup_real_img = summary.image('real_img', real_img_set) sup_real_img = summary.image('real_img', real_img_set, dataformats='HWC') summary_writer.add_summary(sup_real_img, count) for i in range(num_imgs): fake_img = fake_imgs[i][0:num] # The range of fake_img.data (i.e., self.fake_imgs[i][0:num]) # is still [-1. 1]... vutils.save_image(fake_img.data, '%s/count_%09d_fake_samples%d.png' % (image_dir, count, i), normalize=True) fake_img_set = vutils.make_grid(fake_img.data).cpu().numpy() fake_img_set = np.transpose(fake_img_set, (1, 2, 0)) fake_img_set = (fake_img_set + 1) * 255 / 2 fake_img_set = fake_img_set.astype(np.uint8) #doge #sup_fake_img = summary.image('fake_img%d' % i, fake_img_set) sup_fake_img = summary.image('fake_img%d' % i, fake_img_set, dataformats='HWC') summary_writer.add_summary(sup_fake_img, count) summary_writer.flush()
def add_images(self, tag, img_tensor, global_step=None, walltime=None, dataformats='NCHW'): torch._C._log_api_usage_once("tensorboard.logging.add_images") if self._check_caffe2_blob(img_tensor): img_tensor = workspace.FetchBlob(img_tensor) self._get_file_writer().add_summary( image(tag, img_tensor, dataformats=dataformats), global_step, walltime)
def add_image(self, tag, img_tensor, global_step=None, walltime=None): """Add image data to summary. Note that this requires the ``pillow`` package. Args: tag (string): Data identifier img_tensor (torch.Tensor, numpy.array, or string/blobname): Image data global_step (int): Global step value to record walltime (float): Optional override default walltime (time.time()) seconds after epoch of event Shape: img_tensor: Default is :math:`(3, H, W)`. You can use ``torchvision.utils.make_grid()`` to convert a batch of tensor into 3xHxW format or call ``add_images`` and let us do the job. Tensor with :math:`(1, H, W)`, :math:`(H, W)`, :math:`(H, W, 3)` is also suitable as long as corresponding ``dataformats`` argument is passed, e.g. ``CHW``, ``HWC``, ``HW``. Examples:: from torch.utils.tensorboard import SummaryWriter import numpy as np img = np.zeros((3, 100, 100)) img[0] = np.arange(0, 10000).reshape(100, 100) / 10000 img[1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000 img_HWC = np.zeros((100, 100, 3)) img_HWC[:, :, 0] = np.arange(0, 10000).reshape(100, 100) / 10000 img_HWC[:, :, 1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000 writer = SummaryWriter() writer.add_image('my_image', img, 0) # If you have non-default dimension setting, set the dataformats argument. writer.add_image('my_image_HWC', img_HWC, 0, dataformats='HWC') writer.close() Expected result: .. image:: _static/img/tensorboard/add_image.png :scale: 50 % """ self._get_file_writer().add_summary( image(tag, img_tensor, dataformats='HWC'), global_step, walltime)
def add_images(self, tag, img_tensor, global_step=None, walltime=None): """Add batched image data to summary. Note that this requires the ``pillow`` package. Args: tag (string): Data identifier img_tensor (torch.Tensor, numpy.array, or string/blobname): Image data global_step (int): Global step value to record walltime (float): Optional override default walltime (time.time()) seconds after epoch of event Shape: img_tensor: Default is :math:`(N, 3, H, W)`. If ``dataformats`` is specified, other shape will be accepted. e.g. NCHW or NHWC. Examples:: from torch.utils.tensorboard import SummaryWriter import numpy as np img_batch = np.zeros((16, 3, 100, 100)) for i in range(16): img_batch[i, 0] = np.arange(0, 10000).reshape(100, 100) / 10000 / 16 * i img_batch[i, 1] = (1 - np.arange(0, 10000).reshape(100, 100) / 10000) / 16 * i writer = SummaryWriter() writer.add_images('my_image_batch', img_batch, 0) writer.close() Expected result: .. image:: _static/img/tensorboard/add_images.png :scale: 30 % """ self._get_file_writer().add_summary( image(tag, img_tensor, dataformats='NHWC'), global_step, walltime)
def test_image_without_channel(self): self.assertTrue( compare_proto( summary.image('dummy', tensor_N(shape=(8, 8)), dataformats='HW'), self)) # noqa E127
def test_image_with_3_channel_batched(self): self.assertTrue( compare_proto( summary.image('dummy', tensor_N(shape=(2, 3, 8, 8)), dataformats='NCHW'), self)) # noqa E127
def test_image_with_one_channel(self): self.assertTrue(compare_image_proto( summary.image('dummy', tensor_N(shape=(1, 8, 8)), dataformats='CHW'), self)) # noqa: E131
def test_image_without_channel(self): self.assertTrue( compare_proto( summary.image('dummy', np.random.rand(8, 8).astype(np.float32), dataformats='HW'), self)) # noqa E127