From bee34e637aeb0658db62bf7f2b93555e0cecee79 Mon Sep 17 00:00:00 2001 From: vorozhkog Date: Fri, 29 Nov 2024 04:07:22 +0000 Subject: [PATCH] add async --- dev_requirements.txt | 2 +- local.env | 10 +++++++--- src/convert_sly_to_yolov5.py | 29 +++++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/dev_requirements.txt b/dev_requirements.txt index b3a3959..aff197f 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -1 +1 @@ -supervisely==6.73.162 +git+https://github.com/supervisely/supervisely.git@project_download_async diff --git a/local.env b/local.env index de66cea..47aca03 100644 --- a/local.env +++ b/local.env @@ -1,3 +1,7 @@ -TEAM_ID = 448 -WORKSPACE_ID = 690 -PROJECT_ID = 35637 \ No newline at end of file +# TEAM_ID = 448 +# WORKSPACE_ID = 690 +# PROJECT_ID = 35637 + +TEAM_ID = 431 +WORKSPACE_ID = 1019 +PROJECT_ID = 40721 \ No newline at end of file diff --git a/src/convert_sly_to_yolov5.py b/src/convert_sly_to_yolov5.py index d638ef2..e854764 100644 --- a/src/convert_sly_to_yolov5.py +++ b/src/convert_sly_to_yolov5.py @@ -4,6 +4,8 @@ from dotenv import load_dotenv import supervisely as sly from workflow import Workflow +import asyncio +from tinytimer import Timer # region constants TRAIN_TAG_NAME = "train" @@ -178,8 +180,31 @@ def _add_to_split(image_id, img_name, split_ids, split_image_paths, labels_dir, ) train_count += 1 - api.image.download_paths(dataset.id, train_ids, train_image_paths) - api.image.download_paths(dataset.id, val_ids, val_image_paths) + if api.server_address.startswith("https://"): + semaphore = asyncio.Semaphore(100) + else: + semaphore = None + + ids = train_ids + val_ids + paths = train_image_paths + val_image_paths + + with Timer() as t: + coro = api.image.download_paths_async(ids, paths, semaphore) + loop = sly.utils.get_or_create_event_loop() + if loop.is_running(): + future = asyncio.run_coroutine_threadsafe(coro, loop) + future.result() + else: + loop.run_until_complete(coro) + sly.logger.info( + f"Downloading time: {t.elapsed:.4f} seconds per {len(ids)} images ({t.elapsed/len(ids):.4f} seconds per image)" + ) + # with Timer() as t: + # api.image.download_paths(dataset.id, train_ids, train_image_paths) + # api.image.download_paths(dataset.id, val_ids, val_image_paths) + # sly.logger.info( + # f"Downloading time: {t.elapsed:.4f} seconds per {len(train_ids + val_ids) } images ({t.elapsed/len(train_ids + val_ids):.4f} seconds per image)" + # ) progress.iters_done_report(len(batch)) if unsupported_shapes > 0: