(self, base_img, final_img)
| 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) |
no test coverage detected