def inference0(cls, input_tensor, graph, model_input_tensor_name, model_output_tensor_name): """ args: input_tensor: 4-D image tensor with batch_size N; inputs are assumed to be preprocessed graph: parent graph in which classifier is defined model_input_tensor_name: String. name of input tensor model_output_tensor_name: String. name of output tensor returns: logits, tensor of shape N x num_class """ with graph.as_default(): # read frozen graph with gfile.FastGFile(cls.MODEL_PB, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name=cls.NAME) # fetch tensors from frozen graph parent_name_scope = graph.get_name_scope() if len(parent_name_scope) > 0: parent_name_scope += '/' input_t_ph = graph.get_tensor_by_name(parent_name_scope + cls.NAME + '/' + model_input_tensor_name) outputs = graph.get_tensor_by_name(parent_name_scope + cls.NAME + '/' + model_output_tensor_name) # need to detach input_t_ph from graph and attach preprocessed_input_tensor graph_editor.swap_inputs(input_t_ph.op.outputs[0], [input_tensor]) return outputs
def create_op_pruning_no_update( op: tf_compat.Operation, op_input: tf_compat.Tensor, ks_group: str, leave_enabled: bool = True, is_after_end_step: tf_compat.Tensor = None, ) -> PruningOpVars: """ Creates the necessary variables and operators to gradually apply sparsity to an operators variable without returning a PruningOpVars.update value. :param op: the operation to prune to the given sparsity :param op_input: the parameter within the op to create a mask for :param ks_group: the group identifier the scope should be created under mask_creator :param leave_enabled: True to continue masking the weights after end_epoch, False to stop masking :param is_after_end_step: only should be provided if leave_enabled is False; tensor that is true if the current global step is after end_epoch :return: a named tuple containing the assignment op, mask variable, threshold tensor, and masked tensor """ if tf_contrib_err: raise tf_contrib_err op_sgv = graph_editor.sgv(op) # create the necessary variables first with tf_compat.variable_scope(PruningScope.model(op, ks_group), reuse=tf_compat.AUTO_REUSE): mask = tf_compat.get_variable( PruningScope.VAR_MASK, op_input.get_shape(), initializer=tf_compat.ones_initializer(), trainable=False, dtype=op_input.dtype, ) tf_compat.add_to_collection( PruningScope.collection_name(ks_group, PruningScope.VAR_MASK), mask) # create the masked operation and assign as the new input to the op with tf_compat.name_scope( PruningScope.model(op, ks_group, trailing_slash=True)): masked = tf_compat.multiply(mask, op_input, PruningScope.OP_MASKED_VAR) op_inp_tens = (masked if leave_enabled else tf_compat.cond( is_after_end_step, lambda: op_input, lambda: masked)) op_swapped_inputs = [ inp if inp != op_input else op_inp_tens for inp in op_sgv.inputs ] graph_editor.swap_inputs(op, op_swapped_inputs) tf_compat.add_to_collection( PruningScope.collection_name(ks_group, PruningScope.OP_MASKED_VAR), masked) return PruningOpVars(op, op_input, None, mask, masked)
def main(_): # create a graph g = tf.Graph() with g.as_default(): a = tf.constant(1.0, shape=[2, 3], name="a") b = tf.constant(2.0, shape=[2, 3], name="b") c = tf.add(tf.placeholder(dtype=np.float32), tf.placeholder(dtype=np.float32), name="c") # modify the graph ge.swap_inputs(c.op, [a, b]) # print the graph def print(g.as_graph_def()) # and print the value of c with tf.Session(graph=g) as sess: res = sess.run(c) print(res)
print('Outs:', traj_1[0].outputs) input_stack = tf.concat( values=[traj_1[0].outputs[0], traj_2[0].outputs[0]], axis=0, name='new_input') print('Input Vector', input_stack) print('T1Inputs:', [x for x in traj_1[1].inputs]) # for all the preprocessing ops, look if there's an input tensor edge. for o in traj_1[1:]: print('Doing subs for', o.name) if traj_1[0].outputs[0].name in [x.name for x in o.inputs]: print('Before:', [x.name for x in o.inputs]) new_inputs = [input_stack] + [ x for x in o.inputs if x.name != traj_1[0].outputs[0].name ] ge.swap_inputs(o, new_inputs) print('Subbed:', [x.name for x in o.inputs]) post_processing_g1 = [op for op in filtered_ops_1 if op not in traj_1] post_processing_g2 = [op for op in filtered_ops_2 if op not in traj_2] new_g_def = graph_pb2.GraphDef() new_g_def.node.extend([copy.deepcopy(s_op_2.node_def)]) new_g_def.node.extend([copy.deepcopy(s_op_1.node_def)]) concat_ops = [ op for op in sess1.graph.get_operations() if 'new_input' in op.name ] print(concat_ops) tensor_to_split = traj_1[-1] print('SUB OUTS:', tensor_to_split.outputs) s1, s2 = tf.shape(traj_1[0].outputs[0])[0], tf.shape( traj_2[0].outputs[0])[0] split_1, split_2 = tf.split(tensor_to_split.outputs[0], [s1, s2],
def main(args): np.set_printoptions(threshold='nan') images_raw, cout_per_image, nrof_samples, bili = load_and_align_data( args.image_files, args.image_size, args.margin, args.gpu_memory_fraction) # images_raw = np.divide(images_raw0,255.0) mean_bili = np.mean(bili) apple = np.zeros((160, 160, 3)) apple += 120 print("****apple**") print(apple.shape) masking_matrix_raw = misc.imread("masking_final.jpg") masking_matrix0 = np.clip(masking_matrix_raw, 0, 1) reverse_masking = np.mod(np.add(masking_matrix0, 1), 2) # reverse_masking0 = misc.imread("re_masking_final.jpg") # reverse_masking = np.clip(reverse_masking0,0,1) images = np.multiply(images_raw, reverse_masking) with open("embedding_target", 'rb') as emb_file: embeddings_target = pickle.load(emb_file) # embedding_target = embeddings_target[0,:] embeddings_target_tensor = tf.convert_to_tensor(embeddings_target) # print(embedding_target.shape) with tf.Graph().as_default(): with tf.Session() as sess: # sess = tf_debug.LocalCLIDebugWrapperSession(sess) step = 0 # Load the model facenet.load_model(args.model) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") pre_input = tf.Variable(images, dtype='float32', name='pre_input') pre_r = tf.Variable(apple, dtype='float32', name='pre_r') pre_r0 = tf.reshape(pre_r, shape=(1, 160, 160, 3)) pre_r1 = blur1(pre_r0) pre_r1 = tf.reshape(pre_r1, shape=(160, 160, 3)) masking_matrix1 = tf.reshape(masking_matrix0, shape=(160, 160, 3)) masking_matrix = tf.cast(masking_matrix1, dtype='float32') # pre_r.shape=(numbers,160,160,1) masking_matrix.shape=(160,160,3), pre_r_masking.shape=(numbers,160,160,3) pre_r_masking = tf.multiply(pre_r1, masking_matrix) #(160,160,3) pre_r_masking = tf.clip_by_value(pre_r_masking, 0., 255.) pre_input_i_tmp = tf.add(pre_input, pre_r_masking, name='pre_input_i') pre_input_i = tf.clip_by_value(pre_input_i_tmp, 0., 255.) pre_input_whitened = tf.map_fn( lambda frame: tf.image.per_image_standardization(frame), pre_input_i) ge.swap_inputs(images_placeholder.op, [pre_input_whitened]) embeddings_target_tensor = tf.convert_to_tensor(embeddings_target) global_step = tf.Variable(0, trainable=False) learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate') loss = tf.sqrt( tf.reduce_sum( tf.square(tf.subtract(embeddings, embeddings_target_tensor)), 1)) grads = tf.gradients(loss, pre_r) grads = tf.squeeze(grads) grad_absmax = tf.reduce_max(tf.abs(grads)) grad_absmax = tf.maximum(1e-10, grad_absmax) step_size = tf.div(7., grad_absmax) new_pre_r = pre_r.assign(pre_r - tf.multiply(grads, step_size)) classifier_filename_exp = os.path.expanduser( args.classifier_filename) with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"\n' % classifier_filename_exp) predictions = [0., 0., 0.] sess.run(tf.global_variables_initializer()) print(ge.sgv(pre_input_whitened.op)) print(ge.sgv(images_placeholder.op)) flag1 = flag2 = flag3 = flag4 = flag5 = flag6 = flag7 = flag8 = flag9 = 0 pdir = "./output" isExists = os.path.exists(pdir) if not isExists: os.makedirs(pdir) target_index = 7 while True: feed_dict = {phase_train_placeholder: False} emb, ls,new_r,pre_images, r,r_masking,images_attack_rgb, images_attack_whiten = \ sess.run([embeddings, loss, new_pre_r, pre_input, pre_r,pre_r_masking,pre_input_i, images_placeholder], feed_dict=feed_dict) predictions = model.predict_proba(emb) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] print("**step****") print(step) print("**loss****") print(ls) print("*predictions*") print(predictions) print("names") print(class_names) k = 0 #print predictions for i in range(nrof_samples): print("\npeople in image %s :" % (args.image_files[i])) for j in range(cout_per_image[i]): print('%s: %.3f' % (class_names[best_class_indices[k]], best_class_probabilities[k])) k += 1 #debug # images_attack_rgb = tf.multiply(images_attack_rgb,255.0) # if step == 4: if flag8 == 0 and np.min(predictions[:, target_index]) > 0.93: mdir = os.path.join(pdir, "adv-final-lin") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) size = tf.convert_to_tensor([160 * mean_bili, 160]) size = tf.cast(size, dtype=tf.int32) output_image = tf.cast(images_attack_rgb, tf.uint8) eye_glass = np.multiply(images_attack_rgb[0], masking_matrix0) eye_glass1 = tf.cast( np.add(eye_glass, np.multiply(reverse_masking, 255)), tf.uint8) output_eye_glass = tf.image.resize_images(eye_glass1, size) output_eye_glass = tf.cast(output_eye_glass, tf.uint8) glass_tmp = "eye_glass.jpg" glass_dir = os.path.join(mdir, glass_tmp) with open(glass_dir, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg(output_eye_glass))) print("print eyeglass success") output_image_resized = tf.image.resize_images( output_image, size) output_image_resized = tf.cast(output_image_resized, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) filename_jpg_re = 'adversarial_re-%d.jpg' % (i) jpg_file_re = os.path.join(mdir, filename_jpg_re) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag8 = 1 with open(jpg_file_re, 'wb') as f1: f1.write( sess.run( tf.image.encode_jpeg( output_image_resized[i]))) # break # if flag2 == 0 and np.min(predictions[:,target_index]) > 0.20: # mdir = os.path.join(pdir, "adv0.2") # mdir_exist = os.path.exists(mdir) # if not mdir_exist: # os.makedirs(mdir) # output_image = tf.cast(images_attack_rgb, tf.uint8) # for i in range(nrof_samples): # filename_jpg = 'adversarial0.2-%d.jpg' % (i) # jpg_file = os.path.join(mdir,filename_jpg) # with open(jpg_file, 'wb') as f: # f.write(sess.run(tf.image.encode_jpeg(output_image[i]))) # flag2 = 1 # if flag3 == 0 and np.min(predictions[:,target_index]) > 0.30: # mdir = os.path.join(pdir, "adv0.3") # mdir_exist = os.path.exists(mdir) # if not mdir_exist: # os.makedirs(mdir) # output_image = tf.cast(images_attack_rgb, tf.uint8) # for i in range(nrof_samples): # filename_jpg = 'adversarial0.3-%d.jpg' % (i) # jpg_file = os.path.join(mdir,filename_jpg) # with open(jpg_file, 'wb') as f: # f.write(sess.run(tf.image.encode_jpeg(output_image[i]))) # flag3 = 1 # if flag4 == 0 and np.min(predictions[:,target_index]) > 0.40: # mdir = os.path.join(pdir, "adv0.4") # mdir_exist = os.path.exists(mdir) # if not mdir_exist: # os.makedirs(mdir) # output_image = tf.cast(images_attack_rgb, tf.uint8) # for i in range(nrof_samples): # filename_jpg = 'adversarial0.4-%d.jpg' % (i) # jpg_file = os.path.join(mdir,filename_jpg) # with open(jpg_file, 'wb') as f: # f.write(sess.run(tf.image.encode_jpeg(output_image[i]))) # flag4 = 1 # if flag5 == 0 and np.min(predictions[:,target_index]) > 0.50: # mdir = os.path.join(pdir, "adv0.5") # mdir_exist = os.path.exists(mdir) # if not mdir_exist: # os.makedirs(mdir) # output_image = tf.cast(images_attack_rgb, tf.uint8) # for i in range(nrof_samples): # filename_jpg = 'adversarial0.5-%d.jpg' % (i) # jpg_file = os.path.join(mdir,filename_jpg) # with open(jpg_file, 'wb') as f: # f.write(sess.run(tf.image.encode_jpeg(output_image[i]))) # flag5 = 1 # # if flag1 == 0 and np.min(predictions[:,target_index]) > 0.10: # # mdir = os.path.join(pdir, "adv0.1") # # mdir_exist = os.path.exists(mdir) # # if not mdir_exist: # # os.makedirs(mdir) # # output_image = tf.cast(images_attack_rgb, tf.uint8) # # for i in range(nrof_samples): # # filename_jpg = 'adversarial0.1-%d.jpg' % (i) # # jpg_file = os.path.join(mdir,filename_jpg) # # with open(jpg_file, 'wb') as f: # # f.write(sess.run(tf.image.encode_jpeg(output_image[i]))) # # flag1 = 1 if flag7 == 0 and np.min(predictions[:, target_index]) > 0.70: mdir = os.path.join(pdir, "adv0.7") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) output_image = tf.cast(images_attack_rgb, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial0.7-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag7 = 1 if flag6 == 0 and np.min(predictions[:, target_index]) > 0.60: mdir = os.path.join(pdir, "adv0.6") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) output_image = tf.cast(images_attack_rgb, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial0.6-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag6 = 1 # # if flag9 == 0 and np.min(predictions[:,target_index]) > 0.85: # # mdir = os.path.join(pdir, "adv0.85") # # mdir_exist = os.path.exists(mdir) # # if not mdir_exist: # # os.makedirs(mdir) # # output_image = tf.cast(images_attack_rgb, tf.uint8) # # for i in range(nrof_samples): # # filename_jpg = 'adversarial0.85-%d.jpg' % (i) # # jpg_file = os.path.join(mdir,filename_jpg) # # with open(jpg_file, 'wb') as f: # # f.write(sess.run(tf.image.encode_jpeg(output_image[i]))) # # flag9 = 1 # # if np.min(predictions[:,target_index]) > 0.90: # # mdir = os.path.join(pdir, "adv0.9") # # mdir_exist = os.path.exists(mdir) # # if not mdir_exist: # # os.makedirs(mdir) # # output_image = tf.cast(images_attack_rgb, tf.uint8) # # for i in range(nrof_samples): # # filename_jpg = 'adversarial0.9-%d.jpg' % (i) # # jpg_file = os.path.join(mdir,filename_jpg) # # with open(jpg_file, 'wb') as f: # # f.write(sess.run(tf.image.encode_jpeg(output_image[i]))) # # flag1 = 1 # # break # # if step == 1: # # print(gra) # # input() step += 1
def main(args): np.set_printoptions(threshold='nan') images_raw0, cout_per_image, nrof_samples = load_and_align_data( args.image_files, args.image_size, args.margin, args.gpu_memory_fraction) images_raw = np.divide(images_raw0, 255.0) apple = np.zeros((160, 160, 3)) apple += 0.5 print("****apple**") print(apple.shape) masking_matrix_raw = misc.imread("masking_rect.jpg") masking_matrix0 = np.clip(masking_matrix_raw, 0, 1) reverse_masking0 = misc.imread("re_masking_rect.jpg") reverse_masking = np.clip(reverse_masking0, 0, 1) images = np.multiply(images_raw, reverse_masking) with open("embedding_target", 'rb') as emb_file: embeddings_target = pickle.load(emb_file) embedding_target = embeddings_target[0, :] print(embedding_target.shape) with tf.Graph().as_default(): with tf.Session() as sess: step = 0 # Load the model facenet.load_model(args.model) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") pre_input = tf.Variable(images, dtype='float32', name='pre_input') pre_r = tf.Variable(apple, dtype='float32', name='pre_r') masking_matrix1 = tf.reshape(masking_matrix0, shape=(160, 160, 3)) masking_matrix = tf.cast(masking_matrix1, dtype='float32') # pre_r.shape=(numbers,160,160,1) masking_matrix.shape=(160,160,3), pre_r_masking.shape=(numbers,160,160,3) pre_r_masking = tf.multiply(pre_r, masking_matrix) #(160,160,3) TV_loss = tf.reduce_sum(tf.image.total_variation(pre_r_masking)) pre_input_i_tmp = tf.add(pre_input, pre_r_masking, name='pre_input_i') pre_input_i = tf.clip_by_value(pre_input_i_tmp, 0., 1.) # pre_input_whitened = prewhiten(pre_input_i) pre_input_whitened = tf.map_fn( lambda frame: tf.image.per_image_standardization(frame), pre_input_i) print(ge.sgv(pre_input_whitened.op)) ge.swap_inputs(images_placeholder.op, [pre_input_whitened]) global_step = tf.Variable(0, trainable=False) learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate') learning_rate = tf.train.exponential_decay( learning_rate_placeholder, global_step, 100, args.learning_rate_decay_factor, staircase=True) lr = args.learning_rate #calculate loss, grad, and apply them sub_res = tf.subtract(embeddings, embedding_target) loss = tf.norm(sub_res) total_loss = loss + TV_loss # loss = tf.div(1.0, loss0) with tf.control_dependencies( [loss]): #loss_averages_op run first, and go on opt = tf.train.AdagradOptimizer( learning_rate) # AdagradOptimizer is a class grads = opt.compute_gradients(loss, pre_r) apply_gradient_op = opt.apply_gradients(grads, global_step=global_step) classifier_filename_exp = os.path.expanduser( args.classifier_filename) with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"\n' % classifier_filename_exp) predictions = [0., 0., 0.] sess.run(tf.global_variables_initializer()) flag1 = flag2 = flag3 = flag4 = flag5 = flag6 = flag7 = flag8 = flag9 = 0 pdir = "./output" isExists = os.path.exists(pdir) if not isExists: os.makedirs(pdir) while True: feed_dict = { phase_train_placeholder: False, learning_rate_placeholder: lr } emb, ls,_, sub_r,gra,pre_images, r,r_masking,images_attack_rgb, images_attack_whiten = \ sess.run([embeddings, total_loss,apply_gradient_op,sub_res, grads, pre_input, pre_r,pre_r_masking,pre_input_i, images_placeholder], feed_dict=feed_dict) # print(sub_r) # print(loss0_res) # print(ls) # input() predictions = model.predict_proba(emb) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] print("**step****") print(step) print("**loss****") print(ls) print("*predictions*") print(predictions) print("names") print(class_names) k = 0 #print predictions for i in range(nrof_samples): print("\npeople in image %s :" % (args.image_files[i])) for j in range(cout_per_image[i]): print('%s: %.3f' % (class_names[best_class_indices[k]], best_class_probabilities[k])) k += 1 #debug if flag1 == 0 and np.min(predictions[:, 1]) > 0.05: mdir = os.path.join(pdir, "adv0.1") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) output_image = tf.cast(images_attack_rgb, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial0.1-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag1 = 1 input() if flag2 == 0 and np.min(predictions[:, 1]) > 0.20: mdir = os.path.join(pdir, "adv0.2") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) output_image = tf.cast(images_attack_rgb, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial0.2-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag2 = 1 if flag3 == 0 and np.min(predictions[:, 1]) > 0.30: mdir = os.path.join(pdir, "adv0.3") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) output_image = tf.cast(images_attack_rgb, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial0.3-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag3 = 1 if flag4 == 0 and np.min(predictions[:, 1]) > 0.40: mdir = os.path.join(pdir, "adv0.4") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) output_image = tf.cast(images_attack_rgb, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial0.4-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag4 = 1 if flag5 == 0 and np.min(predictions[:, 1]) > 0.50: mdir = os.path.join(pdir, "adv0.5") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) output_image = tf.cast(images_attack_rgb, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial0.5-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag5 = 1 if flag6 == 0 and np.min(predictions[:, 1]) > 0.60: mdir = os.path.join(pdir, "adv0.6") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) output_image = tf.cast(images_attack_rgb, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial0.6-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag6 = 1 if flag7 == 0 and np.min(predictions[:, 1]) > 0.70: mdir = os.path.join(pdir, "adv0.7") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) output_image = tf.cast(images_attack_rgb, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial0.7-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag7 = 1 if flag8 == 0 and np.min(predictions[:, 1]) > 0.80: mdir = os.path.join(pdir, "adv0.8") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) output_image = tf.cast(images_attack_rgb, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial0.8-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag8 = 1 if flag9 == 0 and np.min(predictions[:, 1]) > 0.85: mdir = os.path.join(pdir, "adv0.85") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) output_image = tf.cast(images_attack_rgb, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial0.85-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag9 = 1 if np.min(predictions[:, 1]) > 0.90: mdir = os.path.join(pdir, "adv0.9") mdir_exist = os.path.exists(mdir) if not mdir_exist: os.makedirs(mdir) output_image = tf.cast(images_attack_rgb, tf.uint8) for i in range(nrof_samples): filename_jpg = 'adversarial0.9-%d.jpg' % (i) jpg_file = os.path.join(mdir, filename_jpg) with open(jpg_file, 'wb') as f: f.write( sess.run(tf.image.encode_jpeg( output_image[i]))) flag1 = 1 break # if step == 1: # print(gra) # input() step += 1