def plot_img_and_seg(
        img: torch.Tensor,
        seg: torch.Tensor,
        figsize: Optional[Tuple[float, float]] = None,
        experiment: Optional[neptune.experiments.Experiment] = None,
        neptune_name: Optional[str] = None):
    _exp = experiment if experiment else neptune

    assert len(img.shape) == len(seg.shape) == 4
    n_row = img.shape[-4]
    if n_row <= 1:
        fig, axes = plt.subplots(ncols=2, figsize=figsize)
        axes[0].imshow(img[0, 0], cmap='gray')
        axes[1].imshow(seg[0, 0], cmap='seismic', vmin=-0.5, vmax=10.5)
        axes[0].set_axis_off()
        axes[1].set_axis_off()

    else:
        fig, axes = plt.subplots(ncols=2, nrows=n_row, figsize=figsize)
        for n in range(n_row):
            axes[n, 0].imshow(img[n, 0], cmap='gray')
            axes[n, 1].imshow(seg[n, 0], cmap='seismic', vmin=-0.5, vmax=10.5)
            axes[n, 0].set_axis_off()
            axes[n, 1].set_axis_off()

    fig.tight_layout()
    if neptune_name is not None:
        #log_img_and_chart(name=neptune_name, fig=fig, experiment=experiment)
        log_img_only(name=neptune_name, fig=fig, experiment=_exp)
    plt.close(fig)
    return fig
def plot_grid(img,
              figsize: Optional[Tuple[float, float]] = None,
              experiment: Optional[neptune.experiments.Experiment] = None,
              neptune_name: Optional[str] = None):
    _exp = experiment if experiment else neptune

    assert len(img.shape) == 3
    n_max = img.shape[-3]

    row_max = n_max // 4
    if row_max <= 1:
        fig, axes = plt.subplots(ncols=n_max, figsize=figsize)
        for n in range(n_max):
            axes[n].imshow(img[n])
    else:
        fig, axes = plt.subplots(ncols=4, nrows=row_max, figsize=figsize)
        for n in range(4 * row_max):
            row = n // 4
            col = n % 4
            axes[row, col].imshow(img[n])

    fig.tight_layout()
    if neptune_name is not None:
        #log_img_and_chart(name=neptune_name, fig=fig, experiment=experiment)
        log_img_only(name=neptune_name, fig=fig, experiment=_exp)
    plt.close(fig)
    return fig
def plot_label_contours(
        label: Union[torch.Tensor, numpy.ndarray],
        image: Union[torch.Tensor, numpy.ndarray],
        window: Optional[tuple] = None,
        contour_thickness: int = 2,
        contour_color: str = 'red',
        figsize: tuple = (24, 24),
        experiment: Optional[neptune.experiments.Experiment] = None,
        neptune_name: Optional[str] = None):
    _exp = experiment if experiment else neptune
    assert len(label.shape) == 2
    assert len(image.shape) == 2 or len(image.shape) == 3

    assert len(label.shape) == 2
    if torch.is_tensor(label):
        label = label.cpu().numpy()

    if torch.is_tensor(image):
        if len(image.shape) == 3:
            image = image.permute(1, 2, 0).cpu().numpy()
        else:
            image = image.cpu().numpy()
    if len(image.shape) == 3 and (image.shape[-1] != 3):
        image = image[..., 0]

    assert image.shape[:2] == label.shape[:2]

    print(window)
    if window is None:
        window = [0, 0, label.shape[-2], label.shape[-1]]
    else:
        window = (max(0, window[0]), max(0, window[1]),
                  min(label.shape[-2],
                      window[2]), min(label.shape[-1], window[3]))

    contours = contours_from_labels(
        label[window[0]:window[2], window[1]:window[3]], contour_thickness)
    img_with_contours = draw_contours(image=image[window[0]:window[2],
                                                  window[1]:window[3]],
                                      contours=contours,
                                      contours_color=contour_color)

    fig, ax = plt.subplots(ncols=3, figsize=figsize)
    ax[0].imshow(image[window[0]:window[2], window[1]:window[3]], cmap='gray')
    ax[1].imshow(img_with_contours)
    ax[2].imshow(
        skimage.color.label2rgb(label=label[window[0]:window[2],
                                            window[1]:window[3]],
                                image=image[window[0]:window[2],
                                            window[1]:window[3]],
                                alpha=0.25,
                                bg_label=0))

    fig.tight_layout()
    if neptune_name is not None:
        #log_img_and_chart(name=neptune_name, fig=fig, experiment=experiment)
        log_img_only(name=neptune_name, fig=fig, experiment=_exp)
    plt.close(fig)
    return fig
def plot_tiling(tiling,
                figsize: tuple = (12, 12),
                window: Optional[tuple] = None,
                experiment: Optional[neptune.experiments.Experiment] = None,
                neptune_name: Optional[str] = None):
    _exp = experiment if experiment else neptune

    if window is None:
        window = [
            0, 0, tiling.integer_mask.shape[-2], tiling.integer_mask.shape[-1]
        ]
    else:
        window = (max(0, window[0]), max(0, window[1]),
                  min(tiling.integer_mask.shape[-2],
                      window[2]), min(tiling.integer_mask.shape[-1],
                                      window[3]))

    fig, axes = plt.subplots(ncols=2, nrows=2, figsize=figsize)
    axes[0, 0].imshow(
        skimage.color.label2rgb(
            label=tiling.integer_mask[0, 0, window[0]:window[2],
                                      window[1]:window[3]].cpu().numpy(),
            image=numpy.zeros_like(
                tiling.integer_mask[0, 0, window[0]:window[2],
                                    window[1]:window[3]].cpu().numpy()),
            alpha=1.0,
            bg_label=0))

    axes[0, 1].imshow(
        skimage.color.label2rgb(
            label=tiling.integer_mask[0, 0, window[0]:window[2],
                                      window[1]:window[3]].cpu().numpy(),
            image=tiling.raw_image[0, 0, window[0]:window[2],
                                   window[1]:window[3]].cpu().numpy(),
            alpha=0.25,
            bg_label=0))
    axes[1, 0].imshow(tiling.fg_prob[0, 0, window[0]:window[2],
                                     window[1]:window[3]].cpu().numpy(),
                      cmap='gray')
    axes[1, 1].imshow(tiling.raw_image[0, :, window[0]:window[2],
                                       window[1]:window[3]].cpu().permute(
                                           1, 2, 0).squeeze(-1).numpy(),
                      cmap='gray')

    axes[0, 0].set_title("sample integer mask")
    axes[0, 1].set_title("sample integer mask")
    axes[1, 0].set_title("fg prob")
    axes[1, 1].set_title("raw image")
    fig.tight_layout()
    if neptune_name is not None:
        #log_img_and_chart(name=neptune_name, fig=fig, experiment=experiment)
        log_img_only(name=neptune_name, fig=fig, experiment=_exp)
    plt.close(fig)
    return fig
def plot_concordance(
        concordance,
        figsize: tuple = (12, 12),
        experiment: Optional[neptune.experiments.Experiment] = None,
        neptune_name: Optional[str] = None):
    _exp = experiment if experiment else neptune
    fig, axes = plt.subplots(figsize=figsize)
    axes.imshow(concordance.intersection_mask.cpu(), cmap='gray')
    axes.set_title("intersection mask, iou=" + str(concordance.iou))

    fig.tight_layout()
    if neptune_name is not None:
        log_img_only(name=neptune_name, fig=fig, experiment=_exp)
    plt.close(fig)
    return fig
def show_batch(images: torch.Tensor,
               n_col: int = 4,
               n_padding: int = 10,
               title: Optional[str] = None,
               pad_value: int = 1,
               normalize_range: Optional[tuple] = None,
               figsize: Optional[Tuple[float, float]] = None,
               experiment: Optional[neptune.experiments.Experiment] = None,
               neptune_name: Optional[str] = None):
    _exp = experiment if experiment else neptune
    """Visualize a torch tensor of shape: (batch x ch x width x height) """
    assert len(images.shape) == 4  # batch, ch, width, height
    if images.device != "cpu":
        images = images.cpu()

    # Always normalize the image in (0,1) either using min_max of tensor or normalize_range
    grid = utils.make_grid(images,
                           n_col,
                           n_padding,
                           normalize=True,
                           range=normalize_range,
                           scale_each=False,
                           pad_value=pad_value)

    fig = plt.figure(figsize=figsize)
    plt.imshow(grid.detach().permute(1, 2, 0).squeeze(-1).numpy())
    # plt.axis("off")
    if isinstance(title, str):
        plt.title(title)
    fig.tight_layout()

    if neptune_name is not None:
        #log_img_and_chart(name=neptune_name, fig=fig, experiment=experiment)
        log_img_only(name=neptune_name, fig=fig, experiment=_exp)

    plt.close(fig)
    return fig
Ejemplo n.º 7
0
conditional_crop_train = ConditionalRandomCrop(desired_w=SIZE_CROPS, desired_h=SIZE_CROPS, 
                                               min_roi_fraction=0.9, n_crops_per_image=N_TRAIN)

test_data = conditional_crop_test.crop(img=img_torch,
                                       roi_mask=roi_mask_torch)
if torch.cuda.is_available():
    print("GPU GB after defining test data ->", torch.cuda.memory_allocated()/1E9)


test_loader = SpecialDataSet(img=test_data,
                             store_in_cuda=False,
                             shuffle=False,
                             drop_last=False,
                             batch_size=BATCH_SIZE)
test_batch_example_fig = test_loader.check_batch()
log_img_only(name="test_batch_example", fig=test_batch_example_fig, experiment=exp)

train_loader = SpecialDataSet(img=img_torch,
                              roi_mask=roi_mask_torch,
                              data_augmentation=conditional_crop_train,
                              store_in_cuda=False,
                              shuffle=True,
                              drop_last=True,
                              batch_size=BATCH_SIZE)
train_batch_example_fig = train_loader.check_batch()
log_img_only(name="train_batch_example", fig=train_batch_example_fig, experiment=exp)
if torch.cuda.is_available():
    print("GPU GB after train_loader ->", torch.cuda.memory_allocated()/1E9)

# Make a batch of reference images by cropping the train_data at consecutive locations
reference_imgs_list = []