CropAndResize
产品支持情况
| 产品 | 是否支持 |
|---|---|
| Ascend 950PR/Ascend 950DT | √ |
| Atlas A3 训练系列产品/Atlas A3 推理系列产品 | √ |
| Atlas A2 训练系列产品/Atlas A2 推理系列产品 | √ |
| Atlas 200I/500 A2 推理产品 | × |
| Atlas 推理系列产品 | √ |
| Atlas 训练系列产品 | √ |
功能说明
-
算子功能:从输入图像中提取多个裁剪区域,并将它们统一调整为指定大小,支持双线性插值和最近邻插值。
-
计算公式:
对于输入x的shape为(batch, image_height, image_width, depth),第ii个框(i∈[0,num_boxes)i \in [0, num\_boxes)),令b=box_index[i]b = box\_index[i],(y1,x1,y2,x2)=boxes[i](y_1, x_1, y_2, x_2) = boxes[i],裁剪后图像的长宽crop_size的shape为(crop_height, crop_width),输出yy在位置(i,py,px,d)(i, p_y, p_x, d)的值按以下公式计算。
-
method = bilinear时:
-
计算缩放比例height_scaleheight\_scale和width_scalewidth\_scale:
height_scale={(y2−y1)×(image_height−1)/(crop_height−1),crop_height>10,crop_height=1height\_scale = \begin{cases} (y_2 - y_1) \times (image\_height - 1) / (crop\_height - 1), & crop\_height > 1 \\ 0, & crop\_height = 1 \end{cases}
width_scale={(x2−x1)×(image_width−1)/(crop_width−1),crop_width>10,crop_width=1width\_scale = \begin{cases} (x_2 - x_1) \times (image\_width - 1) / (crop\_width - 1), & crop\_width > 1 \\ 0, & crop\_width = 1 \end{cases}
-
将输出坐标(py,px)(p_y, p_x)映射到输入图像坐标(in_y,in_x)(in\_y, in\_x):
in_y={y1×(image_height−1)+py×height_scale,crop_height>10.5×(y1+y2)×(image_height−1),crop_height=1in\_y = \begin{cases} y_1 \times (image\_height - 1) + p_y \times height\_scale, & crop\_height > 1 \\ 0.5 \times (y_1 + y_2) \times (image\_height - 1), & crop\_height = 1 \end{cases}
in_x={x1×(image_width−1)+px×width_scale,crop_width>10.5×(x1+x2)×(image_width−1),crop_width=1in\_x = \begin{cases} x_1 \times (image\_width - 1) + p_x \times width\_scale, & crop\_width > 1 \\ 0.5 \times (x_1 + x_2) \times (image\_width - 1), & crop\_width = 1 \end{cases}
若in_y<0in\_y < 0或in_y>image_height−1in\_y > image\_height - 1或in_x<0in\_x < 0 或 in_x>image_width−1in\_x > image\_width - 1,则:
y(i,py,px,d)=extrapolation_valuey(i, p_y, p_x, d) = extrapolation\_value
否则,进行双线性插值。令top=⌊in_y⌋top = \lfloor in\_y \rfloor,bottom=⌈in_y⌉bottom = \lceil in\_y \rceil,left=⌊in_x⌋left = \lfloor in\_x \rfloor,right=⌈in_x⌉right = \lceil in\_x \rceil,y_ratio=in_y−topy\_ratio = in\_y - top,x_ratio=in_x−leftx\_ratio = in\_x - left,则:
y(i,py,px,d)= (1−y_ratio)(1−x_ratio)⋅x(b,top,left,d)+ (1−y_ratio)⋅x_ratio⋅x(b,top,right,d)+ y_ratio⋅(1−x_ratio)⋅x(b,bottom,left,d)+ y_ratio⋅x_ratio⋅x(b,bottom,right,d)\begin{aligned} y(i, p_y, p_x, d) =\; & (1 - y\_ratio)(1 - x\_ratio) \cdot x(b, top, left, d) \\ +\; & (1 - y\_ratio) \cdot x\_ratio \cdot x(b, top, right, d) \\ +\; & y\_ratio \cdot (1 - x\_ratio) \cdot x(b, bottom, left, d) \\ +\; & y\_ratio \cdot x\_ratio \cdot x(b, bottom, right, d) \end{aligned}
-
-
method = nearest时:
height_scaleheight\_scale、width_scalewidth\_scale、in_yin\_y、in_xin\_x 的计算方式与bilinear方法相同。
若in_yin\_y或in_xin\_x越界(条件同bilinear),则:
y(i,py,px,d)=extrapolation_valuey(i, p_y, p_x, d) = extrapolation\_value
否则,取最近邻像素:
y(i,py,px,d)=x(b,round(in_y),round(in_x),d)y(i, p_y, p_x, d) = x(b, round(in\_y), round(in\_x), d)
其中round(⋅)round(\cdot)为四舍五入取整。
-
method = bilinear_v2时:
-
首先将归一化框坐标映射为输入图像上的像素坐标,计算裁剪窗口。令y1o=⌊y1×image_height⌋y_{1o} = \lfloor y_1 \times image\_height \rfloor,x1o=⌊x1×image_width⌋x_{1o} = \lfloor x_1 \times image\_width \rfloor,y2o=⌊y2×image_height⌋y_{2o} = \lfloor y_2 \times image\_height \rfloor,x2o=⌊x2×image_width⌋x_{2o} = \lfloor x_2 \times image\_width \rfloor,则裁剪窗口的高hh和宽ww为:
h=max(y2o−y1o+1, 1),w=max(x2o−x1o+1, 1)h = \max(y_{2o} - y_{1o} + 1,\; 1), \quad w = \max(x_{2o} - x_{1o} + 1,\; 1)
-
对于输出位置(py,px)(p_y, p_x),计算其在裁剪窗口中的浮点索引ryr_y和rxr_x:
ry=(py+0.5)×h/crop_height−0.5r_y = (p_y + 0.5) \times h / crop\_height - 0.5
rx=(px+0.5)×w/crop_width−0.5r_x = (p_x + 0.5) \times w / crop\_width - 0.5
-
计算插值下标和权重。令clamp(v,lo,hi)=max(min(v,hi),lo)clamp(v, lo, hi) = \max(\min(v, hi), lo),则:
lower_y=clamp(⌊ry⌋,0,h−1),upper_y=clamp(⌈ry⌉,0,h−1)lower\_y = clamp(\lfloor r_y \rfloor, 0, h - 1), \quad upper\_y = clamp(\lceil r_y \rceil, 0, h - 1)
lower_x=clamp(⌊rx⌋,0,w−1),upper_x=clamp(⌈rx⌉,0,w−1)lower\_x = clamp(\lfloor r_x \rfloor, 0, w - 1), \quad upper\_x = clamp(\lceil r_x \rceil, 0, w - 1)
y_ratio=ry−lower_y,x_ratio=rx−lower_xy\_ratio = r_y - lower\_y, \quad x\_ratio = r_x - lower\_x
-
双线性插值:
y(i,py,px,d)= (1−y_ratio)(1−x_ratio)⋅x(b,y1o+lower_y,x1o+lower_x,d)+ (1−y_ratio)⋅x_ratio⋅x(b,y1o+lower_y,x1o+upper_x,d)+ y_ratio⋅(1−x_ratio)⋅x(b,y1o+upper_y,x1o+lower_x,d)+ y_ratio⋅x_ratio⋅x(b,y1o+upper_y,x1o+upper_x,d)\begin{aligned} y(i, p_y, p_x, d) =\; & (1 - y\_ratio)(1 - x\_ratio) \cdot x(b, y_{1o} + lower\_y, x_{1o} + lower\_x, d) \\ +\; & (1 - y\_ratio) \cdot x\_ratio \cdot x(b, y_{1o} + lower\_y, x_{1o} + upper\_x, d) \\ +\; & y\_ratio \cdot (1 - x\_ratio) \cdot x(b, y_{1o} + upper\_y, x_{1o} + lower\_x, d) \\ +\; & y\_ratio \cdot x\_ratio \cdot x(b, y_{1o} + upper\_y, x_{1o} + upper\_x, d) \end{aligned}
-
-
参数说明
| 参数名 | 输入/输出/属性 | 描述 | 数据类型 | 数据格式 |
|---|---|---|---|---|
| x | 输入 | 输入为四维的Tensor,其shape为(batch, image_height, image_width, depth),其中image_height和image_width必须是正数。对应计算公式描述中的`输入x`。 | UINT8、UINT16、INT8、INT16、INT32、INT64、FLOAT16、FLOAT、DOUBLE | NHWC |
| boxes | 输入 | 二维Tensor,其shape为(num_boxes, 4)。第i行指定了box_index[i]在图像中一个框的位置,并且使用归一化坐标[y1, x1, y2, x2]进行表示。归一化后的y坐标值通过y*(image_height - 1)映射到图像坐标系中,因此归一化图像的高度[0, 1]区间会被映射到原始图像高度的[0,image_height - 1]区间。我们允许y1 > y2, 在这种情况下,采样的裁剪区域会是原始图像的上下翻转版。宽度维度的处理方式相似,归一化坐标值超出[0, 1]范围时,使用extrapolation_value外插值进行补齐。 | FLOAT | ND |
| box_index | 输入 | boxes的索引,其shape为(num_boxes)的一维tensor,取值范围[0, batch),box_index[i]的值指定了第i个框所对应的图像。 | INT32 | ND |
| crop_size | 输入 | 包含2个元素的1-D的Tensor,shape为(crop_height, crop_width),所有裁剪后的图像区域都会被调整为此大小。图像内容的长宽比不会被保留。crop_height和crop_width必须是正数。对应计算公式描述中的`crop_size`。 | INT32 | ND |
| extrapolation_value | 可选属性 |
|
FLOAT | - |
| method | 可选属性 |
|
STRING | - |
| y | 输出 | 返回一个四维Tensor,shape大小为(num_boxes, crop_height, crop_width, depth)。对应计算公式描述中的`输出y`。 | FLOAT | NHWC |
约束说明
无
调用说明
| 调用方式 | 调用样例 | 说明 |
|---|---|---|
| 图模式调用 | test_geir_crop_and_resize | 通过算子IR构图方式调用CropAndResize算子。 |