-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpaths.py
More file actions
115 lines (88 loc) · 4.42 KB
/
paths.py
File metadata and controls
115 lines (88 loc) · 4.42 KB
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
"""
This file contains the Path class that creates, deletes, or
modifies paths within the directory for saving AM results and
other related images.
This file is Copyright (c) 2022 Steven Tin Sui Luo.
"""
import os
import shutil
import glob
from parameters import Params
params = Params()
class Path:
"""This class prepares the directories for saving AM results
in the <vis> folder.
"""
def __init__(self):
self.model_name = params.MODEL_NAME
self.vis_path = params.VIS_PATH
self.am_path = params.AM_PATH
self.act_path = params.ACT_PATH
self.grad_path = params.GRAD_PATH
self.guided_am_path = params.AM_GRAD_PATH
# self.main_path determines the path of concern in the current use case
self.main_path = self.am_path
# Create <vis> folder to save all visualizations.
if 'vis' not in os.listdir('./'):
os.makedirs('vis')
if self.model_name not in os.listdir('vis'):
os.makedirs(self.vis_path)
def create_am_path(self):
"""This method creates a subdirectory in <vis> for am visualization."""
if self.am_path not in os.listdir(self.vis_path):
os.makedirs(os.path.join(self.vis_path, self.am_path))
# Swith main operating path to self.am_path
self.main_path = self.am_path
self.save_subdir = os.path.join(self.vis_path, self.main_path)
def create_act_path(self):
"""This method creates a subdirectory in <vis> for neuron activation
visualization.
Activation Maps includes those generated from real images and synthetic images
(e.g. synthetic rectangular boxes).
"""
if self.act_path not in os.listdir(self.vis_path):
os.makedirs(os.path.join(self.vis_path, self.act_path))
# Swith main operating path to self.act_path
self.main_path = self.act_path
self.save_subdir = os.path.join(self.vis_path, self.main_path)
def create_grad_path(self):
"""This method creates a subdirectory in <vis> for saliency maps.
Saliency maps are generated from vanila gradients or integrated
gradients. There is also the option to multiple gradients to the
values of the image itself for 'better clarity'.
"""
if self.grad_path not in os.listdir(self.vis_path):
os.makedirs(os.path.join(self.vis_path, self.grad_path))
# Swith main operating path to self.act_path
self.main_path = self.grad_path
self.save_subdir = os.path.join(self.vis_path, self.main_path)
def create_guided_am_path(self):
"""This method creates a subdirectory in <vis> for guided AM."""
if self.guided_am_path not in os.listdir(self.vis_path):
os.makedirs(os.path.join(self.vis_path, self.guided_am_path))
# Swith main operating path to self.act_path
self.main_path = self.guided_am_path
self.save_subdir = os.path.join(self.vis_path, self.main_path)
def create_layer_paths(self, layer: str):
"""This method creates sub-directory in <self.am_path> for a specific layer."""
if layer not in os.listdir(os.path.join(self.vis_path, self.main_path)):
os.makedirs(os.path.join(self.vis_path, self.main_path, layer))
self.save_subdir = os.path.join(self.vis_path, self.main_path, layer)
def create_kernel_paths(self, layer:str, kernel: str):
"""This method creates sub-directory in <self.am_path> for a specific layer."""
if kernel not in os.listdir(os.path.join(self.vis_path, self.main_path, layer)):
os.makedirs(os.path.join(self.vis_path, self.main_path, layer, kernel))
self.save_subdir = os.path.join(self.vis_path, self.main_path, layer, kernel)
def create_img_paths(self, img_id: str):
"""This method creates sub-directory in <self.guided_am_path> for a specific image."""
curr_path = os.path.join(self.save_subdir, img_id)
if img_id not in os.listdir(self.save_subdir):
os.makedirs(os.path.join(self.save_subdir, img_id))
#original_img_path = find_image(img_id)
#shutil.copyfile(original_img_path, os.path.join(curr_path, 'image.png'))
self.save_subdir = curr_path
def find_image(img_id):
for file_path in glob.glob('%s/*/*/*RGB.png' % params.DATA_PATH):
img_name = file_path.split('\\')[-1][:-8]
if img_id == img_name:
return file_path