def __init__(self, model_type, model_data_path, mean_file_path=None, oversample=2, oversample_num=10): self._model_data_path = model_data_path self._oversample = oversample self.config_oversample_num(oversample, oversample_num) self._mean_file_path = mean_file_path if mean_file_path and len( mean_file_path) > 0 and osp.exists(mean_file_path) else None # tf.reset_default_graph() self._sesh = tf.Session() # Get the data specifications for the SplitAlexNet model self._spec = models.get_data_spec(model_class=model_class(model_type)) # Create a placeholder for the input image self._input_node = tf.placeholder(tf.float32, shape=(None, self._spec.crop_size, self._spec.crop_size, self._spec.channels)) # Get the net structure self._net = model_class(model_type)({'data': self._input_node}) # Get the io to load pic after handle with mean file self.config_caffe_io() self.load_model_sesh()
def classify(model_data_path, image_paths): '''Classify the given images using GoogleNet.''' # Get the data specifications for the GoogleNet model spec = models.get_data_spec(model_class=models.GoogleNet) # Create a placeholder for the input image input_node = tf.placeholder(tf.float32, shape=(None, spec.crop_size, spec.crop_size, spec.channels)) # Construct the network net = models.GoogleNet({'data': input_node}) with tf.Session() as sesh: # Load the converted parameters print('Loading the model') net.load(model_data_path, sesh) # Load the input image print('Loading the images') input_images = dataset.load_images(image_paths, spec).eval() # Perform a forward pass through the network to get the class probabilities print('Classifying') probs = sesh.run(net.get_output(), feed_dict={input_node: input_images}) display_results(image_paths, probs)
def main(): # Parse arguments parser = argparse.ArgumentParser() parser.add_argument('model_path', help='Path to the converted model parameters (.npy)') parser.add_argument('val_gt', help='Path to validation set ground truth (.txt)') parser.add_argument('imagenet_data_dir', help='ImageNet validation set images directory path') parser.add_argument('--model', default='GoogleNet', help='The name of the model to evaluate') args = parser.parse_args() # Load the network net = load_model(args.model) if net is None: exit(-1) # Load the dataset data_spec = models.get_data_spec(model_instance=net) image_producer = dataset.ImageNetProducer(val_path=args.val_gt, data_path=args.imagenet_data_dir, data_spec=data_spec) # Evaluate its performance on the ILSVRC12 validation set validate(net, args.model_path, image_producer, args.model)
def validate(net, model_path, images, top_k=5): '''Compute the top_k classification accuracy for the given network and images.''' # Get the data specifications for given network spec = models.get_data_spec(model_instance=net) # Get the input node for feeding in the images input_node = net.inputs['data'] # Create a placeholder for the ground truth labels label_node = tf.placeholder(tf.int32, shape=(spec.batch_size,)) # Get the output of the network (class probabilities) probs = net.get_output() # Create a top_k accuracy node top_k_op = tf.nn.in_top_k(probs, label_node, top_k) # The number of images processed count = 0 # The number of correctly classified images correct = 0 # The total number of images total = len(images) with tf.Session() as sesh: # Load the converted parameters net.load(model_path, sesh) # Iterate over and classify mini-batches for idx, (images, labels) in enumerate(images.batches(spec.batch_size)): correct += np.sum(sesh.run(top_k_op, feed_dict={input_node: images.eval(), label_node: labels})) count += images.get_shape()[0].value cur_accuracy = float(correct) * 100 / count print('{:>6}/{:<6} {:>6.2f}%'.format(count, total, cur_accuracy)) print('Top {} Accuracy: {}'.format(top_k, float(correct) / total))
def get_spec(): net = load_model('AlexNet') if net is None: exit(-1) # Load the dataset data_spec = models.get_data_spec(model_instance=net) return data_spec
def main(): # Parse arguments parser = argparse.ArgumentParser(description='Use Adam optimizer to generate adversarial examples.') parser.add_argument('-i', '--input_dir', type=str, required=True, help='Directory of dataset.') parser.add_argument('-o', '--output_dir', type=str, required=True, help='Directory of output image file.') parser.add_argument('--model', type=str, required=True,choices=['GoogleNet','Inception2'], help='Models to be evaluated.') parser.add_argument('--num_images', type=int, default=sys.maxsize, help='Max number of images to be evaluated.') parser.add_argument('--file_list', type=str, default=None, help='Evaluate a specific list of file in dataset.') parser.add_argument('--num_iter', type=int, default=100, help='Number of iterations to generate attack.') parser.add_argument('--save_freq', type=int, default=100, help='Save .npy file when each save_freq iterations.') parser.add_argument('--learning_rate', type=float, default=0.001 * 255, help='Learning rate of each iteration.') parser.add_argument('--target', type=str, default=None, help='Target list of dataset.') parser.add_argument('--weight_loss2', type=float, default=1.0, help='Weight of distance penalty.') parser.add_argument('--not_crop', dest='use_crop', action='store_false', help='Not use crop in image producer.') parser.set_defaults(use_crop=True) args = parser.parse_args() print(args.file_list) assert args.num_iter % args.save_freq == 0 data_spec = models.get_data_spec(model_name=args.model) args.learning_rate = args.learning_rate / 255.0 * (data_spec.rescale[1] - data_spec.rescale[0]) seq_len = 40 batch_size = 1 targets = None if args.target is not None: targets = {} with open(args.target, 'r') as f: for line in f: key, value = line.strip().split() targets[key] = int(value) calc_gradients( args.file_list, args.model, args.output_dir, args.num_iter, args.learning_rate, targets, args.weight_loss2, data_spec, batch_size, seq_len)
def classify(model_data_path, image_paths): '''Classify the given images using GoogleNet.''' # Get the data specifications for the GoogleNet model spec = models.get_data_spec(model_class=models.GoogleNet) # Create a placeholder for the input image input_node = tf.placeholder(tf.float32, shape=(None, spec.crop_size, spec.crop_size, spec.channels)) # Construct the network net = models.GoogleNet({'data': input_node}) # Create an image producer (loads and processes images in parallel) image_producer = dataset.ImageProducer(image_paths=image_paths, data_spec=spec) with tf.Session() as sesh: # Start the image processing workers coordinator = tf.train.Coordinator() threads = image_producer.start(session=sesh, coordinator=coordinator) # Load the converted parameters print('Loading the model') net.load(model_data_path, sesh) # Load the input image print('Loading the images') indices, input_images = image_producer.get(sesh) # Perform a forward pass through the network to get the class probabilities print('Classifying') # probs = sesh.run(net.get_output(), feed_dict={input_node: input_images}) # display_results([image_paths[i] for i in indices], probs) # # //////////////////////////////////////////////////////////////////////////////////// feature_tensor = sesh.graph.get_tensor_by_name('pool5_7x7_s1:0') features = sesh.run(feature_tensor, feed_dict={input_node: input_images}) features = np.squeeze(features) for i, j in enumerate(indices): video_feature[image_paths[j]] = features[i] # print features.shape # print features # //////////////////////////////////////////////////////////////////////////////////// # Stop the worker threads coordinator.request_stop() coordinator.join(threads, stop_grace_period_secs=2)
def classify_new(model_data_path, mean_file_path, image_paths): '''Classify the given images using SplitAlexNet.''' util.log('--------start:%s-------------------------' % image_paths) t1 = time.time() # Get the data specifications for the SplitAlexNet model spec = models.get_data_spec(model_class=models.SplitAlexNet) # Create a placeholder for the input image input_node = tf.placeholder(tf.float32, shape=(None, spec.crop_size, spec.crop_size, spec.channels)) # Construct the network net = models.SplitAlexNet({'data': input_node}) # tf.reset_default_graph() with tf.Session() as sesh: # set to reuse the variables tf.get_variable_scope().reuse_variables() util.log('Loading the model start') net.load(model_data_path, sesh) t2 = time.time() util.log('Loading the model end %.3fs' % (t2 - t1)) rst = 0 for image_path in image_paths: input_image = load_image([image_path], mean_file_path, spec) probs = sesh.run(net.get_output(), feed_dict={input_node: input_image}) if IMAGE_LOAD_TYPE == 'caffe' and OVER_SAMPLE: probs = probs.reshape((len(probs) / OVER_SAMPLE_NUM, OVER_SAMPLE_NUM, -1)) probs = probs.mean(1) util.log('[predicting]:%s,%f,%f'%(image_path,probs[0,0],probs[0,1])) if isinstance(rst, int): rst = probs else: rst = np.concatenate((rst,probs),axis=0) del input_image t3 = time.time() util.log('--------end 总耗时 %.3fs 加载模型耗时 %.3fs 预测耗时 %.3fs-----------------' % ( (t3 - t1), (t2 - t1), (t3 - t2))) return rst
def classify(model_data_path, mean_file_path, image_paths): '''Classify the given images using SplitAlexNet.''' util.log('--------start:%s-------------------------' % image_paths) t1 = time.time() # Get the data specifications for the SplitAlexNet model spec = models.get_data_spec(model_class=models.SplitAlexNet) # Create a placeholder for the input image input_node = tf.placeholder(tf.float32, shape=(None, spec.crop_size, spec.crop_size, spec.channels)) # Construct the network net = models.SplitAlexNet({'data': input_node}) #tf.reset_default_graph() with tf.Session() as sesh: # set to reuse the variables tf.get_variable_scope().reuse_variables() util.log('Loading the model start') net.load(model_data_path, sesh) t2 = time.time() util.log('Loading the model end %.3fs'% (t2-t1)) util.log('Loading the images start') input_images = load_image(image_paths,mean_file_path,spec) t3 = time.time() util.log('Loading the images end %.3fs'% (t3-t2)) # Perform a forward pass through the network to get the class probabilities util.log('judging image:%s'%image_paths) probs = sesh.run(net.get_output(), feed_dict={input_node: input_images}) if IMAGE_LOAD_TYPE == 'caffe' and OVER_SAMPLE: probs = probs.reshape((len(probs) / OVER_SAMPLE_NUM, OVER_SAMPLE_NUM, -1)) probs = probs.mean(1) t4 = time.time() del input_images # free memory util.log('--------end 总耗时 %.3fs 加载模型耗时 %.3fs 加载图片耗时 %.3fs 预测耗时 %.3fs-----------------' % ((t4 - t1), (t2 - t1), (t3 - t2),(t4-t3))) return probs
def validate(net, model_path, image_producer, top_k=5): """ Compute the top_k classification accuracy for the given network and images. """ # Get the data specifications for given network spec = models.get_data_spec(model_instance=net) # Get the input node for feeding in the images input_node = net.inputs['data'] # Create a placeholder for the ground truth labels label_node = tf.placeholder(tf.int32) # Get the output of the network (class probabilities) probs = net.get_output() # Create a top_k accuracy node top_k_op = tf.nn.in_top_k(probs, label_node, top_k) # The number of images processed count = 0 # The number of correctly classified images correct = 0 # The total number of images total = len(image_producer) with tf.Session() as session: coordinator = tf.train.Coordinator() # Load the converted parameters net.load(data_path=model_path, session=session) # Start the image processing workers threads = image_producer.start(session=session, coordinator=coordinator) # Iterate over and classify mini-batches for (labels, images) in image_producer.batches(session): correct += np.sum( session.run(top_k_op, feed_dict={ input_node: images, label_node: labels })) count += len(labels) cur_accuracy = float(correct) * 100 / count print('{:>6}/{:<6} {:>6.2f}%'.format(count, total, cur_accuracy)) # Stop the worker threads coordinator.request_stop() coordinator.join(threads, stop_grace_period_secs=2) print('Top {} Accuracy: {}'.format(top_k, float(correct) / total))
def classify(model_data_path, image_paths): '''Classify the given images using GoogleNet.''' # Get the data specifications for the GoogleNet model spec = models.get_data_spec(model_class=models.GoogleNet) # Create a placeholder for the input image input_node = tf.placeholder(tf.float32, shape=(None, spec.crop_size, spec.crop_size, spec.channels)) # Construct the network net = models.GoogleNet({'data': input_node}) # Create an image producer (loads and processes images in parallel) image_producer = dataset.ImageProducer(image_paths=image_paths, data_spec=spec) with tf.Session() as sesh: # Start the image processing workers coordinator = tf.train.Coordinator() threads = image_producer.start(session=sesh, coordinator=coordinator) # Load the converted parameters print('Loading the model') net.load(model_data_path, sesh) # Load the input image print('Loading the images') indices, input_images = image_producer.get(sesh) # Perform a forward pass through the network to get the class probabilities print('Timing in seconds :') start_time = time.time() probs = sesh.run(net.get_output(), feed_dict={input_node: input_images}) duration = time.time() - start_time print(duration) display_results([image_paths[i] for i in indices], probs) # Stop the worker threads coordinator.request_stop() coordinator.join(threads, stop_grace_period_secs=2)
def main(): # Parse arguments parser = argparse.ArgumentParser() parser.add_argument('model_path', help='Path to the converted model parameters (.npy)') parser.add_argument('val_gt', help='Path to validation set ground truth (.txt)') parser.add_argument('imagenet_data_dir', help='ImageNet validation set images directory path') parser.add_argument('--model', default='GoogleNet', help='The name of the model to evaluate') args = parser.parse_args() # Load the network net = load_model(args.model) if net is None: exit(-1) # Load the dataset data_spec = models.get_data_spec(model_instance=net) images = dataset.ImageNet(args.val_gt, args.imagenet_data_dir, data_spec) # Evaluate its performance on the ILSVRC12 validation set validate(net, args.model_path, images)
def load_model(name): '''Creates and returns an instance of the model given its class name. The created model has a single placeholder node for feeding images. ''' # Find the model class from its name all_models = models.get_models() lut = {model.__name__: model for model in all_models} if name not in lut: print('Invalid model index. Options are:') # Display a list of valid model names for model in all_models: print('\t* {}'.format(model.__name__)) return None NetClass = lut[name] # Create a placeholder for the input image spec = models.get_data_spec(model_class=NetClass) data_node = tf.placeholder(tf.float32, shape=(None, spec.crop_size, spec.crop_size, spec.channels)) # Construct and return the model return NetClass({'data': data_node})
def validate(net, model_path, image_producer, top_k=5): '''Compute the top_k classification accuracy for the given network and images.''' # Get the data specifications for given network spec = models.get_data_spec(model_instance=net) # Get the input node for feeding in the images input_node = net.inputs['data'] # Create a placeholder for the ground truth labels label_node = tf.placeholder(tf.int32) # Get the output of the network (class probabilities) probs = net.get_output() # Create a top_k accuracy node top_k_op = tf.nn.in_top_k(probs, label_node, top_k) # The number of images processed count = 0 # The number of correctly classified images correct = 0 # The total number of images total = len(image_producer) with tf.Session() as sesh: coordinator = tf.train.Coordinator() # Load the converted parameters net.load(data_path=model_path, session=sesh) # Start the image processing workers threads = image_producer.start(session=sesh, coordinator=coordinator) # Iterate over and classify mini-batches for (labels, images) in image_producer.batches(sesh): correct += np.sum(sesh.run(top_k_op, feed_dict={input_node: images, label_node: labels})) count += len(labels) cur_accuracy = float(correct) * 100 / count print('{:>6}/{:<6} {:>6.2f}%'.format(count, total, cur_accuracy)) # Stop the worker threads coordinator.request_stop() coordinator.join(threads, stop_grace_period_secs=2) print('Top {} Accuracy: {}'.format(top_k, float(correct) / total))
def classify(model_data_path, image_paths): """Classify the given images using GoogleNet.""" # Get the data specifications for the GoogleNet model spec = models.get_data_spec(model_class=models.GoogleNet) # Create a placeholder for the input image input_node = tf.placeholder(tf.float32, shape=(None, spec.crop_size, spec.crop_size, spec.channels)) # Construct the network net = models.GoogleNet({"data": input_node}) # Create an image producer (loads and processes images in parallel) image_producer = dataset.ImageProducer(image_paths=image_paths, data_spec=spec) with tf.Session() as sesh: # Start the image processing workers coordinator = tf.train.Coordinator() threads = image_producer.start(session=sesh, coordinator=coordinator) # Load the converted parameters print("Loading the model") net.load(model_data_path, sesh) # Load the input image print("Loading the images") indices, input_images = image_producer.get(sesh) # Perform a forward pass through the network to get the class probabilities print("Classifying") probs = sesh.run(net.get_output(), feed_dict={input_node: input_images}) display_results([image_paths[i] for i in indices], probs) # Stop the worker threads coordinator.request_stop() coordinator.join(threads, stop_grace_period_secs=2)
def interpret_resnet(resnet_type, attention=False): if attention: if resnet_type == 50: spec = models.get_data_spec(model_class=models.attResNet50) net = models.attResNet50 elif resnet_type == 101: spec = models.get_data_spec(model_class=models.attResNet101) net = models.attResNet101 elif resnet_type == 152: spec = models.get_data_spec(model_class=models.attResNet152) net = models.attResNet152 else: if resnet_type == 50: spec = models.get_data_spec(model_class=models.ResNet50) net = models.ResNet50 elif resnet_type == 101: spec = models.get_data_spec(model_class=models.ResNet101) net = models.ResNet101 elif resnet_type == 152: spec = models.get_data_spec(model_class=models.ResNet152) net = models.ResNet152 return net, spec
import models import tensorflow as tf import numpy as np # Get the data specifications for the GoogleNet model spec = models.get_data_spec(model_class=models.VGG_FACE_16_layer) x = tf.placeholder(tf.string) #input filename image_string = tf.read_file(x) testimage = tf.image.decode_image(image_string, channels=3) testimage = tf.cast(testimage, tf.float32) averageImg = tf.constant([129.1863, 104.7624, 93.5940]) averageImg = tf.reshape(averageImg, [1, 1, 3]) testimage = tf.reshape(testimage - averageImg, [-1, 224, 224, 3]) #subtract the mean #testimage = tf.transpose(testimage, perm=[0, 1, 2, 3]) #y_ = tf.placeholder(tf.float32, shape=[None, ylen]) #labels x_img_channel_swap = testimage[:, :, :, :: -1] #RGB to BGR because the model is from caffe # Construct the network net = models.VGG_FACE_16_layer({'data': x_img_channel_swap}) testfile = '/home/djecklocal/Documents/python/FaceGender/Models/VGG_Face_Caffe/ak.png' sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) prediction = testimage.eval(feed_dict={x: testfile}) print(prediction)
def classify(model_data_path, image_pathFolder): '''Classify the given images using AlexNet.''' print(model_data_path) print(image_pathFolder) image_paths = [] for filename in os.listdir(image_pathFolder): image_paths.append(image_pathFolder + filename) print(image_paths) #if(True) #sys.exit(0) # Get the data specifications for the AlexNet model spec = models.get_data_spec(model_class=models.AlexNet) #print(spec) # Create a placeholder for the input image input_node = tf.placeholder(tf.float32, shape=(None, spec.crop_size, spec.crop_size, spec.channels)) print(input_node) # Construct the network net = models.AlexNet({'data': input_node}) print("net---------------------") # Create an image producer (loads and processes images in parallel) image_producer = dataset.ImageProducer(image_paths=image_paths, data_spec=spec) print(image_producer) os.environ["TF_CPP_MIN_LOG_LEVEL"] = '1' with tf.Session() as sesh: print( 'start -----------------------------------------------------------------%s' % datetime.now()) sesh.run(tf.global_variables_initializer()) # Start the image processing workers coordinator = tf.train.Coordinator() threads = image_producer.start(session=sesh, coordinator=coordinator) # Load the converted parameters print( 'Loading the model -----------------------------------------------------------------%s' % datetime.now()) net.load(model_data_path, sesh) # Load the input image print( 'Loading the images-----------------------------------------------------------------%s' % datetime.now()) indices, input_images = image_producer.get(sesh) # Perform a forward pass through the network to get the class probabilities print( 'Classifying -----------------------------------------------------------------%s' % datetime.now()) probs = sesh.run(net.get_output(), feed_dict={input_node: input_images}) print( 'Classifying END -----------------------------------------------------------------%s' % datetime.now()) display_results([image_paths[i] for i in indices], probs) # Stop the worker threads coordinator.request_stop() coordinator.join(threads, stop_grace_period_secs=2)
def validate(net, model_path, image_producer, model_name, top_k=5): '''Compute the top_k classification accuracy for the given network and images.''' # Get the data specifications for given network spec = models.get_data_spec(model_instance=net) # Get the input node for feeding in the images input_node = net.inputs['data'] # Create a placeholder for the ground truth labels label_node = tf.placeholder(tf.int32) # Get the output of the network (class probabilities) probs = net.get_output() compute_ops = {} threshold = 50 # Sparisty in conv layer inputs total_ops = 0 for lname, op_count in net.layer_op_counts.iteritems(): layer_inputs = net.layer_inputs[lname] assert (len(layer_inputs) == 1) for lin in layer_inputs: zero_actv = tf.less(tf.abs(lin), threshold) num_zeroes = tf.reduce_sum(tf.cast(zero_actv, tf.int32)) sparsity = tf.div(tf.cast(num_zeroes, tf.float32), tf.cast(tf.size(lin), tf.float32)) compute_ops[lname] = sparsity compute_ops[lname + '_min'] = tf.reduce_min(lin) compute_ops[lname + '_max'] = tf.reduce_max(lin) total_ops = total_ops + op_count """ for lname in sorted(net.layer_op_counts, key=net.layer_op_counts.get, reverse=True): op_count = net.layer_op_counts[lname] print("%s %.3f" %(lname, float(op_count)/total_ops)) """ # Create a top_k accuracy node top_k_op = tf.nn.in_top_k(probs, label_node, top_k) compute_ops['top_k'] = top_k_op # The number of images processed count = 0 # The number of correctly classified images correct = 0 # The total number of images total = len(image_producer) #merged = tf.merge_all_summaries() sparsity = {} input_min = {} input_max = {} for lname, ops in net.layer_op_counts.iteritems(): sparsity[lname] = 0 input_max[lname] = float('-inf') input_min[lname] = float('inf') with tf.Session() as sesh: #writer = tf.train.SummaryWriter('/tmp/' + model_name, graph = sesh.graph) coordinator = tf.train.Coordinator() # Load the converted parameters net.load(data_path=model_path, session=sesh) # Start the image processing workers threads = image_producer.start(session=sesh, coordinator=coordinator) # Iterate over and classify mini-batches batch_num = 0 for (labels, images) in image_producer.batches(sesh): start = time.time() """ summary, top_k_res = sesh.run([top_k_op], feed_dict={input_node: images, label_node: labels}) """ out_dict = sesh.run(compute_ops, feed_dict={ input_node: images, label_node: labels }) for lname, ops in net.layer_op_counts.iteritems(): sparsity[lname] = sparsity[lname] + out_dict[lname] input_max[lname] = max(input_max[lname], out_dict[lname + '_max']) input_min[lname] = min(input_min[lname], out_dict[lname + '_min']) correct += np.sum(out_dict['top_k']) print('Inference time for batch_size=%d is %.2f ms.' % (len(labels), 1000 * (time.time() - start))) count += len(labels) cur_accuracy = float(correct) * 100 / count print('{:>6}/{:<6} {:>6.2f}%'.format(count, total, cur_accuracy)) #writer.add_summary(summary, batch_num) batch_num = batch_num + 1 # Stop the worker threads coordinator.request_stop() coordinator.join(threads, stop_grace_period_secs=2) #writer.close() print('Top {} Accuracy: {}'.format(top_k, float(correct) / total)) for lname in sorted(net.layer_op_counts, key=net.layer_op_counts.get, reverse=True): print lname, float(net.layer_op_counts[lname])/total_ops, \ sparsity[lname]/total, input_min[lname], input_max[lname]
elif layer_name.startswith('conv') or layer_name.startswith('fc'): parameters[layer_name]['weights'] = np.load( '%s/%s/weights.npy' % (pruned_weights_root, layer_name)) parameters[layer_name]['biases'] = np.load( '%s/%s/biases.npy' % (pruned_weights_root, layer_name)) return parameters # Load the network net = load_model('ResNet50') if net is None: exit(-1) exit(-1) parameters = np.load('res50.npy') # Place the pruned weights into a parameters pruned_parameters = make_pruned_parameters(parameters, 'pruned_weights') # Load the dataset val_gt = 'val.txt' # imagenet_data_dir = '../val_img/' imagenet_data_dir = '/mnt/data/imagenet/ILSVRC2012_img_val' # model_path = 'vgg16.npy' data_spec = models.get_data_spec(model_instance=net) image_producer = dataset.ImageNetProducer(val_path=val_gt, data_path=imagenet_data_dir, data_spec=data_spec) # Evaluate its performance on the ILSVRC12 validation set validate(net, pruned_parameters, image_producer)
def classify(model_data_path, image_paths): '''Classify the given images using GoogleNet.''' # Get the data specifications for the GoogleNet model #spec = models.get_data_spec(model_class=models.GoogleNet) spec = models.get_data_spec(model_class=models.VGG_ILSVRC_16_layer) # Create a placeholder for the input image input_node = tf.placeholder(tf.float32, shape=(None, spec.crop_size, spec.crop_size, spec.channels)) # Construct the network #net = models.GoogleNet({'data': input_node}) net = models.VGG_ILSVRC_16_layer({'data': input_node}) def load_image(image_path, data_spec): # Read the file file_data = tf.read_file(image_path) # Decode the image data img = tf.image.decode_jpeg(file_data, channels=data_spec.channels) if data_spec.expects_bgr: # Convert from RGB channel ordering to BGR # This matches, for instance, how OpenCV orders the channels. img = tf.reverse(img, [2, 1, 0]) return img # Create an image producer (loads and processes images in parallel) image_producer = dataset.ImageProducer(image_paths=image_paths, data_spec=spec) with tf.Session() as sesh: # Start the image processing workers #coordinator = tf.train.Coordinator() #threads = image_producer.start(session=sesh, coordinator=coordinator) # Load the converted parameters print('Loading the model') net.load(model_data_path, sesh) # Load the input image print('Loading the images') is_jpeg = True probs = [] for image_path in image_paths: input_image = load_image(image_path, image_producer.data_spec) processed_img = dataset.process_image( img=input_image, scale=image_producer.data_spec.scale_size, isotropic=image_producer.data_spec.isotropic, crop=image_producer.data_spec.crop_size, mean=image_producer.data_spec.mean) #print('Classifying') prob = sesh.run(net.get_output(), feed_dict={ input_node: np.reshape(processed_img.eval(), (1, 224, 224, 3)) }) probs.extend(prob) #indices, input_images = image_producer.get(sesh) indices = range(len(image_paths)) # Perform a forward pass through the network to get the class probabilities display_results([image_paths[i] for i in indices], probs)
def calc_gradients(sesh, model_name, image_producer, output_file_dir, max_iter, save_freq, learning_rate=1.0, targets=None, weight_loss2=1, data_spec=None, batch_size=1, noise_file=None): """Compute the gradients for the given network and images.""" spec = models.get_data_spec(model_name) modifier = tf.Variable( np.zeros((batch_size, spec.crop_size, spec.crop_size, spec.channels), dtype=np.float32)) input_image = tf.placeholder( tf.float32, (None, spec.crop_size, spec.crop_size, spec.channels)) input_label = tf.placeholder(tf.int32, (None)) true_image = tf.minimum( tf.maximum(modifier + input_image, -spec.mean + spec.rescale[0]), -spec.mean + spec.rescale[1]) diff = true_image - input_image loss2 = tf.sqrt(tf.reduce_mean(tf.square(true_image - input_image))) probs, variable_set = models.get_model(sesh, true_image, model_name) weight_loss1 = 1 true_label_prob = tf.reduce_mean( tf.reduce_sum(probs * tf.one_hot(input_label, 1000), [1])) if targets is None: loss1 = -tf.log(1 - true_label_prob + 1e-6) else: loss1 = -tf.log(true_label_prob + 1e-6) loss = weight_loss1 * loss1 # + weight_loss2 * loss2 optimizer = tf.train.AdamOptimizer(learning_rate) train = optimizer.minimize(loss, var_list=[modifier]) noise = None # Load noise file if noise_file is not None: noise = np.load(noise_file) / 255.0 * \ (spec.rescale[1] - spec.rescale[0]) # The number of images processed # The total number of images total = len(image_producer) save_times = (max_iter - 1) / save_freq + 1 gradient_record = np.zeros(shape=(save_times, total, spec.crop_size, spec.crop_size, spec.channels), dtype=float) rec_iters = [] rec_names = [] rec_dist = [] # initiallize all uninitialized varibales init_varibale_list = set(tf.all_variables()) - variable_set sesh.run(tf.initialize_variables(init_varibale_list)) tot_image = 0 image_producer.startover() # Interactive with mini-batches for (indices, labels, names, images) in image_producer.batches(sesh): sesh.run(tf.initialize_variables(init_varibale_list)) if targets is not None: labels = [targets[e] for e in names] if noise is not None: for i in range(len(indices)): images[i] += noise[indices[i]] feed_dict = {input_image: images, input_label: labels} var_loss, true_prob, var_loss1, var_loss2 = sesh.run( (loss, true_label_prob, loss1, loss2), feed_dict=feed_dict) tot_image += 1 print 'Start!' min_loss = var_loss last_min = -1 # record numer of iteration tot_iter = 0 for cur_iter in range(max_iter): tot_iter += 1 sesh.run(train, feed_dict=feed_dict) var_loss, true_prob, var_loss1, var_loss2 = sesh.run( (loss, true_label_prob, loss1, loss2), feed_dict=feed_dict) break_condition = False if var_loss < min_loss * 0.99: min_loss = var_loss last_min = cur_iter if (cur_iter + 1) % save_freq == 0: noise_diff = sesh.run(modifier) for i in range(len(indices)): gradient_record[(cur_iter + 1) / save_freq - 1][indices[i]] = noise_diff[i] if cur_iter + 1 == max_iter or break_condition: var_diff, var_probs = sesh.run((modifier, probs), feed_dict=feed_dict) var_diff = np.sqrt(np.mean( np.square(var_diff), (1, 2, 3))) / (spec.rescale[1] - spec.rescale[0]) * 255.0 correct_top_1 = 0 for i in range(len(indices)): top1 = var_probs[i].argmax() if labels[i] == top1: correct_top_1 += 1 rec_iters.append(tot_iter) rec_names.append(names[i]) rec_dist.append(var_diff[i]) break return gradient_record
def main(): # Parse arguments parser = argparse.ArgumentParser( description='Use Fast Gradient or Fast Gradient Sign method \ to generate adversarial examples.') parser.add_argument('-i', '--input_dir', type=str, required=True, help='Directory of dataset.') parser.add_argument('-o', '--output_dir', type=str, required=True, help='Directory of output image file.') parser.add_argument('--model', type=str, required=True, choices=['GoogleNet'], help='Models to be evaluated.') parser.add_argument('--file_list', type=str, required=False, help='Evaluate a specific list of file in dataset.') parser.add_argument('--num_iter', type=int, default=100) parser.add_argument('--sign', dest='use_sign', action='store_true') parser.add_argument('--target', type=str, default=None, help='Target list of file in dataset.') parser.add_argument('--noise_file', type=str, default=None) parser.add_argument('-n', '--not_crop', dest='need_rescale', action='store_false') parser.set_defaults(num_images=sys.maxsize) parser.set_defaults(use_sign=False) parser.set_defaults(need_rescale=True) args = parser.parse_args() targets = None if args.target is not None: targets = {} with open(args.target, 'r') as f: for line in f: key, value = line.strip().split() targets[key] = int(value) if os.path.exists(args.output_dir): shutil.rmtree(args.output_dir) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) data_spec = models.get_data_spec(model_name=args.model) sesh = tf.Session() if args.noise_file is None: input_node = tf.placeholder( tf.float32, shape=( None, data_spec.crop_size, data_spec.crop_size, data_spec.channels)) probs_output, variable_list = models.get_model( sesh, input_node, args.model) image_producer = dataset.ImageNetProducer( file_list=args.file_list, data_path=args.input_dir, num_images=args.num_images, need_rescale=args.need_rescale, data_spec=data_spec, batch_size=1) print 'Start compute gradients' if args.noise_file is None: gradients = calc_gradients( sesh, image_producer, input_node, probs_output, data_spec, args.use_sign, targets) else: gradients = np.load(args.noise_file) if args.use_sign: gradients = np.sign(gradients) print 'End compute gradients' gradients /= np.sqrt(np.mean(np.square(gradients))) // Why there is a normalization? print 'RMSE of gradients', np.sqrt(np.mean(np.square(gradients))) for magnitude in range(1, args.num_iter + 1): distance = save_file(sesh, image_producer, os.path.join(args.output_dir, str(magnitude)), gradients * magnitude / 255.0 * (data_spec.rescale[1] - data_spec.rescale[0]), data_spec)
def classify(model_data_path, image_pathFolder): '''Classify the given images using VGG16.''' #print(model_data_path) #print(image_pathFolder) image_paths = [] for filename in os.listdir(image_pathFolder): image_paths.append(image_pathFolder + filename) #print(image_paths) # Get the data specifications for the VggNet model spec = models.get_data_spec(model_class=models.VGG16) ##print(spec) # Create a placeholder for the input image input_node = tf.placeholder(tf.float32, shape=(None, spec.crop_size, spec.crop_size, spec.channels)) #print(input_node) # Construct the network net = models.VGG16({'data': input_node}) #print("net---------------------") # Create an image producer (loads and processes images in parallel) image_producer = dataset.ImageProducer(image_paths=image_paths, data_spec=spec) #print(image_producer) config = tf.ConfigProto() config.gpu_options.allow_growth = True #tf.ConfigProto() log_device_placement = True # 是否打印设备分配日志 allow_soft_placement = False # 如果你指定的设备不存在,允许TF自动分配设备 config = tf.ConfigProto(log_device_placement=True, allow_soft_placement=False) with tf.Session(config=config) as sesh: #print('start -----------------------------------------------------------------%s' % datetime.now()) #sesh.run(tf.global_variables_initializer()) # Start the image processing workers coordinator = tf.train.Coordinator() threads = image_producer.start(session=sesh, coordinator=coordinator) # Load the converted parameters #print('Loading the model -----------------------------------------------------------------%s' % datetime.now()) net.load(model_data_path, sesh) # Load the input image #print('Loading the images-----------------------------------------------------------------%s' % datetime.now()) indices, input_images = image_producer.get(sesh) # Perform a forward pass through the network to get the class probabilities print( 'Classifying -----------------------------------------------------------------%s' % datetime.now()) probs = sesh.run(net.get_output(), feed_dict={input_node: input_images}) print( 'Classifying END -----------------------------------------------------------------%s' % datetime.now()) display_results([image_paths[i] for i in indices], probs) # Stop the worker threads coordinator.request_stop() coordinator.join(threads, stop_grace_period_secs=2)