|
| 1 | +# Copyright 2020-2022 OpenDR European Project |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import torch |
| 17 | +import yaml |
| 18 | +from opendr.perception.activity_recognition import X3DLearner |
| 19 | + |
| 20 | +from pytorch_benchmark import benchmark |
| 21 | +import logging |
| 22 | +from typing import List, Union |
| 23 | +from opendr.engine.target import Category |
| 24 | +from opendr.engine.data import Video |
| 25 | + |
| 26 | +logger = logging.getLogger("benchmark") |
| 27 | +logging.basicConfig() |
| 28 | +logger.setLevel("DEBUG") |
| 29 | + |
| 30 | + |
| 31 | +def benchmark_x3d(): |
| 32 | + temp_dir = "./projects/perception/activity_recognition/benchmark/tmp" |
| 33 | + |
| 34 | + num_runs = 100 |
| 35 | + |
| 36 | + # As found in src/opendr/perception/activity_recognition/x3d/hparams |
| 37 | + input_shape = { |
| 38 | + "xs": (3, 4, 160, 160), |
| 39 | + "s": (3, 13, 160, 160), |
| 40 | + "m": (3, 16, 224, 224), |
| 41 | + "l": (3, 16, 312, 312), |
| 42 | + } |
| 43 | + |
| 44 | + # Max power of 2 |
| 45 | + # batch_size = { # RTX2080Ti |
| 46 | + # "xs": 32, |
| 47 | + # "s": 16, |
| 48 | + # "m": 8, |
| 49 | + # "l": 2, |
| 50 | + # } |
| 51 | + # batch_size = { # TX2 |
| 52 | + # "xs": 16, |
| 53 | + # "s": 8, |
| 54 | + # "m": 4, |
| 55 | + # "l": 2, |
| 56 | + # } |
| 57 | + # batch_size = { # Xavier |
| 58 | + # "xs": 32, |
| 59 | + # "s": 16, |
| 60 | + # "m": 8, |
| 61 | + # "l": 2, |
| 62 | + # } |
| 63 | + batch_size = { # CPU - larger batch sizes don't increase throughput |
| 64 | + "xs": 1, |
| 65 | + "s": 1, |
| 66 | + "m": 1, |
| 67 | + "l": 1, |
| 68 | + } |
| 69 | + |
| 70 | + for backbone in ["xs", "s", "m", "l"]: |
| 71 | + print(f"==== Benchmarking X3DLearner ({backbone}) ====") |
| 72 | + |
| 73 | + learner = X3DLearner( |
| 74 | + device="cuda" if torch.cuda.is_available() else "cpu", |
| 75 | + temp_path=temp_dir, |
| 76 | + backbone=backbone, |
| 77 | + ) |
| 78 | + learner.model.eval() |
| 79 | + |
| 80 | + sample = torch.randn( |
| 81 | + batch_size[backbone], *input_shape[backbone] |
| 82 | + ) # (B, C, T, H, W) |
| 83 | + video_samples = [Video(v) for v in sample] |
| 84 | + video_sample = [Video(sample[0])] |
| 85 | + |
| 86 | + def get_device_fn(*args): |
| 87 | + nonlocal learner |
| 88 | + return next(learner.model.parameters()).device |
| 89 | + |
| 90 | + def transfer_to_device_fn( |
| 91 | + sample: Union[torch.Tensor, List[Category], List[Video]], |
| 92 | + device: torch.device, |
| 93 | + ): |
| 94 | + if isinstance(sample, torch.Tensor): |
| 95 | + return sample.to(device=device) |
| 96 | + |
| 97 | + assert isinstance(sample, list) |
| 98 | + |
| 99 | + if isinstance(sample[0], Video): |
| 100 | + # Video.data i a numpy array, which is always on CPU |
| 101 | + return sample |
| 102 | + |
| 103 | + assert isinstance(sample[0], Category) |
| 104 | + return [ |
| 105 | + Category(prediction=s.data, confidence=s.confidence.to(device=device),) |
| 106 | + for s in sample |
| 107 | + ] |
| 108 | + |
| 109 | + print("== Benchmarking learner.infer ==") |
| 110 | + results1 = benchmark( |
| 111 | + model=learner.infer, |
| 112 | + sample=video_samples, |
| 113 | + sample_with_batch_size1=video_sample, |
| 114 | + num_runs=num_runs, |
| 115 | + get_device_fn=get_device_fn, |
| 116 | + transfer_to_device_fn=transfer_to_device_fn, |
| 117 | + batch_size=batch_size[backbone], |
| 118 | + print_fn=print, |
| 119 | + ) |
| 120 | + print(yaml.dump({"learner.infer": results1})) |
| 121 | + |
| 122 | + print("== Benchmarking model directly ==") |
| 123 | + results2 = benchmark(learner.model, sample, num_runs=num_runs, print_fn=print) |
| 124 | + print(yaml.dump({"learner.model.forward": results2})) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + benchmark_x3d() |
0 commit comments