forked from LambdaLabsML/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loggers.py
169 lines (125 loc) · 5.36 KB
/
loggers.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env.
import wandb
class WandbLogger():
def __init__(self) -> None:
pass
def start(self, project):
self.run = wandb.init(project=project)
def log_cfg(self, cfg):
wandb.config.update(cfg)
def log(self, thing, step):
wandb.log(thing, step=step)
def log_metric(self, name, thing):
wandb.log({name: thing})
def log_image(self, thing):
wandb.log({k: wandb.Image(v) for k, v in thing.items()})
def log_dataset_ref(self, name, ref):
artifact = wandb.Artifact(name, type="dataset")
artifact.add_reference("file://" + ref)
self.run.use_artifact(artifact)
def log_model_weights(self, name, weights):
artifact = wandb.Artifact(name, type="model")
artifact.add_file(weights)
self.run.log_artifact(artifact)
def log_confusion_matrix(self, key, target, pred, label):
cm = wandb.plot.confusion_matrix(y_true=target.numpy(), preds=pred.numpy(), class_names=label)
wandb.log({key: cm})
class CometLogger():
def __init__(self) -> None:
pass
def start(self, project):
import comet_ml as comet
self.experiment = comet.Experiment(project_name=project)
def log_cfg(self, cfg):
self.experiment.log_parameters(cfg)
def log(self, thing, step):
self.experiment.log_metrics(thing, step=step)
def log_metric(self, name, thing):
self.experiment.log_metrics({name: thing})
def log_image(self, thing):
for k, v in thing.items():
self.experiment.log_image(v, name=k)
def log_dataset_ref(self, name, ref):
artifact = comet.Artifact(name, artifact_type="dataset")
artifact.add_remote("file://" + ref)
self.experiment.log_artifact(artifact)
def log_model_weights(self, name, weights):
artifact = comet.Artifact(name, artifact_type="model")
artifact.add(weights)
self.experiment.log_artifact(artifact)
def log_confusion_matrix(self, key, target, pred, label):
self.experiment.log_confusion_matrix(y_true=target, y_predicted=pred, labels=label, title=key)
import neptune.new as neptune
from neptune.new.types import File
class NeptuneLogger():
def __init__(self) -> None:
pass
def start(self, project):
self.project = "justinpinkney/" + project
self.run = neptune.init(project=self.project)
def log_cfg(self, cfg):
self.run["model/parameters"] = (cfg)
def log(self, thing, step):
for k, v in thing.items():
self.run[k].log(v)
self.run["train/step"].log(step)
def log_metric(self, name, thing):
self.run[name].log(thing)
def log_image(self, thing):
for k, v in thing.items():
self.run[k] = File.as_image(v.cpu().permute(1,2,0))
def log_dataset_ref(self, name, ref):
self.run[f"datasets/{name}"].track_files(ref)
def log_model_weights(self, name, weights):
key = "MODEL"
# Maybe there is a better way
try:
self.model = neptune.init_model(name=name, key=key, project=self.project)
except:
self.model = neptune.init_model(model="CIF-MODEL", project=self.project)
self.model_version = neptune.init_model_version(model="CIF-MODEL", project=self.project)
self.model_version["model/binary"].upload(weights)
self.model_version["run/url"] = self.run.get_url()
def log_confusion_matrix(self, key, target, pred, label):
# Not built in confusion matrix, but we can log any plot
cm = ConfusionMatrixDisplay.from_predictions(y_true=target.numpy(), y_pred=pred.numpy(), display_labels=label)
self.run[key].upload(neptune.types.File.as_html(cm.figure_))
from sklearn.metrics import ConfusionMatrixDisplay
import mlflow
class MLflowLogger():
def __init__(self) -> None:
mlflow.set_tracking_uri("")
def start(self, project):
self.experiment = mlflow.set_experiment(project)
self.run = mlflow.start_run()
def log_cfg(self, cfg):
mlflow.log_params(cfg)
def log(self, thing, step):
mlflow.log_metrics(thing, step=step)
def log_metric(self, name, thing):
mlflow.log_metric(name, thing)
def log_image(self, thing):
for k, v in thing.items():
mlflow.log_image(v.cpu().permute(1,2,0).numpy(), artifact_file=k+".jpg")
def log_dataset_ref(self, name, ref):
# can't log by reference
mlflow.log_artifact(ref, name)
def log_model_weights(self, name, weights):
# Not using mlflow model registry here as that is quite different
# mlflow models store both the the model as well as weights
mlflow.log_artifact(weights, name)
def log_confusion_matrix(self, key, target, pred, label):
# Not built in confusion matrix, but we can log any plot
cm = ConfusionMatrixDisplay.from_predictions(y_true=target.numpy(), y_pred=pred.numpy(), display_labels=label)
mlflow.log_figure(cm.figure_, artifact_file=key+".jpg")
log_wrappers = {
"wandb": WandbLogger,
"comet": CometLogger,
"neptune": NeptuneLogger,
"mlflow": MLflowLogger,
}
SUPPORTED_LOGGERS = list(log_wrappers.keys())
def get_logger(log_type):
assert log_type in log_wrappers, f"Don't recognise the logger {log_type}"
return log_wrappers[log_type]()