Question for (TRANSFER LEARNING FOR COMPUTER VISION TUTORIAL)

https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html

def imshow(inp, title=None):
    """Imshow for Tensor."""
    inp = inp.numpy().transpose((1, 2, 0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    inp = std * inp + mean
    *inp = np.clip(inp, 0, 1)*
    plt.imshow(inp)
    if title is not None:
        plt.title(title)
    plt.pause(0.001)  # pause a bit so that plots are updated


# Get a batch of training data
inputs, classes = next(iter(dataloaders['train']))

# Make a grid from batch
out = torchvision.utils.make_grid(inputs)

imshow(out, title=[class_names[x] for x in classes])

Why the bolded line(inp = np.clip(inp,0,1)) is needed??
I hae visualized by commenting out that line and it still worked the same way. Then why do i need to clip??

np.clip is to make sure the tensor values lie in the range [0, 1]. You need the input images normalized and be in a specific range(either [0, 1] or [0, 255]) which is what np.clip ensures.

Visualizing such unbounded tensors may not be a problem though, but they need to be in a certain bound in order to train a network.

1 Like

This function is for visualize only. So, i guess i need not normalize the input .

But Why plt.imshow() i s showing a normalized image correctly??

Right.

plt.imshow supports both 0-1 and 0-255 arrays