ホーム › AI.MachineLearning.DirectML › DML_ROI_ALIGN1_OPERATOR_DESC
DML_ROI_ALIGN1_OPERATOR_DESC
構造体サイズ=各フィールドのバイト数(x64/x86 で異なる場合は x64/x86 と併記)。x64/x86 列=フィールドのバイトオフセット(HSPで dupptr / lpoke / wpoke 等に使用)。
フィールド
| フィールド | 型 | サイズ | x64 | x86 | 説明 |
|---|---|---|---|---|---|
| InputTensor | DML_TENSOR_DESC* | 8/4 | +0 | +0 | 入力データを格納するテンソルで、次元は { BatchCount, ChannelCount, InputHeight, InputWidth } です。 |
| ROITensor | DML_TENSOR_DESC* | 8/4 | +8 | +4 | 注目領域 (ROI) のデータを格納するテンソルです。これは、入力テンソルの X 次元と Y 次元を指す浮動小数点座標で表された一連の境界ボックスです。ROITensor に指定できる次元は { NumROIs, 4 }、{ 1, NumROIs, 4 }、または { 1, 1, NumROIs, 4 } です。各 ROI の値は、左上隅と右下隅の座標を [x1, y1, x2, y2] の順で表します。領域は空でもよく、その場合はすべての出力ピクセルが単一の入力座標から得られます。また、領域は反転していてもよく (たとえば x2 が x1 より小さい場合)、その場合、出力は入力を鏡像反転したものになります。これらの座標は最初に SpatialScaleX と SpatialScaleY によってスケーリングされますが、どちらも 1.0 の場合、領域の矩形は入力テンソルの座標にそのまま直接対応します。 |
| BatchIndicesTensor | DML_TENSOR_DESC* | 8/4 | +16 | +8 | ROI の抽出元となるバッチインデックスを格納するテンソルです。BatchIndicesTensor に指定できる次元は { NumROIs }、{ 1, NumROIs }、{ 1, 1, NumROIs }、または { 1, 1, 1, NumROIs } です。各値は InputTensor のバッチのインデックスです。値が [0, BatchCount) の範囲にない場合、動作は未定義です。 |
| OutputTensor | DML_TENSOR_DESC* | 8/4 | +24 | +12 | 出力データを格納するテンソルです。OutputTensor に期待される次元は { NumROIs, ChannelCount, OutputHeight, OutputWidth } です。 |
| ReductionFunction | DML_REDUCE_FUNCTION | 4 | +32 | +16 | 出力要素に寄与するすべての入力サンプルにわたって縮約を行う際に使用する縮約関数です (DML_REDUCE_FUNCTION_AVERAGE または DML_REDUCE_FUNCTION_MAX)。縮約の対象となる入力サンプルの数は、MinimumSamplesPerOutput と MaximumSamplesPerOutput によって制限されます。 |
| InterpolationMode | DML_INTERPOLATION_MODE | 4 | +36 | +20 | 領域をリサイズする際に使用する補間モードです。
|
| SpatialScaleX | FLOAT | 4 | +40 | +24 | ROITensor の座標を InputHeight および InputWidth に比例させるために乗算するスケーリング係数の X (幅) 成分です。たとえば、ROITensor が正規化された座標 (範囲 [0..1] の値) を格納している場合、SpatialScaleX は通常 InputWidth と同じ値になります。 |
| SpatialScaleY | FLOAT | 4 | +44 | +28 | ROITensor の座標を InputHeight および InputWidth に比例させるために乗算するスケーリング係数の Y (高さ) 成分です。たとえば、ROITensor が正規化された座標 (範囲 [0..1] の値) を格納している場合、SpatialScaleY は通常 InputHeight と同じ値になります。 |
| InputPixelOffset | FLOAT | 4 | +48 | +32 | 入力座標の (0,0) から左上のピクセル中心までのオフセットで、通常は 0 または 0.5 です。この値が 0 の場合、ピクセルの中心ではなく左上隅が使用されます。通常は期待どおりの結果になりませんが、一部のフレームワークとの互換性のために役立ちます。この値が 0.5 の場合、ピクセルは中心にあるものとして扱われ、DML_ROI_ALIGN_OPERATOR_DESC と同じ動作になります。 |
| OutputPixelOffset | FLOAT | 4 | +52 | +36 | 左上のピクセル中心から出力座標の (0,0) までのオフセットで、通常は 0 または -0.5 です。この値が 0 の場合、ピクセルの中心ではなく左上隅が使用されます。通常は期待どおりの結果になりませんが、一部のフレームワークとの互換性のために役立ちます。この値が -0.5 の場合、ピクセルは中心にあるものとして扱われ、DML_ROI_ALIGN_OPERATOR_DESC と同じ動作になります。 |
| OutOfBoundsInputValue | FLOAT | 4 | +56 | +40 | ROI が InputTensor の範囲外にある場合に InputTensor から読み取る値です。これは、ROITensor を SpatialScaleX および SpatialScaleY でスケーリングして得られた値が InputWidth および InputHeight より大きい場合に発生することがあります。 |
| MinimumSamplesPerOutput | DWORD | 4 | +60 | +44 | 出力要素ごとに使用する入力サンプルの最小数です。演算子は ScaledCropSize / OutputSize によって入力サンプル数を計算し、その値を MinimumSamplesPerOutput と MaximumSamplesPerOutput の範囲にクランプします。 |
| MaximumSamplesPerOutput | DWORD | 4 | +64 | +48 | 出力要素ごとに使用する入力サンプルの最大数です。演算子は ScaledCropSize / OutputSize によって入力サンプル数を計算し、その値を MinimumSamplesPerOutput と MaximumSamplesPerOutput の範囲にクランプします。 |
| AlignRegionsToCorners | BOOL | 4 | +68 | +52 | 各領域内の出力サンプル点を、領域内に均等に分散させるのではなく、領域のまさに隅まで引き伸ばすかどうかを指定します。既定値は FALSE であり、これは DML_ROI_ALIGN_OPERATOR_DESC と同じ動作です。 |
公式ドキュメント
Mask R-CNN の論文で説明されている ROI align 操作を実行します。要約すると、この操作は入力画像テンソルから切り出したウィンドウを抽出し、指定された InterpolationMode を使用して、OutputTensor の最後の 2 つの次元で指定される共通の出力サイズにリサイズします。
全体的なロジックは次のとおりです。
for every region roiIndex
outputSizeX = OutputTensor.Sizes[3]
outputSizeY = OutputTensor.Sizes[2]
scaledRegionX1 = ROITensor[roiIndex, 0] * SpatialScaleX
scaledRegionY1 = ROITensor[roiIndex, 1] * SpatialScaleY
scaledRegionX2 = ROITensor[roiIndex, 2] * SpatialScaleX
scaledRegionY2 = ROITensor[roiIndex, 3] * SpatialScaleY
scaledRegionSizeX = scaledRegionX2 - scaledRegionX1
scaledRegionSizeY = scaledRegionY2 - scaledRegionY1
inputSamplesPerOutputSampleX = clamp(scaledRegionSizeX / outputSizeX, MinimumSamplesPerOutput, MaximumSamplesPerOutput)
inputSamplesPerOutputSampleY = clamp(scaledRegionSizeY / outputSizeY, MinimumSamplesPerOutput, MaximumSamplesPerOutput)
outputSampleSizeX = outputSizeX * inputSamplesPerOutputSampleX
outputSampleSizeY = outputSizeY * inputSamplesPerOutputSampleY
outputSampleToInputScaleX = scaledRegionSizeX / outputSampleSizeX
outputSampleToInputScaleY = scaledRegionSizeX / outputSampleSizeX
compute all output values
endfor
現在の領域のすべての出力値は、次のように計算します。
for every output tensor element x y and channel in the region
outputValue = getOutputValue(channel, outputTensorX, outputTensorY)
OutputTensor[roiIndex, channel, outputTensorY, outputTensorX] = outputValue
endfor
出力要素に対する各入力サンプルは、次のように計算します。
outputTensorSampleX = outputTensorX * inputSamplesPerOutputSampleX
outputTensorSampleY = outputTensorY * inputSamplesPerOutputSampleY
outputValue = 0
for sampleX from outputTensorSampleX to <= outputTensorSampleX + inputSamplesPerOutputSampleX
for sampleY from outputTensorSampleY to <= outputTensorSampleY + inputSamplesPerOutputSampleY
inputTensorX = (sampleX - OutputPixelOffset) * outputSampleToInputScaleX + scaledRegionX1 - InputPixelOffset
inputTensorY = (sampleY - OutputPixelOffset) * outputSampleToInputScaleY + scaledRegionY1 - InputPixelOffset
inputValue = interpolate2D(InputTensor, BatchIndicesTensor[roiIndex], channel, inputTensorX, inputTensorY)
outputValue = either average or maximum with inputValue
endfor
endfor
return outputValue
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での定義
#include <windows.h>
// DML_ROI_ALIGN1_OPERATOR_DESC (x64 72 / x86 56 バイト)
typedef struct DML_ROI_ALIGN1_OPERATOR_DESC {
DML_TENSOR_DESC* InputTensor;
DML_TENSOR_DESC* ROITensor;
DML_TENSOR_DESC* BatchIndicesTensor;
DML_TENSOR_DESC* OutputTensor;
DML_REDUCE_FUNCTION ReductionFunction;
DML_INTERPOLATION_MODE InterpolationMode;
FLOAT SpatialScaleX;
FLOAT SpatialScaleY;
FLOAT InputPixelOffset;
FLOAT OutputPixelOffset;
FLOAT OutOfBoundsInputValue;
DWORD MinimumSamplesPerOutput;
DWORD MaximumSamplesPerOutput;
BOOL AlignRegionsToCorners;
} DML_ROI_ALIGN1_OPERATOR_DESC;using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DML_ROI_ALIGN1_OPERATOR_DESC
{
public IntPtr InputTensor;
public IntPtr ROITensor;
public IntPtr BatchIndicesTensor;
public IntPtr OutputTensor;
public int ReductionFunction;
public int InterpolationMode;
public float SpatialScaleX;
public float SpatialScaleY;
public float InputPixelOffset;
public float OutputPixelOffset;
public float OutOfBoundsInputValue;
public uint MinimumSamplesPerOutput;
public uint MaximumSamplesPerOutput;
[MarshalAs(UnmanagedType.Bool)] public bool AlignRegionsToCorners;
}Imports System.Runtime.InteropServices
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
Public Structure DML_ROI_ALIGN1_OPERATOR_DESC
Public InputTensor As IntPtr
Public ROITensor As IntPtr
Public BatchIndicesTensor As IntPtr
Public OutputTensor As IntPtr
Public ReductionFunction As Integer
Public InterpolationMode As Integer
Public SpatialScaleX As Single
Public SpatialScaleY As Single
Public InputPixelOffset As Single
Public OutputPixelOffset As Single
Public OutOfBoundsInputValue As Single
Public MinimumSamplesPerOutput As UInteger
Public MaximumSamplesPerOutput As UInteger
<MarshalAs(UnmanagedType.Bool)> Public AlignRegionsToCorners As Boolean
End Structureimport ctypes
from ctypes import wintypes
class DML_ROI_ALIGN1_OPERATOR_DESC(ctypes.Structure):
_fields_ = [
("InputTensor", ctypes.c_void_p),
("ROITensor", ctypes.c_void_p),
("BatchIndicesTensor", ctypes.c_void_p),
("OutputTensor", ctypes.c_void_p),
("ReductionFunction", ctypes.c_int),
("InterpolationMode", ctypes.c_int),
("SpatialScaleX", ctypes.c_float),
("SpatialScaleY", ctypes.c_float),
("InputPixelOffset", ctypes.c_float),
("OutputPixelOffset", ctypes.c_float),
("OutOfBoundsInputValue", ctypes.c_float),
("MinimumSamplesPerOutput", wintypes.DWORD),
("MaximumSamplesPerOutput", wintypes.DWORD),
("AlignRegionsToCorners", wintypes.BOOL),
]#[repr(C)]
pub struct DML_ROI_ALIGN1_OPERATOR_DESC {
pub InputTensor: *mut core::ffi::c_void,
pub ROITensor: *mut core::ffi::c_void,
pub BatchIndicesTensor: *mut core::ffi::c_void,
pub OutputTensor: *mut core::ffi::c_void,
pub ReductionFunction: i32,
pub InterpolationMode: i32,
pub SpatialScaleX: f32,
pub SpatialScaleY: f32,
pub InputPixelOffset: f32,
pub OutputPixelOffset: f32,
pub OutOfBoundsInputValue: f32,
pub MinimumSamplesPerOutput: u32,
pub MaximumSamplesPerOutput: u32,
pub AlignRegionsToCorners: i32,
}import "golang.org/x/sys/windows"
type DML_ROI_ALIGN1_OPERATOR_DESC struct {
InputTensor uintptr
ROITensor uintptr
BatchIndicesTensor uintptr
OutputTensor uintptr
ReductionFunction int32
InterpolationMode int32
SpatialScaleX float32
SpatialScaleY float32
InputPixelOffset float32
OutputPixelOffset float32
OutOfBoundsInputValue float32
MinimumSamplesPerOutput uint32
MaximumSamplesPerOutput uint32
AlignRegionsToCorners int32
}type
DML_ROI_ALIGN1_OPERATOR_DESC = record
InputTensor: Pointer;
ROITensor: Pointer;
BatchIndicesTensor: Pointer;
OutputTensor: Pointer;
ReductionFunction: Integer;
InterpolationMode: Integer;
SpatialScaleX: Single;
SpatialScaleY: Single;
InputPixelOffset: Single;
OutputPixelOffset: Single;
OutOfBoundsInputValue: Single;
MinimumSamplesPerOutput: DWORD;
MaximumSamplesPerOutput: DWORD;
AlignRegionsToCorners: BOOL;
end;const DML_ROI_ALIGN1_OPERATOR_DESC = extern struct {
InputTensor: ?*anyopaque,
ROITensor: ?*anyopaque,
BatchIndicesTensor: ?*anyopaque,
OutputTensor: ?*anyopaque,
ReductionFunction: i32,
InterpolationMode: i32,
SpatialScaleX: f32,
SpatialScaleY: f32,
InputPixelOffset: f32,
OutputPixelOffset: f32,
OutOfBoundsInputValue: f32,
MinimumSamplesPerOutput: u32,
MaximumSamplesPerOutput: u32,
AlignRegionsToCorners: i32,
};type
DML_ROI_ALIGN1_OPERATOR_DESC {.bycopy.} = object
InputTensor: pointer
ROITensor: pointer
BatchIndicesTensor: pointer
OutputTensor: pointer
ReductionFunction: int32
InterpolationMode: int32
SpatialScaleX: float32
SpatialScaleY: float32
InputPixelOffset: float32
OutputPixelOffset: float32
OutOfBoundsInputValue: float32
MinimumSamplesPerOutput: uint32
MaximumSamplesPerOutput: uint32
AlignRegionsToCorners: int32struct DML_ROI_ALIGN1_OPERATOR_DESC
{
void* InputTensor;
void* ROITensor;
void* BatchIndicesTensor;
void* OutputTensor;
int ReductionFunction;
int InterpolationMode;
float SpatialScaleX;
float SpatialScaleY;
float InputPixelOffset;
float OutputPixelOffset;
float OutOfBoundsInputValue;
uint MinimumSamplesPerOutput;
uint MaximumSamplesPerOutput;
int AlignRegionsToCorners;
}HSP用 定義
HSP3.7/3.8 は構造体機能が無いため4byte整数配列(dim)+peek/poke で操作(32/64bitでサイズ・位置が異なる場合はタブで分割)。IronHSP は NSTRUCT(#defstruct/stdim/->)で32/64bit共通。
; HSP3.7/3.8 は構造体機能が無いため、4byte整数の配列変数で操作します。(x86 レイアウト)
; DML_ROI_ALIGN1_OPERATOR_DESC サイズ: 56 バイト(x86)
dim st, 14 ; 4byte整数×14(構造体サイズ 56 / 4 切り上げ)
; InputTensor : DML_TENSOR_DESC* (+0, 4byte) varptr(st)+0 を基点に操作(4byte:入れ子/配列)
; ROITensor : DML_TENSOR_DESC* (+4, 4byte) varptr(st)+4 を基点に操作(4byte:入れ子/配列)
; BatchIndicesTensor : DML_TENSOR_DESC* (+8, 4byte) varptr(st)+8 を基点に操作(4byte:入れ子/配列)
; OutputTensor : DML_TENSOR_DESC* (+12, 4byte) varptr(st)+12 を基点に操作(4byte:入れ子/配列)
; ReductionFunction : DML_REDUCE_FUNCTION (+16, 4byte) st.4 = 値 / 値 = st.4 (lpoke/lpeek も可)
; InterpolationMode : DML_INTERPOLATION_MODE (+20, 4byte) st.5 = 値 / 値 = st.5 (lpoke/lpeek も可)
; SpatialScaleX : FLOAT (+24, 4byte) st.6 = 値 / 値 = st.6 (lpoke/lpeek も可)
; SpatialScaleY : FLOAT (+28, 4byte) st.7 = 値 / 値 = st.7 (lpoke/lpeek も可)
; InputPixelOffset : FLOAT (+32, 4byte) st.8 = 値 / 値 = st.8 (lpoke/lpeek も可)
; OutputPixelOffset : FLOAT (+36, 4byte) st.9 = 値 / 値 = st.9 (lpoke/lpeek も可)
; OutOfBoundsInputValue : FLOAT (+40, 4byte) st.10 = 値 / 値 = st.10 (lpoke/lpeek も可)
; MinimumSamplesPerOutput : DWORD (+44, 4byte) st.11 = 値 / 値 = st.11 (lpoke/lpeek も可)
; MaximumSamplesPerOutput : DWORD (+48, 4byte) st.12 = 値 / 値 = st.12 (lpoke/lpeek も可)
; AlignRegionsToCorners : BOOL (+52, 4byte) st.13 = 値 / 値 = st.13 (lpoke/lpeek も可)
; ※4byte境界の整数は添字 st.N(N=オフセット/4)で読み書き可。それ以外は peek/poke 系を使用。; HSP3.7/3.8 は構造体機能が無いため、4byte整数の配列変数で操作します。(x64 レイアウト)
; DML_ROI_ALIGN1_OPERATOR_DESC サイズ: 72 バイト(x64)
dim st, 18 ; 4byte整数×18(構造体サイズ 72 / 4 切り上げ)
; InputTensor : DML_TENSOR_DESC* (+0, 8byte) varptr(st)+0 を基点に操作(8byte:入れ子/配列)
; ROITensor : DML_TENSOR_DESC* (+8, 8byte) varptr(st)+8 を基点に操作(8byte:入れ子/配列)
; BatchIndicesTensor : DML_TENSOR_DESC* (+16, 8byte) varptr(st)+16 を基点に操作(8byte:入れ子/配列)
; OutputTensor : DML_TENSOR_DESC* (+24, 8byte) varptr(st)+24 を基点に操作(8byte:入れ子/配列)
; ReductionFunction : DML_REDUCE_FUNCTION (+32, 4byte) st.8 = 値 / 値 = st.8 (lpoke/lpeek も可)
; InterpolationMode : DML_INTERPOLATION_MODE (+36, 4byte) st.9 = 値 / 値 = st.9 (lpoke/lpeek も可)
; SpatialScaleX : FLOAT (+40, 4byte) st.10 = 値 / 値 = st.10 (lpoke/lpeek も可)
; SpatialScaleY : FLOAT (+44, 4byte) st.11 = 値 / 値 = st.11 (lpoke/lpeek も可)
; InputPixelOffset : FLOAT (+48, 4byte) st.12 = 値 / 値 = st.12 (lpoke/lpeek も可)
; OutputPixelOffset : FLOAT (+52, 4byte) st.13 = 値 / 値 = st.13 (lpoke/lpeek も可)
; OutOfBoundsInputValue : FLOAT (+56, 4byte) st.14 = 値 / 値 = st.14 (lpoke/lpeek も可)
; MinimumSamplesPerOutput : DWORD (+60, 4byte) st.15 = 値 / 値 = st.15 (lpoke/lpeek も可)
; MaximumSamplesPerOutput : DWORD (+64, 4byte) st.16 = 値 / 値 = st.16 (lpoke/lpeek も可)
; AlignRegionsToCorners : BOOL (+68, 4byte) st.17 = 値 / 値 = st.17 (lpoke/lpeek も可)
; ※4byte境界の整数は添字 st.N(N=オフセット/4)で読み書き可。それ以外は peek/poke 系を使用。; IronHSP は NSTRUCT(構造体)をサポート。32bit/64bit どちらでも同じコードで動作します。
#defstruct global DML_ROI_ALIGN1_OPERATOR_DESC
#field intptr InputTensor
#field intptr ROITensor
#field intptr BatchIndicesTensor
#field intptr OutputTensor
#field int ReductionFunction
#field int InterpolationMode
#field float SpatialScaleX
#field float SpatialScaleY
#field float InputPixelOffset
#field float OutputPixelOffset
#field float OutOfBoundsInputValue
#field int MinimumSamplesPerOutput
#field int MaximumSamplesPerOutput
#field bool AlignRegionsToCorners
#endstruct
stdim st, DML_ROI_ALIGN1_OPERATOR_DESC ; NSTRUCT 変数を確保
st->ReductionFunction = 100
mes "ReductionFunction=" + st->ReductionFunction