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_resnet_v2.inception_resnet_v2_arg_scope()): _, end_points = inception_resnet_v2.inception_resnet_v2( x_input, num_classes=num_classes, is_training=False) with slim.arg_scope(inception.inception_v3_arg_scope()): _, end_points_2 = inception.inception_v3( x_input, num_classes=num_classes, is_training=False) predicted_labels = tf.argmax(0.5 * end_points['Predictions'] + 0.5 * end_points_2['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))
def conv_tower_fn(self, images, is_training=True, reuse=None): """Computes convolutional features using the InceptionV3 model. Args: images: A tensor of shape [batch_size, height, width, channels]. is_training: whether is training or not. reuse: whether or not the network and its variables should be reused. To be able to reuse 'scope' must be given. Returns: A tensor of shape [batch_size, OH, OW, N], where OWxOH is resolution of output feature map and N is number of output features (depends on the network architecture). """ mparams = self._mparams['conv_tower_fn'] logging.debug('Using final_endpoint=%s', mparams.final_endpoint) with tf.variable_scope('conv_tower_fn/INCE'): if reuse: tf.get_variable_scope().reuse_variables() # with slim.arg_scope(inception.inception_v3_arg_scope()): with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=is_training): net, _ = inception_resnet_v2.inception_resnet_v2_base( images, final_endpoint=mparams.final_endpoint) # net, _ = inception.inception_v3_base( # images, final_endpoint=mparams.final_endpoint) return net
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_resnet_v2.inception_resnet_v2_arg_scope()): _, end_points = inception_resnet_v2.inception_resnet_v2( 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))
def get_network(self, input_tensor, is_training): # Load pre-trained inception-resnet model with slim.arg_scope(inception_resnet_v2_arg_scope(batch_norm_decay = 0.999, weight_decay = 0.0001)): net, end_points = inception_resnet_v2(input_tensor, is_training = is_training) # Adding some modification to original InceptionResnetV2 - changing scoring of AUXILIARY TOWER weight_decay = 0.0005 with tf.variable_scope('NewInceptionResnetV2'): with tf.variable_scope('AuxiliaryScoring'): with slim.arg_scope([layers.convolution2d, layers.convolution2d_transpose], weights_regularizer = slim.l2_regularizer(weight_decay), biases_regularizer = slim.l2_regularizer(weight_decay), activation_fn = None): tf.summary.histogram('Last_layer/activations', net, [KEY_SUMMARIES]) # Scoring net = slim.dropout(net, 0.7, is_training = is_training, scope = 'Dropout') net = layers.convolution2d(net, num_outputs = self.FEATURES, kernel_size = 1, stride = 1, scope = 'Scoring_layer') feature = net tf.summary.histogram('Scoring_layer/activations', net, [KEY_SUMMARIES]) # Upsampling net = layers.convolution2d_transpose(net, num_outputs = 16, kernel_size = 17, stride = 17, padding = 'VALID', scope = 'Upsampling_layer') tf.summary.histogram('Upsampling_layer/activations', net, [KEY_SUMMARIES]) # Smoothing layer - separable gaussian filters net = super()._get_gauss_smoothing_net(net, size = self.SMOOTH_SIZE, std = 1.0, kernel_sum = 0.2) return net, feature
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_resnet_v2.inception_resnet_v2_arg_scope()): _, end_points = inception_resnet_v2.inception_resnet_v2( x_input, num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE) y_logits = tf.reduce_max(end_points['Predictions']) predicted_labels = tf.argmax(end_points['Predictions'], 1) gradients = tf.gradients(y_logits, x_input) # Run computation saver = tf.train.Saver( slim.get_model_variables( scope='InceptionResnetV2')) # only train InceptionResnetV2 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)) ''' watch the input dir for defense ''' observer = Observer() event_handler = FileEventHandler(batch_shape=batch_shape, sess=sess, x_input=x_input, predicted_labels=predicted_labels, gradients=gradients, output_dir=FLAGS.output_dir) observer.schedule(event_handler, FLAGS.input_dir, recursive=True) observer.start() print("watchdog start...") try: while True: time.sleep(0.000001) except KeyboardInterrupt: observer.stop() observer.join() print("\nwatchdog stoped!")
def create_model(x, reuse=None): """Create model graph. Args: x: input images reuse: reuse parameter which will be passed to underlying variable scopes. Should be None first call and True every subsequent call. Returns: (logits, end_points) - tuple of model logits and enpoints Raises: ValueError: if model type specified by --model_name flag is invalid. """ if FLAGS.model_name == 'inception_v3': with slim.arg_scope(inception.inception_v3_arg_scope()): return inception.inception_v3(x, num_classes=NUM_CLASSES, is_training=False, reuse=reuse) elif FLAGS.model_name == 'inception_resnet_v2': with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): return inception_resnet_v2.inception_resnet_v2( x, num_classes=NUM_CLASSES, is_training=False, reuse=reuse) else: raise ValueError('Invalid model name: %s' % (FLAGS.model_name))
def define_cnn_session(): """ Code modified from here: [https://github.com/tensorflow/models/issues/429] """ checkpoint_file = 'tf_int/model.ckpt-25933' SLIM_CKPT = checkpoint_file g_1 = tf.Graph() with g_1.as_default(): # Setup preprocessing input_tensor = tf.placeholder(tf.float32, shape=(None, 299, 299, 3), name='input_image') scaled_input_tensor = tf.scalar_mul((1.0 / 255), input_tensor) scaled_input_tensor = tf.subtract(scaled_input_tensor, 0.5) scaled_input_tensor = tf.multiply(scaled_input_tensor, 2.0) # Setup session sess2 = tf.Session(graph=g_1) arg_scope = inception_resnet_v2_arg_scope # Load the model with g_1.as_default(): with slim.arg_scope(inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2(scaled_input_tensor, num_classes=2, is_training=False) variables_to_restore = slim.get_variables_to_restore() saver2 = tf.train.Saver(variables_to_restore) saver2.restore(sess2, SLIM_CKPT) return sess2, input_tensor, end_points
def inference(inputs, weight_decay): num_classes = None with slim.arg_scope( network.inception_resnet_v2_arg_scope( weight_decay=weight_decay)): net, endpoints = network.inception_resnet_v2(inputs, num_classes) return net, endpoints
def build_graph(): images = tf.placeholder(tf.float32, shape=(None, 480, 720, 3)) labels = tf.placeholder(tf.int32, shape=(None, )) # restore from the inception_resnet model with slim.arg_scope(inception_resnet_v2_arg_scope()): _, end_points = inception_resnet_v2(images, num_classes=1001, is_training=False) # extract the before_logit tensor from the extracted model before_logit = end_points['PreLogitsFlatten'] # define a placeholder for training from the bottle, not from the beginning. bottle_neck = tf.placeholder(tf.float32, shape=(None, 1536)) # now define the fine tune part of the graph with tf.name_scope('my_fine_tune') as scope: # dropout = tf.layers.dropout(bottle_neck, rate=0.3, name='dropout') batch_norm1 = tf.layers.batch_normalization(bottle_neck, name='batch_norm1') dropout1 = tf.layers.dropout(batch_norm1, rate=0.3, name='dropout1') dense1 = tf.layers.dense(dropout1, 128, activation=tf.nn.relu, name='dense1') batch_norm2 = tf.layers.batch_normalization(dense1, name='batch_norm2') dropout2 = tf.layers.dropout(batch_norm2, rate=0.3, name='dropout2') logits = tf.layers.dense(dropout2, 4, name='logits') x_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=logits, labels=labels) loss = tf.reduce_mean(x_entropy, name='loss') correct = tf.nn.in_top_k(logits, labels, 1) accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) # extract variables that need to be trained weight1, bias1 = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'dense1') weight2, bias2 = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'logits') variables_to_train = [weight1, weight2, bias1, bias2] optimizer = tf.train.AdamOptimizer() train_op = slim.learning.create_train_op( loss, optimizer, variables_to_train=variables_to_train) # defiine the name scope we don't need to restore from inception_resnet exclude = [ 'InceptionResnetV2/AuxLogits', 'InceptionResnetV2/Logits', 'my_fine_tune/', 'dense1', 'logits', 'batch_norm' ] variables_to_restore = slim.get_variables_to_restore(exclude=exclude) saver = tf.train.Saver(variables_to_restore) init = tf.global_variables_initializer() builder = tf.saved_model.builder.SavedModelBuilder(save_model_file) return images, labels, before_logit, bottle_neck, batch_norm2, loss, accuracy, train_op, saver, init, builder
def testBuildNoClasses(): batch_size = 5 height, width = 299, 299 num_classes = None inputs = tf.random_uniform((batch_size, height, width, 3),name='MyInputs') with slim.arg_scope(network.inception_resnet_v2_arg_scope()): net, endpoints = network.inception_resnet_v2(inputs, num_classes) return
def main(_): batch_shape = [1, FLAGS.image_height, FLAGS.image_width, 3] # now let's work on one image per time num_classes = 1001 itr = 30 tf.logging.set_verbosity(tf.logging.INFO) print("input dir = {0}".format(FLAGS.input_dir)) with tf.Graph().as_default(): # Prepare graph x_input = tf.placeholder(tf.float32, shape=batch_shape) #print('x_input.shape = {0}'.format(x_input.shape)) cropped_xs = defend_crop(x_input) #print('after crop, x_input.shape = {0}'.format(cropped_xs.shape)) #finished input transformation with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): cropped_logits, end_points_xs = inception_resnet_v2.inception_resnet_v2( cropped_xs, num_classes=num_classes, is_training=False, create_aux_logits=False) cropped_probs = tf.reduce_mean(tf.nn.softmax(cropped_logits), axis=0, keep_dims=True) # 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): #print("filename = {0}".format(filenames)) #final_preds = np.zeros([FLAGS.batch_size, num_classes, itr]) #print('min = {0}'.format(np.amin(images))) #print('max = {0}'.format(np.amax(images))) #print('after jpeg min = {0}'.format(np.amin(images))) #print('after jpeg max = {0}'.format(np.amax(images))) #print('images.shape after = {0}'.format(images.shape)) """ pred, aux_pred = sess.run([end_points_xs['Predictions'], end_points_xs['AuxPredictions']], feed_dict={x_input: images}) final_probs = pred + 0.4 * aux_pred labels = np.argmax(final_probs, 1) """ probs = sess.run(cropped_probs, feed_dict={x_input: images}) labels = np.argmax(probs, 1) for filename, label in zip(filenames, labels): out_file.write('{0},{1}\n'.format(filename, label))
def __call__(self, x_input): """Constructs model and return probabilities for given input.""" reuse = True if self.built else None with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2.inception_resnet_v2( x_input, num_classes=self.num_classes, reuse=tf.AUTO_REUSE, is_training=False, scope=self.scope, create_aux_logits = True) #create_aux_logits=True, reuse=tf.AUTO_REUSE self.built = True return logits, end_points
def get_inception_param(type): if type == 'inception_resnet_v2': arg_scope = inception_resnet_v2_arg_scope() pretrained_model = inception_resnet_v2 ckpt_path = 'pre-trained/tensorflow-inception-pretrained/inception_resnet_v2_2016_08_30.ckpt' else: raise Exception('error model %s' % type) return arg_scope, pretrained_model, ckpt_path
def __call__(self, x_input): """Constructs model and return probabilities for given input.""" reuse = True if self.built else None with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): _, end_points = inception_resnet_v2.inception_resnet_v2( x_input, num_classes=self.num_classes, is_training=False) self.built = True probs = end_points['Predictions'] # Strip off the extra reshape op at the output return probs
def convolve_data_inception(input_data, val, n, dtype): # pass data into the INRV2 Network with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): data = tf.reshape(input_data, [-1, 299, 299, 3]) logits, _ = inception_resnet_v2.inception_resnet_v2( data, num_classes=output_sizes[-1] * output_sizes[-1] * layer_elements[-2], is_training=False, reuse=incep_reuse) return logits
def build_model(graph): with graph.as_default(): with tf.contrib.slim.arg_scope(resnet.inception_resnet_v2_arg_scope()): placeholder = tf.placeholder(tf.string, name='input') image = tf.image.decode_jpeg(placeholder, channels=3, name='image') image = preprocess.preprocess_for_eval(image, FLAGS.image_size, FLAGS.image_size) image = tf.expand_dims(image, 0) logits, end_points = resnet.inception_resnet_v2( image, is_training=False, dropout_keep_prob=1.0) saver = tf.train.Saver(tf.global_variables()) return placeholder, logits, end_points
def main(_): batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3] num_classes = 1001 tf.logging.set_verbosity(tf.logging.INFO) transforms = [ident, flip_and_desat, flip_and_brighten, zoom, tight_zoom] prob_accum_all_models = [] filename_accum = [] for transform in transforms: with tf.Graph().as_default(): # Prepare graph x_input = tf.placeholder(tf.float32, shape=batch_shape) transformed_input = transform(x_input) with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): _, end_points = inception_resnet_v2.inception_resnet_v2( transformed_input, num_classes=num_classes, is_training=False) predicted_probs = end_points['Predictions'] # 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) prob_accum_one_model = [] with tf.train.MonitoredSession( session_creator=session_creator) as sess: for filenames, images in load_images( FLAGS.input_dir, batch_shape): probs = sess.run(predicted_probs, feed_dict={x_input: images}) # deal with incomplete batches (eg at end of pred data) realized_batch_size = len(filenames) prob_accum_one_model.append( probs[:realized_batch_size]) if transform is transforms[0]: filename_accum.extend(filenames) prob_accum_all_models.append(np.vstack(prob_accum_one_model)) all_probs = np.array(prob_accum_all_models) labels = all_probs.sum(0).argmax(1) with tf.gfile.Open(FLAGS.output_file, 'w') as out_file: for filename, label in zip(filename_accum, labels): out_file.write('{0},{1}\n'.format(filename, label))
def defense_random(input_dir, checkpoint_path, itr, batch_size): print('Running defense: Randompadding_IresV2') batch_shape = [batch_size, 299, 299, 3] image_resize = 331 num_classes = 1001 print('iteration: %d' % itr) tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): # Prepare graph x_input = tf.placeholder(tf.float32, shape=batch_shape) img_resize_tensor = tf.placeholder(tf.int32, [2]) x_input_resize = tf.image.resize_images(x_input, img_resize_tensor, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) shape_tensor = tf.placeholder(tf.int32, [3]) padded_input = padding_layer_iyswim(x_input_resize, shape_tensor) # 330 is the last value to keep 8*8 output, 362 is the last value to keep 9*9 output, stride = 32 padded_input.set_shape( (batch_size, image_resize, image_resize, 3)) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): _, end_points = inception_resnet_v2.inception_resnet_v2( padded_input, num_classes=num_classes, is_training=False, create_aux_logits=True) 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=checkpoint_path, master='') with tf.train.MonitoredSession(session_creator=session_creator) as sess: final_labels = {} for filenames, images in load_images(input_dir, batch_shape): final_preds = np.zeros( [batch_size, num_classes, itr]) for j in range(itr): if np.random.randint(0, 2, size=1) == 1: images = images[:, :, ::-1, :] resize_shape_ = np.random.randint(310, 331) pred, aux_pred = sess.run([end_points['Predictions'], end_points['AuxPredictions']], feed_dict={x_input: images, img_resize_tensor: [resize_shape_]*2, shape_tensor: np.array([random.randint(0, image_resize - resize_shape_), random.randint(0, image_resize - resize_shape_), image_resize])}) final_preds[..., j] = pred + 0.4 * aux_pred final_probs = np.sum(final_preds, axis=-1) labels = np.argmax(final_probs, 1) final_labels.update(dict(zip(filenames, labels))) return final_labels
def __call__(self, ens_x_input, vgg_x_input, inc_x_input, tcd_x_input): """Constructs model and return probabilities for given input.""" reuse = True if self.built else None logits = None aux_logits = None weights = [[0.7, 0.1], [0.2, 0.1]] all_inputs = [[ens_x_input, tcd_x_input], [inc_x_input, tcd_x_input]] scopes = [ inception_resnet_v2.inception_resnet_v2_arg_scope(), inception.inception_v3_arg_scope() ] reuse_flags = [reuse, True] for model_idx, model in enumerate( [inception_resnet_v2.inception_resnet_v2, inception.inception_v3]): with slim.arg_scope(scopes[model_idx]): for idx, inputs in enumerate(all_inputs[model_idx]): result = model(inputs, num_classes=self.num_classes, is_training=False, reuse=reuse_flags[idx]) weight = weights[model_idx][idx] # :1 is for slicing out the background class if logits == None: logits = result[0][:, 1:] * weight aux_logits = result[1]['AuxLogits'][:, 1:] * weight else: logits += result[0][:, 1:] * weight aux_logits += result[1]['AuxLogits'][:, 1:] * weight with slim.arg_scope(vgg.vgg_arg_scope()): weight = 0.1 result = vgg.vgg_16(vgg_x_input, num_classes=1000, is_training=False) logits += result[0] * weight with slim.arg_scope(resnet_utils.resnet_arg_scope()): weight = 0.05 result = resnet_v2.resnet_v2_152(vgg_x_input, num_classes=self.num_classes, reuse=reuse) logits += tf.squeeze(result[0])[:, 1:] * weight self.built = True aux_weight = 0.8 logits += aux_logits * aux_weight predictions = layers_lib.softmax(logits) return predictions
def _init_inception_resnet_v2_session(): height, width, channels, checkpoint_file = 299, 299, 3, 'inception_resnet_v2_2016_08_30.ckpt' slim = tf.contrib.slim x = tf.placeholder(tf.float32, shape=(None, height, width, channels)) arg_scope = inception_resnet_v2_arg_scope() with slim.arg_scope(arg_scope): logits, end_points = inception_resnet_v2(x, is_training=False, num_classes=1001) features = end_points['PreLogitsFlatten'] sess = tf.Session() saver = tf.train.Saver() saver.restore(sess, checkpoint_file) return x, features, sess, height, width
def model_fn(features, inception_dir): with tf.contrib.slim.arg_scope(inception_resnet_v2_arg_scope()): output = inception_resnet_v2(features, num_classes=None, is_training=False, dropout_keep_prob=1, reuse=None, scope='InceptionResnetV2', create_aux_logits=False, activation_fn=tf.nn.relu) tf.train.init_from_checkpoint( os.path.join(inception_dir, 'inception_resnet_v2_2016_08_30.ckpt'), {"InceptionResnetV2/": "InceptionResnetV2/"}) return output
def _build(self): reuse = True if self.built else None with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2.inception_resnet_v2( self.input, num_classes=self.num_classes, is_training=False, reuse=reuse) self.built = True self.end_points = end_points self.logits = logits if not self.ckpt_loaded: saver = tf.train.Saver() saver.restore(self.sess, ckpt_dir + 'inception_resnet_v2.ckpt') self.ckpt_loaded = True
def __init__(self, checkpoint_path): self.graph = tf.Graph() self.sess = tf.Session(graph=self.graph) with self.graph.as_default(): self.height, self.width, self.channels = 299, 299, 3 self.x = tf.placeholder( tf.float32, shape=[None, self.height, self.width, self.channels]) arg_scope = inception_resnet_v2_arg_scope() with slim.arg_scope(arg_scope): logits, end_points = inception_resnet_v2(self.x, is_training=False, num_classes=1001) self.features = end_points['PreLogitsFlatten'] saver = tf.train.Saver() saver.restore(self.sess, checkpoint_path)
def run(): # Create log_dir for evaluation information if not os.path.exists(log_eval): os.mkdir(log_eval) # Just construct the graph from scratch again with tf.Graph().as_default() as graph: tf.logging.set_verbosity(tf.logging.INFO) # Get the dataset first and load one batch of validation images and labels tensors. Set is_training as False so as to use the evaluation preprocessing dataset = get_split('validation', dataset_dir) images, raw_images, labels = load_batch(dataset, batch_size=batch_size, is_training=True) # Create some information about the training steps num_batches_per_epoch = dataset.num_samples // batch_size num_steps_per_epoch = num_batches_per_epoch # Now create the inference model but set is_training=False with slim.arg_scope(inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2( images, num_classes=dataset.num_classes, is_training=True) # #get all the variables to restore from the checkpoint file and create the saver function to restore variables_to_restore = slim.get_variables_to_restore() saver = tf.train.Saver(variables_to_restore) def restore_fn(sess): return saver.restore(sess, checkpoint_file) # Just define the metrics to track without the loss or whatsoever predictions = tf.argmax(end_points['Predictions'], 1) sv = tf.train.Supervisor(logdir=log_eval, summary_op=None, saver=None, init_fn=restore_fn) with sv.managed_session() as sess: # sess.run(tf.global_variables_initializer()) # for i in range(424): #predict_class = sess.run(predictions) # print(predict_class) for i in range(53): predict_class = sess.run(predictions) for i in range(batch_size): predicPer = predict_class[i] prediction_name = dataset.labels_to_name[predicPer] print(prediction_name)
def load_cnn(self): print("Loading pre-trained Inception ResNet V2...") # function loads the pretrained inception resnet v2 model X = tf.placeholder( tf.float32, shape=[None, self.height, self.width, self.channels]) with slim.arg_scope(inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2(X, num_classes=1001, is_training=False) # initialize network with checkpoint file self.saver = tf.train.Saver() self.sess = tf.Session() self.saver.restore(self.sess, '/ckpt/inception_resnet_v2_2016_08_30.ckpt') print("Pre-trained Inception ResNet V2 is succesfully loaded!") return sess
def main(_): batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3] num_classes = 1001 itr = 30 tf.logging.set_verbosity(tf.logging.INFO) print("input dir = {0}".format(FLAGS.input_dir)) with tf.Graph().as_default(): # Prepare graph x_input = tf.placeholder(tf.float32, shape=batch_shape) #finished input with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): _, end_points = inception_resnet_v2.inception_resnet_v2( x_input, num_classes=num_classes, is_training=False, create_aux_logits=True) # 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): print("filename = {0}".format(filenames)) final_preds = np.zeros( [FLAGS.batch_size, num_classes, itr]) images = defend_reduce(images) # bit depth reduction pred, aux_pred = sess.run([ end_points['Predictions'], end_points['AuxPredictions'] ], feed_dict={x_input: images}) final_probs = pred + 0.4 * aux_pred labels = np.argmax(final_probs, 1) #labels = randomization(itr, images, sess, end_points, x_input, img_resize_tensor, shape_tensor, final_preds) for filename, label in zip(filenames, labels): out_file.write('{0},{1}\n'.format(filename, label))
def run(): # Create log_dir for evaluation information if not os.path.exists(log_eval): os.mkdir(log_eval) # Just construct the graph from scratch again with tf.Graph().as_default() as graph: tf.logging.set_verbosity(tf.logging.INFO) # Get the dataset first and load one batch of validation images and labels tensors. Set is_training as False so as to use the evaluation preprocessing images = read_test_and_decode( "C:/Users/Fa/Desktop/Test3/test_flower.tfrecord", False, 299) images = tf.train.batch([images], batch_size=batch_size, capacity=424) # images, raw_images = load_batch_2(dataset, batch_size=batch_size, is_training=True) # Create some information about the training steps num_steps_per_epoch = 8 # Now create the inference model but set is_training=False with slim.arg_scope(inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2(images, num_classes=5, is_training=True) variables_to_restore = slim.get_variables_to_restore() saver = tf.train.Saver(variables_to_restore) def restore_fn(sess): return saver.restore(sess, checkpoint_file) # Just define the metrics to track without the loss or whatsoever predictions = tf.argmax(end_points['Predictions'], 1) sv = tf.train.Supervisor(logdir=log_eval, summary_op=None, saver=None, init_fn=restore_fn) with sv.managed_session() as sess: # sess.run(tf.global_variables_initializer()) for i in range(53): predict_class = sess.run(predictions) for i in range(batch_size): predicPer = predict_class[i] prediction_name = dataset.labels_to_name[predicPer] print(prediction_name)
def forward(self, x_input): x_input = (x_input * 2. / 255) - 1 if self.method == 'nat': with slim.arg_scope(inception.inception_v3_arg_scope()): _, end_points = inception.inception_v3(x_input, num_classes=1001, is_training=False, reuse=tf.AUTO_REUSE) else: with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): _, end_points = inception_resnet_v2.inception_resnet_v2( x_input, num_classes=1001, is_training=False, reuse=tf.AUTO_REUSE) return end_points['Logits']
def inception_resnet_model(input_shape, label_shape): ''' Creates Inception ResNet model and return all placeholder and final logit layer ''' inputs = tf.placeholder(shape=input_shape, dtype=tf.float32) labels = tf.placeholder(shape=label_shape, dtype=tf.float32) is_train = tf.placeholder(tf.bool) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): net, _ = inception_resnet_v2.inception_resnet_v2(inputs, num_classes=None, is_training=False) print(net) out_squeeze = tf.reshape(tf.squeeze(net), [-1, 1536]) outputs = tf.layers.dense(out_squeeze, label_shape[1]) return outputs, inputs, labels, is_train, out_squeeze
def main(_): # eps = FLAGS.max_epsilon / 255.0 eps = 2.0 / 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) x_max = tf.clip_by_value(x_input + eps, -1.0, 1.0) x_min = tf.clip_by_value(x_input - eps, -1.0, 1.0) # 2-step FGSM attack: noisy_images = x_input + eps * tf.sign(tf.random_normal(batch_shape)) x_noisy = tf.clip_by_value(noisy_images, x_min, x_max) with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): _, end_points = inception_resnet_v2.inception_resnet_v2( x_noisy, num_classes=num_classes, is_training=False) predictions = end_points['Predictions'] # 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): pred = sess.run(predictions, feed_dict={x_input: images}) for filename, p in zip(filenames, list(pred)): p_str = " ".join([str(i) for i in list(p)]) out_file.write('{0},{1}\n'.format(filename, p_str))
def __load_inception_network(self, processed_images, num_classes): with slim.arg_scope(inception_resnet_v2_arg_scope()): #logits, _ = inception_resnet_v2(processed_images, num_classes=num_classes, # is_training=False) prelogits, end_points = inception_resnet_v2( processed_images, num_classes=num_classes, is_training=False) embeddings = tf.nn.l2_normalize(prelogits, dim=1, epsilon=1e-10, name='embeddings') logits = slim.fully_connected( prelogits, num_classes, activation_fn=None, weights_initializer=tf.truncated_normal_initializer(stddev=0.1), weights_regularizer=slim.l2_regularizer(0.001), scope='Logits', reuse=False) return logits
def create_model(x, reuse=None): """Create model graph. Args: x: input images reuse: reuse parameter which will be passed to underlying variable scopes. Should be None first call and True every subsequent call. Returns: (logits, end_points) - tuple of model logits and enpoints Raises: ValueError: if model type specified by --model_name flag is invalid. """ if FLAGS.model_name == 'inception_v3': with slim.arg_scope(inception.inception_v3_arg_scope()): return inception.inception_v3( x, num_classes=NUM_CLASSES, is_training=False, reuse=reuse) elif FLAGS.model_name == 'inception_resnet_v2': with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): return inception_resnet_v2.inception_resnet_v2( x, num_classes=NUM_CLASSES, is_training=False, reuse=reuse) else: raise ValueError('Invalid model name: %s' % (FLAGS.model_name))
label, image = getImage(train_files, True) vlabel, vimage = getImage(validation_files, False) imageBatch, labelBatch = tf.train.shuffle_batch( [image, label], batch_size=batch_size, capacity=1000, min_after_dequeue=100) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.InteractiveSession(config=config) num_samples = 2700*4 #TODO with slim.arg_scope(inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2(imageBatch, num_classes = nLabel, is_training = True) exclude = ['InceptionResnetV2/Logits', 'InceptionResnetV2/AuxLogits'] variables_to_restore = slim.get_variables_to_restore(exclude = exclude) one_hot_labels = slim.one_hot_encoding(labelBatch, nLabel) #loss = tf.losses.softmax_cross_entropy(onehot_labels = one_hot_labels, logits = logits) #total_loss = tf.losses.get_total_loss() loss = slim.losses.softmax_cross_entropy(logits, one_hot_labels) # TODO: zamienilem dwie linijki wyzej na to total_loss = slim.losses.get_total_loss() num_batches_per_epoch = int(num_samples / batch_size) num_steps_per_epoch = num_batches_per_epoch #Because one step is one batch processed decay_steps = int(num_epochs_before_decay * num_steps_per_epoch)