In case it’s of use for the community, I am sharing how I plugged the experiment identifier for further logging.
I assume that I am running a tao training. First, I create a mechanism to capture the output displayed from TAO training:
import sys
from io import StringIO
from contextlib import contextmanager
@contextmanager
def capture_output_and_display():
class Tee(StringIO):
def write(self, string):
StringIO.write(self, string)
sys.__stdout__.write(string)
old_stdout = sys.stdout
capture_buffer = Tee()
sys.stdout = capture_buffer
yield capture_buffer
sys.stdout = old_stdout
Then, I run the TAO training with the context above in mind:
with capture_output_and_display() as output:
!tao model faster_rcnn train --gpu_index $GPU_INDEX -e $SPECS_DIR/default_spec_resnet18.txt -r /workspace/tao-experiments/faster_rcnn
This will capture all the output.
Once the outputs are captured, I parse the experiment identifier and I dictionnarize the specs file and log them into clearML:
from log_clearml import logthem
captured = output.getvalue()
logthem(captured, 'specs/default_spec_resnet18.txt')
The code for logthem()
is below:
import re
from clearml import Task
def parse_block(lines, idx):
block = {}
while idx < len(lines):
line = lines[idx].strip()
if line.endswith("{"):
key = line[:-1].strip()
idx, value = parse_block(lines, idx+1)
if key in block:
if isinstance(block[key], list):
block[key].append(value)
else:
block[key] = [block[key], value]
else:
block[key] = value
elif line == "}":
return idx, block
elif ":" in line:
key, value = [x.strip() for x in line.split(":", 1)]
# Simple type conversion
if value.lower() == "true":
value = True
elif value.lower() == "false":
value = False
else:
try:
value = float(value) if '.' in value else int(value)
except ValueError:
value = value.strip('\'"')
block[key] = value
idx += 1
return idx, block
def parse_text(text):
lines = [line.strip() for line in text.splitlines() if line.strip() and not line.strip().startswith("#")]
_, result = parse_block(lines, 0)
return result
def read_file(filename):
with open(filename, 'r') as f:
return f.read()
def logthem(captured, specs):
match = re.search(r'ClearML Task: created new task id=(\w+)', captured)
if match:
task_id = match.group(1)
print(f"ClearML Task ID: {task_id}")
else:
print("Task ID not found in the captured output.")
clearml_task = Task.get_task(task_id=task_id)
text = read_file(specs)
parsed_dict = parse_text(text)
clearml_task.connect(parsed_dict)