Skip to content

faceboxesのbatch処理を追加#4

Merged
yujidn merged 12 commits into
masterfrom
feature/batch_process
Feb 18, 2022
Merged

faceboxesのbatch処理を追加#4
yujidn merged 12 commits into
masterfrom
feature/batch_process

Conversation

@yujidn
Copy link
Copy Markdown

@yujidn yujidn commented Dec 8, 2021

batch処理を追加
1000枚あたりの処理時間が、
get_faceboxes(): 12.5s
get_batch_faceboxes(): 1.67s
となる(約10分の1

動作確認

各画像での

  • boundingbox座標が0.01以上のずれがない(画像座標)
  • confの値が0.0001以上のずれがない
    ことを確認している

image


im_height, im_width, im_ch = images[0].shape
scale = torch.Tensor([im_width, im_height, im_width, im_height])
images = np.array(images).reshape(len(images), im_height, im_width, im_ch)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

リスト内部の画像サイズが違う場合はここでValueErrorが発生する

image_inds = thres_tuple[0].data.cpu().numpy()
boxes_inds = thres_tuple[1].data.cpu().numpy()

# ここgpu化しなくても処理時間が支配的ではないのでcpuでやる
Copy link
Copy Markdown
Author

@yujidn yujidn Dec 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1000枚画像相手だと全体が1000ms ~ 1500msに対して30~50ms程度だった
(gpuでやれるならgpuでやりたい

@yujidn yujidn changed the title Feature/batch process faceboxesのbatch処理を追加 Dec 9, 2021
# 以降gpu上の画像は使わないので解放する
# 速度に影響なさそうなのでループごとに実行
del imgs
torch.cuda.empty_cache()
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://pytorch.org/docs/stable/notes/cuda.html#memory-management

memory_allocatedとmemory_reserved があり
del tensor -> 該当tensorが使っていた分のメモリが解放されてmemory_allocatedが増える
empty_cache -> pytorchがreservedしているmemoryのうち使っていない分が解放される

model.load_state_dict(pretrained_dict, strict=False)
return model

@torch.no_grad()
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://pytorch.org/docs/stable/generated/torch.no_grad.html
with句 または デコレータをつけることで内部の勾配計算をなくしてメモリ節約

new_result = (confs, [b * ratios for b in boxes])
new_results.append(new_result)

return new_results
Copy link
Copy Markdown
Author

@yujidn yujidn Dec 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実行時にリサイズし、結果をオリジナルスケールに直す処理を追加

  • オリジナル画像 -> get_faceboxes
  • オリジナル画像s -> get_batch_faceboxes_with_resize -> resized_images -> get_batch_faceboxes

この2パターンを比較すると、最大50ピクセルほどずれる(実装ミスあるかも

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yujidn yujidn merged commit 29cd043 into master Feb 18, 2022
@yujidn yujidn deleted the feature/batch_process branch February 18, 2022 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant