<commit_before>#!/usr/bin/env python
import cPickle as pkl
import glob
import os.path as pth
from pdb import set_trace
import cStringIO as StringIO
import io
import socket

import numpy as np
import scipy

from jinja2 import Template
import flask
from flask import Flask, request, render_template, send_file, jsonify, url_for
app = Flask(__name__)

from urllib import unquote

from recon import *
from recon.config import config



@app.route('/imgs/feat/<path:path>')
def img_feat(path):
    return send_file(pth.join('data/feat/', path))


@app.route('/imgs/gallery/<path:path>')
def img_gallery(path):
    return send_file(pth.join('data/gallery/', path))


@app.route('/gallery')
def gallery():
    img_fnames = [pth.basename(fname) for fname in glob.glob('data/gallery/*')]
    # TODO: better/parameterized defaults
    return render_template('gallery.html', img_fnames=img_fnames, imgs_per_row=5)


@app.route('/vis/<path:img_id>')
def vis(img_id):
    blob_name = request.args.get('blob_name', '')
    act_id = int(request.args.get('act_id', ''))
    return render_template('vis.html', blob_name=blob_name, act_id=act_id, img_id=img_id)


@app.route('/vis/<path:img_id>/img.jpg')
def vis_img(img_id):
    img = get_vis_tree(net_id, img_id).image()
    return send_img(img, 'img.jpg')


@app.route('/vis/<path:img_id>/labels.json')
def vis_labels(img_id):
    labels = get_vis_tree(net_id, img_id).labels()
    label_str = ''.join(['<p>' + label + '</p>' for label in labels])
    return jsonify(labels=label_str)


@app.route('/vis/<path:img_id>/overview')
def vis_overview(img_id):
    tree = get_vis_tree(net_id, img_id)
    layers = list(tree.config['layers'].itervalues())
    layers.sort(key=lambda l: -l['idx'])
    for layer in layers:
        blob_name = layer['blob_name']
        maxes = get_vis_tree(net_id, img_id).max_idxs(blob_name)[:5]
        layer['maxes'] = maxes
    return render_template('overview.html', layers=layers, imgs_per_row=5, img_id=img_id)


@app.route('/vis/<path:img_id>/tree/get')
def json_tree(img_id):
    blob_name = request.args.get('blob_name', '')
    act_id = int(request.args.get('act_id', ''))
    # should input layer_name, not blob_name
    tree = get_vis_tree(net_id, img_id).tree(blob_name, act_id)
    return jsonify(tree)


@app.route('/vis/<path:img_id>/tree/maxes')
def json_tree_maxes(img_id):
    blob_name = request.args.get('blob_name', '')
    maxes = get_vis_tree(net_id, img_id).max_idxs(blob_name)[:5]
    return jsonify(maxes=maxes)

<commit_msg>parameterize number of maxes in overview<commit_after>#!/usr/bin/env python
import cPickle as pkl
import glob
import os.path as pth
from pdb import set_trace
import cStringIO as StringIO
import io
import socket

import numpy as np
import scipy

from jinja2 import Template
import flask
from flask import Flask, request, render_template, send_file, jsonify, url_for
app = Flask(__name__)

from urllib import unquote

from recon import *
from recon.config import config



@app.route('/imgs/feat/<path:path>')
def img_feat(path):
    return send_file(pth.join('data/feat/', path))


@app.route('/imgs/gallery/<path:path>')
def img_gallery(path):
    return send_file(pth.join('data/gallery/', path))


@app.route('/gallery')
def gallery():
    img_fnames = [pth.basename(fname) for fname in glob.glob('data/gallery/*')]
    # TODO: better/parameterized defaults
    return render_template('gallery.html', img_fnames=img_fnames, imgs_per_row=5)


@app.route('/vis/<path:img_id>')
def vis(img_id):
    blob_name = request.args.get('blob_name', '')
    act_id = int(request.args.get('act_id', ''))
    return render_template('vis.html', blob_name=blob_name, act_id=act_id, img_id=img_id)


@app.route('/vis/<path:img_id>/img.jpg')
def vis_img(img_id):
    img = get_vis_tree(net_id, img_id).image()
    return send_img(img, 'img.jpg')


@app.route('/vis/<path:img_id>/labels.json')
def vis_labels(img_id):
    labels = get_vis_tree(net_id, img_id).labels()
    label_str = ''.join(['<p>' + label + '</p>' for label in labels])
    return jsonify(labels=label_str)


@app.route('/vis/<path:img_id>/overview')
def vis_overview(img_id):
    num_maxes = int(request.args.get('num_maxes', '5'))
    tree = get_vis_tree(net_id, img_id)
    layers = list(tree.config['layers'].itervalues())
    layers.sort(key=lambda l: -l['idx'])
    for layer in layers:
        blob_name = layer['blob_name']
        maxes = get_vis_tree(net_id, img_id).max_idxs(blob_name)[:num_maxes]
        layer['maxes'] = maxes
    return render_template('overview.html', layers=layers, imgs_per_row=5, img_id=img_id)


@app.route('/vis/<path:img_id>/tree/get')
def json_tree(img_id):
    blob_name = request.args.get('blob_name', '')
    act_id = int(request.args.get('act_id', ''))
    # should input layer_name, not blob_name
    tree = get_vis_tree(net_id, img_id).tree(blob_name, act_id)
    return jsonify(tree)


@app.route('/vis/<path:img_id>/tree/maxes')
def json_tree_maxes(img_id):
    blob_name = request.args.get('blob_name', '')
    maxes = get_vis_tree(net_id, img_id).max_idxs(blob_name)[:5]
    return jsonify(maxes=maxes)


{'fraction_non_alphanumeric': 0.15354561437780817, 'fraction_numerical': 0.0019535065442469234, 'mean_word_length': 4.778781038374718, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1471258', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1904, 'n_words': 364}