Beispiel #1
0
def push():
    """
    Create a pre-trained model from model store
    """
    model_id = flask.request.args.get('id')
    model_grand_list = app.config['store_cache'].read()
    found = False
    if model_grand_list is not None:
        for store in model_grand_list:
            for model in model_grand_list[store]['model_list']:
                if model['id'] == model_id:
                    url = model_grand_list[store]['base_url']
                    directory = model['dir_name']
                    found = True
                    break
            if found:
                break
    if not found:
        return 'Unable to find requested model', 404
    else:
        progress = Progress(model_id)
        weights, model, label, meta_data, python_layer = retrieve_files(url, directory, progress)
        job = PretrainedModelJob(
            weights,
            model,
            label,
            meta_data['framework'],
            username=auth.get_username(),
            name=meta_data['name']
        )
        scheduler.add_job(job)
        response = flask.make_response(job.id())
        return response
Beispiel #2
0
def push():
    """
    Create a pre-trained model from model store
    """
    model_id = flask.request.args.get('id')
    model_grand_list = app.config['store_cache'].read()
    found = False
    if model_grand_list is not None:
        for store in model_grand_list:
            for model in model_grand_list[store]['model_list']:
                if model['id'] == model_id:
                    url = model_grand_list[store]['base_url']
                    directory = model['dir_name']
                    found = True
                    break
            if found:
                break
    if not found:
        return 'Unable to find requested model', 404
    else:
        progress = Progress(model_id)
        weights, model, label, meta_data, python_layer = retrieve_files(
            url, directory, progress)
        job = PretrainedModelJob(weights,
                                 model,
                                 label,
                                 meta_data['framework'],
                                 username=auth.get_username(),
                                 name=meta_data['name'])
        scheduler.add_job(job)
        response = flask.make_response(job.id())
        return response
Beispiel #3
0
def new():
    """
    Upload a pretrained model
    """
    labels_path = None
    mean_path = None
    framework = None

    form = flask.request.form
    files = flask.request.files

    if 'framework' not in form:
        framework = "caffe"
    else:
        framework = form['framework']

    if 'job_name' not in flask.request.form:
        raise werkzeug.exceptions.BadRequest('Missing job name')
    elif str(flask.request.form['job_name']) is '':
        raise werkzeug.exceptions.BadRequest('Missing job name')

    if framework == "caffe":
        weights_path, model_def_path = validate_caffe_files(files)
    else:
        weights_path, model_def_path = validate_torch_files(files)

    if str(flask.request.files['labels_file'].filename) is not '':
        labels_path = get_tempfile(flask.request.files['labels_file'], ".txt")

    if str(flask.request.files['mean_file'].filename) is not '':
        mean_path = get_tempfile(flask.request.files['mean_file'], ".prototxt")

    job = PretrainedModelJob(weights_path,
                             model_def_path,
                             labels_path,
                             mean_path,
                             framework,
                             form["image_type"],
                             form["resize_mode"],
                             form["height"],
                             form["width"],
                             username=utils.auth.get_username(),
                             name=flask.request.form['job_name'])
    scheduler.add_job(job)

    job.wait_completion()

    weights_job = WeightsJob(job,
                             name=flask.request.form['job_name'],
                             username=utils.auth.get_username())
    scheduler.add_job(weights_job)

    return flask.redirect(flask.url_for('digits.views.home', tab=3)), 302
Beispiel #4
0
def new():
    """
    Upload a pretrained model
    """
    labels_path = None
    framework = None

    form = flask.request.form
    files = flask.request.files

    if 'framework' not in form:
        framework = "caffe"
    else:
        framework = form['framework']

    if 'job_name' not in flask.request.form:
        raise werkzeug.exceptions.BadRequest('Missing job name')
    elif str(flask.request.form['job_name']) is '':
        raise werkzeug.exceptions.BadRequest('Missing job name')

    if framework == "caffe":
        weights_path, model_def_path = validateCaffeFiles(files)
    else:
        weights_path, model_def_path = validateTorchFiles(files)

    if str(flask.request.files['labels_file'].filename) is not '':
        labels_path = get_tempfile(flask.request.files['labels_file'], ".txt")

    job = PretrainedModelJob(
        weights_path,
        model_def_path,
        labels_path,
        framework,
        username=utils.auth.get_username(),
        name=flask.request.form['job_name'],
    )

    scheduler.add_job(job)

    return flask.redirect(flask.url_for('digits.views.home', tab=3)), 302
Beispiel #5
0
def upload_archive():
    """
    Upload archive
    """
    files = flask.request.files
    archive_file = get_tempfile(files["archive"], ".archive")

    if tarfile.is_tarfile(archive_file):
        archive = tarfile.open(archive_file, 'r')
        names = archive.getnames()
    elif zipfile.is_zipfile(archive_file):
        archive = zipfile.ZipFile(archive_file, 'r')
        names = archive.namelist()
    else:
        return flask.jsonify({"status": "Incorrect Archive Type"}), 500

    if "info.json" in names:

        # Create a temp directory to storce archive
        tempdir = tempfile.mkdtemp()
        archive.extractall(path=tempdir)

        with open(os.path.join(tempdir, "info.json")) as data_file:
            info = json.load(data_file)

        valid, key = validate_archive_keys(info)

        if valid is False:
            return flask.jsonify(
                {"status": "Missing Key '" + key + "' in info.json"}), 500

        # Get path to files needed to be uploaded in directory
        weights_file = os.path.join(tempdir, info["snapshot file"])

        if "model file" in info:
            model_file = os.path.join(tempdir, info["model file"])
        elif "network file" in info:
            model_file = os.path.join(tempdir, info["network file"])
        else:
            return flask.jsonify(
                {"status": "Missing model definition in info.json"}), 500

        labels_file = os.path.join(tempdir, info["labels file"])

        # Upload the Model:
        job = PretrainedModelJob(weights_file,
                                 model_file,
                                 labels_file,
                                 info["framework"],
                                 username=utils.auth.get_username(),
                                 name=info["name"])

        scheduler.add_job(job)
        job.wait_completion()

        # Delete temp directory
        shutil.rmtree(tempdir, ignore_errors=True)

        return flask.jsonify({"status": "success"}), 200
    else:
        return flask.jsonify({"status": "Missing or Incorrect json file"}), 500
Beispiel #6
0
def upload_archive():
    """
    Upload archive
    """
    files = flask.request.files
    archive_file = get_tempfile(files["archive"],".archive");

    if tarfile.is_tarfile(archive_file):
        archive = tarfile.open(archive_file,'r')
        names = archive.getnames()
    elif zipfile.is_zipfile(archive_file):
        archive = zipfile.ZipFile(archive_file, 'r')
        names = archive.namelist()
    else:
        return flask.jsonify({"status": "Incorrect Archive Type"}), 500

    if "info.json" in names:

        # Create a temp directory to storce archive
        tempdir = tempfile.mkdtemp()
        archive.extractall(path=tempdir)

        with open(os.path.join(tempdir, "info.json")) as data_file:
            info = json.load(data_file)

        valid, key = validate_archive_keys(info)

        if valid is False:
            return flask.jsonify({"status": "Missing Key '"+ key +"' in info.json"}), 500

        # Get path to files needed to be uploaded in directory
        weights_file = os.path.join(tempdir, info["snapshot file"])

        if "model file" in info:
            model_file   = os.path.join(tempdir, info["model file"])
        elif "network file" in info:
            model_file   = os.path.join(tempdir, info["network file"])
        else:
            return flask.jsonify({"status": "Missing model definition in info.json"}), 500

        labels_file  = os.path.join(tempdir, info["labels file"])

        # Upload the Model:
        job = PretrainedModelJob(
            weights_file,
            model_file ,
            labels_file,
            info["framework"],
            username = utils.auth.get_username(),
            name = info["name"]
        )

        scheduler.add_job(job)
        job.wait_completion()

        # Delete temp directory
        shutil.rmtree(tempdir, ignore_errors=True)

        return flask.jsonify({"status": "success"}), 200
    else:
        return flask.jsonify({"status": "Missing or Incorrect json file"}), 500