gains = paddle.stack((1.0 / red_gain, paddle.to_tensor([1.0]), 1.0 / blue_gain)) / rgb_gain File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/manipulation.py", line 798, in stack return layers.stack(x, axis, name) File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/nn.py", line 10184, in stack return _C_ops.stack(x, 'axis', axis) ValueError: (InvalidArgument) stack(): argument 'X' (position 0) must be list of Tensors, but got list of float (at /paddle/paddle/fluid/pybind/op_function.h:886)
时间: 2023-09-12 21:12:06 浏览: 196
这个错误是由于 `paddle.stack()` 函数要求输入的是张量列表,但您在输入时将列表中的一些元素提供为标量(float)。为了解决这个问题,您可以将标量转换为张量,或者在输入列表中将所有元素都作为张量提供。以下是这两种方法的示例代码:
将标量转换为张量:
```python
gains = paddle.stack((
1.0 / paddle.to_tensor(red_gain),
paddle.to_tensor([1.0]),
1.0 / paddle.to_tensor(blue_gain)
)) / paddle.to_tensor(rgb_gain)
```
将所有元素都作为张量提供:
```python
gains = paddle.stack((
paddle.to_tensor([1.0 / red_gain]),
paddle.to_tensor([1.0]),
paddle.to_tensor([1.0 / blue_gain])
)) / paddle.to_tensor([rgb_gain])
```
在这两个示例中,我们使用 `paddle.to_tensor()` 函数将标量转换为张量,并将它们添加到输入列表中。在第一个示例中,我们使用 `paddle.to_tensor()` 函数将所有元素都转换为张量。
相关问题
转化为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
以下是将上述代码转换为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等函数来代替。
下面 tensorflow代码转化为paddlepaddle2.2.2代码:import tensorflow as tf import numpy as np def ccm(): """Generates random RGB -> Camera color correction matrices.""" # Fixed XYZ -> Camera CCM. cam2xyz = [[0.4679,0.2145,0.3176],[0.1433,0.8236,0.0331],[0.0003,-0.3607,1.3604]] xyz2cam = tf.convert_to_tensor(np.linalg.inv(np.array(cam2xyz)),dtype = np.float32) # Multiplies with RGB -> XYZ to get RGB -> Camera CCM. rgb2xyz = tf.to_float([[0.4124564, 0.3575761, 0.1804375], [0.2126729, 0.7151522, 0.0721750], [0.0193339, 0.1191920, 0.9503041]]) rgb2cam = tf.matmul(xyz2cam, rgb2xyz) # Normalizes each row. rgb2cam = rgb2cam / tf.reduce_sum(rgb2cam, axis=-1, keepdims=True) return rgb2cam def gains(): """Generates random gains for brightening and white balance.""" # RGB gain represents brightening. rgb_gain = 1.0 # Red and blue gains represent white balance. red_gain = 1.0 / 0.5527 blue_gain = 1.0 / 0.4844 return rgb_gain, red_gain, blue_gain
这是一样的问题,上一个回答已经给出了代码转换的结果。以下是转换后的代码:
```python
import paddle.fluid as fluid
import numpy as np
def ccm():
"""Generates random RGB -> Camera color correction matrices."""
# Fixed XYZ -> Camera CCM.
cam2xyz = np.array([[0.4679,0.2145,0.3176],[0.1433,0.8236,0.0331],[0.0003,-0.3607,1.3604]], dtype=np.float32)
xyz2cam = fluid.layers.create_tensor(data=np.linalg.inv(cam2xyz), dtype='float32')
# Multiplies with RGB -> XYZ to get RGB -> Camera CCM.
rgb2xyz = np.array([[0.4124564, 0.3575761, 0.1804375],[0.2126729, 0.7151522, 0.0721750],[0.0193339, 0.1191920, 0.9503041]], dtype=np.float32)
rgb2cam = fluid.layers.matmul(xyz2cam, rgb2xyz)
# Normalizes each row.
rgb2cam = rgb2cam / fluid.layers.reduce_sum(rgb2cam, dim=-1, keep_dim=True)
return rgb2cam
def gains():
"""Generates random gains for brightening and white balance."""
# RGB gain represents brightening.
rgb_gain = 1.0
# Red and blue gains represent white balance.
red_gain = 1.0 / 0.5527
blue_gain = 1.0 / 0.4844
return rgb_gain, red_gain, blue_gain
```
阅读全文