Esempio n. 1
0
def get_app(model,
            classes,
            top,
            html_base_dir,
            temp_folder='./tmp',
            input_folder='./'):
    '''
    The base of the Flask application to be run
    :param model: the model to show
    :param classes: list of names of output classes to show in the GUI.
        if None passed - ImageNet classes will be used
    :param top: number of top predictions to show in the GUI
    :param html_base_dir: the directory for the HTML (usually inside the
        packages, quiverboard/dist must be a subdirectory)
    :param temp_folder: where the temporary image data should be saved
    :param input_folder: the image directory for the raw data
    :return:
    '''

    single_input_shape, input_channels = get_input_config(model)

    app = Flask(__name__)
    app.threaded = True
    CORS(app)

    @app.route('/')
    def home():
        return send_from_directory(join(html_base_dir, 'quiverboard/dist'),
                                   'index.html')

    @app.route('/<path>')
    def get_board_files(path):
        return send_from_directory(join(html_base_dir, 'quiverboard/dist'),
                                   path)

    @app.route('/inputs')
    def get_inputs():
        image_regex = re.compile(r'.*\.(jpg|png|gif)$')
        return jsonify([
            filename for filename in listdir(abspath(input_folder))
            if image_regex.match(filename) is not None
        ])

    @app.route('/temp-file/<path>')
    def get_temp_file(path):
        return send_from_directory(abspath(temp_folder), path)

    @app.route('/input-file/<path>')
    def get_input_file(path):
        return send_from_directory(abspath(input_folder), path)

    @app.route('/model')
    def get_config():
        return jsonify(json.loads(model.to_json()))

    @app.route('/layer/<layer_name>/<input_path>')
    def get_layer_outputs(layer_name, input_path):
        return jsonify(
            save_layer_outputs(
                load_img(join(abspath(input_folder), input_path),
                         single_input_shape,
                         grayscale=input_channels == 1), model, layer_name,
                temp_folder, input_path))

    @app.route('/predict/<input_path>')
    def get_prediction(input_path):
        is_grayscale = (input_channels == 1)
        input_img = load_img(join(abspath(input_folder), input_path),
                             single_input_shape,
                             grayscale=is_grayscale)
        with get_evaluation_context():
            return jsonify(
                json.loads(
                    get_json(
                        decode_predictions(model.predict(input_img), classes,
                                           top))))

    return app
Esempio n. 2
0
def get_app(model,
            classes,
            top,
            html_base_dir,
            temp_folder='./tmp',
            input_folder='./',
            mean=None,
            std=None):
    '''
    The base of the Flask application to be run
    :param model: the model to show
    :param classes: list of names of output classes to show in the GUI.
        if None passed - ImageNet classes will be used
    :param top: number of top predictions to show in the GUI
    :param html_base_dir: the directory for the HTML (usually inside the
        packages, quiverboard/dist must be a subdirectory)
    :param temp_folder: where the temporary image data should be saved
    :param input_folder: the image directory for the raw data
    :param mean: list of float mean values
    :param std: lost of float std values
    :return:
    '''

    single_input_shape, input_channels = get_input_config(model)

    app = Flask(__name__)
    app.threaded = True
    CORS(app)
    '''
        Static Routes
    '''
    @app.route('/')
    def home():
        return send_from_directory(join(html_base_dir, 'quiverboard/dist'),
                                   'index.html')

    @app.route('/<path>')
    def get_board_files(path):
        return send_from_directory(join(html_base_dir, 'quiverboard/dist'),
                                   path)

    @app.route('/temp-file/<path>')
    def get_temp_file(path):
        return send_from_directory(abspath(temp_folder), path)

    @app.route('/input-file/<path>')
    def get_input_file(path):
        return send_from_directory(abspath(input_folder), path)

    '''
        Computations
    '''

    @app.route('/model')
    def get_config():
        return jsonify(json.loads(model.to_json()))

    @app.route('/inputs')
    def get_inputs():
        return jsonify(list_img_files(input_folder))

    @app.route('/layer/<layer_name>/<input_path>')
    def get_layer_outputs(layer_name, input_path):
        return jsonify(
            save_layer_outputs(
                load_img(join(abspath(input_folder), input_path),
                         single_input_shape,
                         grayscale=(input_channels == 1),
                         mean=mean,
                         std=std), model, layer_name, temp_folder, input_path))

    @app.route('/predict/<input_path>')
    def get_prediction(input_path):
        with get_evaluation_context():
            return safe_jsonify(
                decode_predictions(
                    model.predict(
                        load_img(join(abspath(input_folder), input_path),
                                 single_input_shape,
                                 grayscale=(input_channels == 1),
                                 mean=mean,
                                 std=std)), classes, top))

    return app
Esempio n. 3
0
def get_app(model, classes, top, html_base_dir, temp_folder='./tmp', input_folder='./',
            mean=None, std=None):
    '''
    The base of the Flask application to be run
    :param model: the model to show
    :param classes: list of names of output classes to show in the GUI.
        if None passed - ImageNet classes will be used
    :param top: number of top predictions to show in the GUI
    :param html_base_dir: the directory for the HTML (usually inside the
        packages, quiverboard/dist must be a subdirectory)
    :param temp_folder: where the temporary image data should be saved
    :param input_folder: the image directory for the raw data
    :param mean: list of float mean values
    :param std: lost of float std values
    :return:
    '''

    single_input_shape, input_channels = get_input_config(model)

    app = Flask(__name__)
    app.threaded = True
    CORS(app)

    '''
        Static Routes
    '''


    @app.route('/')
    def home():
        return send_from_directory(
            join(html_base_dir, 'quiverboard/dist'),
            'index.html'
        )

    @app.route('/<path>')
    def get_board_files(path):
        return send_from_directory(
            join(html_base_dir, 'quiverboard/dist'),
            path
        )

    @app.route('/temp-file/<path>')
    def get_temp_file(path):
        return send_from_directory(abspath(temp_folder), path)

    @app.route('/input-file/<path>')
    def get_input_file(path):
        return send_from_directory(abspath(input_folder), path)



    '''
        Computations
    '''

    @app.route('/model')
    def get_config():
        return jsonify(json.loads(model.to_json()))


    @app.route('/inputs')
    def get_inputs():
        return jsonify(list_img_files(input_folder))

    @app.route('/layer/<layer_name>/<input_path>')
    def get_layer_outputs(layer_name, input_path):
        return jsonify(
            save_layer_outputs(
                load_img(
                    join(abspath(input_folder), input_path),
                    single_input_shape,
                    grayscale=(input_channels == 1),
                    mean=mean, std=std
                ),
                model,
                layer_name,
                temp_folder,
                input_path
            )
        )

    @app.route('/predict/<input_path>')
    def get_prediction(input_path):
        with get_evaluation_context():
            return safe_jsonify(
                decode_predictions(
                    model.predict(
                        load_img(
                            join(abspath(input_folder), input_path),
                            single_input_shape,
                            grayscale=(input_channels == 1),
                            mean=mean, std=std
                        )
                    ),
                    classes,
                    top
                )
            )

    return app
Esempio n. 4
0
def get_app(model,
            classes,
            top,
            html_base_dir,
            temp_folder='./tmp',
            input_folder='./'):
    '''
    The base of the Flask application to be run
    :param model: the model to show
    :param classes: list of names of output classes to show in the GUI.
        if None passed - ImageNet classes will be used
    :param top: number of top predictions to show in the GUI
    :param html_base_dir: the directory for the HTML (usually inside the
        packages, quiverboard/dist must be a subdirectory)
    :param temp_folder: where the temporary image data should be saved
    :param input_folder: the image directory for the raw data
    :return:
    '''

    length_time, num_channels = get_input_config(model)

    app = Flask(__name__)
    app.threaded = True
    CORS(app)
    '''
        Static Routes
    '''
    @app.route('/')
    def home():
        return send_from_directory(join(html_base_dir, 'quiverboard/dist'),
                                   'index.html')

    @app.route('/<path>')
    def get_board_files(path):
        return send_from_directory(join(html_base_dir, 'quiverboard/dist'),
                                   path)

    @app.route('/temp-file/<path>')
    def get_temp_file(path):
        return send_from_directory(abspath(temp_folder), path)

    @app.route('/input-file/<path>')
    def get_input_file(path):
        return send_from_directory(abspath(input_folder), path)

    '''
        Computations
    '''

    @app.route('/model')
    def get_config():
        return jsonify(json.loads(model.to_json()))

    @app.route('/inputs')
    def get_inputs():
        # return jsonify(list_sig_npy_files(input_folder))
        npy_files = list_sig_npy_files(input_folder)
        png_files = list_sig_png_files(input_folder)
        unvisualized_npy_files = [
            fn for fn in npy_files
            if fn[:-4] not in [fn[:-4] for fn in png_files]
        ]
        for fn in unvisualized_npy_files:
            timeseries = tsv.np.load(
                input_folder + '/' + fn
            )[0]  # element 0, because network thingies expect [?,bins,channels] shape,
            # so for these  single timeseries, ? = 1 and we can just index it out
            # print(timeseries, timeseries.shape)
            tsv.generate_timeseries_image(timeseries,
                                          input_folder + '/' + fn[:-3] + 'png')
        return jsonify(list_sig_png_files(input_folder))

    @app.route('/layer/<layer_name>/<input_path>')
    def get_layer_outputs(layer_name, input_path):
        '''
        Pushes input_path through model and saves layer output (hijacked to save as timeseries-png)
        Returns list timeseries-png in relpath form
        '''
        return jsonify(
            save_layer_outputs(
                load_sig(join(abspath(input_folder), input_path[:-3] + 'npy')),
                model, layer_name, temp_folder, input_path[:-3] + 'npy'))

    @app.route('/predict/<input_path>')
    def get_prediction(input_path):
        with get_evaluation_context():
            return safe_jsonify(
                decode_predictions(
                    model.predict(
                        load_sig(
                            join(abspath(input_folder),
                                 input_path[:-3] + 'npy'))), classes, top))

    return app