From d63f3b5ca7eb53febf12db5ce4f03f25b67a7c0a Mon Sep 17 00:00:00 2001 From: allegroai <> Date: Sun, 26 Jun 2022 18:19:50 +0300 Subject: [PATCH] Fix taking artifacts from non-completed tasks might fail --- examples/reporting/artifacts.py | 81 ++++++++++--------- examples/reporting/artifacts_retrieval.py | 6 +- examples/reporting/using_artifacts_example.py | 4 +- 3 files changed, 50 insertions(+), 41 deletions(-) diff --git a/examples/reporting/artifacts.py b/examples/reporting/artifacts.py index 9cc59185..1c12acd4 100644 --- a/examples/reporting/artifacts.py +++ b/examples/reporting/artifacts.py @@ -7,45 +7,50 @@ from PIL import Image from clearml import Task -# Connecting ClearML with the current process, -# from here on everything is logged automatically -task = Task.init(project_name='examples', task_name='Artifacts example') +def main(): + # Connecting ClearML with the current process, + # from here on everything is logged automatically + task = Task.init(project_name='examples', task_name='Artifacts example') -df = pd.DataFrame( - { - 'num_legs': [2, 4, 8, 0], - 'num_wings': [2, 0, 0, 0], - 'num_specimen_seen': [10, 2, 1, 8] - }, - index=['falcon', 'dog', 'spider', 'fish'] -) + df = pd.DataFrame( + { + 'num_legs': [2, 4, 8, 0], + 'num_wings': [2, 0, 0, 0], + 'num_specimen_seen': [10, 2, 1, 8] + }, + index=['falcon', 'dog', 'spider', 'fish'] + ) -# Register Pandas object as artifact to watch -# (it will be monitored in the background and automatically synced and uploaded) -task.register_artifact('train', df, metadata={'counting': 'legs', 'max legs': 69}) -# change the artifact object -df.sample(frac=0.5, replace=True, random_state=1) -# or access it from anywhere using the Task's get_registered_artifacts() -Task.current_task().get_registered_artifacts()['train'].sample(frac=0.5, replace=True, random_state=1) + # Register Pandas object as artifact to watch + # (it will be monitored in the background and automatically synced and uploaded) + task.register_artifact('train', df, metadata={'counting': 'legs', 'max legs': 69}) + # change the artifact object + df.sample(frac=0.5, replace=True, random_state=1) + # or access it from anywhere using the Task's get_registered_artifacts() + Task.current_task().get_registered_artifacts()['train'].sample(frac=0.5, replace=True, random_state=1) -# add and upload pandas.DataFrame (onetime snapshot of the object) -task.upload_artifact('Pandas', artifact_object=df) -# add and upload local file artifact -task.upload_artifact('local file', artifact_object=os.path.join('data_samples', 'dancing.jpg')) -# add and upload dictionary stored as JSON) -task.upload_artifact('dictionary', df.to_dict()) -# add and upload Numpy Object (stored as .npz file) -task.upload_artifact('Numpy Eye', np.eye(100, 100)) -# add and upload Image (stored as .png file) -im = Image.open(os.path.join('data_samples', 'dancing.jpg')) -task.upload_artifact('pillow_image', im) -# add and upload a folder, artifact_object should be the folder path -task.upload_artifact('local folder', artifact_object=os.path.join('data_samples')) -# add and upload a wildcard -task.upload_artifact('wildcard jpegs', artifact_object=os.path.join('data_samples', '*.jpg')) -# do something here -sleep(1.) -print(df) + # add and upload pandas.DataFrame (onetime snapshot of the object) + task.upload_artifact('Pandas', artifact_object=df) + # add and upload local file artifact + task.upload_artifact('local file', artifact_object=os.path.join('data_samples', 'dancing.jpg')) + # add and upload dictionary stored as JSON) + task.upload_artifact('dictionary', df.to_dict()) + # add and upload Numpy Object (stored as .npz file) + task.upload_artifact('Numpy Eye', np.eye(100, 100)) + # add and upload Image (stored as .png file) + im = Image.open(os.path.join('data_samples', 'dancing.jpg')) + task.upload_artifact('pillow_image', im) + # add and upload a folder, artifact_object should be the folder path + task.upload_artifact('local folder', artifact_object=os.path.join('data_samples')) + # add and upload a wildcard + task.upload_artifact('wildcard jpegs', artifact_object=os.path.join('data_samples', '*.jpg')) + # do something here + sleep(1.) + print(df) -# we are done -print('Done') + # we are done + print('Done') + + +if __name__ == '__main__': + main() diff --git a/examples/reporting/artifacts_retrieval.py b/examples/reporting/artifacts_retrieval.py index 79d34e99..0d21be4e 100644 --- a/examples/reporting/artifacts_retrieval.py +++ b/examples/reporting/artifacts_retrieval.py @@ -8,7 +8,11 @@ from clearml import Task def main(): # Getting the task we want to get the artifacts from - artifacts_task = Task.get_task(project_name='examples', task_name='Artifacts example') + artifacts_task = Task.get_task( + project_name='examples', + task_name='Artifacts example', + task_filter={'status': ['completed']} + ) # getting the numpy object back numpy_artifact = artifacts_task.artifacts['Numpy Eye'].get() diff --git a/examples/reporting/using_artifacts_example.py b/examples/reporting/using_artifacts_example.py index d1a7a73c..31fe6050 100644 --- a/examples/reporting/using_artifacts_example.py +++ b/examples/reporting/using_artifacts_example.py @@ -5,7 +5,6 @@ Upload artifacts from a Task, and then a different Task can access and utilize t from clearml import Task from time import sleep - task1 = Task.init(project_name='examples', task_name='Create artifact') # upload data file to the initialized task, inputting a name and file location task1.upload_artifact(name='data file', artifact_object='data_samples/sample.json') @@ -15,7 +14,8 @@ task1.close() # initialize another task to use some other task's artifacts task2 = Task.init(project_name='examples', task_name='Use artifact from other task') # get instance of Task that created artifact (task1), using Task's project and name. You could also use its ID number. -preprocess_task = Task.get_task(project_name='examples', task_name='Create artifact') +preprocess_task = Task.get_task(project_name='examples', task_name='Create artifact', + task_filter={'status': ['completed']}) # access artifact from task1, using the artifact's name # get_local_copy() caches the files for later use and returns a path to the cached file local_json = preprocess_task.artifacts['data file'].get_local_copy()