explaining_framework/main.py

109 lines
4.4 KiB
Python
Raw Normal View History

2022-12-29 22:29:32 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
2023-01-02 22:37:40 +00:00
import copy
import logging
2022-12-29 22:29:32 +00:00
import os
2023-01-02 22:37:40 +00:00
import torch
from torch_geometric import seed_everything
from torch_geometric.data.makedirs import makedirs
from torch_geometric.explain import Explainer
from torch_geometric.explain.config import ThresholdConfig
from torch_geometric.graphgym.config import cfg
from torch_geometric.graphgym.utils.device import auto_select_device
from tqdm import tqdm
2022-12-29 22:29:32 +00:00
from explaining_framework.config.explaining_config import explaining_cfg
from explaining_framework.utils.explaining.cmd_args import parse_args
from explaining_framework.utils.explaining.outline import ExplainingOutline
2022-12-30 18:41:56 +00:00
from explaining_framework.utils.explanation.adjust import Adjust
2023-01-08 19:12:38 +00:00
from explaining_framework.utils.io import (dump_cfg, is_exists,
obj_config_to_str, read_json,
set_printing, write_json)
2022-12-30 18:41:56 +00:00
if __name__ == "__main__":
args = parse_args()
2023-01-09 12:08:42 +00:00
outline = ExplainingOutline(args.explaining_cfg_file)
pbar = tqdm(total=len(outline.dataset) * len(outline.attacks))
2023-01-08 19:12:38 +00:00
item, index = outline.get_item()
while not (item is None or index is None):
for attack in outline.attacks:
attack_path = os.path.join(
outline.out_dir, attack.__class__.__name__, obj_config_to_str(attack)
2023-01-08 19:12:38 +00:00
)
makedirs(attack_path)
data_attack_path = os.path.join(attack_path, f"{index}.json")
data_attack = outline.get_attack(
attack=attack, item=item, path=data_attack_path
)
2023-01-08 19:12:38 +00:00
item, index = outline.get_item()
2023-01-08 19:12:38 +00:00
outline.reload_dataloader()
2023-01-04 09:41:34 +00:00
item, index = outline.get_item()
while not (item is None or index is None):
2023-01-08 19:12:38 +00:00
for attack in outline.attacks:
attack_path_ = os.path.join(
outline.explainer_path,
attack.__class__.__name__,
obj_config_to_str(attack),
2023-01-04 09:41:34 +00:00
)
2023-01-08 19:12:38 +00:00
makedirs(attack_path_)
data_attack_path_ = os.path.join(attack_path_, f"{index}.json")
attack_data = outline.get_attack(
attack=attack, item=item, path=data_attack_path_
)
exp = outline.get_explanation(item=attack_data, path=data_attack_path_)
pbar.update(1)
if exp is None:
continue
else:
for adjust in outline.adjusts:
adjust_path = os.path.join(
attack_path_,
adjust.__class__.__name__,
obj_config_to_str(adjust),
2023-01-04 09:41:34 +00:00
)
makedirs(adjust_path)
exp_adjust_path = os.path.join(adjust_path, f"{index}.json")
exp_adjust = outline.get_adjust(
adjust=adjust, item=exp, path=exp_adjust_path
2023-01-04 09:41:34 +00:00
)
for threshold_conf in outline.thresholds_configs:
outline.set_explainer_threshold_config(threshold_conf)
masking_path = os.path.join(
adjust_path,
2023-01-08 19:12:38 +00:00
"ThresholdConfig",
obj_config_to_str(threshold_conf),
)
makedirs(masking_path)
exp_masked_path = os.path.join(masking_path, f"{index}.json")
exp_masked = outline.get_threshold(
item=exp_adjust, path=exp_masked_path
2023-01-08 19:12:38 +00:00
)
if exp_masked is None:
continue
else:
for metric in outline.metrics:
metric_path = os.path.join(
masking_path,
metric.__class__.__name__,
obj_config_to_str(metric),
)
makedirs(metric_path)
metric_path = os.path.join(metric_path, f"{index}.json")
out_metric = outline.get_metric(
metric=metric, item=exp_masked, path=metric_path
)
2023-01-08 19:12:38 +00:00
item, index = outline.get_item()
2023-01-08 22:23:55 +00:00
with open(os.path.join(outline.out_dir, "done"), "w") as f:
f.write("")
2023-01-08 22:23:55 +00:00
pbar.close()