def initialize(self, config, num_classes): ''' Initialize the graph from scratch according config. ''' with self.graph.as_default(): with self.sess.as_default(): # Set up placeholders w, h = config.image_size channels = config.channels image_batch_placeholder = tf.placeholder( tf.float32, shape=[None, h, w, channels], name='image_batch') label_batch_placeholder = tf.placeholder(tf.int32, shape=[None], name='label_batch') learning_rate_placeholder = tf.placeholder( tf.float32, name='learning_rate') keep_prob_placeholder = tf.placeholder(tf.float32, name='keep_prob') phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') global_step = tf.Variable(0, trainable=False, dtype=tf.int32, name='global_step') image_splits = tf.split(image_batch_placeholder, config.num_gpus) label_splits = tf.split(label_batch_placeholder, config.num_gpus) grads_splits = [] split_dict = {} def insert_dict(k, v): if k in split_dict: split_dict[k].append(v) else: split_dict[k] = [v] for i in range(config.num_gpus): scope_name = '' if i == 0 else 'gpu_%d' % i with tf.name_scope(scope_name): with tf.variable_scope('', reuse=i > 0): with tf.device('/gpu:%d' % i): images = tf.identity(image_splits[i], name='inputs') labels = tf.identity(label_splits[i], name='labels') # Save the first channel for testing if i == 0: self.inputs = images # Build networks network = imp.load_source( 'network', config.network) prelogits = network.inference( images, keep_prob_placeholder, phase_train_placeholder, bottleneck_layer_size=config. embedding_size, weight_decay=config.weight_decay, model_version=config.model_version) prelogits = tf.identity(prelogits, name='prelogits') embeddings = tf.nn.l2_normalize( prelogits, dim=1, name='embeddings') if i == 0: self.outputs = tf.identity(embeddings, name='outputs') # Build all losses losses = [] # Orignal Softmax if 'softmax' in config.losses.keys(): logits = slim.fully_connected( prelogits, num_classes, weights_regularizer=slim. l2_regularizer(config.weight_decay), weights_initializer=slim. xavier_initializer(), biases_initializer=tf. constant_initializer(0.0), activation_fn=None, scope='Logits') cross_entropy = tf.reduce_mean( tf.nn. sparse_softmax_cross_entropy_with_logits( labels=labels, logits=logits), name='cross_entropy') losses.append(cross_entropy) insert_dict('sloss', cross_entropy) # L2-Softmax if 'cosine' in config.losses.keys(): logits, cosine_loss = tflib.cosine_softmax( prelogits, labels, num_classes, weight_decay=config.weight_decay, **config.losses['cosine']) losses.append(cosine_loss) insert_dict('closs', cosine_loss) # A-Softmax if 'angular' in config.losses.keys(): angular_loss = tflib.angular_softmax( prelogits, labels, num_classes, global_step, weight_decay=config.weight_decay, **config.losses['angular']) losses.append(angular_loss) insert_dict('aloss', angular_loss) # AM-Softmax if 'am' in config.losses.keys(): am_loss = tflib.am_softmax( prelogits, labels, num_classes, weight_decay=config.weight_decay, **config.losses['am']) losses.append(am_loss) insert_dict('loss', am_loss) # Max-margin Pairwise Score (MPS) if 'pair' in config.losses.keys(): pair_loss = tflib.pair_loss( prelogits, labels, num_classes, **config.losses['pair']) losses.append(pair_loss) insert_dict('loss', pair_loss) # DIAM-Softmax if 'diam' in config.losses.keys(): diam_loss = tflib.diam_softmax( prelogits, labels, num_classes, **config.losses['diam']) diam_loss = tf.identity(diam_loss, name='diam_loss') losses.append(diam_loss) insert_dict('amloss', diam_loss) # Collect all losses reg_loss = tf.reduce_sum(tf.get_collection( tf.GraphKeys.REGULARIZATION_LOSSES), name='reg_loss') losses.append(reg_loss) insert_dict('reg_loss', reg_loss) total_loss = tf.add_n(losses, name='total_loss') grads_split = tf.gradients( total_loss, tf.trainable_variables()) grads_splits.append(grads_split) # Merge the splits self.watchlist = {} grads = tflib.average_grads(grads_splits) for k, v in split_dict.items(): v = tflib.average_tensors(v) self.watchlist[k] = v if 'loss' in k: tf.summary.scalar('losses/' + k, v) else: tf.summary.scalar(k, v) # Training Operaters apply_gradient_op = tflib.apply_gradient( tf.trainable_variables(), grads, config.optimizer, learning_rate_placeholder, config.learning_rate_multipliers) update_global_step_op = tf.assign_add(global_step, 1) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) train_ops = [apply_gradient_op, update_global_step_op ] + update_ops train_op = tf.group(*train_ops) tf.summary.scalar('learning_rate', learning_rate_placeholder) summary_op = tf.summary.merge_all() # Initialize variables self.sess.run(tf.local_variables_initializer()) self.sess.run(tf.global_variables_initializer()) self.saver = tf.train.Saver(tf.trainable_variables()) # Keep useful tensors self.image_batch_placeholder = image_batch_placeholder self.label_batch_placeholder = label_batch_placeholder self.learning_rate_placeholder = learning_rate_placeholder self.keep_prob_placeholder = keep_prob_placeholder self.phase_train_placeholder = phase_train_placeholder self.global_step = global_step self.train_op = train_op self.summary_op = summary_op
def initialize(self, config, num_classes): ''' Initialize the graph from scratch according config. ''' with self.graph.as_default(): with self.sess.as_default(): # Set up placeholders w, h = config.image_size channels = config.channels image_batch_placeholder = tf.placeholder(tf.float32, shape=[None, h, w, channels], name='image_batch') label_batch_placeholder = tf.placeholder(tf.int32, shape=[None], name='label_batch') learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate') keep_prob_placeholder = tf.placeholder(tf.float32, name='keep_prob') phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') switch_place_holder = tf.placeholder(tf.bool, shape=[None], name="switch_all") global_step = tf.Variable(0, trainable=False, dtype=tf.int32, name='global_step') image_splits = tf.split(image_batch_placeholder, config.num_gpus) switch_splits = tf.split(switch_place_holder, config.num_gpus) label_splits = tf.split(label_batch_placeholder, config.num_gpus) grads_splits = [] split_dict = {} def insert_dict(k,v): if k in split_dict: split_dict[k].append(v) else: split_dict[k] = [v] for i in range(config.num_gpus): scope_name = '' if i==0 else 'gpu_%d' % i with tf.name_scope(scope_name): with tf.variable_scope('', reuse=i>0): with tf.device('/gpu:%d' % i): images = tf.identity(image_splits[i], name='inputs') switch = tf.identity(switch_splits[i], name='switch') labels = tf.identity(label_splits[i], name='labels') # Save the first channel for testing if i == 0: self.inputs = images self.switch = switch images_tmp = tf.boolean_mask(images, tf.logical_not(switch), name="images_template") images_pro = tf.boolean_mask(images, switch,name="images_probe") labels_tmp = tf.boolean_mask(labels, tf.logical_not(switch), name="labels_template") labels_pro = tf.boolean_mask(labels, switch,name="labels_probe") network = imp.load_source('network', config.network) with tf.variable_scope('TemplateNet'): prelogits_tmp = network.inference(images_tmp, keep_prob_placeholder, phase_train_placeholder, bottleneck_layer_size = config.embedding_size, weight_decay = config.weight_decay, model_version = config.model_version) prelogits_tmp = tf.identity(prelogits_tmp, name='prelogits_tmp') embeddings_tmp = tf.nn.l2_normalize(prelogits_tmp, dim=1, name='embeddings_tmp') with tf.variable_scope('ProbeNet'): prelogits_pro = network.inference(images_pro, keep_prob_placeholder, phase_train_placeholder, bottleneck_layer_size = config.embedding_size, weight_decay = config.weight_decay, model_version = config.model_version) prelogits_pro = tf.identity(prelogits_pro, name='prelogits_pro') embeddings_pro = tf.nn.l2_normalize(prelogits_pro, dim=1, name='embeddings_pro') if i == 0: self.outputs_tmp = tf.identity(embeddings_tmp, name='outputs_tmp') self.outputs_pro = tf.identity(embeddings_pro, name='outputs_pro') # Build all losses losses = [] # L2-Softmax if 'cosine' in config.losses.keys(): logits, cosine_loss_tmp = tflib.cosine_softmax(prelogits_tmp, labels_tmp, num_classes, weight_decay=config.weight_decay, **config.losses['cosine']) logits, cosine_loss_pro = tflib.cosine_softmax(prelogits_pro, labels_pro, num_classes, weight_decay=config.weight_decay, reuse=True, **config.losses['cosine']) cosine_loss = tf.identity(cosine_loss_tmp + cosine_loss_pro, name='cosine_loss') losses.append(cosine_loss) insert_dict('closs', cosine_loss) # AM-Softmax if 'am' in config.losses.keys(): split_loss_tmp = tflib.am_softmax(prelogits_tmp, labels_tmp, num_classes, global_step, weight_decay=config.weight_decay, **config.losses['am']) split_loss_pro = tflib.am_softmax(prelogits_pro, labels_pro, num_classes, global_step, weight_decay=config.weight_decay, reuse=True, **config.losses['am']) split_loss = tf.identity(split_loss_tmp + split_loss_pro, name='split_loss') losses.append(split_loss) insert_dict('amloss', split_loss) # Max-margin Pairwise Score (MPS) if 'pair' in config.losses.keys(): pair_loss = tflib.pair_loss_sibling(prelogits_tmp, prelogits_pro, labels_tmp, labels_pro, num_classes, global_step, weight_decay=config.weight_decay, **config.losses['pair']) losses.append(pair_loss) insert_dict('loss', pair_loss) # Collect all losses reg_loss = tf.reduce_sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES), name='reg_loss') losses.append(reg_loss) insert_dict('reg_loss', reg_loss) total_loss = tf.add_n(losses, name='total_loss') grads_split = tf.gradients(total_loss, tf.trainable_variables()) grads_splits.append(grads_split) # Merge the splits self.watchlist = {} grads = tflib.average_grads(grads_splits) for k,v in split_dict.items(): v = tflib.average_tensors(v) self.watchlist[k] = v if 'loss' in k: tf.summary.scalar('losses/' + k, v) else: tf.summary.scalar(k, v) # Training Operaters apply_gradient_op = tflib.apply_gradient(tf.trainable_variables(), grads, config.optimizer, learning_rate_placeholder, config.learning_rate_multipliers) update_global_step_op = tf.assign_add(global_step, 1) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) train_ops = [apply_gradient_op, update_global_step_op] + update_ops train_op = tf.group(*train_ops) tf.summary.scalar('learning_rate', learning_rate_placeholder) summary_op = tf.summary.merge_all() # Initialize variables self.sess.run(tf.local_variables_initializer()) self.sess.run(tf.global_variables_initializer()) self.saver = tf.train.Saver(tf.trainable_variables()) # Keep useful tensors self.image_batch_placeholder = image_batch_placeholder self.label_batch_placeholder = label_batch_placeholder self.switch_place_holder = switch_place_holder self.learning_rate_placeholder = learning_rate_placeholder self.keep_prob_placeholder = keep_prob_placeholder self.phase_train_placeholder = phase_train_placeholder self.global_step = global_step self.train_op = train_op self.summary_op = summary_op
def initialize(self, config, num_classes): ''' Initialize the graph from scratch according config. ''' #A default graph is registered, operations will be added to the graph with self.graph.as_default(): #A default session is created, operations will be added to the session with self.sess.as_default(): # Set up placeholders #width and height from image size, [112,112] w, h = config.image_size #channels = 3 (RGB) channels = config.channels #A placeholder is a variable that we will assign data to at a later date #It allows us to create our operations and build our computation graph without needing the data. #In TensorFlowterminology, we then feed data into the graph through these placeholders. image_batch_placeholder = tf.placeholder(tf.float32, shape=[None, h, w, channels], name='image_batch') label_batch_placeholder = tf.placeholder(tf.int32, shape=[None], name='label_batch') learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate') keep_prob_placeholder = tf.placeholder(tf.float32, name='keep_prob') phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') global_step = tf.Variable(0, trainable=False, dtype=tf.int32, name='global_step') #splits a tensor into sub tensors image_splits = tf.split(image_batch_placeholder, config.num_gpus) label_splits = tf.split(label_batch_placeholder, config.num_gpus) grads_splits = [] split_dict = {} #function for insering values into a dicitonary based on a key def insert_dict(k,v): if k in split_dict: split_dict[k].append(v) else: split_dict[k] = [v] #numgpus = 1 for i in range(config.num_gpus): scope_name = '' if i==0 else 'gpu_%d' % i # A context manager for use when defining a Python op # context manager pushes a name scope, which will make the name of all operations added within it have a prefix. with tf.name_scope(scope_name): with tf.variable_scope('', reuse=i>0): #Specifies the device for ops created/executed in this context with tf.device('/gpu:%d' % i): #identity returns a tensor with same shape and contents as input images = tf.identity(image_splits[i], name='inputs') labels = tf.identity(label_splits[i], name='labels') # Save the first channel for testing if i == 0: self.inputs = images # Build networks if config.localization_net is not None: localization_net = utils.import_file(config.localization_net, 'network') imsize = (112, 112) images, theta = localization_net.inference(images, imsize, phase_train_placeholder, weight_decay = 0.0) images = tf.identity(images, name='transformed_image') if i == 0: tf.summary.image('transformed_image', images) else: images = images #calls import_file, passes sealnet as network network = utils.import_file(config.network, 'network') #calls inference function in sealnet file prelogits = network.inference(images, keep_prob_placeholder, phase_train_placeholder, bottleneck_layer_size = config.embedding_size, weight_decay = config.weight_decay, model_version = config.model_version) prelogits = tf.identity(prelogits, name='prelogits') #Normalizes along dimension axis using an L2 norm embeddings = tf.nn.l2_normalize(prelogits, dim=1, name='embeddings') if i == 0: self.outputs = tf.identity(embeddings, name='outputs') # Build all loss functions losses = [] # Orignal Softmax if 'softmax' in config.losses.keys(): logits = slim.fully_connected(prelogits, num_classes, weights_regularizer=slim.l2_regularizer(config.weight_decay), # weights_initializer=tf.truncated_normal_initializer(stddev=0.1), weights_initializer=slim.xavier_initializer(), biases_initializer=tf.constant_initializer(0.0), activation_fn=None, scope='Logits') cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( labels=labels, logits=logits), name='cross_entropy') losses.append(cross_entropy) insert_dict('sloss', cross_entropy) # L2-Softmax if 'cosine' in config.losses.keys(): logits, cosine_loss = tflib.cosine_softmax(prelogits, labels, num_classes, gamma=config.losses['cosine']['gamma'], weight_decay=config.weight_decay) losses.append(cosine_loss) insert_dict('closs', cosine_loss) # A-Softmax if 'angular' in config.losses.keys(): a_cfg = config.losses['angular'] angular_loss = tflib.angular_softmax(prelogits, labels, num_classes, global_step, a_cfg['m'], a_cfg['lamb_min'], a_cfg['lamb_max'], weight_decay=config.weight_decay) losses.append(angular_loss) insert_dict('aloss', angular_loss) # Split Loss if 'split' in config.losses.keys(): split_losses = tflib.split_softmax(prelogits, labels, num_classes, global_step, gamma=config.losses['split']['gamma'], weight_decay=config.weight_decay) losses.extend(split_losses) insert_dict('loss', split_losses[0]) # Collect all losses reg_loss = tf.reduce_sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES), name='reg_loss') losses.append(reg_loss) insert_dict('reg_loss', reg_loss) total_loss = tf.add_n(losses, name='total_loss') grads_split = tf.gradients(total_loss, tf.trainable_variables()) grads_splits.append(grads_split) # Merge the splits grads = tflib.average_grads(grads_splits) for k,v in split_dict.items(): v = tflib.average_tensors(v) split_dict[k] = v if 'loss' in k: tf.summary.scalar('losses/' + k, v) else: tf.summary.scalar(k, v) # Training Operaters apply_gradient_op = tflib.apply_gradient(tf.trainable_variables(), grads, config.optimizer, learning_rate_placeholder, config.learning_rate_multipliers) update_global_step_op = tf.assign_add(global_step, 1) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) train_ops = [apply_gradient_op, update_global_step_op] + update_ops train_op = tf.group(*train_ops) tf.summary.scalar('learning_rate', learning_rate_placeholder) summary_op = tf.summary.merge_all() # Initialize variables self.sess.run(tf.local_variables_initializer()) self.sess.run(tf.global_variables_initializer()) self.saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=None) # Keep useful tensors self.image_batch_placeholder = image_batch_placeholder self.label_batch_placeholder = label_batch_placeholder self.learning_rate_placeholder = learning_rate_placeholder self.keep_prob_placeholder = keep_prob_placeholder self.phase_train_placeholder = phase_train_placeholder self.global_step = global_step self.watch_list = split_dict self.train_op = train_op self.summary_op = summary_op