2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GPUのメモリが足りない時のちょっとした工夫

Last updated at Posted at 2019-01-08

画像処理でGPUを使ったライブラリを動かしたとき、一度に全部データを渡すと落ちちゃう場合がありました。
こんな場合には入力を分割して処理を回しますが、なるべく他の処理を増やしたくありません。
こういった場合の対処方法をメモします。

入出力numpyの関数gpu_process(imgs)を使ってgpu上で画像を処理し、その結果が返されるものとします。

print(imgs.shape) # (NUM_SAMPLE,WIDTH,HEIGHT,3)

outputs = gpu_process(imgs)  # ここで落ちる

print(outputs.shape) # (NUM_SAMPLE,WIDTH,HEIGHT,3)

これを下のようにnp.array_split()で分割して渡し、得られたarrayのリストをnp.concatenate()してnumpyに戻します。

print(imgs.shape) # (NUM_SAMPLE,WIDTH,HEIGHT,3)

NUM_SPLIT = 10 # ←増やすほどGPUの処理を軽く出来る
imgs_list = np.array_split(imgs, NUM_SPLIT)
outputs = np.concatenate(
    [gpu_process(imgs) for imgs in imgs_list],
    axis=0,
)

print(outputs.shape) # (NUM_SAMPLE,WIDTH,HEIGHT,3)
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?