@torch.enable_grad()
def visualize_pred(self):
    'Visualize model prediction and CAM\n        \n        '
    LOGGER.text('Visualizing model predictions...', level=LoggerObserver.DEBUG)
    visualizer = Visualizer()
    batch = next(iter(self.valloader))
    images = batch['inputs']
    targets = batch['targets']
    self.model.eval()
    model_name = self.model.model.name
    grad_cam = CAMWrapper.get_method(name='gradcam', model=self.model.model.get_model(), model_name=model_name, use_cuda=next(self.model.parameters()).is_cuda)
    (grayscale_cams, label_indices, scores) = grad_cam(images, return_probs=True)
    gradcam_batch = []
    pred_batch = []
    for idx in range(len(grayscale_cams)):
        image = images[idx]
        target = targets[idx].item()
        label = label_indices[idx]
        grayscale_cam = grayscale_cams[idx, :]
        score = scores[idx]
        img_show = visualizer.denormalize(image)
        visualizer.set_image(img_show)
        if (self.valloader.dataset.classnames is not None):
            label = self.valloader.dataset.classnames[label]
            target = self.valloader.dataset.classnames[target]
        if (label == target):
            color = [0, 1, 0]
        else:
            color = [1, 0, 0]
        visualizer.draw_label(f'''GT: {target}
P: {label}
C: {score:.4f}''', fontColor=color, fontScale=0.8, thickness=2, outline=None, offset=100)
        img_cam = show_cam_on_image(img_show, grayscale_cam, use_rgb=True)
        img_cam = TFF.to_tensor(img_cam)
        gradcam_batch.append(img_cam)
        pred_img = visualizer.get_image()
        pred_img = TFF.to_tensor(pred_img)
        pred_batch.append(pred_img)
        if (idx == 63):
            break
    gradcam_grid_img = visualizer.make_grid(gradcam_batch)
    fig = plt.figure(figsize=(8, 8))
    plt.imshow(gradcam_grid_img)
    plt.axis('off')
    plt.tight_layout(pad=0)
    LOGGER.log([{'tag': 'Validation/gradcam', 'value': fig, 'type': LoggerObserver.FIGURE, 'kwargs': {'step': self.iters}}])
    pred_grid_img = visualizer.make_grid(pred_batch)
    fig = plt.figure(figsize=(10, 10))
    plt.imshow(pred_grid_img)
    plt.axis('off')
    plt.tight_layout(pad=0)
    LOGGER.log([{'tag': 'Validation/prediction', 'value': fig, 'type': LoggerObserver.FIGURE, 'kwargs': {'step': self.iters}}])
    self.optimizer.zero_grad()
Visualize model prediction and CAM
@torch.enable_grad()
def visualize_pred(self):
    '\n        \n        '
    LOGGER.text('Visualizing model predictions...', level=LoggerObserver.DEBUG)
    visualizer = Visualizer()
    batch = next(iter(self.valloader))
    images = batch['inputs']
    targets = batch['targets']
    self.model.eval()
    model_name = self.model.model.name
    grad_cam = CAMWrapper.get_method(name='gradcam', model=self.model.model.get_model(), model_name=model_name, use_cuda=next(self.model.parameters()).is_cuda)
    (grayscale_cams, label_indices, scores) = grad_cam(images, return_probs=True)
    gradcam_batch = []
    pred_batch = []
    for idx in range(len(grayscale_cams)):
        image = images[idx]
        target = targets[idx].item()
        label = label_indices[idx]
        grayscale_cam = grayscale_cams[idx, :]
        score = scores[idx]
        img_show = visualizer.denormalize(image)
        visualizer.set_image(img_show)
        if (self.valloader.dataset.classnames is not None):
            label = self.valloader.dataset.classnames[label]
            target = self.valloader.dataset.classnames[target]
        if (label == target):
            color = [0, 1, 0]
        else:
            color = [1, 0, 0]
        visualizer.draw_label(f'GT: {target}
P: {label}
C: {score:.4f}', fontColor=color, fontScale=0.8, thickness=2, outline=None, offset=100)
        img_cam = show_cam_on_image(img_show, grayscale_cam, use_rgb=True)
        img_cam = TFF.to_tensor(img_cam)
        gradcam_batch.append(img_cam)
        pred_img = visualizer.get_image()
        pred_img = TFF.to_tensor(pred_img)
        pred_batch.append(pred_img)
        if (idx == 63):
            break
    gradcam_grid_img = visualizer.make_grid(gradcam_batch)
    fig = plt.figure(figsize=(8, 8))
    plt.imshow(gradcam_grid_img)
    plt.axis('off')
    plt.tight_layout(pad=0)
    LOGGER.log([{'tag': 'Validation/gradcam', 'value': fig, 'type': LoggerObserver.FIGURE, 'kwargs': {'step': self.iters}}])
    pred_grid_img = visualizer.make_grid(pred_batch)
    fig = plt.figure(figsize=(10, 10))
    plt.imshow(pred_grid_img)
    plt.axis('off')
    plt.tight_layout(pad=0)
    LOGGER.log([{'tag': 'Validation/prediction', 'value': fig, 'type': LoggerObserver.FIGURE, 'kwargs': {'step': self.iters}}])
    self.optimizer.zero_grad()