Skip to content

Instantly share code, notes, and snippets.

@RogerioLS
Created February 21, 2025 20:45
Show Gist options
  • Save RogerioLS/a147915d05fb2d3461c3e3f0b048fec3 to your computer and use it in GitHub Desktop.
Save RogerioLS/a147915d05fb2d3461c3e3f0b048fec3 to your computer and use it in GitHub Desktop.
Solution on how to work around the issue with logging mlflow artifacts with s3.

Hello everyone, I recently came across this problem where MLflow artifacts were not registered in S3 even though I provided the artifact address. I saw that some colleagues were also experiencing this, so I decided to share the code that helped me solve this problem. It is very simple. Here is the code.

import mlflow
import mlflow.pytorch
from ultralytics import YOLO
import os
import time

os.environ['WANDB_DISABLED'] = 'true'

# Configurações do AWS
os.environ['AWS_ACCESS_KEY_ID'] = ""
os.environ['AWS_SECRET_ACCESS_KEY'] = ""
os.environ['AWS_DEFAULT_REGION'] = ''

EXPERIMENT_NAME = "YOLOv8 Full Parameter Tracking"
os.environ["MLFLOW_TRACKING_URI"] = "http://ec2-"
mlflow.set_experiment(EXPERIMENT_NAME)

experiment = mlflow.get_experiment_by_name(EXPERIMENT_NAME)
print("experiment_id:", experiment.experiment_id)
print("artifact_location:", experiment.artifact_location)
print("lifecycle_stage:", experiment.lifecycle_stage)

try:
    with mlflow.start_run(experiment_id=experiment.experiment_id, run_name="") as run:
        model = YOLO('yolov8x.pt')

        # Define as tags para o experimento
        tags = {"Model": "Yolov8", "User": "",
                "TCC": "", "Approach": "best_model"}
        mlflow.set_tags(tags)

        # Loga o modelo treinado
        mlflow.pytorch.log_model(model, "model")

        # Treina o modelo YOLOv8
        results = model.train(
            data='',  # Caminho do arquivo YAML do dataset
            epochs=100,
            batch=16,
            imgsz=640
        )

        # Valida o modelo
        val_results = model.val('')

except mlflow.exceptions.MlflowException as e:
    print(f"Erro no MLflow: {str(e)}")
except Exception as e:
    print(f"Erro inesperado: {str(e)}")

print("Training completed and fully tracked with MLflow.")

Another detail is that for my architecture I am using EC2 for tracking. I hope this helps,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment