转化为paddlepaddle2.2.2代码:def safe_invert_gains(image, rgb_gain, red_gain, blue_gain): """Inverts gains while safely handling saturated pixels.""" image = image.permute(1, 2, 0) # Permute the image tensor to HxWxC format from CxHxW format gains = torch.stack((1.0 / red_gain, torch.tensor([1.0]), 1.0 / blue_gain)) / rgb_gain gains = gains.squeeze() gains = gains[None, None, :] # Prevents dimming of saturated pixels by smoothly masking gains near white. gray = torch.mean(image, dim=-1, keepdim=True) inflection = 0.9 mask = (torch.clamp(gray - inflection, min=0.0) / (1.0 - inflection)) ** 2.0 safe_gains = torch.max(mask + (1.0 - mask) * gains, gains) out = image * safe_gains out = out.permute(2, 0, 1) # Re-Permute the tensor back to CxHxW format return out
时间: 2024-02-18 20:04:45 浏览: 176
以下是将上述代码转换为PaddlePaddle 2.2.2的代码:
```
import paddle
import paddle.nn.functional as F
def safe_invert_gains(image, rgb_gain, red_gain, blue_gain):
"""Inverts gains while safely handling saturated pixels."""
image = paddle.transpose(image, perm=[1, 2, 0]) # Permute the image tensor to HxWxC format from CxHxW format
gains = paddle.stack((1.0 / red_gain, paddle.to_tensor([1.0]), 1.0 / blue_gain)) / rgb_gain
gains = paddle.squeeze(gains)
gains = gains.unsqueeze(0).unsqueeze(0) # Prevents dimming of saturated pixels by smoothly masking gains near white.
gray = paddle.mean(image, axis=-1, keepdim=True)
inflection = 0.9
mask = (F.relu(gray - inflection, inplace=True) / (1.0 - inflection)) ** 2.0
safe_gains = paddle.maximum(mask + (1.0 - mask) * gains, gains)
out = image * safe_gains
out = paddle.transpose(out, perm=[2, 0, 1]) # Re-Permute the tensor back to CxHxW format
return out
```
需要注意的是,PaddlePaddle中的API与PyTorch不完全一致,因此需要对代码做一些调整。主要包括:
1. 将torch转换为paddle。
2. 将permute函数转换为transpose函数,并调整参数格式。
3. 将torch.mean函数转换为paddle.mean,并调整参数格式。
4. 将torch.clamp函数转换为F.relu函数,并调整参数格式。
5. 将torch.max函数转换为paddle.maximum函数。
需要注意的是,在PaddlePaddle中,使用inplace=True时需要用F.relu等函数来代替。
阅读全文