I’m building an application where repeated pre- and deprocessing is required. Small changes are made to the image between pre- and deprocessing, but even without these small changes, the following problem occurs, the image slowly clips to black.
I’ve tried to make the problem as simple as possible:
from PIL import Image
from torchvision import transforms
import torch

std_norm = torch.tensor([0.229, 0.224, 0.225])
mean_norm = torch.tensor([0.485, 0.456, 0.406])

preprocess = transforms.Compose([transforms.ToTensor(), transforms.Normalize(mean_norm, std_norm)])

def deprocess(img):
	img = img * std_norm.reshape((1, 3, 1, 1)) + mean_norm.reshape((1, 3, 1, 1))
	return img

import numpy as np
from PIL import Image

img = Image.open("your_image_here.png").convert('RGB')

img_np = np.float32(img)

for i in range(1000):
	img = preprocess(img)
	img = img.unsqueeze(0)
	img = deprocess(img).squeeze(0).permute(1,2,0).numpy()
	img = Image.fromarray((255.*img).astype(np.uint8))

	img.save("{0}.png".format(i))

The reason why I think it’s due to preprocessing and deprocessing is because if I remove the normalization from the transform composition and return img in deprocess the image retains its quality. Is there a way of getting more numerically stable results?