# Result of convolution changes every time the cell is compiled

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 Image

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)
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()

I assume you mean the colors of `result` change?
If that’s the case, then it would be expected, since you are applying a random `nn.Conv2d` layer to the image.

1 Like

Thanks for the hint! In your opinion, what’s the more pytorchy way to apply a convolution layer to all the channels of the image?

The “standard” conv layer will already use kernels, which use all input channels to create an activation map.
If you would like to apply a single channel filter to each input channel, you could use a grouped convolution via `groups=in_channels`.