| 5 | import numpy as np |
| 6 | from tqdm import tqdm |
| 7 | class MACAnalysis(BaseAnalysis): |
| 8 | def __init__(self, opt): |
| 9 | super().__init__(opt) |
| 10 | |
| 11 | def analyze(self): |
| 12 | total_filter_mac = [0.0] * len(self.hook_list) |
| 13 | for test_loader in self.test_loaders: |
| 14 | test_set_name = test_loader.dataset.opt['name'] |
| 15 | num_samples = self.opt.get('num_samples',10) |
| 16 | print(f'Analyzing {test_set_name}..\n') |
| 17 | pbar = tqdm(total=num_samples, desc='') |
| 18 | for idx, val_data in enumerate(test_loader): |
| 19 | if idx >= num_samples: |
| 20 | break |
| 21 | tensor_lq = val_data['lq'].to(self.device) |
| 22 | imgname = osp.basename(val_data['lq_path'][0]) |
| 23 | tensor_base = torch.zeros_like(tensor_lq) |
| 24 | layer_conductance = self._mask_attribute_conductance(tensor_base, tensor_lq) |
| 25 | total_filter_mac = [a + b for a, b in zip(total_filter_mac, layer_conductance)] |
| 26 | pbar.set_description(f'Read {imgname}') |
| 27 | pbar.update(1) |
| 28 | self._save_results(total_filter_mac, 'mac') |
| 29 | |
| 30 | def _mask_attribute_conductance(self, base_img, final_img): |
| 31 | total_step = self.opt['total_step'] |
| 32 | order_array = np.random.permutation(final_img.shape[-2] * final_img.shape[-1]) |
| 33 | start_ratio = self.opt['pretrained_ratio'] |
| 34 | all_hook_layer_conductance = [0.0] * len(self.hook_list) |
| 35 | last_hook_layer_output = [] |
| 36 | |
| 37 | for step in range(total_step): |
| 38 | alpha = 1 - start_ratio + start_ratio * step / total_step |
| 39 | interpolated_img = self._get_interpolated_img_from_mask_attribute_path(base_img, final_img, alpha, order_array).to(self.device) |
| 40 | self.model.zero_grad() |
| 41 | interpolated_output = self.model(interpolated_img) |
| 42 | loss = attr_grad(interpolated_output,reduce='sum') |
| 43 | loss.backward() |
| 44 | now_hook_layer_output = [] |
| 45 | for hook in self.hook_list: |
| 46 | now_hook_layer_output.append(hook.output.detach()) |
| 47 | if step > 0: |
| 48 | dfdy = [hook.grad.detach() for hook in self.hook_list] |
| 49 | approx_dydx = [now - last for now, last in zip(now_hook_layer_output, last_hook_layer_output)] |
| 50 | all_hook_layer_conductance = [cond + df * dy for cond, df, dy in zip(all_hook_layer_conductance, dfdy, approx_dydx)] |
| 51 | last_hook_layer_output = now_hook_layer_output |
| 52 | |
| 53 | return [torch.mean(torch.abs(cond)).detach().cpu().numpy() for cond in all_hook_layer_conductance] |
| 54 | def main(): |
| 55 | root_path = osp.abspath(osp.join(__file__, osp.pardir, osp.pardir)) |
| 56 | opt, _ = parse_options(root_path, is_train=False) |