def write(self, msg): self.console.write(msg) if self.file is not None: may_make_dirs(self, os.path.dirname(osp.abspath(self.file))) if self.immediately_visible: with open(self.file, 'a') as f: f.write(msg) else: if self.f is None: self.f = open(self.file, 'w') self.f.write(msg) self.flush()
def src_copy_to_dst(src_path, dst_path): """ :param src_path: :param dst_path: :return: We may make destination path. Then we will copy files. """ if osp.exists(src_path): may_make_dirs(dst_path=osp.dirname(dst_path)) shutil.copy(src_path, dst_path)
def may_save_ckpt(self, score, epoch): """ :param score: mAP and CMC scores :param epoch: :return: """ state_dicts = {} if not self.cfg.optim.trial_run: state_dicts = { key: item.state_dict() for key, item in self.save_ckpt.items() if item is not None } ckpt = dict(state_dicts=state_dicts, epoch=epoch, score=score) may_make_dirs(dst_path=osp.dirname(self.cfg.log.ckpt_file)) torch.save(ckpt, self.cfg.log.ckpt_file) msg = '=> Checkpoint Saved to {}'.format(self.cfg.log.ckpt_file) print(msg)
def _save_png_as_jpg(self): png_im_dir = osp.join(self.dataset_root, self.png_im_root) jpg_im_dir = osp.join(self.dataset_root, self.im_root) assert osp.exists(png_im_dir), "The PNG image dir {} should be placed inside {}".format(png_im_dir, self.dataset_root) png_paths = list(walkdir(png_im_dir, '.png')) if osp.exists(jpg_im_dir): jpg_paths = list(walkdir(jpg_im_dir, '.jpg')) if len(png_paths) == len(jpg_paths): print('=> Found same number of JPG images. Skip transforming PNG to JPG.') return else: shutil.rmtree(jpg_im_dir) print('=> JPG image dir exists but does not contain same number of images. So it is removed and would be re-generated.') # CUHK03 contains 14097 detected + 14096 labeled images = 28193 print('change dir is :', osp.dirname(png_paths[0])) for png_path in tqdm(png_paths, desc='PNG->JPG', miniters=2000, ncols=120, unit=' images'): jpg_path = png_path.replace(self.png_im_root, self.im_root).replace('.png', '.jpg') may_make_dirs(dst_path=osp.dirname(jpg_path)) imsave(jpg_path, np.array(Image.open(png_path)))
def may_save_ckpt(self, score, epoch): """ :param score: mAP and CMC scores :param epoch: :return: """ state_dicts = {} if hasattr(self, 'save_ckpt') is False: raise AttributeError( '{} object has no attribute \'save_ckpt\''.format( self.__class__.__name__)) if not self.cfg.optim.trial_run: state_dicts = { key: item.state_dict() for key, item in self.save_ckpt.items() if item is not None } ckpt = dict(state_dicts=state_dicts, epoch=epoch, score=score) may_make_dirs(dst_path=osp.dirname(self.cfg.log.ckpt_file)) torch.save(ckpt, self.cfg.log.ckpt_file) msg = '=> Checkpoint Saved to {}'.format(self.cfg.log.ckpt_file) print(msg)