Ejemplo n.º 1
0
def main(input_file_path, stream_size, num_of_asks, output_file_path):
    global bloom_filter
    bx = BlackBox()
    gt_set = set()
    fp = 0
    output_file = open(output_file_path, "wt")
    output_file.write("Time,FPR\n")
    for it in range(num_of_asks):
        stream_users = bx.ask(input_file_path, stream_size)
        for s in stream_users:
            indices = myhashs(s)
            is_not_present = False

            for i in indices:
                if (not bloom_filter[i]):
                    is_not_present = True
                    break

            if (is_not_present):
                for i in indices:
                    bloom_filter[i] = True

            if (not s in gt_set and not is_not_present):
                fp += 1

            gt_set.add(s)
        output_file.write("{},{}\n".format(it, fp / ((it + 1) * stream_size)))
    output_file.close()
    return
Ejemplo n.º 2
0
def main():
    blackBox = BlackBox()
    op = "Time,FPR"
    for i in range(numOfAsks):
        stream_users = blackBox.ask(inputFile, streamSize)
        op += bloom_filtering(i, stream_users)
    writeToFile(op)
Ejemplo n.º 3
0
def driver():
    random.seed(553)
    reservoir = [0] * 100
    sequence_no = 0
    num_of_asks = int(argv[3])
    stream_size = int(argv[2])
    bx = BlackBox()
    results = []

    for _ in range(num_of_asks):
        stream_users = bx.ask(str(argv[1]), stream_size)
        for user in stream_users:
            sequence_no += 1
            if sequence_no <= 100:
                reservoir[sequence_no - 1] = user
            else:
                # reservoir full.
                # choose to keep user with prob 100 / sequence_no.
                p_keep_user = random.randint(0, 100000) % sequence_no
                if p_keep_user < 100:
                    # have to keep user.
                    # replace one elt in reservoir with uniform prob.
                    position_to_replace = random.randint(0, 100000) % 100
                    reservoir[position_to_replace] = user

            if sequence_no % 100 == 0:
                results.append(
                    str(
                        str(sequence_no) + "," + reservoir[0] + "," +
                        reservoir[20] + "," + reservoir[40] + "," +
                        reservoir[60] + "," + reservoir[80]))

    return results
Ejemplo n.º 4
0
def main():
    blackBox = BlackBox()
    op = "Time,Ground Truth,Estimation"
    for i in range(numOfAsks):
        stream_users = blackBox.ask(inputFile, streamSize)
        op += flajolet_martin(i, stream_users)
    writeToFile(op)
Ejemplo n.º 5
0
def flajolet_martin(input_file_path, stream_size, num_of_asks,
                    output_file_path):
    global number_of_hashes
    bx = BlackBox()
    output_file = open(output_file_path, "wt")
    output_file.write("Time,Ground Truth,Estimation\n")
    predicted_sum = 0
    actual_sum = 0
    for it in range(num_of_asks):
        stream_users = bx.ask(input_file_path, stream_size)
        max_number_of_trainling_zeros = [-sys.maxsize] * number_of_hashes
        for s in stream_users:
            hashes = myhashs(s)
            for i, h in enumerate(hashes):
                h = format(h, '016b')
                number_of_trailing_zeros = len(h) - len(h.rstrip('0'))

                if (number_of_trailing_zeros >
                        max_number_of_trainling_zeros[i]):
                    max_number_of_trainling_zeros[i] = number_of_trailing_zeros
        count = calculate_count(max_number_of_trainling_zeros)
        output_file.write("{},{},{}\n".format(it, stream_size, int(count)))
        predicted_sum += count
        actual_sum += stream_size
    print(predicted_sum / actual_sum)
    output_file.close()
    return
Ejemplo n.º 6
0
def driver():
    bx = BlackBox()
    num_of_asks = int(argv[3])
    stream_size = int(argv[2])
    results = []

    for i in range(num_of_asks):
        stream_users = bx.ask(str(argv[1]), stream_size)
        ground_truth = set()
        for user in stream_users:
            ground_truth.add(user)
        estimation = flajolet_martin(stream_users)
        results.append((i, len(ground_truth), int(estimation)))

    sum_estimations = 0
    sum_ground_truth = 0
    for i in results:
        # print(i)
        sum_ground_truth += i[1]
        sum_estimations += i[2]

    print("Final Result = ", i[2] / i[1])

    with open(str(argv[4]), "w") as file:
        file.write("Time,Ground Truth,Estimation")
        for r in results:
            file.write("\n" + str(r[0]) + "," + str(r[1]) + "," + str(r[2]))
        file.close()
def main():
    input_file, output_file = sys.argv[1], sys.argv[4]
    stream_size, num_of_asks = int(sys.argv[2]), int(
        sys.argv[3])  #stream size = 100

    bx = BlackBox()
    seq_num = 0
    window_size = 100
    random.seed(553)
    user_list = []
    with open(output_file, "w") as f:
        f.write("seqnum,0_id,20_id,40_id,60_id,80_id")
        for i in range(num_of_asks):
            stream_users = bx.ask(input_file, stream_size)
            if seq_num == 0:
                user_list += stream_users
                seq_num += stream_size
            else:
                for user in stream_users:
                    seq_num += 1
                    prob = random.randint(0, 100000) % seq_num
                    if prob < window_size:
                        pos = random.randint(0, 100000) % window_size
                        user_list[pos] = user
            f.write("\n{},{},{},{},{},{}".format(seq_num, user_list[0],
                                                 user_list[20], user_list[40],
                                                 user_list[60], user_list[80]))
            print("{},{},{},{},{},{}".format(seq_num, user_list[0],
                                             user_list[20], user_list[40],
                                             user_list[60], user_list[80]))
Ejemplo n.º 8
0
def main():
    input_file, output_file = sys.argv[1], sys.argv[4]
    stream_size, num_of_asks = int(sys.argv[2]), int(sys.argv[3])

    hash_function_num = 16
    m = 69997

    bx = BlackBox()
    boom_filter = [0 for _ in range(m)]
    with open(output_file, "w") as f:
        f.write("Time,FPR")
        for time in range(num_of_asks):
            stream_users = bx.ask(input_file, stream_size)
            visited_users = set()
            FP, TN = 0, 0
            for user in stream_users:
                hash_values = myhashs(user)
                count = 0
                for hash_value in hash_values:
                    if boom_filter[hash_value] == 1:
                        count += 1

                if user not in visited_users:
                    if count == hash_function_num:
                        FP += 1
                    else:
                        TN += 1
                    visited_users.add(user)

                for hash_value in hash_values:
                    boom_filter[hash_value] = 1
            FPR = float(FP / (FP + TN))
            f.write("\n{},{}".format(time, FPR))
Ejemplo n.º 9
0
def getServices(body, userId):
    userService = UserService(userServiceApi+"?format=json&agencyId="+agencyId+"&userId="+str(userId))
    
    print(" [x] Loading user service "+userServiceApi+"?format=json&agencyId="+agencyId+"&userId="+str(userId))
    box = BlackBox(serviceApi, userService)
    services = box.getWidgets(body)
    for service in services:
        service["AgencyID"] = agencyId
    result = json.dumps(services)
    return result
Ejemplo n.º 10
0
def main():
    blackbox = BlackBox(config="repack/config.ini")
    #blackbox.clearCache()
    blackbox.loadData()

    blackbox.train()
    blackbox.predict()
    #blackbox.updateConfig("POLYFIT", "poly_degree", 15, wipe=False)
    print(
        f"[+] {blackbox.accuracy}, {blackbox.test_hash}, {blackbox.train_hash}"
    )
    print(blackbox.accuracy_topf)
Ejemplo n.º 11
0
 def __init__(self, files, debugMode):
     self.__cards = None
     self.__tableCards = None
     self.__table = None
     self.__players = None
     self.doCallback = False
     self.endGameCallback = None
     self.win = True
     self.debugMode = debugMode
     self.slackMessage = ""
     self.blackbox = BlackBox(files, 6, 7, 7, 6)
     self.playerName = -1
     self.response = [0, 0, 0, 0, 0, 0]
     self.reload = 0
     self.betAmount = 100
def main():
    input_file, output_file = sys.argv[1], sys.argv[4]
    stream_size, num_of_asks = int(sys.argv[2]), int(sys.argv[3])

    hash_function_num = 1000
    groups_len = 10
    hash_functions_per_group = int(hash_function_num / groups_len)

    bx = BlackBox()

    with open(output_file, "w") as f:
        f.write("Time,Ground Truth,Estimation")
        est_all = 0
        gt_all = 0
        for time in range(num_of_asks):
            gt = set()
            stream_users = bx.ask(input_file, stream_size)
            all_hash_values = []
            for user in stream_users:
                gt.add(user)
                hash_values = myhashs(user)
                all_hash_values.append(hash_values)

            estimates = []
            for i in range(hash_function_num):
                longest_trailing_zeros = 0
                for hash_values in all_hash_values:
                    hash_value = hash_values[i]
                    trailing_zeros = 0
                    while hash_value & 1 == 0 and hash_value > 0:
                        trailing_zeros += 1
                        hash_value = hash_value >> 1
                    longest_trailing_zeros = max(trailing_zeros,
                                                 longest_trailing_zeros)
                estimates.append(2**longest_trailing_zeros)

            estimates_avg = []
            for i in range(groups_len):
                sum_est = 0
                for j in range(hash_functions_per_group):
                    sum_est += estimates[i * hash_functions_per_group + j]
                estimates_avg.append(float(sum_est / hash_functions_per_group))
            estimates_avg.sort()
            estimate = round(estimates_avg[int(groups_len / 2)])

            est_all += estimate
            gt_all += len(gt)
            f.write("\n{},{},{}".format(time, len(gt), estimate))
Ejemplo n.º 13
0
def driver():
    bx = BlackBox()
    num_of_asks = int(argv[3])
    stream_size = int(argv[2])
    fpr = []
    bit_array = [0 for _ in range(69997)]
    previous_users = set()

    for i in range(num_of_asks):
        stream_users = bx.ask(str(argv[1]), stream_size)
        batch_fpr = bloom_filter(bit_array, stream_users, previous_users)
        fpr.append((i, batch_fpr))

    for f in fpr:
        print(f)

    with open(str(argv[4]), "w") as file:
        file.write("Time,FPR")
        for f in fpr:
            file.write("\n" + str(f[0]) + "," + str(f[1]))
        file.close()
Ejemplo n.º 14
0
        return gen_func

    def one_run(stream):
        fp, tn = 0., 0.
        filters = set()
        prev = set()
        for s in stream:
            h = set(myhashs(s))
            if h.issubset(filters) == True and set(s).issubset(prev) == False:
                fp += 1
            if h.issubset(filters) == False and set(s).issubset(prev) == False:
                tn += 1
            filters |= h
            prev.add(s)
        return float(fp) / float((fp + tn))


if __name__ == "__main__":
    infile = sys.argv[1]
    stream_size = int(sys.argv[2])
    num_asks = int(sys.argv[3])
    outfile = sys.argv[4]
    t0 = time.time()
    with open(outfile, 'w+') as f:
        f.write("Time,FPR\n")
        for i in range(num_asks):
            stream = BlackBox().ask(infile, stream_size)
            fpr = Task1.one_run(stream)
            f.write(str(i) + ',' + str(fpr) + '\n')
    print("Duration:", time.time() - t0)
Ejemplo n.º 15
0
        for s in stream:
            h = myhashs(s)
            hash_str = list(map(lambda x: bin(x), h))
            tzeros = list(map(lambda x: len(x) - len(x.rstrip("0")), hash_str))
            longest_tzeros = [
                max(a, b) for a, b in zip(longest_tzeros, tzeros)
            ]
        data = sorted([2**r for r in longest_tzeros])
        data = [
            data[GROUP_SIZE * i:GROUP_SIZE * (i + 1)] for i in range(NUM_GROUP)
        ]
        avg = list(map(lambda x: sum(x) / len(x), data))
        med = int(len(avg) / 2)
        return round(avg[med])


if __name__ == "__main__":
    infile = sys.argv[1]
    stream_size = int(sys.argv[2])
    num_asks = int(sys.argv[3])
    outfile = sys.argv[4]
    previous_user = BlackBox().ask(infile, stream_size)
    t0 = time.time()
    with open(outfile, 'w+') as f:
        f.write("Time,FPR\n")
        for i in range(num_asks):
            stream = BlackBox().ask(infile, stream_size)
            fpr = Task2.one_run(stream)
            f.write(str(i) + ',' + str(stream_size) + ',' + str(fpr) + '\n')
    print("Duration:", time.time() - t0)
def testBlackBoxSentimentAnalysis(data_type, model_type, num_samples):
    start = time.time()
    ## Import glove-vectors once
    if (data_type == 'RT' or data_type == 'IMDB'):
        glove_vectors = json.load( open( "glove_final.json".format(data_type, data_type), "rb") )
    elif (data_type == 'Kaggle'):
        glove_vectors = json.load( open( "datasets/{}/glove_kaggle.json".format(data_type, data_type), "rb") )

    
    embed_map = pickle.load( open( "datasets/{}/{}_embed_map.p".format(data_type, data_type), "rb" ) )

    ## Get Dataset (3 types: IMDB, RT, Kaggle)
    if (data_type == 'IMDB'):
        data = pickle.load( open( "datasets/IMDB/IMDB_tokens.p", "rb" ) )
    elif (data_type == 'RT'):
        data = pickle.load( open( "datasets/RT/RT_tokens.p", "rb" ) )
    elif(data_type == 'Kaggle'):
        data = pickle.load(open("datasets/Kaggle/Kaggle_tokens.p","rb"))

    ## Get Model (3 types: LR, LSTM, CNN)
    if (model_type == 'LR'):
        if (data_type == 'IMDB'):
            model = pickle.load( open( "models/LR/LR_SA_IMDB.p", "rb" ))
        elif(data_type == 'RT'):
            model = pickle.load( open( "models/LR/LR_SA_RT.p", "rb" ))
        elif(data_type == 'Kaggle'):
            model = pickle.load( open( "models/LR/LR_TCD_Kaggle.p", "rb" ))
    elif (model_type == 'LSTM'):
        if (data_type == 'IMDB'):
            model = pickle.load( open( "models/LSTM/LSTM_SA_IMDB.p", "rb" ))
        elif(data_type == 'RT'):
            model = pickle.load( open( "models/LSTM/LSTM_SA_RT.p", "rb" ))
        elif(data_type == 'Kaggle'):
            model = pickle.load( open( "models/LSTM/LSTM_TCD_Kaggle.p", "rb" ))
    elif (model_type == 'CNN'):
        if (data_type == 'IMDB'):
            model = pickle.load( open( "models/CNN/CNN_SA_IMDB.p", "rb" ))
        elif(data_type == 'RT'):
            model = pickle.load( open( "models/CNN/CNN_SA_RT.p", "rb" ))
        elif(data_type == 'Kaggle'):
            model = pickle.load( open( "models/CNN/CNN_TCD_Kaggle.p", "rb" ))

    #---- DONE LOADING ----------
    end = time.time()
    print("DONE LOADING: {} minutes".format(np.round((end-start)/60),4))

    num_successes = 0 
    sample_id = 1
    percent_perturbed = []

    pos_samples = data['test']['pos'][0:num_samples]
    neg_samples = data['test']['neg'][0:num_samples]


    num_successes = 0 
    total_docs = 0



    for token_list in pos_samples:
        sentence = TreebankWordDetokenizer().detokenize(token_list)
        y = get_blackbox_classifier_score(model_type, sentence)
        y_class = np.round(y,0)
        # print(sentence)
        # print('Original Score: {} | Label: {}'.format(y,y_class))

        blackbox = BlackBox(token_list,y_class,0.8, model_type, glove_vectors, data_type)
        res = blackbox.blackBoxAttack()
        if res != None:
            num_successes += 1
            percent_perturbed.append(res[1])
        total_docs += 1

    for token_list in neg_samples:
        sentence = TreebankWordDetokenizer().detokenize(token_list)
        y = get_blackbox_classifier_score(model_type, sentence)
        y_class = np.round(y,0)
        # print(sentence)
        # print('Original Score: {} | Label: {}'.format(y,y_class))

        blackbox = BlackBox(token_list,y_class,0.8, model_type, glove_vectors, data_type)
        res = blackbox.blackBoxAttack()
        if res != None:
            num_successes += 1
            percent_perturbed.append(res[1])
            # print("Successful adversary. Fraction of original input perturbed: {}".format(np.round(percent_perturbed,2)))
        total_docs += 1
    

    total_docs = 2 * num_samples
    success_rate = np.round((num_successes/total_docs)*100,3)
    perturb_rate = np.round(np.mean(percent_perturbed)*100,3)
    print("Avg % Perturbed: {}".format(perturb_rate))
    print("{} | {} | {}".format(data_type, model_type, success_rate))



# testBlackBoxSentimentAnalysis('RT', 'Google_NLP')
# testBlackBoxSentimentAnalysis('RT', 'IBM_Watson')
# testBlackBoxSentimentAnalysis('RT', 'Microsoft_Azure')
# testBlackBoxSentimentAnalysis('RT', 'AWS_Comprehend')
# testBlackBoxSentimentAnalysis('RT', 'FB_fastText')

# testBlackBoxSentimentAnalysis('IMDB', 'Google_NLP')
# testBlackBoxSentimentAnalysis('IMDB', 'IBM_Watson')
# testBlackBoxSentimentAnalysis('IMDB', 'Microsoft_Azure')
# testBlackBoxSentimentAnalysis('IMDB', 'AWS_Comprehend')
# testBlackBoxSentimentAnalysis('IMDB', 'FB_fastText')

# testBlackBoxSentimentAnalysis('Kaggle', 'Google_NLP', 2)
# testBlackBoxSentimentAnalysis('Kaggle', 'IBM_Watson', 2)
# testBlackBoxSentimentAnalysis('Kaggle', 'Microsoft_Azure',10)
# testBlackBoxSentimentAnalysis('Kaggle', 'AWS_Comprehend',10)
# testBlackBoxSentimentAnalysis('Kaggle', 'FB_fastText',2) 16.2%P | 75%A
Ejemplo n.º 17
0
 def test_apiConnection(self):
     box = BlackBox(blackboxTests.apiUrl, TestUserService('anonymous'))
     content = box.getContent()
     self.assertTrue(len(content) > 0)
Ejemplo n.º 18
0
 def test_singleSearchOnServiceLowercase(self):
     box = BlackBox(blackboxTests.apiUrl, TestUserService('anonymous'))
     services = box.getServices("application")
     self.assertTrue(len(services) == 1)
Ejemplo n.º 19
0
 def test_serviceWidgetMapperAllFields(self):
     userService = TestUserService('allfields')
     box = BlackBox(blackboxTests.apiUrl, userService)
     service = box.getServices("application")[0]
     mapper = ServiceWidgetMapper(userService)
     result = mapper.getWidget(service)
Ejemplo n.º 20
0
random.seed(553)

## Defining the path to the data. 
dataSetPath = sys.argv[1]

## Defining the stream size.
streamSize = int(sys.argv[2])

## Defining the number of asks.
numAsks = int(sys.argv[3])

## Defining the path to the output file.
outfilePath = sys.argv[4]

## Initiate an instance of blackbox.
bxInstance = BlackBox()

## Initiating a list to hold the user ID's.
userIDList = []

## Defining the maximum size of the list.
maxListSize = 100

## Initialising  a variable to hold the number 
## of elements seen till now.
numElems = 0

## Function to run the reservoir sampling.
def reservoirSampling(userStream):

	## Global parameters.
Ejemplo n.º 21
0
 def test_testWithCamping(self):
     userService = TestUserService('allfields')
     box = BlackBox("http://mstrong.info/api/views/dnspr.json", userService)
     services = box.getServices("camping")
Ejemplo n.º 22
0
    # calculate FPR = FP / (FP + TN)
    false_positive_rate = 0.0 if (false_pos + true_neg
                                  == 0) else false_pos / (false_pos + true_neg)

    with open(output, 'a') as f:
        f.write(str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
        f.write("," + str(float(false_positive_rate)))
        f.write("\n")
        print(
            str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + " " +
            str(float(false_positive_rate)))


if __name__ == '__main__':
    file = "users.txt"
    num = 100
    times = 30
    output = "Bloomfilter_output.csv"

    filter_array = [0 for i in range(69997)]
    seen_user = set()

    with open(output, 'w+') as f:
        f.write("Time,FPR")
        f.write("\n")

    from blackbox import BlackBox
    bx = BlackBox()
    for i in range(times):
        bloom(bx.ask(file, num))
Ejemplo n.º 23
0
def main(_):
  start_t = time.time()
  try:
      welcome_message()
      # import data
      mdl = BlackBox(FLAGS)
      # NOTE this will work with format: mdl.oracle = (image, pred_val, true_val)
  except:
      logger.error('black blox training failed...........shutting down!')
      return
  logger.info('obtained black box training data')
  logger.info('oracle data capture time: %f' %(time.time()-start_t))
  mnist = mdl.oracle
  prep_t = time.time()
  with tf.device('/cpu:0'):
      # translate into tensorflow style nparrays
      x_vals = image_list_to_np(mnist, 0)
      true_vals = image_list_to_np(mnist,2)
      
      # yvals converted to one hot vector
      y_vals = [ x[1] for x in mnist ]
      y_vals = [ one_hot(i) for i in y_vals]
      y_vals = np.array(y_vals)
      y_vals = y_vals.reshape((len(y_vals),10))
      
      # split training and test data into nparrays
      train_images, train_labels, test_images, test_labels =split_train_data(x_vals, y_vals, FLAGS.split)
 
  # Tensorflow variable setup

  # input vector
  x = tf.placeholder(tf.float32, [None, 784])
  # y output vector
  y_ = tf.placeholder(tf.float32, [None, 10])
  # build the graph for the deep net
  y_conv, keep_prob = deepnn(x)
  # define loss function -> cross entropy for now with softmax
  cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
  # train step, 1e-4 is default, best to use -2/-3 depending on time
  train_step = tf.train.AdamOptimizer(FLAGS.optimize).minimize(cross_entropy)
  # define correct prediction vectore and accuracy comparison
  correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
 
  logger.info('training sets: %d test sets: %d' % (len(train_images),len(test_images)))
  cnn_saver = tf.train.Saver()
  logger.info('cpu preprocessing time: %f' %(time.time()-prep_t))
  train_t = time.time()
  logger.info('starting adversarvial model training')
  #begin training with session
  with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    # TODO create batching loop
    for i in range(FLAGS.iters):
      #sanity check on accuracy should be going down -> necessary but not sufficient
      if i % int((FLAGS.iters/5)) == 0:
        train_accuracy = accuracy.eval(feed_dict={x: train_images, y_: train_labels, keep_prob: 1.0})
        logger.info('step %d, training accuracy %g' % (i, train_accuracy))
      #update rule - softmax vector obtained for checking
      trainer, softmax = sess.run([train_step, cross_entropy],feed_dict={x: train_images, y_: train_labels, keep_prob: 0.5})
    logger.info('adversarial model has been trained')
    # snag the gradient vector wrt X inputs
    grads = tf.gradients(cross_entropy, [x])
    jacobian = sess.run(grads, feed_dict={x:test_images, y_:test_labels, keep_prob: 1.0})
    #use test data as input for perturbations
    #test_ = tf.argmax(y_,1)
    #test_vals = test_.eval(feed_dict={y_:test_labels})
    # use this...
    verify = sess.run(tf.argmax(test_labels, 1))
    #for ver in zip(verify, test_vals):
        #print(ver, test_vals)
    pred_ = tf.argmax(y_conv,1)
    #vify = tf.argmax(y_,1)
    pred_vals = pred_.eval(feed_dict={x:test_images, y_:test_labels, keep_prob:1.0})
    #vify_vals = vify.eval(feed_dict={x:test_images, y_:test_labels, keep_prob:1.0})
    true_pred = [ (pxl, p) for pxl, p, r in zip(test_images, pred_vals, verify) if p==r ]
    logger.info('true positive test exemplars: %f' %(len(true_pred)))
    logger.results('adversary accuracy: %g' % (accuracy.eval(feed_dict={x: test_images, y_: test_labels, keep_prob: 1.0})))
    #setup the goodfellow attack iterations
    logger.info('attack model train time: %f' %(time.time()-train_t))
    pert_t = time.time()
    adv_list = []
    for idx,pos in enumerate(true_pred):
      for epsilon in np.linspace(0.025,.25,num=FLAGS.augments):
          xp = goodfellow_mod(np.array(pos[0]), jacobian[0][idx], epsilon)
          prime_label = one_hot(int(pos[1]))
          xprime = np.array(xp).reshape((1,784))
          #xprime = xprime.reshape((1,784))
          yprime = np.array(prime_label).reshape((1,10))
          #yprime = yprime.reshape((1, 10))
          pred_vals = pred_.eval(feed_dict={x: xprime, y_: yprime, keep_prob:1.0})
          acc = accuracy.eval(feed_dict={x: xprime, y_: yprime, keep_prob: 1.0})
          #corr = sess.run(mdl.y, feed_dict={x:xprime})
          if acc < 1.0:
              #plt.figure(1)
              #plt.subplot(121)
              #img = pos[0].reshape((28,28))
              #plt.imshow(img)
             
              #plt.subplot(122)
              #img1 = xp.reshape((28,28))
              #plt.imshow(img1)
              #print(pos[1], np.argmax(yprime), pred_vals, epsilon, np.sum(xp), np.sum(pos[0]), np.sum(xprime))
              #plt.show()
              adv_list.append((xprime, np.argmax(yprime), pred_vals, epsilon, pos[0]))
              #logger.results('YES adversary accuracy: %g %f' % (acc, epsilon))
              break
        #can do this each iteration - or as a whole...at this point timing doesnt matter, but will
    logger.results('true positive adversary count: %f' % (float(len(adv_list))/float(len(true_pred))))

    logger.info('distortion vector time: %f' %(time.time()-pert_t))
    att_t = time.time()
    # save model to file
    cnn_saver_path = cnn_saver.save(sess, 'cnn_saver.ckpt')
    
    # at this point adv_list is a tuple (x modifed image, y label, true label, epsilon found) 
    adv_images = [ a[0] for a in adv_list ]
    l = len(adv_list)
    adv_images = np.array(adv_images).reshape((l,784))
    #adv_images = adv_images.reshape((l, 784))
    adv_labels = [ a[1] for a in adv_list ]
    adv_labels = [ one_hot(int(v)) for v in adv_labels ]
    adv_labels = np.array(adv_labels).reshape((l,10))

    adv_real = [ a[2] for a in adv_list ]
    adv_real = np.array(adv_real)
    #adv_labels = adv_labels.reshape((l,10))
    adv_epsilon = [ a[3] for a in adv_list ]
    adv_epsilon = np.array(adv_epsilon)

    adv_real_image = [ a[4] for a in adv_list ]

    # test for transferability
    adv_real = mdl.sess.run(tf.argmax(adv_labels,1))
    adv_ = tf.argmax(mdl.y,1)
    adv_pred = mdl.sess.run(adv_, feed_dict={mdl.x: adv_images})
    winners = []
    epsilon_tracker = collections.defaultdict(int)
    for idx, (a, l, r) in enumerate(zip(adv_pred, adv_labels, adv_real)):
        #print( a, l, r, a == r)
        if a != r:
            #logger.info('found adversarial example: %g %g' % (a, r))
            winners.append(idx)
            epsilon_tracker[adv_epsilon[idx]] += 1
    logger.info('attack results time: %f' %(time.time()-att_t))
    logger.info('****************** results **************')
    logger.results('black box adversarial attack transferability: %g' % (1 - sess.run(mdl.accuracy, feed_dict={mdl.x: adv_images,mdl.y_: adv_labels})))
    for d,v in sorted(epsilon_tracker.items()):
        logger.results('epsilon %s %s' % (d,v))
    # grab first two success stories and show them -> lets assume two or error handle later
    adv_pic0 = adv_images[winners[0]].reshape((28,28))
    adv_pic0_real = adv_real_image[winners[0]].reshape((28,28))
    rando = random.randint(1,(len(winners)-1))
    adv_pic1 = adv_images[winners[rando]].reshape((28,28))
    adv_pic1_real = adv_real_image[winners[rando]].reshape((28,28))
    true_pic = mdl.pictrue
    false_pic = mdl.picfalse
    labels = ['ORIGINAL NEURAL NET CORRECT ON THIS %s' %( mdl.pictruelabel[0]), 'ORIGINAL NEURAL NET THOUGHT UNTAMPERED %s WAS %s'% (mdl.picfalselabel[1], mdl.picfalselabel[0]), 'ORIGINAL IMAGE %s' % (adv_real[winners[0]]), 'ORIGINAL NET THOUGHT %s'%(adv_pred[winners[0]]),'ORIGINAL IMAGE %s' % (adv_real[winners[rando]]), 'ORIGINAL NET THOUGHT %s' % (adv_pred[winners[rando]]) ]
    logger.info('total program run time: %f' %(time.time()-start_t))
    if not FLAGS.nograph:
      graphics([true_pic, false_pic, adv_pic0_real, adv_pic0, adv_pic1_real, adv_pic1], labels)
Ejemplo n.º 24
0

def reservoir(seqNum, data):
    global reservoirList
    count = seqNum
    if len(reservoirList) == 0:
        reservoirList = data
        count += len(data)
    else:
        for d in data:
            count += 1
            prob = random.randint(0, 100000) % count
            if (prob < streamSize):
                pos = random.randint(0, 100000) % streamSize
                reservoirList[pos] = d
    return str(count) + "," + str(reservoirList[0]) + "," + str(
        reservoirList[20]) + "," + str(reservoirList[40]) + "," + str(
            reservoirList[60]) + "," + str(reservoirList[80] + "\n")


if __name__ == "__main__":
    blackBox = BlackBox()
    random.seed(553)
    op = "seqnum,0_id,20_id,40_id,60_id,80_id\n"
    for i in range(numOfAsks):
        data = blackBox.ask(inputFile, streamSize)
        op += reservoir(i * streamSize, data)
    writeToFile(op)
    end = time.time()
    print("Duration:" + str(round(end - start, 2)))