Threading #2
andrey-churkin
started this conversation in
Ideas
Replies: 1 comment
-
baseline: 00:05:59
import time
from pathlib import Path
import multiprocessing as mp
import numpy as np
import openvino.runtime as ov
from openvino.tools.pot.api.samples.object_detection.data_loader import COCOLoader
from openvino.tools.pot.api.samples.object_detection.metric import MAP
ROOT = Path(__file__).parent.resolve()
DATASET_DIR = ROOT / 'coco2017' / 'val2017'
ANNOTATION_FILE = ROOT / 'coco2017' / 'instances_val2017.json'
def load_model() -> ov.Model:
ir_model_xml = ROOT / 'public' / 'ssd_mobilenet_v1_fpn_coco' / 'FP32' / 'ssd_mobilenet_v1_fpn_coco.xml'
model = ov.Core().read_model(model=ir_model_xml)
return model
def main():
model = load_model()
dataset_config = {
'images_path': f'{DATASET_DIR}/',
'annotation_path': ANNOTATION_FILE,
}
data_source = COCOLoader(dataset_config)
if False:
start_time = time.time()
metric = task(model, data_source, 1)
finish_time = time.time()
total = finish_time - start_time
print('Time:', time.strftime("%H:%M:%S", time.gmtime(total)))
print(f'Metric: {metric}')
exit(0)
p1 = mp.Process(target=task, args=(model, data_source, 4))
p2 = mp.Process(target=task, args=(model, data_source, 4))
start_time = time.time()
p1.start()
p2.start()
p1.join()
p2.join()
finish_time = time.time()
total = finish_time - start_time
print('Time:', time.strftime("%H:%M:%S", time.gmtime(total)))
def task(model: ov.Model, data_source, num_threads: int):
config = {}
if num_threads > 1:
config['AFFINITY'] = 'NUMA'
config['INFERENCE_NUM_THREADS'] = mp.cpu_count() / num_threads
compiled_model = ov.Core().compile_model(model, device_name='CPU', config=config)
output_layer = compiled_model.output(0)
metric = MAP(91, data_source.labels)
for images, labels in data_source:
inputs = np.expand_dims(images, 0)
output = compiled_model([inputs])[output_layer]
metric.update(output, [labels])
return metric.avg_value['MAP'].item()
if __name__ == '__main__':
main() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Beta Was this translation helpful? Give feedback.
All reactions