Click here to Skip to main content
15,442,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this is the counterfeit tutorial , I keep getting this error
ValueError: empty range for randrange() (0,0, 0)
EXCEPTION of type 'ValueError' occurred with message: empty range for randrange() (0,0, 0).

they say the problem is occurring in counterfit/counterfit/commands/predict.py", line 72, in do_predict which is this
Python
from counterfit.core.state import CFState
import argparse
import cmd2
import random
import numpy as np
from cmd2.table_creator import Column, SimpleTable, HorizontalAlignment
from typing import Any, List

from counterfit.core.run_scan_utils import get_printable_batch, printable_numpy

parser = argparse.ArgumentParser()
parser.add_argument("-i", "--index", type=int, default=None, help="Send the selected sample to the target model")
parser.add_argument("-s", "--surprise", action="store_true", help="Send a randomly selected sample to the target model")
parser.add_argument("-r", "--result", action="store_true", help="Send the result of the active_attack to the target model")


def set_attack_samples(target, sample_index=0):
    if hasattr(sample_index, "__iter__"):
        # (unused) multiple index
        out = np.array([target.X[i] for i in sample_index])
        batch_shape = (-1,) + target.model_input_shape
    elif type(target.X[sample_index]) is str:
        # array of strings (textattack)
        out = np.array(target.X[sample_index])
        batch_shape = (-1,)
    else:
        # array of arrays (art)
        out = np.atleast_2d(target.X[sample_index])
        batch_shape = (-1,) + target.model_input_shape

    return out.reshape(batch_shape)


@cmd2.with_category("Counterfit Commands")
@cmd2.with_argparser(parser)
def do_predict(self, args):
    """Predict a single sample for the active target"""
    if CFState.get_instance().active_target is None:
        self.pwarning("\n [!] must first `interact` with a target.\n")
        return
    else:
        target = CFState.get_instance().active_target

    if sum([args.surprise, args.index is not None, args.result]) > 1:
        self.pwarning("\n [!] must specify only one of {surprise, index, result}.\n")
        return

    heading1 = "Sample Index"
    if args.surprise:
        sample_index = random.randint(0, len(target.X) - 1)
        samples = set_attack_samples(target, sample_index)

    elif args.index is not None:  # default behavior
        sample_index = args.index
        samples = set_attack_samples(target, sample_index)

    elif args.result:
        try:
            samples = target.active_attack.results['final']['input']
            sample_index = [target.active_attack.attack_id] * len(samples)            
            heading1 = "Attack ID"
        except (KeyError, AttributeError):
            self.pwarning("\n [!] No results found. First 'run' an attack.\n")
            return

    elif target.active_attack is not None and target.active_attack.sample_index is not None:
        sample_index = target.active_attack.sample_index
        samples = set_attack_samples(target, sample_index)        

    else:
        self.pwarning("\n [!] No index sample, setting random index.\n")
        sample_index =  random.randint(0, abs(len(target.X)-1))
        samples = set_attack_samples(target, sample_index)        

    result = target._submit(samples)

    columns: List[Column] = list()
    columns.append(
        Column(
            heading1,
            width=8,
            header_horiz_align=HorizontalAlignment.LEFT,
            data_horiz_align=HorizontalAlignment.RIGHT,
        )
    )
    columns.append(
        Column(
            "Sample",
            width=60,
            header_horiz_align=HorizontalAlignment.CENTER,
            data_horiz_align=HorizontalAlignment.RIGHT,
        )
    )
    columns.append(
        Column(
            "Output Scores\n" + str(target.model_output_classes).replace(',', ''),
            width=30,
            header_horiz_align=HorizontalAlignment.CENTER,
            data_horiz_align=HorizontalAlignment.RIGHT,
        )
    )

    if not hasattr(sample_index, "__iter__"):
        sample_index = [sample_index]

    samples_str = get_printable_batch(target, samples)
    results_str = printable_numpy(result)

    data_list: List[List[Any]] = list()
    for idx, samp, res in zip(sample_index, samples_str, results_str):
        data_list.append([idx, samp, res])

    st = SimpleTable(columns)
    self.poutput("\n" + st.generate_table(data_list, row_spacing=0) + "\n")


I would like to know or given a example of how to do this. here's the GitHub link

Tutorials · Azure/counterfit Wiki · GitHub[^]

What I have tried:

just looking through all the counterfeit files for answers
Posted
Updated 7-Dec-21 22:41pm
v2
Comments
Richard MacCutchan 8-Dec-21 4:39am    
Which is the line (72) that gives the error?
James Rich443 8-Dec-21 9:56am    
sample_index = random.randint(0, abs(len(target.X)-1))
Richard MacCutchan 8-Dec-21 10:24am    
OK, so you need to check the target object to see why that call returns zero.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900