コード例 #1
0
ファイル: graphprobe.py プロジェクト: bonyuta0204/net2vec
def loadviz(directory, blob):
    ed = expdir.ExperimentDirectory(directory)
    html_fn = ed.filename(['html', '%s.html' % expdir.fn_safe(blob)])
    with open(html_fn) as f:
        lines = f.readlines()
    result = OrderedDict()
    for line in lines:
        if 'unit ' in line:
            u, v = line.split(':', 1)
            unit = int(re.search(r'unit (\d+)', u).group(1)) - 1
            u_result = []
            for w in v.split(';'):
                m = re.search(r'(\w.*\w)? \((\w+), ([.\d]+)\)', w)
                if not m:
                    print 'On line', v
                    print 'Error with', w
                label = m.group(1)
                category = m.group(2)
                score = float(m.group(3))
                u_result.append((label, category, score))
            result[unit] = u_result
    return result
コード例 #2
0
ファイル: report.py プロジェクト: bonyuta0204/net2vec
def generate_html_summary(ed,
                          ds,
                          layer,
                          imsize=None,
                          imcount=50,
                          imscale=None,
                          tally_stats=None,
                          gridwidth=None,
                          gap=3,
                          limit=None,
                          force=False,
                          include_hist=False,
                          threshold=0.04,
                          verbose=False):
    print 'Generating html summary', (ed.filename('html/%s.html' %
                                                  expdir.fn_safe(layer)))
    # Grab tally stats
    # bestcat_pciou, name_pciou, score_pciou, _, _, _, _ = (tally_stats)
    if verbose:
        print 'Sorting units by score.'
        sys.stdout.flush()
    layerprobe = LayerProbe(ed, ds, layer)
    if imsize is None:
        imsize = layerprobe.input_dim[0]
    if imscale is None:
        imscale = imsize
    top = max_act_indexes(layerprobe, count=imcount)
    ed.ensure_dir('html', 'image')
    html = [html_prefix]
    rendered_order = []
    barfn = 'image/%s-bargraph.svg' % (expdir.fn_safe(layer))
    bargraph.bar_graph_svg(ed,
                           layer,
                           barheight=100,
                           barwidth=12,
                           threshold=threshold,
                           rendered_order=rendered_order,
                           save=ed.filename('html/' + barfn))
    html.extend([
        '<div class="histogram">',
        '<img class="img-fluid" src="%s" title="Summary of %s %s">' %
        (barfn, ed.basename(), layer), '</div>'
    ])
    html.append('<div class="gridheader">')
    html.append('<div class="layerinfo">')
    html.append(
        '%d/%d units covering %d concepts with IoU &ge; %.2f' %
        (len([
            record
            for record in rendered_order if float(record['score']) >= threshold
        ]), len(rendered_order),
         len(
             set(record['label'] for record in rendered_order
                 if float(record['score']) >= threshold)), threshold))
    html.append('</div>')
    html.append(html_sortheader)
    html.append('</div>')

    if gridwidth is None:
        gridname = ''
        gridwidth = imcount
        gridheight = 1
    else:
        gridname = '-%d' % gridwidth
        gridheight = (imcount + gridwidth - 1) // gridwidth

    html.append('<div class="unitgrid"')  # Leave off > to eat spaces
    if limit is not None:
        rendered_order = rendered_order[:limit]
    for i, record in enumerate(
            sorted(rendered_order,
                   key=lambda record: -float(record['score']))):
        record['score-order'] = i
    for label_order, record in enumerate(rendered_order):
        unit = int(record['unit']) - 1  # zero-based unit indexing
        imfn = 'image/%s%s-%04d.jpg' % (expdir.fn_safe(layer), gridname, unit)
        if force or not ed.has('html/%s' % imfn):
            if verbose:
                print 'Visualizing %s unit %d' % (layer, unit)
                sys.stdout.flush()
            # Generate the top-patch image
            tiled = numpy.full(((imsize + gap) * gridheight - gap,
                                (imsize + gap) * gridwidth - gap, 3),
                               255,
                               dtype='uint8')
            for x, index in enumerate(top[unit]):
                row = x // gridwidth
                col = x % gridwidth
                vis = activation_visualization(ds, layerprobe, unit, index)
                if vis.shape[:2] != (imsize, imsize):
                    vis = imresize(vis, (imsize, imsize))
                tiled[row * (imsize + gap):row * (imsize + gap) + imsize, col *
                      (imsize + gap):col * (imsize + gap) + imsize, :] = vis
            imsave(ed.filename('html/' + imfn), tiled)
        # Generate the wrapper HTML
        graytext = ' lowscore' if float(record['score']) < threshold else ''
        html.append('><div class="unit%s" data-order="%d %d %d">' %
                    (graytext, label_order, record['score-order'], unit + 1))
        html.append('<div class="unitlabel">%s</div>' % fix(record['label']))
        html.append(
            '<div class="info">' +
            '<span class="layername">%s</span> ' % layer +
            '<span class="unitnum">unit %d</span> ' % (unit + 1) +
            '<span class="category">(%s)</span> ' % record['category'] +
            '<span class="iou">IoU %.2f</span>' % float(record['score']) +
            '</div>')
        html.append('<div class="thumbcrop"><img src="%s" height="%d"></div>' %
                    (imfn, imscale))
        html.append('</div')  # Leave off > to eat spaces
    html.append('></div>')
    html.extend([html_suffix])
    with open(ed.filename('html/%s.html' % expdir.fn_safe(layer)), 'w') as f:
        f.write('\n'.join(html))
コード例 #3
0
ファイル: viewprobe.py プロジェクト: bonyuta0204/NetDissec
 def generate_html_summary(self,
                           layer,
                           imsize=64,
                           imcount=16,
                           imscale=None,
                           tally_stats=None,
                           gridwidth=None,
                           verbose=False):
     print 'Generating html summary', (self.ed.filename(
         ['html', '%s.html' % expdir.fn_safe(layer)]))
     # Grab tally stats
     bestcat_pciou, name_pciou, score_pciou, _, _, _, _ = (tally_stats)
     if verbose:
         print 'Sorting units by score.'
         sys.stdout.flush()
     if imscale is None:
         imscale = imsize
     categories = self.ds.category_names()
     ordering = score_pciou.max(axis=0).argsort()[::-1]
     top = self.max_act_indexes(layer, count=imcount)
     self.ed.ensure_dir('html', 'image')
     css = ('https://maxcdn.bootstrapcdn.com/bootstrap/latest' +
            '/css/bootstrap.min.css')
     html = [
         '<!doctype html>', '<html>', '<head>',
         '<link rel="stylesheet" href="%s">' % css, '</head>', '<body>',
         '<div class="container-fluid">'
     ]
     if gridwidth is None:
         gridname = ''
         gridwidth = imcount
         gridheight = 1
     else:
         gridname = '-%d' % gridwidth
         gridheight = (imcount + gridwidth - 1) // gridwidth
     for unit in ordering:
         if verbose:
             print 'Visualizing %s unit %d' % (layer, unit)
             sys.stdout.flush()
         tiled = numpy.full(((imsize + 1) * gridheight - 1,
                             (imsize + 1) * gridwidth - 1, 3),
                            255,
                            dtype='uint8')
         for x, index in enumerate(top[unit]):
             row = x // gridwidth
             col = x % gridwidth
             vis = self.activation_visualization(layer, unit, index)
             if vis.shape[:2] != (imsize, imsize):
                 vis = imresize(vis, (imsize, imsize))
             tiled[row * (imsize + 1):row * (imsize + 1) + imsize,
                   col * (imsize + 1):col * (imsize + 1) + imsize, :] = vis
         imfn = 'image/%s%s-%04d.jpg' % (expdir.fn_safe(layer), gridname,
                                         unit)
         imsave(self.ed.filename(['html', imfn]), tiled)
         labels = '; '.join([
             '%s (%s, %f)' %
             (name_pciou[c][unit], categories[c], score_pciou[c, unit])
             for c in bestcat_pciou[:, unit]
         ])
         html.extend([
             '<h6>%s unit %d: %s</h6>' % (layer, unit + 1, labels),
             '<img src="%s" height="%d">' % (imfn, imscale)
         ])
     html.extend(['</div>', '</body>', '</html>', ''])
     with open(
             self.ed.filename(['html',
                               '%s.html' % expdir.fn_safe(layer)]),
             'w') as f:
         f.write('\n'.join(html))
コード例 #4
0
ファイル: viewprobe.py プロジェクト: bonyuta0204/NetDissec
        args = parser.parse_args()
        np = NetworkProbe(args.directory, blobs=args.blobs)
        for blob in args.blobs:
            formats = args.format.split(',')
            if 'imgmax' in formats:
                np.generate_imgmax(blob)
            if 'html' in formats or 'csv' in formats:
                tally_stats = np.score_tally_stats(blob, verbose=True)
            if 'html' in formats:
                np.generate_html_summary(blob,
                                         imsize=args.imsize,
                                         imscale=args.imscale,
                                         imcount=args.imcount,
                                         tally_stats=tally_stats,
                                         gridwidth=args.gridwidth,
                                         verbose=True)
            if 'csv' in formats:
                filename = os.path.join(args.directory,
                                        '%s-result.csv' % expdir.fn_safe(blob))
                np.generate_csv_summary(blob,
                                        filename,
                                        tally_stats,
                                        order=(args.csvorder.split(',')
                                               if args.csvorder else None),
                                        verbose=True)
            if 'quantmat' in formats:
                np.generate_quantmat(blob, verbose=True)
    except:
        traceback.print_exc(file=sys.stdout)
        sys.exit(1)