-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add resnet 50 v1 in pytorch framework (#52)
* add pytorch runner * wip * wip * add proper preprocessing, clean up for PR * cleanup * make 'NHWC' a Default Argument Value for dataset class and imagenet class * add setting the threads number * Framework name messages Co-authored-by: Jan Grzybek <52271793+jangrzybek@users.noreply.github.com>
- Loading branch information
1 parent
bdd7ef8
commit beb50cc
Showing
10 changed files
with
151 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import argparse | ||
|
||
import numpy as np | ||
|
||
from utils.benchmark import run_model | ||
from utils.imagenet import ImageNet | ||
from utils.pytorch import PyTorchRunner | ||
|
||
PYTORCH_MODEL_NAME = 'resnet50' | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser(description="Run ResNet-50 v1 model.") | ||
parser.add_argument("-p", "--precision", | ||
type=str, choices=["fp32"], required=True, | ||
help="precision of the model provided") | ||
parser.add_argument("-b", "--batch_size", | ||
type=int, default=1, | ||
help="batch size to feed the model with") | ||
parser.add_argument("--timeout", | ||
type=float, default=60.0, | ||
help="timeout in seconds") | ||
parser.add_argument("--num_runs", | ||
type=int, | ||
help="number of passes through network to execute") | ||
parser.add_argument("--images_path", | ||
type=str, | ||
help="path to directory with ImageNet validation images") | ||
parser.add_argument("--labels_path", | ||
type=str, | ||
help="path to file with validation labels") | ||
return parser.parse_args() | ||
|
||
|
||
def run_torch_fp32(batch_size, num_of_runs, timeout, images_path, labels_path): | ||
|
||
def run_single_pass(pytorch_runner, imagenet): | ||
shape = (224, 224) | ||
output = pytorch_runner.run(imagenet.get_input_array(shape)) | ||
|
||
for i in range(batch_size): | ||
imagenet.submit_predictions( | ||
i, | ||
imagenet.extract_top1(output[i]), | ||
imagenet.extract_top5(output[i]) | ||
) | ||
|
||
dataset = ImageNet(batch_size, "RGB", images_path, labels_path, | ||
pre_processing='Inception', is1001classes=False, order='NCHW') | ||
runner = PyTorchRunner(PYTORCH_MODEL_NAME) | ||
|
||
return run_model(run_single_pass, runner, dataset, batch_size, num_of_runs, timeout) | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
if args.precision == "fp32": | ||
run_torch_fp32( | ||
args.batch_size, args.num_runs, args.timeout, args.images_path, args.labels_path | ||
) | ||
else: | ||
assert False, f"Behaviour undefined for precision {args.precision}" | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import torchvision | ||
import torch | ||
import utils.misc as utils | ||
import time | ||
import utils.benchmark as bench_utils | ||
import numpy as np | ||
import sys | ||
|
||
|
||
class PyTorchRunner: | ||
""" | ||
A class providing facilities to run PyTorch model (as pretrained torchvision model). | ||
""" | ||
|
||
def __init__(self, model: str): | ||
|
||
torch.set_num_threads(bench_utils.get_intra_op_parallelism_threads()) | ||
|
||
if model not in torchvision.models.__dict__: | ||
utils.print_goodbye_message_and_die( | ||
f"{model} not supported by torchvision!") | ||
|
||
self.__model = torchvision.models.__dict__[model](pretrained=True) | ||
self.__model.eval() | ||
self.__warm_up_run_latency = 0.0 | ||
self.__total_inference_time = 0.0 | ||
self.__times_invoked = 0 | ||
|
||
print("\nRunning with PyTorch\n") | ||
|
||
def run(self, input): | ||
""" | ||
A function assigning values to input tensor, executing single pass over the network, measuring the time needed | ||
and finally returning the output. | ||
:return: dict, output dictionary with tensor names and corresponding output | ||
""" | ||
|
||
input_tensor = torch.from_numpy(input) | ||
|
||
with torch.no_grad(): | ||
|
||
start = time.time() | ||
output_tensor = self.__model(input_tensor) | ||
finish = time.time() | ||
output_tensor = output_tensor.detach().numpy() | ||
|
||
self.__total_inference_time += finish - start | ||
if self.__times_invoked == 0: | ||
self.__warm_up_run_latency += finish - start | ||
self.__times_invoked += 1 | ||
|
||
return output_tensor | ||
|
||
def print_performance_metrics(self, batch_size): | ||
perf = bench_utils.print_performance_metrics( | ||
self.__warm_up_run_latency, self.__total_inference_time, self.__times_invoked, batch_size) | ||
return perf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters