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:
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?
I think the process of turning the image in uint8 format after each iteration of preprocess + deprocess may destruct some usefull information that can not be retrieved correctly during the next iteration. Can you try something like this:
Or an other idea would be to, at least, round the result when converting it to int. When you do tensor.to(int) or numpy.astype(int), the value is floored, witch can actually be the source of your problem. So I suggest this alternative:
from PIL import Image
from torchvision import transforms
import torch
import numpy as np
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(np.round(255.*img).astype(np.uint8))
img.save("{0}.png".format(i))