Beispiel #1
0
def visualize_attack():
    """Run selected attack on uploaded image.
    """
    form = VisualizeAttackForm()
    if form.validate_on_submit():
        index_model = int(form.model.data)
        index_attack = int(form.attacks.data)
        img = Image.open(form.image.data)

        width, height = img.size

        classifier_id = int(form.model.data)
        attack_id = int(form.attacks.data)

        ratio = min(MAX_WIDTH / 100, MAX_HEIGHT / 100)

        (original_image, result_image, _, _, _) = run_attack(img,
                                                             classifier_id,
                                                             attack_id,
                                                             scale=ratio)

        result_file = save_image(result_image,
                                 path="tmp",
                                 output_size=(MAX_WIDTH, MAX_HEIGHT))
        original_file = save_image(original_image,
                                   path="tmp",
                                   output_size=(MAX_WIDTH, MAX_HEIGHT))

        # Get predictions before and after running
        # the attack
        preds = []
        probs = []
        size = 5  # top 5 predictions
        if form.classify.data:
            _, original_label = predict(img, classifier_id)
            _, result_label = predict(result_image, classifier_id)

            preds.append([original_label[i][1] for i in range(size)])
            preds.append([result_label[i][1] for i in range(size)])
            probs.append(
                [np.round(original_label[i][2] / 100, 4) for i in range(size)])
            probs.append(
                [np.round(result_label[i][2] / 100, 4) for i in range(size)])

        flash("Attack successully run!", 'success')

        return render_template('visualize_attack.html',
                               form=form,
                               image_file=original_file,
                               result_file=result_file,
                               index_model=index_model,
                               index_attack=index_attack,
                               preds=preds,
                               probs=probs,
                               size=size)
    return render_template('visualize_attack.html', form=form)
Beispiel #2
0
def form_predict():
    form = PredictForm()

    if form.validate_on_submit():
        index_model = int(form.model.data)
        paper = pretrained_classifiers[index_model].paper
        name = pretrained_classifiers[index_model].name
        img = Image.open(form.image.data)
        width, height = img.size

        # ratio is used to preserve the aspect-ratio
        ratio = min(MAX_WIDTH / 100, MAX_HEIGHT / 100)

        image, label = predict(img, int(form.model.data), scale=ratio)
        image_file = save_image(image,
                                path="tmp",
                                output_size=(MAX_WIDTH, MAX_HEIGHT))

        flash("Done!", 'success')

        return render_template('predict.html',
                               title='Classify an image.',
                               image_file=image_file,
                               form=form,
                               label=label,
                               index_model=index_model,
                               paper=paper,
                               name=name)
    return render_template('predict.html',
                           title='Classify an image.',
                           form=form)
Beispiel #3
0
def account():
    form = UpdateAccountForm()

    if form.validate_on_submit():
        if form.image.data:
            image_file = save_image(form.image.data, path="profile_pictures")
            current_user.image_file = image_file

        current_user.username = form.username.data
        current_user.email = form.email.data
        db.session.commit()
        flash("Your account has been successfully updated!", 'success')
        return redirect(url_for('account'))

    elif request.method == 'GET':
        form.username.data = current_user.username
        form.email.data = current_user.email

    image_file = url_for('static',
                         filename=f"profile_pictures/"
                         f"{current_user.image_file}")

    page = request.args.get('page', 1, type=int)
    models = Classifier.query.filter_by(user=current_user)\
                             .order_by(Classifier.upload_date.desc())\
                             .paginate(page=page, per_page=5)

    return render_template('account.html',
                           title='Account',
                           image_file=image_file,
                           form=form,
                           models=models)
Beispiel #4
0
def visualize_attack():
    """Run selected attack on uploaded image.

    TODO
    ----
    - For now, we don't have any logic that goes over what specific
    attack was selected. This can be easily fixed, but because we ran
    out of time, we didn't implement it.

    - The way the path is passed to the attack method is also messy:
    this can be done more elegantly using url_for to avoid problems
    that would happen on deployment (I think Docker is weird with this
    sort of stuff), and anyway it's bad practice to hardcode a path like
    this. I'll add it as an issue after the commit.
    """
    form = VisualizeAttackForm()
    if form.validate_on_submit():
        index = len(form.model.choices) - int(form.model.data)
        image_file = save_image(form.image.data,
                                path="tmp",
                                output_size=(400, 400))

        result_file = gray(f"./loki/static/tmp/" f"{image_file}")
        flash("Attack successully run!", 'success')

        return render_template('visualize_attack.html',
                               form=form,
                               image_file=image_file,
                               result_file=result_file,
                               index=index)
    return render_template('visualize_attack.html', form=form)
Beispiel #5
0
def predict():
    form = PredictForm()

    if form.validate_on_submit():
        index = int(form.model.data) - 1

        image_file = save_image(form.image.data, path="tmp")
        path = url_for('static', filename=f"tmp/" f"{image_file}")
        classifier = IR()
        label = classifier.predict(path)

        flash("Done!", 'success')

        return render_template('predict.html',
                               title='Classify an image.',
                               image_file=image_file,
                               form=form,
                               label=label,
                               index=index)
    return render_template('predict.html',
                           title='Classify an image.',
                           form=form)