I wrote a simple script which applies a 2D convolution operation to an input image. I am running the code as a cell in a Jupyter Notebook. Every time I compile the cell, the colors of the processed image change. Why is that? Should I change how convolution is calculated?
Here is my code:
import torch
import torchvision
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
import imageio
from PIL import ImageA = imageio.imread(“Gioconda.jpg”)
del transposed_image, fc, fc1, result, image_d
!!! from [H, W, C] to [C, H, W]
transposed_image = A.transpose((2, 0, 1))
!!! add batch dim
transposed_image = np.expand_dims(transposed_image, 0)conv2 = torch.nn.Conv2d(in_channels=3, out_channels=3, kernel_size=10, stride=10, padding = 10)
image_d = torch.FloatTensor(transposed_image)
fc = conv2(image_d)
fc1 = fc.permute(0, 2, 3, 1)[0]
result = fc1.data.numpy()
max_ = np.max(result)
min_ = np.min(result)
result -= min_
result /= max_plt.figure(figsize=(16,8))
plt.subplot(1,2,1)
plt.imshow(A)
plt.subplot(1,2,2)
plt.imshow(result)
plt.show()