MCPcopy Create free account
hub / github.com/CSAILVision/gandissect / dissect

Function dissect

netdissect/dissection.py:47–172  ·  view source on GitHub ↗

Runs net dissection in-memory, using pytorch, and saves visualizations and metadata into outdir.

(outdir, model, dataset,
        segrunner=None,
        train_dataset=None,
        model_segmenter=None,
        quantile_threshold=0.005,
        iou_threshold=0.05,
        iqr_threshold=0.01,
        examples_per_unit=100,
        batch_size=100,
        num_workers=24,
        seg_batch_size=5,
        make_images=True,
        make_labels=True,
        make_maxiou=False,
        make_covariance=False,
        make_report=True,
        make_row_images=True,
        make_single_images=False,
        rank_all_labels=False,
        netname=None,
        meta=None,
        merge=None,
        settings=None,
        )

Source from the content-addressed store, hash-verified

45from .segmenter import UnifiedParsingSegmenter
46
47def dissect(outdir, model, dataset,
48 segrunner=None,
49 train_dataset=None,
50 model_segmenter=None,
51 quantile_threshold=0.005,
52 iou_threshold=0.05,
53 iqr_threshold=0.01,
54 examples_per_unit=100,
55 batch_size=100,
56 num_workers=24,
57 seg_batch_size=5,
58 make_images=True,
59 make_labels=True,
60 make_maxiou=False,
61 make_covariance=False,
62 make_report=True,
63 make_row_images=True,
64 make_single_images=False,
65 rank_all_labels=False,
66 netname=None,
67 meta=None,
68 merge=None,
69 settings=None,
70 ):
71 '''
72 Runs net dissection in-memory, using pytorch, and saves visualizations
73 and metadata into outdir.
74 '''
75 assert not model.training, 'Run model.eval() before dissection'
76 if netname is None:
77 netname = type(model).__name__
78 if segrunner is None:
79 segrunner = ClassifierSegRunner(dataset)
80 if train_dataset is None:
81 train_dataset = dataset
82 make_iqr = (quantile_threshold == 'iqr')
83 with torch.no_grad():
84 device = next(model.parameters()).device
85 levels = None
86 labelnames, catnames = None, None
87 maxioudata, iqrdata = None, None
88 labeldata = None
89 iqrdata, cov = None, None
90
91 labelnames, catnames = segrunner.get_label_and_category_names()
92 label_category = [catnames.index(c) if c in catnames else 0
93 for l, c in labelnames]
94
95 # First, always collect qunatiles and topk information.
96 segloader = torch.utils.data.DataLoader(dataset,
97 batch_size=batch_size, num_workers=num_workers,
98 pin_memory=(device.type == 'cuda'))
99 quantiles, topk = collect_quantiles_and_topk(outdir, model,
100 segloader, segrunner, k=examples_per_unit)
101
102 # Thresholds can be automatically chosen by maximizing iqr
103 if make_iqr:
104 # Get thresholds based on an IQR optimization

Callers 2

mainFunction · 0.90
test_dissectionFunction · 0.90

Calls 12

typeFunction · 0.85
ClassifierSegRunnerClass · 0.85
collect_iqrFunction · 0.85
generate_imagesFunction · 0.85
collect_maxiouFunction · 0.85
collect_bincountsFunction · 0.85
collect_covarianceFunction · 0.85
generate_reportFunction · 0.85
quantilesMethod · 0.80
parametersMethod · 0.45

Tested by 1

test_dissectionFunction · 0.72