From f236394232d80c5d71b4551ba88c575804bf1661 Mon Sep 17 00:00:00 2001 From: Ng Ngai Fai Date: Thu, 3 Dec 2020 15:41:32 +0800 Subject: [PATCH] change summary subdir mode to 750 Recursively create sub dirs Optimize heatmap saving better coding format fix typo add os.chmod() to make sure forgot to append the image filename to the returned path use realpath() instead of abspath() --- .../explainer/_image_classification_runner.py | 51 +++++++++++++------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/mindspore/explainer/_image_classification_runner.py b/mindspore/explainer/_image_classification_runner.py index a15a84375a..9c42a546fd 100644 --- a/mindspore/explainer/_image_classification_runner.py +++ b/mindspore/explainer/_image_classification_runner.py @@ -100,7 +100,9 @@ class ImageClassificationRunner: _DATASET_SEED = 58 # printing spacer _SPACER = "{:120}\r" - # file permission for writing files + # datafile directory's permission + _DIR_MODE = 0o750 + # datafile's permission _FILE_MODE = 0o600 def __init__(self, @@ -654,31 +656,50 @@ class ImageClassificationRunner: def _save_original_image(self, sample_id, image): """Save an image to summary directory.""" id_dirname = self._get_sample_dirname(sample_id) - relative_dir = os.path.join(self._DATAFILE_DIRNAME_PREFIX + str(self._summary_timestamp), - self._ORIGINAL_IMAGE_DIRNAME, - id_dirname) - abs_dir_path = os.path.abspath(os.path.join(self._summary_dir, relative_dir)) - os.makedirs(abs_dir_path, mode=self._FILE_MODE, exist_ok=True) + path_tokens = [self._summary_dir, + self._DATAFILE_DIRNAME_PREFIX + str(self._summary_timestamp), + self._ORIGINAL_IMAGE_DIRNAME, + id_dirname] + + abs_dir_path = self._create_subdir(*path_tokens) filename = f"{sample_id}.jpg" save_path = os.path.join(abs_dir_path, filename) image.save(save_path) os.chmod(save_path, self._FILE_MODE) - return os.path.join(relative_dir, filename) + return os.path.join(*path_tokens[1:], filename) def _save_heatmap(self, explain_method, class_id, sample_id, image): """Save heatmap image to summary directory.""" id_dirname = self._get_sample_dirname(sample_id) - relative_dir = os.path.join(self._DATAFILE_DIRNAME_PREFIX + str(self._summary_timestamp), - self._HEATMAP_DIRNAME, - explain_method, - id_dirname) - abs_dir_path = os.path.abspath(os.path.join(self._summary_dir, relative_dir)) - os.makedirs(abs_dir_path, mode=self._FILE_MODE, exist_ok=True) + path_tokens = [self._summary_dir, + self._DATAFILE_DIRNAME_PREFIX + str(self._summary_timestamp), + self._HEATMAP_DIRNAME, + explain_method, + id_dirname] + + abs_dir_path = self._create_subdir(*path_tokens) filename = f"{sample_id}_{class_id}.jpg" save_path = os.path.join(abs_dir_path, filename) - image.save(save_path) + image.save(save_path, optimize=True) os.chmod(save_path, self._FILE_MODE) - return os.path.join(relative_dir, filename) + return os.path.join(*path_tokens[1:], filename) + + def _create_subdir(self, *args): + """Recursively create subdirectories.""" + abs_path = None + for token in args: + if abs_path is None: + abs_path = os.path.realpath(token) + else: + abs_path = os.path.join(abs_path, token) + # os.makedirs() don't set intermediate dir permission properly, we mkdir() one by one + try: + os.mkdir(abs_path, mode=self._DIR_MODE) + # In some platform, mode may be ignored in os.mkdir(), we have to chmod() again to make sure + os.chmod(abs_path, mode=self._DIR_MODE) + except FileExistsError: + pass + return abs_path @classmethod def _get_sample_dirname(cls, sample_id):