Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial MNIST step 7 #5

Closed
cheahengsoon opened this issue May 8, 2021 · 2 comments
Closed

Tutorial MNIST step 7 #5

cheahengsoon opened this issue May 8, 2021 · 2 comments

Comments

@cheahengsoon
Copy link

When I type in predict command.
The error shown
mnist> predict

[!] No index sample, setting random index.

EXCEPTION of type 'ValueError' occurred with message: empty range for randrange() (0,0, 0)
[!] To enable full traceback, run the following command: 'setg debug true'

the sample of my code.
C:\Users\Eng Soon Cheah\Desktop\counterfit-main\counterfit\commands\new.py

import questionary
import os
import importlib
import cmd2
import inspect
import argparse

from counterfit.core.state import CFState
from counterfit.core import config

parser = argparse.ArgumentParser()
# including this print the correct help message


def nix_path(path):
    new_path = "/".join(path.split("\\"))
    return new_path

@cmd2.with_argparser(parser)
@cmd2.with_category("Counterfit Commands")
def do_new(self, args):
    """Optional wizard to aid in creating a new attack target."""
    model_name = questionary.text("Target name:").ask()
    model_name = model_name.replace(" ", "")

    available_frameworks = list(CFState.get_instance().loaded_frameworks.keys())
    framework_choice = questionary.select("Which framework?", choices=available_frameworks).ask()

    if "textattack" in framework_choice:
        framework = "TextTarget"
    elif "art" in framework_choice:
        framework = "ArtTarget"
    else:
        raise ValueError("invalid framework")

    if framework == "TextTarget":
        model_data_type = "text"
    elif framework == "ArtTarget":
        model_data_type = questionary.select("What data type?", choices=["numpy", "image"]).ask()
    else:
        raise ValueError("invalid framework")

    if model_name not in os.listdir(config.targets_path):
        try:
            os.mkdir(f"{config.targets_path}/{model_name}")
            open(f"{config.targets_path}/{model_name}/__init__.py", "w").close()
            with open(f"{config.targets_path}/{model_name}/{model_name}.py", "w") as f:
                f.write(
                    f"""

# Generated by counterfit #

import pickle
import numpy as np
from counterfit.core.targets import ArtTarget


class Mnist(ArtTarget):
    model_name = "mnist"
    model_data_type = "image"
    model_endpoint = "counterfit/targets/tutorial/mnist_sklearn_pipeline.pkl"
    model_input_shape = (1, 28, 28)
    model_output_classes = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
    X = []

    def __init__(self):
        self.clip_values = (0, 255)
        with open(self.model_endpoint, "rb") as f:
            self.model = pickle.load(f)

        self.data_file = "counterfit/targets/tutorial/mnist_784.npz"
        self.sample_data = np.load(self.data_file, allow_pickle=True)

        self.X = self.sample_data["X"]

    def __call__(self, x):
        scores = self.model.predict_proba(x.reshape(x.shape[0], -1))
        return scores.tolist()
"""
                )

            CFState.get_instance().import_targets()
        except Exception as e:

            self.pwarning(f"\n [!] Failed to write target file: {e}.\n")

    else:
        self.pwarning(f"\n [!] {model_name} already exists. Choose a new name.\n")


@moohax
Copy link
Contributor

moohax commented May 10, 2021

Thank you for submitting an issue.

The new command will write a new target to the targets folder. For the tutorial, the new command should remain unaltered.
Check C:\Users\Eng Soon Cheah\Desktop\counterfit-main\counterfit\targets\ for the target after executing the new command.

  1. Revert the new command back to the original.
  2. Instead of using the new command. Create the following folder structure,
C:\Users\Eng Soon Cheah\Desktop\counterfit-main\counterfit\targets\mnist
C:\Users\Eng Soon Cheah\Desktop\counterfit-main\counterfit\targets\mnist\__init__.py
C:\Users\Eng Soon Cheah\Desktop\counterfit-main\counterfit\targets\mnist\mnist_issue.py
  1. Paste the following code into mnist_issue.py
# Generated by counterfit #

import pickle
import numpy as np
from counterfit.core.targets import ArtTarget


class Mnist(ArtTarget):
    model_name = "mnist"
    model_data_type = "image"
    model_endpoint = "counterfit/targets/tutorial/mnist_sklearn_pipeline.pkl"
    model_input_shape = (1, 28, 28)
    model_output_classes = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
    X = []

    def __init__(self):
        self.clip_values = (0, 255)
        with open(self.model_endpoint, "rb") as f:
            self.model = pickle.load(f)

        self.data_file = "counterfit/targets/tutorial/mnist_784.npz"
        self.sample_data = np.load(self.data_file, allow_pickle=True)

        self.X = self.sample_data["X"]

    def __call__(self, x):
        scores = self.model.predict_proba(x.reshape(x.shape[0], -1))
        return scores.tolist()
  1. Make sure all the files required exist and you should be good to go.

Le me know if that worked and I'll close this issue.

In case anyone sees the above error - it usually means that self.X is empty.

@cheahengsoon
Copy link
Author

@moohax you can close the issue. Thank you.

@drhyrum drhyrum closed this as completed May 11, 2021
dlmgary added a commit that referenced this issue Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants