複数のy1:y2、x1:x2を持つnumpy配列の複数のフレームをスライスします

Aug 19 2020

複数のフレーム(multiple_frames)のnumpy配列があり、各フレームの高さと幅を異なるy1、y2、x1、x2でスライスして、各フレームに「1」の正方形を描画したいと思います。(slice_yyxx)はnumpy配列であり、フレームごとにy1、y2、x1、x2の配列が1つ含まれています。

slice_yyxx = np.array(slice_yyxx).astype(int)
nbr_frame = slice_yyxx.shape[0]

multiple_frames = np.zeros(shape=(nbr_frame, target_shape[0], target_shape[1], target_shape[2]))
print(multiple_frames.shape)
# (5, 384, 640, 1)

print(slice_yyxx)
# Value ok

print(slice_yyxx.shape)
# (5, 4)
# Then 5 array of coord like [y1, y2, x1, x2] for slice each frames

print(slice_yyxx.dtype)
# np.int64

multiple_frames[:, slice_yyxx[:,0]:slice_yyxx[:,1], slice_yyxx[:,2]:slice_yyxx[:,3]] = 1
# ERROR: TypeError: only integer scalar arrays can be converted to a scalar index

回答

1 MadPhysicist Aug 20 2020 at 17:52

ここでの本当の問題は、任意のスライスを、ループせずに複数の次元で使用できるものに変換する方法です。トリックは、派手なインデックス付け、、、arangeおよびの巧妙な組み合わせを使用することだと思いrepeatます。

目標は、各ディメンションに対応する行と列のインデックスの配列を作成することです。視覚化が容易な単純なケースを考えてみましょう。3x3行列の3フレームセットで、左上と右下の2x2サブ配列を最初の2つのフレームに割り当て、全体を最後のフレームに割り当てます。 :

multi_array = np.zeros((3, 3, 3))
slice_rrcc = np.array([[0, 2, 0, 2], [1, 3, 1, 3], [0, 3, 0, 3]])

それぞれに一致するインデックスと、サイズと形状を考えてみましょう。

nframes = slice_rrcc.shape[0]                       # 3
nrows = np.diff(slice_rrcc[:, :2], axis=1).ravel()  # [2, 2, 3]
ncols = np.diff(slice_rrcc[:, 2:], axis=1).ravel()  # [2, 2, 3]
sizes = nrows * ncols                               # [4, 4, 9]

割り当てを行うには、次の派手なインデックスが必要です。

frame_index = np.array([0, 0, 0, 0,   1, 1, 1, 1,   2, 2, 2, 2, 2, 2, 2, 2, 2])
row_index   = np.array([0, 0, 1, 1,   1, 1, 2, 2,   0, 0, 0, 1, 1, 1, 2, 2, 2])
col_index   = np.array([0, 1, 0, 1,   1, 2, 1, 2,   0, 1, 2, 0, 1, 2, 0, 1, 2])

我々は、配列を得ることができる場合frame_indexrow_indexおよびcol_index次のように、我々は、各セグメントのデータを設定することができます。

multi_array[frame_index, row_index, col_index] = 1

frame_index インデックスは簡単に取得できます。

frame_index = np.repeat(np.arange(nframes), sizes)

row_indexもう少し手間がかかります。nrows個々のフレームごとに一連のインデックスを生成し、それらをncols何度も繰り返す必要があります。これを行うには、連続範囲を生成し、減算を使用して各フレームでカウントを再開します。

row_range = np.arange(nrows.sum())
row_offsets = np.zeros_like(row_range)
row_offsets[np.cumsum(nrows[:-1])] = nrows[:-1]
row_index = row_range - np.cumsum(row_offsets) + np.repeat(slice_rrcc[:, 0], nrows)
segments = np.repeat(ncols, nrows)
row_index = np.repeat(row_index, segments)

col_indexそれでもささいなことではありません。正しいオフセットで各行のシーケンスを生成し、それを各行、次に各フレームに対してチャンクで繰り返す必要があります。アプローチはの場合と似ていrow_indexますが、順序を正しくするための追加のファンシーインデックスがあります。

col_index_index = np.arange(sizes.sum())
col_index_resets = np.cumsum(segments[:-1])
col_index_offsets = np.zeros_like(col_index_index)
col_index_offsets[col_index_resets] = segments[:-1]
col_index_offsets[np.cumsum(sizes[:-1])] -= ncols[:-1]
col_index_index -= np.cumsum(col_index_offsets)

col_range = np.arange(ncols.sum())
col_offsets = np.zeros_like(col_range)
col_offsets[np.cumsum(ncols[:-1])] = ncols[:-1]
col_index = col_range - np.cumsum(col_offsets) + np.repeat(slice_rrcc[:, 2], ncols)
col_index = col_index[col_index_index]

この定式化を使用すると、ステップアップしてフレームごとに異なる値を指定することもできます。values = [1, 2, 3]私の例のフレームに割り当てたい場合は、

multi_array[frame_index, row_index, col_index] = np.repeat(values, sizes)

これを行うためのより効率的な方法があるかどうかを確認します。私が尋ねた一部はここにあります。

基準

ループとnframesin {10、100、1000}のベクトル化されたソリューション、およびmulti_arrayinの幅と高さの比較{100, 1000, 10000}

def set_slices_loop(arr, slice_rrcc):
    for a, s in zip(arr, slice_rrcc):
        a[s[0]:s[1], s[2]:s[3]] = 1

np.random.seed(0xABCDEF)
for nframes in [10, 100, 1000]:
    for dim in [10, 32, 100]:
        print(f'Size = {nframes}x{dim}x{dim}')
        arr = np.zeros((nframes, dim, dim), dtype=int)
        slice = np.zeros((nframes, 4), dtype=int)
        slice[:, ::2] = np.random.randint(0, dim - 1, size=(nframes, 2))
        slice[:, 1::2] = np.random.randint(slice[:, ::2] + 1, dim, size=(nframes, 2))
        %timeit set_slices_loop(arr, slice)
        arr[:] = 0
        %timeit set_slices(arr, slice)

非常に多数のフレームと小さなフレームサイズを除いて、結果は圧倒的にループを支持しています。ほとんどの「通常の」ケースは、ループを使用すると桁違いに高速になります。

ループ

        |          Dimension          |
        |   100   |   1000  |  10000  |
--------+---------+---------+---------+
F    10 | 33.8 µs | 35.8 µs | 43.4 µs |
r  -----+---------+---------+---------+
a   100 |  310 µs |  331 µs |  401 µs |
m  -----+---------+---------+---------+
e  1000 | 3.09 ms | 3.31 ms | 4.27 ms |
--------+---------+---------+---------+

ベクトル化

        |          Dimension          |
        |   100   |   1000  |  10000  |
--------+---------+---------+---------+
F    10 |  225 µs |  266 µs |  545 µs |
r  -----+---------+---------+---------+
a   100 |  312 µs |  627 µs | 4.11 ms |
m  -----+---------+---------+---------+
e  1000 | 1.07 ms | 4.63 ms | 48.5 ms |
--------+---------+---------+---------+

TL; DR

実行できますが、推奨されません。

def set_slices(arr, slice_rrcc, value):
    nframes = slice_rrcc.shape[0]
    nrows = np.diff(slice_rrcc[:, :2], axis=1).ravel()
    ncols = np.diff(slice_rrcc[:, 2:], axis=1).ravel()
    sizes = nrows * ncols

    segments = np.repeat(ncols, nrows)

    frame_index = np.repeat(np.arange(nframes), sizes)

    row_range = np.arange(nrows.sum())
    row_offsets = np.zeros_like(row_range)
    row_offsets[np.cumsum(nrows[:-1])] = nrows[:-1]
    row_index = row_range - np.cumsum(row_offsets) + np.repeat(slice_rrcc[:, 0], nrows)
    row_index = np.repeat(row_index, segments)

    col_index_index = np.arange(sizes.sum())
    col_index_resets = np.cumsum(segments[:-1])
    col_index_offsets = np.zeros_like(col_index_index)
    col_index_offsets[col_index_resets] = segments[:-1]
    col_index_offsets[np.cumsum(sizes[:-1])] -= ncols[:-1]
    col_index_index -= np.cumsum(col_index_offsets)

    col_range = np.arange(ncols.sum())
    col_offsets = np.zeros_like(col_range)
    col_offsets[np.cumsum(ncols[:-1])] = ncols[:-1]
    col_index = col_range - np.cumsum(col_offsets) + np.repeat(slice_rrcc[:, 2], ncols)
    col_index = col_index[col_index_index]

    if values.size == 1:
        arr[frame_index, row_index, col_index] = value
    else:
        arr[frame_index, row_index, col_index] = np.repeat(values, sizes)
1 Divakar Aug 20 2020 at 22:22

これは、benchit提案されたソリューションをベンチマークするためのパッケージ(一緒にパッケージ化されたベンチマークツールはほとんどありません。免責事項:私はその作成者です)を使用したベンチマーク投稿です。

私たちは、ベンチマークをしているset_slicesと@Mad物理学者のSOLNからarr[frame_index, row_index, col_index] = 1set_slices_loopランタイムを取得するために変更なし(sec)

np.random.seed(0xABCDEF)
in_ = {}
for nframes in [10, 100, 1000]:
    for dim in [10, 32, 100]:
        arr = np.zeros((nframes, dim, dim), dtype=int)
        slice = np.zeros((nframes, 4), dtype=int)
        slice[:, ::2] = np.random.randint(0, dim - 1, size=(nframes, 2))
        slice[:, 1::2] = np.random.randint(slice[:, ::2] + 1, dim, size=(nframes, 2))
        in_[(nframes, dim)] = [arr, slice] 
    
import benchit
funcs = [set_slices, set_slices_loop]
t = benchit.timings(funcs, in_, input_name=['NumFrames', 'Dim'], multivar=True)
t.plot(sp_argID=1, logx=True, save='timings.png')