Skip to content

Commit

Permalink
ToDType before ToPILImage for uint8 rounding
Browse files Browse the repository at this point in the history
ToPILImage converts to uint8 without rounding:
(npimg * 255).astype(np.uint8)

ToDtype converts to uint8 while rounding:
image.mul(255 + 1.0 - 0.001).to(torch.uint8)
  • Loading branch information
Cyberbeing committed Jan 30, 2024
1 parent 9141166 commit 33b5c27
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions modules/upscaler_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def upscale_pil_patch(model, img: Image.Image) -> Image.Image:
tensor = T.PILToTensor()(img)
tensor = T.ToDtype(torch.float32, scale=True)(tensor)
tensor = tensor.clamp_(0.0, 1.0).unsqueeze(0).to(device=param.device, dtype=param.dtype)
return T.ToPILImage(mode="RGB")(model(tensor).squeeze(0).clamp_(0.0, 1.0))
tensorimg = T.ToDtype(torch.uint8, scale=True)(model(tensor).squeeze(0).clamp_(0.0, 1.0))
return T.ToPILImage(mode="RGB")(tensorimg)


def upscale_with_model(
Expand Down Expand Up @@ -165,5 +166,6 @@ def upscale_2(
desc=desc,
device=param.device,
)
return T.ToPILImage(mode="RGB")(output.squeeze(0).clamp_(0.0, 1.0))
output = T.ToDtype(torch.uint8, scale=True)(output.squeeze(0).clamp_(0.0, 1.0))
return T.ToPILImage(mode="RGB")(output)

0 comments on commit 33b5c27

Please sign in to comment.