Python tensorflow.placeholder() Examples
The following are 30
code examples of tensorflow.placeholder().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.
Example #1
Source File: test_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) orig_labs = np.argmax(self.sess.run(self.model(x_val)), axis=1) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), orig_labs] = 1 x = tf.placeholder(tf.float32, x_val.shape) y = tf.placeholder(tf.float32, feed_labs.shape) x_adv_p = self.attack.generate(x, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y=y) self.assertEqual(x_val.shape, x_adv_p.shape) x_adv = self.sess.run(x_adv_p, {x: x_val, y: feed_labs}) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(orig_labs == new_labs) < 0.1)
Example #2
Source File: attacks_tf.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def jacobian_graph(predictions, x, nb_classes): """ Create the Jacobian graph to be ran later in a TF session :param predictions: the model's symbolic output (linear output, pre-softmax) :param x: the input placeholder :param nb_classes: the number of classes the model has :return: """ # This function will return a list of TF gradients list_derivatives = [] # Define the TF graph elements to compute our derivatives for each class for class_ind in xrange(nb_classes): derivatives, = tf.gradients(predictions[:, class_ind], x) list_derivatives.append(derivatives) return list_derivatives
Example #3
Source File: adaptive_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def binary_refinement(sess,Best_X_adv, X_adv, Y, ALPHA, ub, lb, model, dataset='cifar'): num_samples = np.shape(X_adv)[0] print(dataset) if(dataset=="mnist"): X_place = tf.placeholder(tf.float32, shape=[1, 1, 28, 28]) else: X_place = tf.placeholder(tf.float32, shape=[1, 3, 32, 32]) pred = model(X_place) for i in range(num_samples): logits_op = sess.run(pred,feed_dict={X_place:X_adv[i:i+1,:,:,:]}) if(not np.argmax(logits_op) == np.argmax(Y[i,:])): # Success, increase alpha Best_X_adv[i,:,:,:] = X_adv[i,:,:,] lb[i] = ALPHA[i,0] else: ub[i] = ALPHA[i,0] ALPHA[i] = 0.5*(lb[i] + ub[i]) return ALPHA, Best_X_adv
Example #4
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 6 votes |
def autosummary(name, value): id = name.replace('/', '_') if is_tf_expression(value): with tf.name_scope('summary_' + id), tf.device(value.device): update_op = _create_autosummary_var(name, value) with tf.control_dependencies([update_op]): return tf.identity(value) else: # python scalar or numpy array if name not in _autosummary_immediate: with absolute_name_scope('Autosummary/' + id), tf.device(None), tf.control_dependencies(None): update_value = tf.placeholder(tf.float32) update_op = _create_autosummary_var(name, update_value) _autosummary_immediate[name] = update_op, update_value update_op, update_value = _autosummary_immediate[name] run(update_op, {update_value: np.float32(value)}) return value # Create the necessary ops to include autosummaries in TensorBoard report. # Note: This should be done only once per graph.
Example #5
Source File: test_model.py From models with MIT License | 6 votes |
def network_surgery(): tf.reset_default_graph() inputs = tf.placeholder(tf.float32, shape=(None, 131072, 4), name='inputs') targets = tf.placeholder(tf.float32, shape=(None, 1024, 4229), name='targets') targets_na = tf.placeholder(tf.bool, shape=(None, 1024), name="targets_na") preds_adhoc = tf.placeholder(tf.float32, shape=(None, 960, 4229), name="Placeholder_15") saver = tf.train.import_meta_graph("model_files/model.tf.meta", input_map={'Placeholder_15:0': preds_adhoc, 'Placeholder:0': targets_na, 'inputs:0': inputs, 'targets:0': targets }) ops = tf.get_default_graph().get_operations() out = tf.train.export_meta_graph(filename='model_files/model.tf-modified.meta', as_text=True) ops[:15]
Example #6
Source File: 2_mnist.py From deep-learning-note with MIT License | 6 votes |
def __init__(self, channel_1_num, channel_2_num, conv_size, hidden_size, pool_size, learning_rate, x_dim=784, y_dim=10): self.channel_1_num = channel_1_num self.channel_2_num = channel_2_num self.conv_size = conv_size self.hidden_size = hidden_size self.pool_size = pool_size self.learning_rate = learning_rate self.x_dim = x_dim self.y_dim = y_dim self.images = tf.placeholder(tf.float32, [None, self.x_dim], name='input_x') self.labels = tf.placeholder(tf.float32, [None, self.y_dim], name='input_y') self.keep_prob = tf.placeholder(tf.float32, name='keep_prob') self.train_step = None self.accuracy = None
Example #7
Source File: model.py From Neural-LP with MIT License | 6 votes |
def _build_input(self): self.tails = tf.placeholder(tf.int32, [None]) self.heads = tf.placeholder(tf.int32, [None]) self.targets = tf.one_hot(indices=self.heads, depth=self.num_entity) if not self.query_is_language: self.queries = tf.placeholder(tf.int32, [None, self.num_step]) self.query_embedding_params = tf.Variable(self._random_uniform_unit( self.num_query + 1, # <END> token self.query_embed_size), dtype=tf.float32) rnn_inputs = tf.nn.embedding_lookup(self.query_embedding_params, self.queries) else: self.queries = tf.placeholder(tf.int32, [None, self.num_step, self.num_word]) self.vocab_embedding_params = tf.Variable(self._random_uniform_unit( self.num_vocab + 1, # <END> token self.vocab_embed_size), dtype=tf.float32) embedded_query = tf.nn.embedding_lookup(self.vocab_embedding_params, self.queries) rnn_inputs = tf.reduce_mean(embedded_query, axis=2) return rnn_inputs
Example #8
Source File: build.py From Traffic_sign_detection_YOLO with MIT License | 6 votes |
def build_forward(self): verbalise = self.FLAGS.verbalise # Placeholders inp_size = [None] + self.meta['inp_size'] self.inp = tf.placeholder(tf.float32, inp_size, 'input') self.feed = dict() # other placeholders # Build the forward pass state = identity(self.inp) roof = self.num_layer - self.ntrain self.say(HEADER, LINE) for i, layer in enumerate(self.darknet.layers): scope = '{}-{}'.format(str(i),layer.type) args = [layer, state, i, roof, self.feed] state = op_create(*args) mess = state.verbalise() self.say(mess) self.say(LINE) self.top = state self.out = tf.identity(state.out, name='output')
Example #9
Source File: test_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_targeted_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), np.random.randint(0, 1, 100)] = 1 x = tf.placeholder(tf.float32, x_val.shape) y = tf.placeholder(tf.float32, feed_labs.shape) x_adv_p = self.attack.generate(x, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y_target=y) self.assertEqual(x_val.shape, x_adv_p.shape) x_adv = self.sess.run(x_adv_p, {x: x_val, y: feed_labs}) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(np.argmax(feed_labs, axis=1) == new_labs) > 0.9)
Example #10
Source File: test_utils_keras.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_fprop(self): import tensorflow as tf model = KerasModelWrapper(self.model) x = tf.placeholder(tf.float32, shape=(None, 100)) out_dict = model.fprop(x) self.assertEqual(set(out_dict.keys()), set(['l1', 'l2', 'softmax'])) # Test the dimension of the hidden represetation self.assertEqual(int(out_dict['l1'].shape[1]), 20) self.assertEqual(int(out_dict['l2'].shape[1]), 10) # Test the caching x2 = tf.placeholder(tf.float32, shape=(None, 100)) out_dict2 = model.fprop(x2) self.assertEqual(set(out_dict2.keys()), set(['l1', 'l2', 'softmax'])) self.assertEqual(int(out_dict2['l1'].shape[1]), 20)
Example #11
Source File: test_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) orig_labs = np.argmax(self.sess.run(self.model(x_val)), axis=1) x = tf.placeholder(tf.float32, x_val.shape) x_adv_p = self.attack.generate(x, over_shoot=0.02, max_iter=50, nb_candidate=2, clip_min=-5, clip_max=5) self.assertEqual(x_val.shape, x_adv_p.shape) x_adv = self.sess.run(x_adv_p, {x: x_val}) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(orig_labs == new_labs) < 0.1)
Example #12
Source File: test_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) orig_labs = np.argmax(self.sess.run(self.model(x_val)), axis=1) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), orig_labs] = 1 x = tf.placeholder(tf.float32, x_val.shape) y = tf.placeholder(tf.float32, feed_labs.shape) x_adv_p = self.attack.generate(x, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y=y) self.assertEqual(x_val.shape, x_adv_p.shape) x_adv = self.sess.run(x_adv_p, {x: x_val, y: feed_labs}) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(orig_labs == new_labs) < 0.1)
Example #13
Source File: 2_tf_linear.py From deep-learning-note with MIT License | 6 votes |
def createLinearModel(dimension): np.random.seed(1024) # 定义 x 和 y x = tf.placeholder(tf.float64, shape=[None, dimension], name='x') # 写成矩阵形式会大大加快运算速度 y = tf.placeholder(tf.float64, shape=[None, 1], name='y') # 定义参数估计值和预测值 betaPred = tf.Variable(np.random.random([dimension, 1])) yPred = tf.matmul(x, betaPred, name='y_pred') # 定义损失函数 loss = tf.reduce_mean(tf.square(yPred - y)) model = { 'loss_function': loss, 'independent_variable': x, 'dependent_variable': y, 'prediction': yPred, 'model_params': betaPred } return model
Example #14
Source File: test_utils_tf.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_kl_with_logits(self): p_logits = tf.placeholder(tf.float32, shape=(100, 20)) q_logits = tf.placeholder(tf.float32, shape=(100, 20)) p_logits_np = np.random.normal(0, 10, size=(100, 20)) q_logits_np = np.random.normal(0, 10, size=(100, 20)) with tf.Session() as sess: kl_div_tf = sess.run(utils_tf.kl_with_logits(p_logits, q_logits), feed_dict={p_logits: p_logits_np, q_logits: q_logits_np}) kl_div_ref = numpy_kl_with_logits(p_logits_np, q_logits_np) self.assertTrue(np.allclose(kl_div_ref, kl_div_tf))
Example #15
Source File: attacks_tf.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def vatm(model, x, logits, eps, num_iterations=1, xi=1e-6, clip_min=None, clip_max=None, scope=None): """ Tensorflow implementation of the perturbation method used for virtual adversarial training: https://arxiv.org/abs/1507.00677 :param model: the model which returns the network unnormalized logits :param x: the input placeholder :param logits: the model's unnormalized output tensor (the input to the softmax layer) :param eps: the epsilon (input variation parameter) :param num_iterations: the number of iterations :param xi: the finite difference parameter :param clip_min: optional parameter that can be used to set a minimum value for components of the example returned :param clip_max: optional parameter that can be used to set a maximum value for components of the example returned :param seed: the seed for random generator :return: a tensor for the adversarial example """ with tf.name_scope(scope, "virtual_adversarial_perturbation"): d = tf.random_normal(tf.shape(x), dtype=tf_dtype) for i in range(num_iterations): d = xi * utils_tf.l2_batch_normalize(d) logits_d = model.get_logits(x + d) kl = utils_tf.kl_with_logits(logits, logits_d) Hd = tf.gradients(kl, d)[0] d = tf.stop_gradient(Hd) d = eps * utils_tf.l2_batch_normalize(d) adv_x = x + d if (clip_min is not None) and (clip_max is not None): adv_x = tf.clip_by_value(adv_x, clip_min, clip_max) return adv_x
Example #16
Source File: mnist_train.py From deep-learning-note with MIT License | 5 votes |
def train(mnist): x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input') y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input') regularizer = tf.contrib.layers.l2_regularizer(REGULARAZATION_RATE) y = mnist_inference.inference(x, regularizer) global_step = tf.Variable(0, trainable=False) variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) variables_averages_op = variable_averages.apply(tf.trainable_variables()) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1)) cross_entropy_mean = tf.reduce_mean(cross_entropy) loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses')) learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) with tf.control_dependencies([train_step, variables_averages_op]): train_op = tf.no_op(name='train') # 初始化 TF 持久化类 saver = tf.train.Saver() with tf.Session() as sess: tf.global_variables_initializer().run() for i in range(TRAINING_STEPS): xs, ys = mnist.train.next_batch(BATCH_SIZE) _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: ys}) if i % 1000 == 0: print("After %d training step(s), loss on training batch is %g." % (step, loss_value)) # 保存当前模型 saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
Example #17
Source File: trainer.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _init_inputs(self): preproc_func = self.preproc_func input_shape = self.input_shape # Define input TF placeholder with tf.device('/gpu:0'): x_pre = tf.placeholder(tf.float32, shape=input_shape, name='x') x = preprocess_batch(x_pre, preproc_func) y = tf.placeholder(tf.float32, shape=(self.batch_size, 10), name='y') self.g0_inputs = {'x_pre': x_pre, 'x': x, 'y': y}
Example #18
Source File: lenet_mnist_eval.py From deep-learning-note with MIT License | 5 votes |
def evaluate(mnist): with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32, [ mnist_train.BATCH_SIZE, mnist_inference.IMAGE_SIZE, mnist_inference.IMAGE_SIZE, mnist_inference.NUM_CHANNELS], name='x-input') y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input') validate_feed = { x: mnist.validation.images, y_: mnist.validation.labels} y = mnist_inference.inference(x, None) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) variable_averages = tf.train.ExponentialMovingAverage(mnist_train.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) while True: with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(mnist_train.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] accuracy_score = sess.run(accuracy, feed_dict=validate_feed) print("After %s training step(s), validation accuracy = %g" % (global_step, accuracy_score)) else: print("No Checkpoint file found") return time.sleep(EVAL_INTERVAL_SECS)
Example #19
Source File: mnist_eval.py From deep-learning-note with MIT License | 5 votes |
def evaluate(mnist): with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input') y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input') validate_feed = { x: mnist.validation.images, y_: mnist.validation.labels} y = mnist_inference.inference(x, None) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) variable_averages = tf.train.ExponentialMovingAverage(mnist_train.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) while True: with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(mnist_train.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] accuracy_score = sess.run(accuracy, feed_dict=validate_feed) print("After %s training step(s), validation accuracy = %g" % (global_step, accuracy_score)) else: print("No Checkpoint file found") return time.sleep(EVAL_INTERVAL_SECS)
Example #20
Source File: mnist_eval.py From deep-learning-note with MIT License | 5 votes |
def evaluate(mnist): with tf.Graph().as_default() as g: x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input') y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input') validate_feed = { x: mnist.validation.images, y_: mnist.validation.labels} y = mnist_inference.inference(x, None) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) variable_averages = tf.train.ExponentialMovingAverage(mnist_train.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) while True: with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(mnist_train.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] accuracy_score = sess.run(accuracy, feed_dict=validate_feed) print("After %s training step(s), validation accuracy = %g" % (global_step, accuracy_score)) else: print("No Checkpoint file found") return time.sleep(EVAL_INTERVAL_SECS)
Example #21
Source File: 1_basic_linear.py From deep-learning-note with MIT License | 5 votes |
def main(): train_x = tf.placeholder(tf.float32) train_label = tf.placeholder(tf.float32) test_x = tf.placeholder(tf.float32) test_label = tf.placeholder(tf.float32) with tf.variable_scope("inference"): train_y = inference(train_x) tf.get_variable_scope().reuse_variables() test_y = inference(test_x) train_loss = tf.square(train_y - train_label) test_loss = tf.square(test_y - test_label) opt = tf.train.GradientDescentOptimizer(0.002) train_op = opt.minimize(train_loss) init = tf.global_variables_initializer() train_data_x, train_data_label = get_data(1000) test_data_x, test_data_label = get_data(1) with tf.Session() as sess: sess.run(init) for i in range(1000): sess.run(train_op, feed_dict={train_x: train_data_x[i], train_label: train_data_label[i]}) if i % 10 == 0: test_loss_value = sess.run(test_loss, feed_dict={test_x:test_data_x[0], test_label:test_data_label[0]}) print("step %d eval loss is %.3f" % (i, test_loss_value))
Example #22
Source File: actor.py From neural-combinatorial-optimization-rl-tensorflow with MIT License | 5 votes |
def __init__(self, config): self.config=config # Data config self.batch_size = config.batch_size # batch size self.max_length = config.max_length # input sequence length (number of cities) self.input_dimension = config.input_dimension # dimension of a city (coordinates) self.speed = config.speed # agent's speed # Network config self.input_embed = config.input_embed # dimension of embedding space self.num_neurons = config.hidden_dim # dimension of hidden states (LSTM cell) self.initializer = tf.contrib.layers.xavier_initializer() # variables initializer # Reward config self.beta = config.beta # penalty for constraint # Training config (actor) self.global_step = tf.Variable(0, trainable=False, name="global_step") # global step self.lr1_start = config.lr1_start # initial learning rate self.lr1_decay_rate = config.lr1_decay_rate # learning rate decay rate self.lr1_decay_step = config.lr1_decay_step # learning rate decay step self.is_training = not config.inference_mode # Training config (critic) self.global_step2 = tf.Variable(0, trainable=False, name="global_step2") # global step self.lr2_start = config.lr1_start # initial learning rate self.lr2_decay_rate= config.lr1_decay_rate # learning rate decay rate self.lr2_decay_step= config.lr1_decay_step # learning rate decay step # Tensor block holding the input sequences [Batch Size, Sequence Length, Features] self.input_ = tf.placeholder(tf.float32, [self.batch_size, self.max_length+1, self.input_dimension+2], name="input_raw") # +1 for depot / +2 for TW mean and TW width self.build_permutation() self.build_critic() self.build_reward() self.build_optim() self.merged = tf.summary.merge_all()
Example #23
Source File: 1_mnist_before.py From deep-learning-note with MIT License | 5 votes |
def __init__(self, channel_1_num, channel_2_num, conv_size, hidden_size, pool_size, learning_rate, x_dim=784, y_dim=10): self.channel_1_num = channel_1_num self.channel_2_num = channel_2_num self.conv_size = conv_size self.hidden_size = hidden_size self.pool_size = pool_size self.learning_rate = learning_rate self.x_dim = x_dim self.y_dim = y_dim self.images = tf.placeholder( tf.float32, [None, self.x_dim], name='input_x') self.labels = tf.placeholder( tf.float32, [None, self.y_dim], name='input_y') self.keep_prob = tf.placeholder(tf.float32, name='keep_prob') self.train_step = None self.accuracy = None
Example #24
Source File: test_imagenet_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_clean_accuracy(self): """Check model is accurate on unperturbed images.""" input_dir = FLAGS.input_image_dir metadata_file_path = FLAGS.metadata_file_path num_images = 16 batch_shape = (num_images, 299, 299, 3) images, labels = load_images( input_dir, metadata_file_path, batch_shape) num_classes = 1001 tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): # Prepare graph x_input = tf.placeholder(tf.float32, shape=batch_shape) y_label = tf.placeholder(tf.int32, shape=(num_images,)) model = InceptionModel(num_classes) logits = model.get_logits(x_input) acc = _top_1_accuracy(logits, y_label) # Run computation saver = tf.train.Saver(slim.get_model_variables()) session_creator = tf.train.ChiefSessionCreator( scaffold=tf.train.Scaffold(saver=saver), checkpoint_filename_with_path=FLAGS.checkpoint_path, master=FLAGS.master) with tf.train.MonitoredSession( session_creator=session_creator) as sess: acc_val = sess.run(acc, feed_dict={ x_input: images, y_label: labels}) tf.logging.info('Accuracy: %s', acc_val) assert acc_val > 0.8
Example #25
Source File: defense.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def main(_): batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3] num_classes = 1001 tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): # Prepare graph x_input = tf.placeholder(tf.float32, shape=batch_shape) with slim.arg_scope(inception.inception_v3_arg_scope()): _, end_points = inception.inception_v3( x_input, num_classes=num_classes, is_training=False) predicted_labels = tf.argmax(end_points['Predictions'], 1) # Run computation saver = tf.train.Saver(slim.get_model_variables()) session_creator = tf.train.ChiefSessionCreator( scaffold=tf.train.Scaffold(saver=saver), checkpoint_filename_with_path=FLAGS.checkpoint_path, master=FLAGS.master) with tf.train.MonitoredSession(session_creator=session_creator) as sess: with tf.gfile.Open(FLAGS.output_file, 'w') as out_file: for filenames, images in load_images(FLAGS.input_dir, batch_shape): labels = sess.run(predicted_labels, feed_dict={x_input: images}) for filename, label in zip(filenames, labels): out_file.write('{0},{1}\n'.format(filename, label))
Example #26
Source File: defense.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def main(_): batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3] num_classes = 1001 tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): # Prepare graph x_input = tf.placeholder(tf.float32, shape=batch_shape) with slim.arg_scope(inception.inception_v3_arg_scope()): _, end_points = inception.inception_v3( x_input, num_classes=num_classes, is_training=False) predicted_labels = tf.argmax(end_points['Predictions'], 1) # Run computation saver = tf.train.Saver(slim.get_model_variables()) session_creator = tf.train.ChiefSessionCreator( scaffold=tf.train.Scaffold(saver=saver), checkpoint_filename_with_path=FLAGS.checkpoint_path, master=FLAGS.master) with tf.train.MonitoredSession(session_creator=session_creator) as sess: with tf.gfile.Open(FLAGS.output_file, 'w') as out_file: for filenames, images in load_images(FLAGS.input_dir, batch_shape): labels = sess.run(predicted_labels, feed_dict={x_input: images}) for filename, label in zip(filenames, labels): out_file.write('{0},{1}\n'.format(filename, label))
Example #27
Source File: attack_random_noise.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def main(_): eps = FLAGS.max_epsilon / 255.0 batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3] with tf.Graph().as_default(): x_input = tf.placeholder(tf.float32, shape=batch_shape) noisy_images = x_input + eps * tf.sign(tf.random_normal(batch_shape)) x_output = tf.clip_by_value(noisy_images, 0.0, 1.0) with tf.Session(FLAGS.master) as sess: for filenames, images in load_images(FLAGS.input_dir, batch_shape): out_images = sess.run(x_output, feed_dict={x_input: images}) save_images(out_images, filenames, FLAGS.output_dir)
Example #28
Source File: attack_fgsm.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def main(_): # Images for inception classifier are normalized to be in [-1, 1] interval, # eps is a difference between pixels so it should be in [0, 2] interval. # Renormalizing epsilon from [0, 255] to [0, 2]. eps = 2.0 * FLAGS.max_epsilon / 255.0 batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3] num_classes = 1001 tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): # Prepare graph x_input = tf.placeholder(tf.float32, shape=batch_shape) model = InceptionModel(num_classes) fgsm = FastGradientMethod(model) x_adv = fgsm.generate(x_input, eps=eps, clip_min=-1., clip_max=1.) # Run computation saver = tf.train.Saver(slim.get_model_variables()) session_creator = tf.train.ChiefSessionCreator( scaffold=tf.train.Scaffold(saver=saver), checkpoint_filename_with_path=FLAGS.checkpoint_path, master=FLAGS.master) with tf.train.MonitoredSession(session_creator=session_creator) as sess: for filenames, images in load_images(FLAGS.input_dir, batch_shape): adv_images = sess.run(x_adv, feed_dict={x_input: images}) save_images(adv_images, filenames, FLAGS.output_dir)
Example #29
Source File: run_audio_attack.py From Black-Box-Audio with MIT License | 5 votes |
def setup_graph(self, input_audio_batch, target_phrase): batch_size = input_audio_batch.shape[0] weird = (input_audio_batch.shape[1] - 1) // 320 logits_arg2 = np.tile(weird, batch_size) dense_arg1 = np.array(np.tile(target_phrase, (batch_size, 1)), dtype=np.int32) dense_arg2 = np.array(np.tile(target_phrase.shape[0], batch_size), dtype=np.int32) pass_in = np.clip(input_audio_batch, -2**15, 2**15-1) seq_len = np.tile(weird, batch_size).astype(np.int32) with tf.variable_scope('', reuse=tf.AUTO_REUSE): inputs = tf.placeholder(tf.float32, shape=pass_in.shape, name='a') len_batch = tf.placeholder(tf.float32, name='b') arg2_logits = tf.placeholder(tf.int32, shape=logits_arg2.shape, name='c') arg1_dense = tf.placeholder(tf.float32, shape=dense_arg1.shape, name='d') arg2_dense = tf.placeholder(tf.int32, shape=dense_arg2.shape, name='e') len_seq = tf.placeholder(tf.int32, shape=seq_len.shape, name='f') logits = get_logits(inputs, arg2_logits) target = ctc_label_dense_to_sparse(arg1_dense, arg2_dense, len_batch) ctcloss = tf.nn.ctc_loss(labels=tf.cast(target, tf.int32), inputs=logits, sequence_length=len_seq) decoded, _ = tf.nn.ctc_greedy_decoder(logits, arg2_logits, merge_repeated=True) sess = tf.Session() saver = tf.train.Saver(tf.global_variables()) saver.restore(sess, "models/session_dump") func1 = lambda a, b, c, d, e, f: sess.run(ctcloss, feed_dict={inputs: a, len_batch: b, arg2_logits: c, arg1_dense: d, arg2_dense: e, len_seq: f}) func2 = lambda a, b, c, d, e, f: sess.run([ctcloss, decoded], feed_dict={inputs: a, len_batch: b, arg2_logits: c, arg1_dense: d, arg2_dense: e, len_seq: f}) return (func1, func2)
Example #30
Source File: test_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def setUp(self): super(TestSaliencyMapMethod, self).setUp() self.sess = tf.Session() self.sess.as_default() self.model = DummyModel() self.attack = SaliencyMapMethod(self.model, sess=self.sess) # initialize model with tf.name_scope('dummy_model'): self.model(tf.placeholder(tf.float32, shape=(None, 1000))) self.sess.run(tf.global_variables_initializer()) self.attack = SaliencyMapMethod(self.model, sess=self.sess)