34
34
is_comet_ml_available ,
35
35
is_dvclive_available ,
36
36
is_mlflow_available ,
37
+ is_swanlab_available ,
37
38
is_tensorboard_available ,
38
39
is_wandb_available ,
39
40
listify ,
63
64
if is_dvclive_available ():
64
65
_available_trackers .append (LoggerType .DVCLIVE )
65
66
67
+ if is_swanlab_available ():
68
+ _available_trackers .append (LoggerType .SWANLAB )
69
+
66
70
logger = get_logger (__name__ )
67
71
68
72
@@ -1061,6 +1065,106 @@ def finish(self):
1061
1065
self .live .end ()
1062
1066
1063
1067
1068
+ class SwanLabTracker (GeneralTracker ):
1069
+ """
1070
+ A `Tracker` class that supports `swanlab`. Should be initialized at the start of your script.
1071
+
1072
+ Args:
1073
+ run_name (`str`):
1074
+ The name of the experiment run.
1075
+ **kwargs (additional keyword arguments, *optional*):
1076
+ Additional key word arguments passed along to the `swanlab.init` method.
1077
+ """
1078
+
1079
+ name = "swanlab"
1080
+ requires_logging_directory = False
1081
+ main_process_only = False
1082
+
1083
+ def __init__ (self , run_name : str , ** kwargs ):
1084
+ super ().__init__ ()
1085
+ self .run_name = run_name
1086
+ self .init_kwargs = kwargs
1087
+
1088
+ @on_main_process
1089
+ def start (self ):
1090
+ import swanlab
1091
+
1092
+ self .run = swanlab .init (project = self .run_name , ** self .init_kwargs )
1093
+ swanlab .config ["FRAMEWORK" ] = "🤗Accelerate" # add accelerate logo in config
1094
+ logger .debug (f"Initialized SwanLab project { self .run_name } " )
1095
+ logger .debug (
1096
+ "Make sure to log any initial configurations with `self.store_init_configuration` before training!"
1097
+ )
1098
+
1099
+ @property
1100
+ def tracker (self ):
1101
+ return self .run
1102
+
1103
+ @on_main_process
1104
+ def store_init_configuration (self , values : dict ):
1105
+ """
1106
+ Logs `values` as hyperparameters for the run. Should be run at the beginning of your experiment.
1107
+
1108
+ Args:
1109
+ values (Dictionary `str` to `bool`, `str`, `float` or `int`):
1110
+ Values to be stored as initial hyperparameters as key-value pairs. The values need to have type `bool`,
1111
+ `str`, `float`, `int`, or `None`.
1112
+ """
1113
+ import swanlab
1114
+
1115
+ swanlab .config .update (values , allow_val_change = True )
1116
+ logger .debug ("Stored initial configuration hyperparameters to SwanLab" )
1117
+
1118
+ @on_main_process
1119
+ def log (self , values : dict , step : Optional [int ] = None , ** kwargs ):
1120
+ """
1121
+ Logs `values` to the current run.
1122
+
1123
+ Args:
1124
+ data : Dict[str, DataType]
1125
+ Data must be a dict. The key must be a string with 0-9, a-z, A-Z, " ", "_", "-", "/". The value must be a
1126
+ `float`, `float convertible object`, `int` or `swanlab.data.BaseType`.
1127
+ step : int, optional
1128
+ The step number of the current data, if not provided, it will be automatically incremented.
1129
+ If step is duplicated, the data will be ignored.
1130
+ kwargs:
1131
+ Additional key word arguments passed along to the `swanlab.log` method. Likes:
1132
+ print_to_console : bool, optional
1133
+ Whether to print the data to the console, the default is False.
1134
+ """
1135
+ self .run .log (values , step = step , ** kwargs )
1136
+ logger .debug ("Successfully logged to SwanLab" )
1137
+
1138
+ @on_main_process
1139
+ def log_images (self , values : dict , step : Optional [int ] = None , ** kwargs ):
1140
+ """
1141
+ Logs `images` to the current run.
1142
+
1143
+ Args:
1144
+ values (Dictionary `str` to `List` of `np.ndarray` or `PIL.Image`):
1145
+ Values to be logged as key-value pairs. The values need to have type `List` of `np.ndarray` or
1146
+ step (`int`, *optional*):
1147
+ The run step. If included, the log will be affiliated with this step.
1148
+ kwargs:
1149
+ Additional key word arguments passed along to the `swanlab.log` method. Likes:
1150
+ print_to_console : bool, optional
1151
+ Whether to print the data to the console, the default is False.
1152
+ """
1153
+ import swanlab
1154
+
1155
+ for k , v in values .items ():
1156
+ self .log ({k : [swanlab .Image (image ) for image in v ]}, step = step , ** kwargs )
1157
+ logger .debug ("Successfully logged images to SwanLab" )
1158
+
1159
+ @on_main_process
1160
+ def finish (self ):
1161
+ """
1162
+ Closes `swanlab` writer
1163
+ """
1164
+ self .run .finish ()
1165
+ logger .debug ("SwanLab run closed" )
1166
+
1167
+
1064
1168
LOGGER_TYPE_TO_CLASS = {
1065
1169
"aim" : AimTracker ,
1066
1170
"comet_ml" : CometMLTracker ,
@@ -1069,6 +1173,7 @@ def finish(self):
1069
1173
"wandb" : WandBTracker ,
1070
1174
"clearml" : ClearMLTracker ,
1071
1175
"dvclive" : DVCLiveTracker ,
1176
+ "swanlab" : SwanLabTracker ,
1072
1177
}
1073
1178
1074
1179
@@ -1093,6 +1198,7 @@ def filter_trackers(
1093
1198
- `"comet_ml"`
1094
1199
- `"mlflow"`
1095
1200
- `"dvclive"`
1201
+ - `"swanlab"`
1096
1202
If `"all"` is selected, will pick up all available trackers in the environment and initialize them. Can
1097
1203
also accept implementations of `GeneralTracker` for custom trackers, and can be combined with `"all"`.
1098
1204
logging_dir (`str`, `os.PathLike`, *optional*):
0 commit comments