Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark au #231

Merged
merged 68 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
c7a3017
Add Human Activity Recognition benchmark scripts
LukasHedegaard Feb 11, 2022
c7bc50a
Remove pandas dependency in activity_recognition
LukasHedegaard Feb 14, 2022
6e85dd7
Remove pyav dependency
LukasHedegaard Feb 14, 2022
71960ab
Add decoder option to kinetics loader
LukasHedegaard Feb 14, 2022
dc59eb4
Add script for har benchmark install
LukasHedegaard Feb 14, 2022
1efac6f
Update har-benchmark install script
LukasHedegaard Feb 14, 2022
73a786e
Add variable batch_sizes to har benchmark script
LukasHedegaard Feb 14, 2022
7868885
Add har benchmark RTX2080Ti results
LukasHedegaard Feb 14, 2022
7b09bfe
Update har benchmark scripts
LukasHedegaard Feb 14, 2022
d3168ed
Add xavier benchmark har results
LukasHedegaard Feb 14, 2022
72feb9a
Update har benchmark scripts with CPU batch_sizes
LukasHedegaard Feb 14, 2022
3ddad9e
Add har benchmark CPU results
LukasHedegaard Feb 14, 2022
cffd54f
Fix black lint issue
LukasHedegaard Feb 14, 2022
ff6b82e
Remove obsolete dependency
LukasHedegaard Feb 14, 2022
653da79
Redo cox3d RTX2080Ti benchmark
LukasHedegaard Feb 15, 2022
c59353f
Update har benchmark batch sizes
LukasHedegaard Feb 15, 2022
d556d84
Fix install script env vars
LukasHedegaard Feb 18, 2022
b3f5827
Fix exception handling in kinetics
LukasHedegaard Feb 17, 2022
707acbf
Update Xavier har benchmark
LukasHedegaard Feb 18, 2022
113d892
Add har results on TX2
LukasHedegaard Feb 21, 2022
34bb814
Add batch size for tx2 in har benchmark scripts
LukasHedegaard Feb 21, 2022
76708ca
Add 3D object detection benchmark
iliiliiliili Feb 17, 2022
4e52159
Fix bounding box to str
iliiliiliili Feb 21, 2022
fd9e5c0
Add detection_3d and tracking_2d benchmarks
iliiliiliili Feb 21, 2022
182235f
Fix imports not working in python 3.6
LukasHedegaard Feb 17, 2022
7254a3a
Update file names
iliiliiliili Feb 21, 2022
1d193e7
Update far mot names
iliiliiliili Feb 21, 2022
fbff375
Add AB3DMOT benchmark
iliiliiliili Feb 21, 2022
35f7a2a
Update media tracking 3d
iliiliiliili Feb 21, 2022
7247461
benchmark_stgcn added
negarhdr Feb 22, 2022
1871b12
benchmark_stgcn added
negarhdr Feb 22, 2022
2a7a4aa
benchmark_stgcn added
negarhdr Feb 22, 2022
28209a6
benchmark_stgcn added
negarhdr Feb 22, 2022
0d832fd
benchmark_stgcn added
negarhdr Feb 22, 2022
e49b896
benchmark_stgcn added
negarhdr Feb 22, 2022
96a7b2a
benchmark_stgcn added
negarhdr Feb 22, 2022
2760c27
benchmark_stgcn added
negarhdr Feb 22, 2022
d362abc
benchmark_stgcn modified
negarhdr Feb 22, 2022
04d2b04
benchmark_stgcn modified
negarhdr Feb 22, 2022
3f65ed1
benchmark_stgcn modified
negarhdr Feb 22, 2022
3d0c59c
benchmark_stgcn modified
negarhdr Feb 22, 2022
2f9352b
benchmark_stbln modified
negarhdr Feb 22, 2022
de70a07
benchmark_stbln modified
negarhdr Feb 22, 2022
10c021e
benchmark_stbln modified
negarhdr Feb 22, 2022
547098f
benchmark_stbln modified
negarhdr Feb 22, 2022
4fd2ec4
benchmark_stbln modified
negarhdr Feb 22, 2022
5b72d5f
mcdo is modified
negarhdr Feb 22, 2022
779d60f
mcdo is modified
negarhdr Feb 22, 2022
5f01389
mcdo is modified
negarhdr Feb 22, 2022
092d6fb
mcdo is modified
negarhdr Feb 22, 2022
6a32fa9
mcdo is modified
negarhdr Feb 22, 2022
9b74859
mcdo is modified
negarhdr Feb 22, 2022
d62bb89
Fix style in kinetics
LukasHedegaard Feb 28, 2022
637e5a9
Fix W503
LukasHedegaard Feb 28, 2022
41f497d
code style fixed
negarhdr Mar 1, 2022
6588275
Merge branch 'develop' into benchmark-au
ad-daniel Mar 3, 2022
2a72c21
style fixed
negarhdr Mar 7, 2022
8f32f0e
Merge remote-tracking branch 'origin/benchmark-au' into benchmark-au
negarhdr Mar 7, 2022
e103ac2
Fix style errors
iliiliiliili Mar 7, 2022
9f99a22
Fix kinetics unused torchvision
iliiliiliili Mar 7, 2022
38e7beb
Merge branch 'develop' into benchmark-au
LukasHedegaard Mar 8, 2022
bb21576
Merge branch 'develop' into benchmark-au
LukasHedegaard Mar 14, 2022
361becb
Remove activity recognition results
LukasHedegaard Mar 14, 2022
9dbe98c
Merge branch 'benchmark-au' of https://github.com/opendr-eu/opendr in…
LukasHedegaard Mar 14, 2022
9b9d8ab
Merge branch 'develop' into benchmark-au
ad-daniel Mar 22, 2022
a9819e0
Add av dependency back
LukasHedegaard Mar 28, 2022
e204513
Merge branch 'develop' into benchmark-au
ad-daniel Mar 29, 2022
94faada
Merge branch 'develop' into benchmark-au
iliiliiliili Mar 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions projects/perception/activity_recognition/benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Human Activity Recognition Benchmark
This folder contains a script for benchmarking the inference of Human Activity Recognition learners found at
```python
from opendr.perception.activity_recognition import X3DLearner
from opendr.perception.activity_recognition import CoX3DLearner
```

The script include logging of FLOPS and params for `learner.model`, inference timing, and energy-consumption (NVIDIA Jetson only).

The benchmarking runs twice; Once using `learner.infer` and once using `learner.model.forward`. The results of each are printed accordingly.


## Setup
Please install [`pytorch-benchmark`](https://github.com/LukasHedegaard/pytorch-benchmark):
```bash
pip install pytorch-benchmark
```

## Running the benchmark
X3D
```bash
./benchmark_x3d.py
```

CoX3D
```bash
./benchmark_cox3d.py
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Copyright 2020-2022 OpenDR European Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import torch
import yaml
from opendr.perception.activity_recognition import CoX3DLearner

from pytorch_benchmark import benchmark
import logging
from typing import List, Union
from opendr.engine.target import Category
from opendr.engine.data import Image

logger = logging.getLogger("benchmark")
logging.basicConfig()
logger.setLevel("DEBUG")


def benchmark_cox3d():
temp_dir = "./projects/perception/activity_recognition/benchmark/tmp"

num_runs = 100

# As found in src/opendr/perception/activity_recognition/x3d/hparams
input_shape = {
"xs": (3, 160, 160),
"s": (3, 160, 160),
"m": (3, 224, 224),
"l": (3, 312, 312),
}

# Max power of 2
# batch_size = { # RTX2080Ti
# "xs": 128,
# "s": 64,
# "m": 64,
# "l": 8,
# }
# batch_size = { # TX2
# "xs": 32,
# "s": 16,
# "m": 8,
# "l": 4,
# }
# batch_size = { # Xavier
# "xs": 64,
# "s": 32,
# "m": 16,
# "l": 8,
# }
batch_size = { # CPU - larger batch sizes don't increase throughput
"xs": 1,
"s": 1,
"m": 1,
"l": 1,
}

for backbone in ["s", "m", "l"]:
print(f"==== Benchmarking CoX3DLearner ({backbone}) ====")

learner = CoX3DLearner(
device="cuda" if torch.cuda.is_available() else "cpu",
temp_path=temp_dir,
backbone=backbone,
)

sample = torch.randn(
batch_size[backbone], *input_shape[backbone]
) # (B, C, T, H, W)
image_samples = [Image(v) for v in sample]
image_sample = [Image(sample[0])]

def get_device_fn(*args):
nonlocal learner
return next(learner.model.parameters()).device

def transfer_to_device_fn(
sample: Union[torch.Tensor, List[Category], List[Image]],
device: torch.device,
):
if isinstance(sample, torch.Tensor):
return sample.to(device=device)

assert isinstance(sample, list)

if isinstance(sample[0], Image):
# Image.data i a numpy array, which is always on CPU
return sample

assert isinstance(sample[0], Category)
return [
Category(prediction=s.data, confidence=s.confidence.to(device=device),)
for s in sample
]

print("== Benchmarking learner.infer ==")
results1 = benchmark(
model=learner.infer,
sample=image_samples,
sample_with_batch_size1=image_sample,
num_runs=num_runs,
get_device_fn=get_device_fn,
transfer_to_device_fn=transfer_to_device_fn,
batch_size=batch_size[backbone],
print_fn=print,
)
print(yaml.dump({"learner.infer": results1}))

print("== Benchmarking model directly ==")
results2 = benchmark(learner.model, sample, num_runs=num_runs, print_fn=print)
print(yaml.dump({"learner.model.forward": results2}))


if __name__ == "__main__":
benchmark_cox3d()
128 changes: 128 additions & 0 deletions projects/perception/activity_recognition/benchmark/benchmark_x3d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Copyright 2020-2022 OpenDR European Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import torch
import yaml
from opendr.perception.activity_recognition import X3DLearner

from pytorch_benchmark import benchmark
import logging
from typing import List, Union
from opendr.engine.target import Category
from opendr.engine.data import Video

logger = logging.getLogger("benchmark")
logging.basicConfig()
logger.setLevel("DEBUG")


def benchmark_x3d():
temp_dir = "./projects/perception/activity_recognition/benchmark/tmp"

num_runs = 100

# As found in src/opendr/perception/activity_recognition/x3d/hparams
input_shape = {
"xs": (3, 4, 160, 160),
"s": (3, 13, 160, 160),
"m": (3, 16, 224, 224),
"l": (3, 16, 312, 312),
}

# Max power of 2
# batch_size = { # RTX2080Ti
# "xs": 32,
# "s": 16,
# "m": 8,
# "l": 2,
# }
# batch_size = { # TX2
# "xs": 16,
# "s": 8,
# "m": 4,
# "l": 2,
# }
# batch_size = { # Xavier
# "xs": 32,
# "s": 16,
# "m": 8,
# "l": 2,
# }
batch_size = { # CPU - larger batch sizes don't increase throughput
"xs": 1,
"s": 1,
"m": 1,
"l": 1,
}

for backbone in ["xs", "s", "m", "l"]:
print(f"==== Benchmarking X3DLearner ({backbone}) ====")

learner = X3DLearner(
device="cuda" if torch.cuda.is_available() else "cpu",
temp_path=temp_dir,
backbone=backbone,
)
learner.model.eval()

sample = torch.randn(
batch_size[backbone], *input_shape[backbone]
) # (B, C, T, H, W)
video_samples = [Video(v) for v in sample]
video_sample = [Video(sample[0])]

def get_device_fn(*args):
nonlocal learner
return next(learner.model.parameters()).device

def transfer_to_device_fn(
sample: Union[torch.Tensor, List[Category], List[Video]],
device: torch.device,
):
if isinstance(sample, torch.Tensor):
return sample.to(device=device)

assert isinstance(sample, list)

if isinstance(sample[0], Video):
# Video.data i a numpy array, which is always on CPU
return sample

assert isinstance(sample[0], Category)
return [
Category(prediction=s.data, confidence=s.confidence.to(device=device),)
for s in sample
]

print("== Benchmarking learner.infer ==")
results1 = benchmark(
model=learner.infer,
sample=video_samples,
sample_with_batch_size1=video_sample,
num_runs=num_runs,
get_device_fn=get_device_fn,
transfer_to_device_fn=transfer_to_device_fn,
batch_size=batch_size[backbone],
print_fn=print,
)
print(yaml.dump({"learner.infer": results1}))

print("== Benchmarking model directly ==")
results2 = benchmark(learner.model, sample, num_runs=num_runs, print_fn=print)
print(yaml.dump({"learner.model.forward": results2}))


if __name__ == "__main__":
benchmark_x3d()
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

conda create --name opendr python=3.8 -y
conda activate opendr
conda env config vars set OPENDR_HOME=$PWD
LukasHedegaard marked this conversation as resolved.
Show resolved Hide resolved
conda env config vars set PYTHONPATH=$PWD/src:$PYTHONPATH

# Reactivate env to let env vars take effect
conda deactivate
conda activate opendr

pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 -f https://download.pytorch.org/whl/torch_stable.html
pip install pytorch_lightning==1.2.3 onnxruntime==1.3.0 joblib>=1.0.1 pytorch_benchmark
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytorch-benchmark >= 0.2
Loading