You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

563 line
27KB

  1. """Utilities and tools for tracking runs with Weights & Biases."""
  2. import logging
  3. import os
  4. import sys
  5. from contextlib import contextmanager
  6. from pathlib import Path
  7. from typing import Dict
  8. import yaml
  9. from tqdm import tqdm
  10. FILE = Path(__file__).resolve()
  11. ROOT = FILE.parents[3] # YOLOv5 root directory
  12. if str(ROOT) not in sys.path:
  13. sys.path.append(str(ROOT)) # add ROOT to PATH
  14. from utils.datasets import LoadImagesAndLabels, img2label_paths
  15. from utils.general import LOGGER, check_dataset, check_file
  16. try:
  17. import wandb
  18. assert hasattr(wandb, '__version__') # verify package import not local dir
  19. except (ImportError, AssertionError):
  20. wandb = None
  21. RANK = int(os.getenv('RANK', -1))
  22. WANDB_ARTIFACT_PREFIX = 'wandb-artifact://'
  23. def remove_prefix(from_string, prefix=WANDB_ARTIFACT_PREFIX):
  24. return from_string[len(prefix):]
  25. def check_wandb_config_file(data_config_file):
  26. wandb_config = '_wandb.'.join(data_config_file.rsplit('.', 1)) # updated data.yaml path
  27. if Path(wandb_config).is_file():
  28. return wandb_config
  29. return data_config_file
  30. def check_wandb_dataset(data_file):
  31. is_trainset_wandb_artifact = False
  32. is_valset_wandb_artifact = False
  33. if check_file(data_file) and data_file.endswith('.yaml'):
  34. with open(data_file, errors='ignore') as f:
  35. data_dict = yaml.safe_load(f)
  36. is_trainset_wandb_artifact = (isinstance(data_dict['train'], str) and
  37. data_dict['train'].startswith(WANDB_ARTIFACT_PREFIX))
  38. is_valset_wandb_artifact = (isinstance(data_dict['val'], str) and
  39. data_dict['val'].startswith(WANDB_ARTIFACT_PREFIX))
  40. if is_trainset_wandb_artifact or is_valset_wandb_artifact:
  41. return data_dict
  42. else:
  43. return check_dataset(data_file)
  44. def get_run_info(run_path):
  45. run_path = Path(remove_prefix(run_path, WANDB_ARTIFACT_PREFIX))
  46. run_id = run_path.stem
  47. project = run_path.parent.stem
  48. entity = run_path.parent.parent.stem
  49. model_artifact_name = 'run_' + run_id + '_model'
  50. return entity, project, run_id, model_artifact_name
  51. def check_wandb_resume(opt):
  52. process_wandb_config_ddp_mode(opt) if RANK not in [-1, 0] else None
  53. if isinstance(opt.resume, str):
  54. if opt.resume.startswith(WANDB_ARTIFACT_PREFIX):
  55. if RANK not in [-1, 0]: # For resuming DDP runs
  56. entity, project, run_id, model_artifact_name = get_run_info(opt.resume)
  57. api = wandb.Api()
  58. artifact = api.artifact(entity + '/' + project + '/' + model_artifact_name + ':latest')
  59. modeldir = artifact.download()
  60. opt.weights = str(Path(modeldir) / "last.pt")
  61. return True
  62. return None
  63. def process_wandb_config_ddp_mode(opt):
  64. with open(check_file(opt.data), errors='ignore') as f:
  65. data_dict = yaml.safe_load(f) # data dict
  66. train_dir, val_dir = None, None
  67. if isinstance(data_dict['train'], str) and data_dict['train'].startswith(WANDB_ARTIFACT_PREFIX):
  68. api = wandb.Api()
  69. train_artifact = api.artifact(remove_prefix(data_dict['train']) + ':' + opt.artifact_alias)
  70. train_dir = train_artifact.download()
  71. train_path = Path(train_dir) / 'data/images/'
  72. data_dict['train'] = str(train_path)
  73. if isinstance(data_dict['val'], str) and data_dict['val'].startswith(WANDB_ARTIFACT_PREFIX):
  74. api = wandb.Api()
  75. val_artifact = api.artifact(remove_prefix(data_dict['val']) + ':' + opt.artifact_alias)
  76. val_dir = val_artifact.download()
  77. val_path = Path(val_dir) / 'data/images/'
  78. data_dict['val'] = str(val_path)
  79. if train_dir or val_dir:
  80. ddp_data_path = str(Path(val_dir) / 'wandb_local_data.yaml')
  81. with open(ddp_data_path, 'w') as f:
  82. yaml.safe_dump(data_dict, f)
  83. opt.data = ddp_data_path
  84. class WandbLogger():
  85. """Log training runs, datasets, models, and predictions to Weights & Biases.
  86. This logger sends information to W&B at wandb.ai. By default, this information
  87. includes hyperparameters, system configuration and metrics, model metrics,
  88. and basic data metrics and analyses.
  89. By providing additional command line arguments to train.py, datasets,
  90. models and predictions can also be logged.
  91. For more on how this logger is used, see the Weights & Biases documentation:
  92. https://docs.wandb.com/guides/integrations/yolov5
  93. """
  94. def __init__(self, opt, run_id=None, job_type='Training'):
  95. """
  96. - Initialize WandbLogger instance
  97. - Upload dataset if opt.upload_dataset is True
  98. - Setup trainig processes if job_type is 'Training'
  99. arguments:
  100. opt (namespace) -- Commandline arguments for this run
  101. run_id (str) -- Run ID of W&B run to be resumed
  102. job_type (str) -- To set the job_type for this run
  103. """
  104. # Pre-training routine --
  105. self.job_type = job_type
  106. self.wandb, self.wandb_run = wandb, None if not wandb else wandb.run
  107. self.val_artifact, self.train_artifact = None, None
  108. self.train_artifact_path, self.val_artifact_path = None, None
  109. self.result_artifact = None
  110. self.val_table, self.result_table = None, None
  111. self.bbox_media_panel_images = []
  112. self.val_table_path_map = None
  113. self.max_imgs_to_log = 16
  114. self.wandb_artifact_data_dict = None
  115. self.data_dict = None
  116. # It's more elegant to stick to 1 wandb.init call,
  117. # but useful config data is overwritten in the WandbLogger's wandb.init call
  118. if isinstance(opt.resume, str): # checks resume from artifact
  119. if opt.resume.startswith(WANDB_ARTIFACT_PREFIX):
  120. entity, project, run_id, model_artifact_name = get_run_info(opt.resume)
  121. model_artifact_name = WANDB_ARTIFACT_PREFIX + model_artifact_name
  122. assert wandb, 'install wandb to resume wandb runs'
  123. # Resume wandb-artifact:// runs here| workaround for not overwriting wandb.config
  124. self.wandb_run = wandb.init(id=run_id,
  125. project=project,
  126. entity=entity,
  127. resume='allow',
  128. allow_val_change=True)
  129. opt.resume = model_artifact_name
  130. elif self.wandb:
  131. self.wandb_run = wandb.init(config=opt,
  132. resume="allow",
  133. project='YOLOv5' if opt.project == 'runs/train' else Path(opt.project).stem,
  134. entity=opt.entity,
  135. name=opt.name if opt.name != 'exp' else None,
  136. job_type=job_type,
  137. id=run_id,
  138. allow_val_change=True) if not wandb.run else wandb.run
  139. if self.wandb_run:
  140. if self.job_type == 'Training':
  141. if opt.upload_dataset:
  142. if not opt.resume:
  143. self.wandb_artifact_data_dict = self.check_and_upload_dataset(opt)
  144. if opt.resume:
  145. # resume from artifact
  146. if isinstance(opt.resume, str) and opt.resume.startswith(WANDB_ARTIFACT_PREFIX):
  147. self.data_dict = dict(self.wandb_run.config.data_dict)
  148. else: # local resume
  149. self.data_dict = check_wandb_dataset(opt.data)
  150. else:
  151. self.data_dict = check_wandb_dataset(opt.data)
  152. self.wandb_artifact_data_dict = self.wandb_artifact_data_dict or self.data_dict
  153. # write data_dict to config. useful for resuming from artifacts. Do this only when not resuming.
  154. self.wandb_run.config.update({'data_dict': self.wandb_artifact_data_dict},
  155. allow_val_change=True)
  156. self.setup_training(opt)
  157. if self.job_type == 'Dataset Creation':
  158. self.wandb_run.config.update({"upload_dataset": True})
  159. self.data_dict = self.check_and_upload_dataset(opt)
  160. def check_and_upload_dataset(self, opt):
  161. """
  162. Check if the dataset format is compatible and upload it as W&B artifact
  163. arguments:
  164. opt (namespace)-- Commandline arguments for current run
  165. returns:
  166. Updated dataset info dictionary where local dataset paths are replaced by WAND_ARFACT_PREFIX links.
  167. """
  168. assert wandb, 'Install wandb to upload dataset'
  169. config_path = self.log_dataset_artifact(opt.data,
  170. opt.single_cls,
  171. 'YOLOv5' if opt.project == 'runs/train' else Path(opt.project).stem)
  172. with open(config_path, errors='ignore') as f:
  173. wandb_data_dict = yaml.safe_load(f)
  174. return wandb_data_dict
  175. def setup_training(self, opt):
  176. """
  177. Setup the necessary processes for training YOLO models:
  178. - Attempt to download model checkpoint and dataset artifacts if opt.resume stats with WANDB_ARTIFACT_PREFIX
  179. - Update data_dict, to contain info of previous run if resumed and the paths of dataset artifact if downloaded
  180. - Setup log_dict, initialize bbox_interval
  181. arguments:
  182. opt (namespace) -- commandline arguments for this run
  183. """
  184. self.log_dict, self.current_epoch = {}, 0
  185. self.bbox_interval = opt.bbox_interval
  186. if isinstance(opt.resume, str):
  187. modeldir, _ = self.download_model_artifact(opt)
  188. if modeldir:
  189. self.weights = Path(modeldir) / "last.pt"
  190. config = self.wandb_run.config
  191. opt.weights, opt.save_period, opt.batch_size, opt.bbox_interval, opt.epochs, opt.hyp, opt.imgsz = str(
  192. self.weights), config.save_period, config.batch_size, config.bbox_interval, config.epochs,\
  193. config.hyp, config.imgsz
  194. data_dict = self.data_dict
  195. if self.val_artifact is None: # If --upload_dataset is set, use the existing artifact, don't download
  196. self.train_artifact_path, self.train_artifact = self.download_dataset_artifact(data_dict.get('train'),
  197. opt.artifact_alias)
  198. self.val_artifact_path, self.val_artifact = self.download_dataset_artifact(data_dict.get('val'),
  199. opt.artifact_alias)
  200. if self.train_artifact_path is not None:
  201. train_path = Path(self.train_artifact_path) / 'data/images/'
  202. data_dict['train'] = str(train_path)
  203. if self.val_artifact_path is not None:
  204. val_path = Path(self.val_artifact_path) / 'data/images/'
  205. data_dict['val'] = str(val_path)
  206. if self.val_artifact is not None:
  207. self.result_artifact = wandb.Artifact("run_" + wandb.run.id + "_progress", "evaluation")
  208. columns = ["epoch", "id", "ground truth", "prediction"]
  209. columns.extend(self.data_dict['names'])
  210. self.result_table = wandb.Table(columns)
  211. self.val_table = self.val_artifact.get("val")
  212. if self.val_table_path_map is None:
  213. self.map_val_table_path()
  214. if opt.bbox_interval == -1:
  215. self.bbox_interval = opt.bbox_interval = (opt.epochs // 10) if opt.epochs > 10 else 1
  216. if opt.evolve:
  217. self.bbox_interval = opt.bbox_interval = opt.epochs + 1
  218. train_from_artifact = self.train_artifact_path is not None and self.val_artifact_path is not None
  219. # Update the the data_dict to point to local artifacts dir
  220. if train_from_artifact:
  221. self.data_dict = data_dict
  222. def download_dataset_artifact(self, path, alias):
  223. """
  224. download the model checkpoint artifact if the path starts with WANDB_ARTIFACT_PREFIX
  225. arguments:
  226. path -- path of the dataset to be used for training
  227. alias (str)-- alias of the artifact to be download/used for training
  228. returns:
  229. (str, wandb.Artifact) -- path of the downladed dataset and it's corresponding artifact object if dataset
  230. is found otherwise returns (None, None)
  231. """
  232. if isinstance(path, str) and path.startswith(WANDB_ARTIFACT_PREFIX):
  233. artifact_path = Path(remove_prefix(path, WANDB_ARTIFACT_PREFIX) + ":" + alias)
  234. dataset_artifact = wandb.use_artifact(artifact_path.as_posix().replace("\\", "/"))
  235. assert dataset_artifact is not None, "'Error: W&B dataset artifact doesn\'t exist'"
  236. datadir = dataset_artifact.download()
  237. return datadir, dataset_artifact
  238. return None, None
  239. def download_model_artifact(self, opt):
  240. """
  241. download the model checkpoint artifact if the resume path starts with WANDB_ARTIFACT_PREFIX
  242. arguments:
  243. opt (namespace) -- Commandline arguments for this run
  244. """
  245. if opt.resume.startswith(WANDB_ARTIFACT_PREFIX):
  246. model_artifact = wandb.use_artifact(remove_prefix(opt.resume, WANDB_ARTIFACT_PREFIX) + ":latest")
  247. assert model_artifact is not None, 'Error: W&B model artifact doesn\'t exist'
  248. modeldir = model_artifact.download()
  249. # epochs_trained = model_artifact.metadata.get('epochs_trained')
  250. total_epochs = model_artifact.metadata.get('total_epochs')
  251. is_finished = total_epochs is None
  252. assert not is_finished, 'training is finished, can only resume incomplete runs.'
  253. return modeldir, model_artifact
  254. return None, None
  255. def log_model(self, path, opt, epoch, fitness_score, best_model=False):
  256. """
  257. Log the model checkpoint as W&B artifact
  258. arguments:
  259. path (Path) -- Path of directory containing the checkpoints
  260. opt (namespace) -- Command line arguments for this run
  261. epoch (int) -- Current epoch number
  262. fitness_score (float) -- fitness score for current epoch
  263. best_model (boolean) -- Boolean representing if the current checkpoint is the best yet.
  264. """
  265. model_artifact = wandb.Artifact('run_' + wandb.run.id + '_model', type='model', metadata={
  266. 'original_url': str(path),
  267. 'epochs_trained': epoch + 1,
  268. 'save period': opt.save_period,
  269. 'project': opt.project,
  270. 'total_epochs': opt.epochs,
  271. 'fitness_score': fitness_score
  272. })
  273. model_artifact.add_file(str(path / 'last.pt'), name='last.pt')
  274. wandb.log_artifact(model_artifact,
  275. aliases=['latest', 'last', 'epoch ' + str(self.current_epoch), 'best' if best_model else ''])
  276. LOGGER.info(f"Saving model artifact on epoch {epoch + 1}")
  277. def log_dataset_artifact(self, data_file, single_cls, project, overwrite_config=False):
  278. """
  279. Log the dataset as W&B artifact and return the new data file with W&B links
  280. arguments:
  281. data_file (str) -- the .yaml file with information about the dataset like - path, classes etc.
  282. single_class (boolean) -- train multi-class data as single-class
  283. project (str) -- project name. Used to construct the artifact path
  284. overwrite_config (boolean) -- overwrites the data.yaml file if set to true otherwise creates a new
  285. file with _wandb postfix. Eg -> data_wandb.yaml
  286. returns:
  287. the new .yaml file with artifact links. it can be used to start training directly from artifacts
  288. """
  289. upload_dataset = self.wandb_run.config.upload_dataset
  290. log_val_only = isinstance(upload_dataset, str) and upload_dataset == 'val'
  291. self.data_dict = check_dataset(data_file) # parse and check
  292. data = dict(self.data_dict)
  293. nc, names = (1, ['item']) if single_cls else (int(data['nc']), data['names'])
  294. names = {k: v for k, v in enumerate(names)} # to index dictionary
  295. # log train set
  296. if not log_val_only:
  297. self.train_artifact = self.create_dataset_table(LoadImagesAndLabels(
  298. data['train'], rect=True, batch_size=1), names, name='train') if data.get('train') else None
  299. if data.get('train'):
  300. data['train'] = WANDB_ARTIFACT_PREFIX + str(Path(project) / 'train')
  301. self.val_artifact = self.create_dataset_table(LoadImagesAndLabels(
  302. data['val'], rect=True, batch_size=1), names, name='val') if data.get('val') else None
  303. if data.get('val'):
  304. data['val'] = WANDB_ARTIFACT_PREFIX + str(Path(project) / 'val')
  305. path = Path(data_file)
  306. # create a _wandb.yaml file with artifacts links if both train and test set are logged
  307. if not log_val_only:
  308. path = (path.stem if overwrite_config else path.stem + '_wandb') + '.yaml' # updated data.yaml path
  309. path = ROOT / 'data' / path
  310. data.pop('download', None)
  311. data.pop('path', None)
  312. with open(path, 'w') as f:
  313. yaml.safe_dump(data, f)
  314. LOGGER.info(f"Created dataset config file {path}")
  315. if self.job_type == 'Training': # builds correct artifact pipeline graph
  316. if not log_val_only:
  317. self.wandb_run.log_artifact(
  318. self.train_artifact) # calling use_artifact downloads the dataset. NOT NEEDED!
  319. self.wandb_run.use_artifact(self.val_artifact)
  320. self.val_artifact.wait()
  321. self.val_table = self.val_artifact.get('val')
  322. self.map_val_table_path()
  323. else:
  324. self.wandb_run.log_artifact(self.train_artifact)
  325. self.wandb_run.log_artifact(self.val_artifact)
  326. return path
  327. def map_val_table_path(self):
  328. """
  329. Map the validation dataset Table like name of file -> it's id in the W&B Table.
  330. Useful for - referencing artifacts for evaluation.
  331. """
  332. self.val_table_path_map = {}
  333. LOGGER.info("Mapping dataset")
  334. for i, data in enumerate(tqdm(self.val_table.data)):
  335. self.val_table_path_map[data[3]] = data[0]
  336. def create_dataset_table(self, dataset: LoadImagesAndLabels, class_to_id: Dict[int, str], name: str = 'dataset'):
  337. """
  338. Create and return W&B artifact containing W&B Table of the dataset.
  339. arguments:
  340. dataset -- instance of LoadImagesAndLabels class used to iterate over the data to build Table
  341. class_to_id -- hash map that maps class ids to labels
  342. name -- name of the artifact
  343. returns:
  344. dataset artifact to be logged or used
  345. """
  346. # TODO: Explore multiprocessing to slpit this loop parallely| This is essential for speeding up the the logging
  347. artifact = wandb.Artifact(name=name, type="dataset")
  348. img_files = tqdm([dataset.path]) if isinstance(dataset.path, str) and Path(dataset.path).is_dir() else None
  349. img_files = tqdm(dataset.im_files) if not img_files else img_files
  350. for img_file in img_files:
  351. if Path(img_file).is_dir():
  352. artifact.add_dir(img_file, name='data/images')
  353. labels_path = 'labels'.join(dataset.path.rsplit('images', 1))
  354. artifact.add_dir(labels_path, name='data/labels')
  355. else:
  356. artifact.add_file(img_file, name='data/images/' + Path(img_file).name)
  357. label_file = Path(img2label_paths([img_file])[0])
  358. artifact.add_file(str(label_file),
  359. name='data/labels/' + label_file.name) if label_file.exists() else None
  360. table = wandb.Table(columns=["id", "train_image", "Classes", "name"])
  361. class_set = wandb.Classes([{'id': id, 'name': name} for id, name in class_to_id.items()])
  362. for si, (img, labels, paths, shapes) in enumerate(tqdm(dataset)):
  363. box_data, img_classes = [], {}
  364. for cls, *xywh in labels[:, 1:].tolist():
  365. cls = int(cls)
  366. box_data.append({"position": {"middle": [xywh[0], xywh[1]], "width": xywh[2], "height": xywh[3]},
  367. "class_id": cls,
  368. "box_caption": "%s" % (class_to_id[cls])})
  369. img_classes[cls] = class_to_id[cls]
  370. boxes = {"ground_truth": {"box_data": box_data, "class_labels": class_to_id}} # inference-space
  371. table.add_data(si, wandb.Image(paths, classes=class_set, boxes=boxes), list(img_classes.values()),
  372. Path(paths).name)
  373. artifact.add(table, name)
  374. return artifact
  375. def log_training_progress(self, predn, path, names):
  376. """
  377. Build evaluation Table. Uses reference from validation dataset table.
  378. arguments:
  379. predn (list): list of predictions in the native space in the format - [xmin, ymin, xmax, ymax, confidence, class]
  380. path (str): local path of the current evaluation image
  381. names (dict(int, str)): hash map that maps class ids to labels
  382. """
  383. class_set = wandb.Classes([{'id': id, 'name': name} for id, name in names.items()])
  384. box_data = []
  385. avg_conf_per_class = [0] * len(self.data_dict['names'])
  386. pred_class_count = {}
  387. for *xyxy, conf, cls in predn.tolist():
  388. if conf >= 0.25:
  389. cls = int(cls)
  390. box_data.append(
  391. {"position": {"minX": xyxy[0], "minY": xyxy[1], "maxX": xyxy[2], "maxY": xyxy[3]},
  392. "class_id": cls,
  393. "box_caption": f"{names[cls]} {conf:.3f}",
  394. "scores": {"class_score": conf},
  395. "domain": "pixel"})
  396. avg_conf_per_class[cls] += conf
  397. if cls in pred_class_count:
  398. pred_class_count[cls] += 1
  399. else:
  400. pred_class_count[cls] = 1
  401. for pred_class in pred_class_count.keys():
  402. avg_conf_per_class[pred_class] = avg_conf_per_class[pred_class] / pred_class_count[pred_class]
  403. boxes = {"predictions": {"box_data": box_data, "class_labels": names}} # inference-space
  404. id = self.val_table_path_map[Path(path).name]
  405. self.result_table.add_data(self.current_epoch,
  406. id,
  407. self.val_table.data[id][1],
  408. wandb.Image(self.val_table.data[id][1], boxes=boxes, classes=class_set),
  409. *avg_conf_per_class
  410. )
  411. def val_one_image(self, pred, predn, path, names, im):
  412. """
  413. Log validation data for one image. updates the result Table if validation dataset is uploaded and log bbox media panel
  414. arguments:
  415. pred (list): list of scaled predictions in the format - [xmin, ymin, xmax, ymax, confidence, class]
  416. predn (list): list of predictions in the native space - [xmin, ymin, xmax, ymax, confidence, class]
  417. path (str): local path of the current evaluation image
  418. """
  419. if self.val_table and self.result_table: # Log Table if Val dataset is uploaded as artifact
  420. self.log_training_progress(predn, path, names)
  421. if len(self.bbox_media_panel_images) < self.max_imgs_to_log and self.current_epoch > 0:
  422. if self.current_epoch % self.bbox_interval == 0:
  423. box_data = [{"position": {"minX": xyxy[0], "minY": xyxy[1], "maxX": xyxy[2], "maxY": xyxy[3]},
  424. "class_id": int(cls),
  425. "box_caption": f"{names[int(cls)]} {conf:.3f}",
  426. "scores": {"class_score": conf},
  427. "domain": "pixel"} for *xyxy, conf, cls in pred.tolist()]
  428. boxes = {"predictions": {"box_data": box_data, "class_labels": names}} # inference-space
  429. self.bbox_media_panel_images.append(wandb.Image(im, boxes=boxes, caption=path.name))
  430. def log(self, log_dict):
  431. """
  432. save the metrics to the logging dictionary
  433. arguments:
  434. log_dict (Dict) -- metrics/media to be logged in current step
  435. """
  436. if self.wandb_run:
  437. for key, value in log_dict.items():
  438. self.log_dict[key] = value
  439. def end_epoch(self, best_result=False):
  440. """
  441. commit the log_dict, model artifacts and Tables to W&B and flush the log_dict.
  442. arguments:
  443. best_result (boolean): Boolean representing if the result of this evaluation is best or not
  444. """
  445. if self.wandb_run:
  446. with all_logging_disabled():
  447. if self.bbox_media_panel_images:
  448. self.log_dict["BoundingBoxDebugger"] = self.bbox_media_panel_images
  449. try:
  450. wandb.log(self.log_dict)
  451. except BaseException as e:
  452. LOGGER.info(
  453. f"An error occurred in wandb logger. The training will proceed without interruption. More info\n{e}")
  454. self.wandb_run.finish()
  455. self.wandb_run = None
  456. self.log_dict = {}
  457. self.bbox_media_panel_images = []
  458. if self.result_artifact:
  459. self.result_artifact.add(self.result_table, 'result')
  460. wandb.log_artifact(self.result_artifact, aliases=['latest', 'last', 'epoch ' + str(self.current_epoch),
  461. ('best' if best_result else '')])
  462. wandb.log({"evaluation": self.result_table})
  463. columns = ["epoch", "id", "ground truth", "prediction"]
  464. columns.extend(self.data_dict['names'])
  465. self.result_table = wandb.Table(columns)
  466. self.result_artifact = wandb.Artifact("run_" + wandb.run.id + "_progress", "evaluation")
  467. def finish_run(self):
  468. """
  469. Log metrics if any and finish the current W&B run
  470. """
  471. if self.wandb_run:
  472. if self.log_dict:
  473. with all_logging_disabled():
  474. wandb.log(self.log_dict)
  475. wandb.run.finish()
  476. @contextmanager
  477. def all_logging_disabled(highest_level=logging.CRITICAL):
  478. """ source - https://gist.github.com/simon-weber/7853144
  479. A context manager that will prevent any logging messages triggered during the body from being processed.
  480. :param highest_level: the maximum logging level in use.
  481. This would only need to be changed if a custom level greater than CRITICAL is defined.
  482. """
  483. previous_level = logging.root.manager.disable
  484. logging.disable(highest_level)
  485. try:
  486. yield
  487. finally:
  488. logging.disable(previous_level)