Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

dagshub_yolo_cb.py 1.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  1. import mlflow
  2. import ultralytics
  3. import os
  4. class CustomYOLO(mlflow.pyfunc.PythonModel):
  5. def load_context(self, context):
  6. self.model = ultralytics.YOLO(context.artifacts['path'], task='segment')
  7. def predict(self, context, img):
  8. preds = self.model(img)
  9. return preds
  10. def custom_on_train_end(trainer):
  11. """Called at end of train loop to log model artifact info."""
  12. if mlflow:
  13. experiment_name = os.environ.get('MLFLOW_EXPERIMENT_NAME') or trainer.args.project or '/Shared/YOLOv8'
  14. mlflow.log_artifact(trainer.save_dir)
  15. mlflow.pyfunc.log_model(artifact_path=experiment_name,
  16. artifacts={'path': str(trainer.best)},
  17. python_model=CustomYOLO())
  18. def custom_callbacks_fn(instance):
  19. from ultralytics.utils.callbacks.mlflow import callbacks as mlflow_cb
  20. mlflow_cb['on_train_end'] = custom_on_train_end
  21. for k, v in mlflow_cb.items():
  22. if v not in instance.callbacks[k]: # prevent duplicate callbacks addition
  23. instance.callbacks[k].append(v)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...