Skip to content

Log assets¶

Comet's assets allow you to log any type of data to the Comet platform, which is useful (and should be used only) for data types that do not have a dedicated logging method (e.g., you should log metrics with log_metrics(), artifacts with log_artifacts(), etc.).

Example of Assets & Artifacts tab
View logged assets in the Comet UI for Comet Tutorial 2

Comet allows you to analyze experiment assets by loading them into Python Panels with custom code.

The following methods can be used to log assets:

The following panel can be used to visualize assets:

In addition all the assets logged to an Experiment can be view in the Single Experiment page tab:

For example, you could...

Log research papers, technical reports, blog posts, project proposals, or other reference documentation, that you have used as inspiration or baseline for the model experimentation.

Log a single asset¶

The example below logs a single .txt file to Comet.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import comet_ml

# Initialize the Comet Experiment
comet_ml.login()
exp = comet_ml.start()

# Create an example .txt file
with open("/tmp/test.txt", "w+") as f:
    f.write("hello world")

# Log the asset to Comet
exp.log_asset(
    file_data="/tmp/test.txt",
    file_name="test",
    step=0,
)

Additionally, you could use the overwrite argument to overwrite any existing asset with the same name or add a metadata argument to log extra information (such as tags) in a JSON format.

Log an asset folder¶

The example below logs a folder named example, with all its content, to Comet.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import comet_ml
import os

# Initialize the Comet Experiment
comet_ml.login()
exp = comet_ml.start()

# Create an example folder
os.makedirs("/tmp/example", exist_ok=True)
with open("/tmp/example/test.json", "w+") as f:
    f.write('{"hello": "world"}')

# Log the asset folder to Comet
exp.log_asset_folder(
    folder="/tmp/example",
    step=0,
    log_file_name=True
)

With the log_file_name argument set to True, Comet logs the file path with each file. Additionally, you could decide to set the recursive argument as True to recursively navigate the folder but this is not necessary for this example.

Display an asset with a Python Panel¶

The example below showcases how to code a Python Panel to display the content of any logged text-based asset for any available experiment.
Please refer to the Python Panel documentation for information on how to create the panel end-to-end.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from comet_ml import API, ui

# Initialize a Comet API object
api = API()

# Add a dropdown to select experiment and asset
experiments = api.get_panel_experiments()
selected_experiment = ui.dropdown('Select an experiment:', experiments)
assets = selected_experiment.get_asset_list(asset_type="all")
selected_asset = ui.dropdown(
    'Select an asset:',
    assets,
    format_func= lambda asset: asset['fileName'],
)

# Display the content of the asset (for text only)
value = selected_experiment.get_asset(selected_asset['assetId'])
try:
    ui.display_text(value.decode("utf8"))
except:
    ui.display("This example panel only supports text display.")

The Python panel created with the example code above enables the user to select the desired experiment and asset from dropdown. Then, the panel automatically displays the content for the selected asset (if supported). We could call this panel "Display text asset".

The screenshot below showcases this functionality for the test.txt file created in the Log a single asset section above.

Example Python Panel loading a text asset
The preview of the custom "Display text asset" panel
Dec. 17, 2024