def sos(labels_oh, reuse=None): if reuse: sos_emb = slim.get_variables_by_name('dec/sos')[0] else: sos_emb = tf.get_variable('sos', shape=[1, FLAGS.embedding_size]) sos_emb = tf.tile(sos_emb, [FLAGS.batch_size, 1]) return sos_emb
def map_var(name_list): # {pretrain文件中的名字 :自己模型中的tensor} var_list = {} for name in name_list: new_name = restore_name(name) var_list[name] = slim.get_variables_by_name(new_name)[0] return var_list
def testTrainAllVarsHasLowerLossThanTrainSubsetOfVars(self): logdir1 = os.path.join(self.get_temp_dir(), 'tmp_logs3/') if tf.gfile.Exists(logdir1): # For running on jenkins. tf.gfile.DeleteRecursively(logdir1) # First, train only the weights of the model. g = tf.Graph() with g.as_default(): tf.set_random_seed(0) total_loss = self.ModelLoss() optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0) weights = slim.get_variables_by_name('weights') train_op = slim.learning.create_train_op( total_loss, optimizer, variables_to_train=weights) loss = slim.learning.train(train_op, logdir1, number_of_steps=200) self.assertGreater(loss, .015) self.assertLess(loss, .05) # Next, train the biases of the model. g = tf.Graph() with g.as_default(): tf.set_random_seed(1) total_loss = self.ModelLoss() optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0) biases = slim.get_variables_by_name('biases') train_op = slim.learning.create_train_op(total_loss, optimizer, variables_to_train=biases) loss = slim.learning.train(train_op, logdir1, number_of_steps=300) self.assertGreater(loss, .015) self.assertLess(loss, .05) # Finally, train both weights and bias to get lower loss. g = tf.Graph() with g.as_default(): tf.set_random_seed(2) total_loss = self.ModelLoss() optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0) train_op = slim.learning.create_train_op(total_loss, optimizer) loss = slim.learning.train(train_op, logdir1, number_of_steps=400) self.assertLess(loss, .015)
def testTrainAllVarsHasLowerLossThanTrainSubsetOfVars(self): logdir1 = os.path.join(self.get_temp_dir(), "tmp_logs3/") if tf.gfile.Exists(logdir1): # For running on jenkins. tf.gfile.DeleteRecursively(logdir1) # First, train only the weights of the model. g = tf.Graph() with g.as_default(): tf.set_random_seed(0) total_loss = self.ModelLoss() optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0) weights = slim.get_variables_by_name("weights") train_op = slim.learning.create_train_op(total_loss, optimizer, variables_to_train=weights) loss = slim.learning.train(train_op, logdir1, number_of_steps=200) self.assertGreater(loss, 0.015) self.assertLess(loss, 0.05) # Next, train the biases of the model. g = tf.Graph() with g.as_default(): tf.set_random_seed(1) total_loss = self.ModelLoss() optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0) biases = slim.get_variables_by_name("biases") train_op = slim.learning.create_train_op(total_loss, optimizer, variables_to_train=biases) loss = slim.learning.train(train_op, logdir1, number_of_steps=300) self.assertGreater(loss, 0.015) self.assertLess(loss, 0.05) # Finally, train both weights and bias to get lower loss. g = tf.Graph() with g.as_default(): tf.set_random_seed(2) total_loss = self.ModelLoss() optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0) train_op = slim.learning.create_train_op(total_loss, optimizer) loss = slim.learning.train(train_op, logdir1, number_of_steps=400) self.assertLess(loss, 0.015)
def make_summaries(model): for layer in ['conv1', 'conv2', 'fc1', 'fc2']: for var_type in ['weights', 'biases']: with tf.name_scope(layer), tf.name_scope(var_type): var = '/'.join([layer, var_type]) variable_summaries(slim.get_variables_by_name(var)[0]) tf.summary.histogram('keep_prob', model.keep_prob) tf.summary.histogram('predictions', model.logits) tf.summary.scalar('loss', model.loss) tf.summary.scalar('accuracy', model.accuracy) merged_summaries = tf.summary.merge_all() return merged_summaries
def decode_beam(cnt, labels_oh, reuse=False, batch_size=None, decode_num_steps=None): if not batch_size: batch_size = FLAGS.batch_size if not decode_num_steps: decode_num_steps = FLAGS.decode_max_steps initial_state = cnt end_token = 0 beam_width = 10 word_vectors = slim.get_variables_by_name('rnn/word_embedding')[0] decoder_cell = tf.contrib.rnn.GRUCell(num_units=FLAGS.dec_lstm_size, reuse=reuse) sos_emb = sos(labels_oh, reuse=reuse) sos_emb = tf.tile(tf.expand_dims(sos_emb, 1), (1, beam_width, 1)) with tf.variable_scope('classifier', reuse=reuse): projection_layer = layers_core.Dense(FLAGS.vocab_size, name="classifier") initial_state = tf.contrib.seq2seq.tile_batch(initial_state, multiplier=beam_width) # Define a beam-search decoder with tf.variable_scope('rnn', reuse=reuse): decoder = BeamSearchDecoder(cell=decoder_cell, embedding=word_vectors, start_tokens=sos_emb, end_token=end_token, initial_state=initial_state, beam_width=beam_width, output_layer=projection_layer, length_penalty_weight=0.0) outputs, final_context_state, _ = tf.contrib.seq2seq.dynamic_decode( decoder, maximum_iterations=FLAGS.decode_max_steps, swap_memory=True) #scope=decoder_scope) predictions = outputs.predicted_ids[:, :, 0] return predictions, None
def embedding_tower(hidden_layer, embedding_sizes, reuse=False): """ Creates the embedding tower on top of a feature extractor. Args: hidden_layer: Last hidden layer of the feature extractor. embedding_sizes: array indicating the sizes of our embedding, e.g. [96, 160, 256] reuse: tensorflow reuse flag for the variable scope. Returns: A tuple consisting of the embedding and end_points. """ end_points = {} final_layers = [] with tf.variable_scope(EMBEDDING_SCOPE_NAME, reuse=reuse) as scope: hidden_layer = slim.flatten(hidden_layer) for idx, embedding_size in enumerate(embedding_sizes): scope_name = 'embedding/fc_{}'.format(idx) embedding = slim.fully_connected(hidden_layer, embedding_size, activation_fn=None, scope=scope_name) regul_out = slim.fully_connected(tf.stop_gradient(hidden_layer), embedding_size, scope=scope_name, reuse=True, activation_fn=None, biases_initializer=None) end_points['{}/embedding/fc_{}'.format(EMBEDDING_SCOPE_NAME, idx)] = embedding end_points['{}/embedding/fc_{}_regularizer'.format( EMBEDDING_SCOPE_NAME, idx)] = regul_out final_layers.append(embedding) embedding = tf.concat(final_layers, axis=1) end_points['{}/embedding'.format(EMBEDDING_SCOPE_NAME)] = embedding weight_variables = slim.get_variables_by_name('weights', scope=scope) for w in weight_variables: tf.add_to_collection('weights', w) return embedding, end_points
def netG_Unet_decoder_gamma_32(feature, output_channel, reuse=False): with tf.variable_scope('generator', reuse=reuse): kernel_size = [3, 3] filter_num = 32 with tf.variable_scope('decoding') as vs: if reuse: vs.reuse_variables() with slim.arg_scope([slim.conv2d_transpose], activation_fn=tf.nn.elu, normalizer_fn=inst_norm, padding='SAME', weights_initializer=tf.truncated_normal_initializer(stddev=0.02)): # 先将vector组织为6*6*320的tensor#slim.batch_norm fc1 = slim.fully_connected(feature, 4 * 4 * filter_num * 8, activation_fn=None, weights_initializer=tf.truncated_normal_initializer(stddev=0.02), scope='fc1') # reshape the vector[n,6,6,320] inputs_img = tf.reshape(fc1, [-1, 4, 4, filter_num * 8]) # Unet改变 fc1_en = slim.get_variables_by_name('fc1', 'encoding')[0] tf.concat([fc1_en, inputs_img], axis=3, name='defc1') # print 'inputs_img',inputs_img.shape # 4 net = slim.conv2d(inputs_img, filter_num * 8, kernel_size, scope='deconv01') net = slim.conv2d(net, filter_num * 6, kernel_size, scope='deconv02') # 8 net = slim.conv2d_transpose(net, filter_num * 3, stride=2, kernel_size=kernel_size, scope='deconv2') net = slim.conv2d_transpose(net, filter_num * 4, kernel_size, scope='deconv3') # 16 net = slim.conv2d_transpose(net, filter_num * 2, stride=2, kernel_size=kernel_size, scope='deconv5') net = slim.conv2d_transpose(net, filter_num * 2, kernel_size, scope='deconv6') # 32 net = slim.conv2d_transpose(net, filter_num * 2, stride=2, kernel_size=kernel_size, scope='deconv8') net = slim.conv2d_transpose(net, filter_num, kernel_size, scope='deconv9') # 为什么放到外面就好了呢? net = slim.conv2d_transpose(net, output_channel, kernel_size, activation_fn=tf.nn.tanh, normalizer_fn=None, scope='deconv13', weights_initializer=tf.contrib.layers.xavier_initializer()) output = net return output
def main(args): if not os.path.isdir(args.train_dir): os.makedirs(args.train_dir) if not os.path.isdir(args.log_dir): os.makedirs(args.log_dir) np.random.seed(seed=random_seed) random.seed(random_seed) train_set = facenet.get_dataset(args.data_dir) num_classes = len(train_set) cur_time = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S') print('LFW directory: %s' % args.lfw_dir) pairs = lfw.read_pairs(os.path.expanduser(lfw_pairs)) lfw_paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, 'jpg') with tf.Graph().as_default(): tf.set_random_seed(random_seed) global_step = tf.Variable(0, trainable=False) image_list, label_list = facenet.get_image_paths_and_labels(train_set) labels = ops.convert_to_tensor(label_list, dtype=tf.int32) range_size = array_ops.shape(labels)[0] index_queue = tf.train.range_input_producer(range_size, num_epochs=None, shuffle=True, seed=None, capacity=32) index_dequeue_op = index_queue.dequeue_many( args.batch_size * args.epoch_size, 'index_dequeue') learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate') batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size') phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') image_paths_placeholder = tf.placeholder(tf.string, shape=(None, 1), name='image_paths') labels_placeholder = tf.placeholder(tf.int64, shape=(None, 1), name='labels') input_queue = data_flow_ops.FIFOQueue(capacity=5822700, dtypes=[tf.string, tf.int64], shapes=[(1, ), (1, )], shared_name=None, name=None) enqueue_op = input_queue.enqueue_many( [image_paths_placeholder, labels_placeholder], name='enqueue_op') num_preprocess_threads = 4 images_and_labels = [] for _ in range(num_preprocess_threads): filenames, label = input_queue.dequeue() images = [] for filename in tf.unstack(filenames): file_contents = tf.read_file(filename) image = tf.cast( tf.image.decode_image(file_contents, channels=3), tf.float32) image = tf.image.random_flip_left_right(image) image.set_shape((image_size[0], image_size[1], 3)) images.append(tf.subtract(image, 127.5) * 0.0078125) images_and_labels.append([images, label]) image_batch, label_batch = tf.train.batch_join( images_and_labels, batch_size=batch_size_placeholder, shapes=[(image_size[0], image_size[1], 3), ()], enqueue_many=True, capacity=4 * num_preprocess_threads * args.batch_size, allow_smaller_final_batch=True) image_batch = tf.identity(image_batch, 'input') label_batch = tf.identity(label_batch, 'label_batch') print('Total number of classes: %d' % num_classes) print('Total number of examples: %d' % len(image_list)) print('Building training graph') # Build the inference graph prelogits, _ = network.inference(image_batch, 0.8, phase_train=phase_train_placeholder, bottleneck_layer_size=embedding_size, weight_decay=args.weight_decay) embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings') # embeddings = tf.identity(prelogits, 'embeddings') logits = logits_compute(embeddings, label_batch, embedding_size, num_classes, args.batch_size) learning_rate = tf.train.exponential_decay(learning_rate_placeholder, global_step, args.epoch_size, 1, staircase=True) tf.summary.scalar('learning_rate', learning_rate) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=label_batch, logits=logits, name='cross_entropy_per_example') cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') for weights in slim.get_variables_by_name('kernel'): kernel_regularization = tf.contrib.layers.l2_regularizer( args.weight_decay)(weights) print(weights) tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, kernel_regularization) regularization_losses = tf.get_collection( tf.GraphKeys.REGULARIZATION_LOSSES) if args.weight_decay == 0: total_loss = tf.add_n([cross_entropy_mean], name='total_loss') else: total_loss = tf.add_n([cross_entropy_mean] + regularization_losses, name='total_loss') tf.add_to_collection('losses', total_loss) loader = tf.train.Saver(tf.trainable_variables(), max_to_keep=1) saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=1) # train_op = facenet.train(total_loss, global_step, args.optimizer, # learning_rate, args.moving_average_decay, tf.trainable_variables(), args.log_histograms) # train_op = tf.train.AdamOptimizer(learning_rate).minimize(total_loss, global_step=global_step, # var_list=tf.trainable_variables()) train_op = tf.train.MomentumOptimizer( learning_rate, momentum=0.9).minimize(total_loss, global_step=global_step, var_list=tf.trainable_variables()) summary_op = tf.summary.merge_all() sess = tf.Session(config=tf.ConfigProto(log_device_placement=False)) sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) summary_writer = tf.summary.FileWriter(args.log_dir, sess.graph) coord = tf.train.Coordinator() tf.train.start_queue_runners(coord=coord, sess=sess) with sess.as_default(): if args.pretrained: print('Restoring model: %s' % args.pretrained) loader.restore(sess, args.pretrained) print('Running training') epoch = 0 best_accuracy = 0.0 # step = sess.run(global_step, feed_dict=None) # best_accuracy = evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, # phase_train_placeholder, batch_size_placeholder, embeddings, label_batch, # lfw_paths, actual_issame, lfw_batch_size, 10, args.log_dir, step, # summary_writer, best_accuracy, saver, args.train_dir, cur_time) while epoch < args.num_epochs: step = sess.run(global_step, feed_dict=None) epoch = step // args.epoch_size train(args, sess, epoch, image_list, label_list, index_dequeue_op, enqueue_op, image_paths_placeholder, labels_placeholder, learning_rate_placeholder, phase_train_placeholder, batch_size_placeholder, global_step, total_loss, train_op, summary_op, summary_writer, regularization_losses, saver, args.train_dir, cur_time) print('validation running...') # accuracy = evaluate_double(sess, phase_train_placeholder, embeddings, lfw_batch_size, step, # summary_writer, image_batch, args.lfw_dir, lfw_pairs, 'jpg', image_size) best_accuracy = evaluate( sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, embeddings, label_batch, lfw_paths, actual_issame, lfw_batch_size, 10, args.log_dir, step, summary_writer, best_accuracy, saver, args.train_dir, cur_time)
''' https://www.cnblogs.com/bmsl/p/dongbin_bmsl_01.html http://blog.csdn.net/guvcolie/article/details/77686555 https://www.2cto.com/kf/201706/649266.html 《Learning TensorFlow_ A Guide to - Tom Hope》 很好的一章第七章slim ''' ''' TF - Slim的优势:slim作为一种轻量级的tensorflow库,使得模型的构建,训练,测试都变得更加简单。 ''' # 1.使用方法: import tensorflow as tf import tensorflow.contrib.slim as slim # 2.组成部分: # arg_scope: 使得用户可以在同一个arg_scope中使用默认的参数 # data,evaluation,layers,learning,losses,metrics,nets,queues,regularizers,variables # 3.定义模型 # 在slim中,组合使用 variables, layers 和 scopes 可以简洁的定义模型。 ''' variales ''' # (1)variables: 定义于variables.py。生成一个weight变量, 用truncated # normal初始化它, 并使用l2正则化,并将其放置于CPU上, 只需下面的代码即可: weights = slim.variable( 'weights', shape=[10, 10, 3, 3], initializer=tf.truncated_normal_initializer(stddev=0.1), regularizer=slim.l2_regularizer(0.05), device='/CPU:0') # 原生tensorflow包含两类变量:普通变量和局部变量。大部分变量都是普通变量,它们一旦生成就可以通过食用saver存入硬盘, # 局部变量只在session中存在,不会保存。 # slim进一步的区分了变量类型,定义了model
def main(args): network = importlib.import_module(args.model_def) subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S') log_dir = os.path.join(os.path.expanduser(args.logs_base_dir), subdir) if not os.path.isdir(log_dir): os.makedirs(log_dir) model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir) if not os.path.isdir(model_dir): os.makedirs(model_dir) np.random.seed(seed=args.seed) random.seed(args.seed) train_set = facenet.get_dataset(args.data_dir) if args.filter_filename: train_set = filter_dataset(train_set, os.path.expanduser(args.filter_filename), args.filter_percentile, args.filter_min_nrof_images_per_class) nrof_classes = len(train_set) print('Model directory: %s' % model_dir) print('Log directory: %s' % log_dir) pretrained_model = None if args.pretrained_model: pretrained_model = os.path.expanduser(args.pretrained_model) print('Pre-trained model: %s' % pretrained_model) if args.lfw_dir: print('LFW directory: %s' % args.lfw_dir) pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs)) lfw_paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext) with tf.Graph().as_default(): tf.set_random_seed(args.seed) global_step = tf.Variable(0, trainable=False) image_list, label_list = facenet.get_image_paths_and_labels(train_set) assert len(image_list)>0, 'The dataset should not be empty' labels = ops.convert_to_tensor(label_list, dtype=tf.int32) range_size = array_ops.shape(labels)[0] index_queue = tf.train.range_input_producer(range_size, num_epochs=None, shuffle=True, seed=None, capacity=32) index_dequeue_op = index_queue.dequeue_many(args.batch_size*args.epoch_size, 'index_dequeue') learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate') batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size') phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') image_paths_placeholder = tf.placeholder(tf.string, shape=(None,1), name='image_paths') labels_placeholder = tf.placeholder(tf.int64, shape=(None,1), name='labels') input_queue = data_flow_ops.FIFOQueue(capacity=256000, dtypes=[tf.string, tf.int64], shapes=[(1,), (1,)], shared_name=None, name=None) enqueue_op = input_queue.enqueue_many([image_paths_placeholder, labels_placeholder], name='enqueue_op') nrof_preprocess_threads = 4 images_and_labels = [] for _ in range(nrof_preprocess_threads): filenames, label = input_queue.dequeue() images = [] for filename in tf.unstack(filenames): file_contents = tf.read_file(filename) image = tf.cast(tf.image.decode_image(file_contents, channels=3),tf.float32) # if args.random_crop: # image = tf.random_crop(image, [args.image_size, args.image_size, 3]) # #image = tf.image.resize_image_with_crop_or_pad(image, args.image_size, args.image_size) # else: # image = tf.image.resize_image_with_crop_or_pad(image, args.image_size, args.image_size) if args.random_flip: image = tf.image.random_flip_left_right(image) #image = tf.image.random_brightness(image,max_delta=30) #image = tf.image.random_contrast(image,lower=0.8,upper=1.2) #image = tf.image.random_saturation(image,lower=0.8,upper=1.2) image.set_shape((112, 96, 3)) images.append(tf.subtract(image,127.5) * 0.0078125) images_and_labels.append([images, label]) image_batch, label_batch = tf.train.batch_join( images_and_labels, batch_size=batch_size_placeholder, shapes=[(112, 96, 3), ()], enqueue_many=True, capacity=4 * nrof_preprocess_threads * args.batch_size, allow_smaller_final_batch=True) image_batch = tf.identity(image_batch, 'input') label_batch = tf.identity(label_batch, 'label_batch') print('Total number of classes: %d' % nrof_classes) print('Total number of examples: %d' % len(image_list)) print('Building training graph') # Build the inference graph prelogits, _ = network.inference(image_batch, args.keep_probability, phase_train=phase_train_placeholder, bottleneck_layer_size=args.embedding_size, weight_decay=args.weight_decay) embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings') AM_logits = AM_logits_compute(embeddings, label_batch, args, nrof_classes) #AM_logits = Arc_logits(embeddings, label_batch, args, nrof_classes) learning_rate = tf.train.exponential_decay(learning_rate_placeholder, global_step, args.learning_rate_decay_epochs*args.epoch_size, args.learning_rate_decay_factor, staircase=True) tf.summary.scalar('learning_rate', learning_rate) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=label_batch, logits=AM_logits, name='cross_entropy_per_example') cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') #print('test',tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) for weights in slim.get_variables_by_name('kernel'): kernel_regularization = tf.contrib.layers.l2_regularizer(args.weight_decay)(weights) print(weights) tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, kernel_regularization) regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) if args.weight_decay==0: total_loss = tf.add_n([cross_entropy_mean], name='total_loss') else: total_loss = tf.add_n([cross_entropy_mean] + regularization_losses, name='total_loss') tf.add_to_collection('losses', total_loss) #define two saver in case under 'finetuning on different dataset' situation saver_load = tf.train.Saver(tf.trainable_variables(), max_to_keep=1) saver_save = tf.train.Saver(tf.trainable_variables(), max_to_keep =1) #train_op = facenet.train(total_loss, global_step, args.optimizer, # learning_rate, args.moving_average_decay, tf.trainable_variables(), args.log_histograms) #train_op = tf.train.AdamOptimizer(learning_rate).minimize(total_loss,global_step = global_step,var_list=tf.trainable_variables()) train_op = tf.train.MomentumOptimizer(learning_rate,momentum=0.9).minimize(total_loss,global_step=global_step,var_list=tf.trainable_variables()) summary_op = tf.summary.merge_all() # Start running operations on the Graph. gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) summary_writer = tf.summary.FileWriter(log_dir, sess.graph) coord = tf.train.Coordinator() tf.train.start_queue_runners(coord=coord, sess=sess) with sess.as_default(): if pretrained_model: print('Restoring pretrained model: %s' % pretrained_model) saver_load.restore(sess, pretrained_model) print('Running training') epoch = 0 best_accuracy = 0.0 while epoch < args.max_nrof_epochs: step = sess.run(global_step, feed_dict=None) epoch = step // args.epoch_size train(args, sess, epoch, image_list, label_list, index_dequeue_op, enqueue_op, image_paths_placeholder, labels_placeholder, learning_rate_placeholder, phase_train_placeholder, batch_size_placeholder, global_step, total_loss, train_op, summary_op, summary_writer, regularization_losses, args.learning_rate_schedule_file) print('validation running...') if args.lfw_dir: #best_accuracy = evaluate_double(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, embeddings, # label_batch, lfw_paths, actual_issame, args.lfw_batch_size, args.lfw_nrof_folds, log_dir, step, summary_writer,best_accuracy, saver_save,model_dir,subdir,image_batch,args) best_accuracy = evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, embeddings, label_batch, lfw_paths, actual_issame, args.lfw_batch_size, args.lfw_nrof_folds, log_dir, step, summary_writer,best_accuracy,saver_save,model_dir,subdir) return model_dir
def main(args): network = importlib.import_module(args.model_def) subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S') log_dir = os.path.join(os.path.expanduser(args.logs_base_dir), subdir) if not os.path.isdir(log_dir): os.makedirs(log_dir) model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir) if not os.path.isdir(model_dir): os.makedirs(model_dir) np.random.seed(seed=args.seed) random.seed(args.seed) train_set = facenet.get_dataset(args.data_dir) if args.filter_filename: train_set = filter_dataset(train_set, os.path.expanduser(args.filter_filename), args.filter_percentile, args.filter_min_nrof_images_per_class) nrof_classes = len(train_set) print('Model directory: %s' % model_dir) print('Log directory: %s' % log_dir) pretrained_model = None if args.pretrained_model: pretrained_model = os.path.expanduser(args.pretrained_model) print('Pre-trained model: %s' % pretrained_model) if args.lfw_dir: print('LFW directory: %s' % args.lfw_dir) pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs)) lfw_paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext) with tf.Graph().as_default(): tf.set_random_seed(args.seed) global_step = tf.Variable(0, trainable=False) image_list, label_list = facenet.get_image_paths_and_labels(train_set) assert len(image_list)>0, 'The dataset should not be empty' labels = ops.convert_to_tensor(label_list, dtype=tf.int32) range_size = array_ops.shape(labels)[0] index_queue = tf.train.range_input_producer(range_size, num_epochs=None, shuffle=True, seed=None, capacity=32) index_dequeue_op = index_queue.dequeue_many(args.batch_size*args.epoch_size, 'index_dequeue') learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate') batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size') phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') image_paths_placeholder = tf.placeholder(tf.string, shape=(None,1), name='image_paths') labels_placeholder = tf.placeholder(tf.int64, shape=(None,1), name='labels') input_queue = data_flow_ops.FIFOQueue(capacity=256000, dtypes=[tf.string, tf.int64], shapes=[(1,), (1,)], shared_name=None, name=None) enqueue_op = input_queue.enqueue_many([image_paths_placeholder, labels_placeholder], name='enqueue_op') nrof_preprocess_threads = 4 images_and_labels = [] for _ in range(nrof_preprocess_threads): filenames, label = input_queue.dequeue() images = [] for filename in tf.unstack(filenames): file_contents = tf.read_file(filename) image = tf.cast(tf.image.decode_image(file_contents, channels=3),tf.float32) # if args.random_crop: # image = tf.random_crop(image, [args.image_size, args.image_size, 3]) # #image = tf.image.resize_image_with_crop_or_pad(image, args.image_size, args.image_size) # else: # image = tf.image.resize_image_with_crop_or_pad(image, args.image_size, args.image_size) if args.random_flip: image = tf.image.random_flip_left_right(image) #image = tf.image.random_brightness(image,max_delta=30) #image = tf.image.random_contrast(image,lower=0.8,upper=1.2) #image = tf.image.random_saturation(image,lower=0.8,upper=1.2) image.set_shape((112, 96, 3)) images.append(tf.subtract(image,127.5) * 0.0078125) images_and_labels.append([images, label]) image_batch, label_batch = tf.train.batch_join( images_and_labels, batch_size=batch_size_placeholder, shapes=[(112, 96, 3), ()], enqueue_many=True, capacity=4 * nrof_preprocess_threads * args.batch_size, allow_smaller_final_batch=True) image_batch = tf.identity(image_batch, 'input') label_batch = tf.identity(label_batch, 'label_batch') print('Total number of classes: %d' % nrof_classes) print('Total number of examples: %d' % len(image_list)) print('Building training graph') # Build the inference graph prelogits, _ = network.inference(image_batch, args.keep_probability, phase_train=phase_train_placeholder, bottleneck_layer_size=args.embedding_size, weight_decay=args.weight_decay) embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings') AM_logits = AM_logits_compute(embeddings, label_batch, args, nrof_classes) #AM_logits = Arc_logits(embeddings, label_batch, args, nrof_classes) learning_rate = tf.train.exponential_decay(learning_rate_placeholder, global_step, args.learning_rate_decay_epochs*args.epoch_size, args.learning_rate_decay_factor, staircase=True) tf.summary.scalar('learning_rate', learning_rate) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=label_batch, logits=AM_logits, name='cross_entropy_per_example') cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') for weights in slim.get_variables_by_name('kernel'): kernel_regularization = tf.contrib.layers.l2_regularizer(args.weight_decay)(weights) print(weights) tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, kernel_regularization) regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) total_loss = tf.add_n([cross_entropy_mean] + regularization_losses, name='total_loss') tf.add_to_collection('losses', total_loss) #define two saver in case under 'finetuning on different dataset' situation saver_load = tf.train.Saver(tf.trainable_variables(), max_to_keep=1) saver_save = tf.train.Saver(tf.trainable_variables(), max_to_keep =1) #train_op = facenet.train(total_loss, global_step, args.optimizer, # learning_rate, args.moving_average_decay, tf.trainable_variables(), args.log_histograms) train_op = tf.train.AdamOptimizer(learning_rate).minimize(total_loss,global_step = global_step,var_list=tf.trainable_variables()) summary_op = tf.summary.merge_all() # Start running operations on the Graph. gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) summary_writer = tf.summary.FileWriter(log_dir, sess.graph) coord = tf.train.Coordinator() tf.train.start_queue_runners(coord=coord, sess=sess) with sess.as_default(): if pretrained_model: print('Restoring pretrained model: %s' % pretrained_model) saver_load.restore(sess, pretrained_model) print('Running training') epoch = 0 best_accuracy = 0.0 while epoch < args.max_nrof_epochs: step = sess.run(global_step, feed_dict=None) epoch = step // args.epoch_size train(args, sess, epoch, image_list, label_list, index_dequeue_op, enqueue_op, image_paths_placeholder, labels_placeholder, learning_rate_placeholder, phase_train_placeholder, batch_size_placeholder, global_step, total_loss, train_op, summary_op, summary_writer, regularization_losses, args.learning_rate_schedule_file) print('validation running...') if args.lfw_dir: best_accuracy = evaluate_double(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, embeddings, label_batch, lfw_paths, actual_issame, args.lfw_batch_size, args.lfw_nrof_folds, log_dir, step, summary_writer,best_accuracy,saver_save,model_dir,subdir,image_batch,args) return model_dir
def _main(): train_file_list, train_label_list, train_image_size_list, \ val_file_list, val_label_list, val_image_size_list, cls_names = load_translated_data( '/media/wx/新加卷/datasets/COCODataset') batch_generator = _image_batch(train_file_list, train_label_list, train_image_size_list) with tf.name_scope('inputs'): tf_images = tf.placeholder(dtype=tf.float32, shape=[frc.IMAGE_BATCH_SIZE, None, None, 3], name='images') tf_labels = tf.placeholder(dtype=tf.int32, shape=[None, 5], name='ground_truth_bbox') tf_shape = tf.placeholder(dtype=tf.int32, shape=[None], name='image_shape') # Preprocess input images preprocessed_inputs, preprocessed_labels, preprocessed_shape = _preprocess(tf_images, tf_labels, tf_shape) final_bbox, final_score, final_categories, loss_dict, acc_dict, image_summary = _network(preprocessed_inputs, preprocessed_shape, preprocessed_labels, cls_names) total_loss = frc.RPN_CLASSIFICATION_LOSS_WEIGHTS * loss_dict['rpn_cls_loss'] + \ frc.RPN_LOCATION_LOSS_WEIGHTS * loss_dict['rpn_bbox_loss'] + \ frc.FASTER_RCNN_CLASSIFICATION_LOSS_WEIGHTS * loss_dict['rcnn_cls_loss'] + \ frc.FASTER_RCNN_LOCATION_LOSS_WEIGHTS * loss_dict['rcnn_bbox_loss'] + \ 0.0005 * tf.reduce_sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) global_step = tf.train.get_or_create_global_step() learning_rate = tf.train.piecewise_constant(global_step, frc.LEARNING_RATE_BOUNDARIES, frc.LEARNING_RATE_SCHEDULAR) # Adam # train_op = tf.train.AdamOptimizer(learning_rate).minimize(total_loss, global_step=global_step) train_op = tf.train.AdamOptimizer(0.001).minimize(total_loss, global_step=global_step) # Momentum # train_op = tf.train.MomentumOptimizer(learning_rate, momentum=0.9).minimize(total_loss, global_step=global_step) # RMS # train_op = tf.train.RMSPropOptimizer(learning_rate, momentum=0.9).minimize(total_loss, global_step=global_step) # Add train summary. with tf.name_scope('loss'): total_loss_summary = tf.summary.scalar('total_loss', total_loss) rpn_cls_loss_summary = tf.summary.scalar('rpn_cls_loss', loss_dict['rpn_cls_loss']) rpn_bbox_loss_summary = tf.summary.scalar('rpn_bbox_loss', loss_dict['rpn_bbox_loss']) rcnn_cls_loss_summary = tf.summary.scalar('rcnn_cls_loss', loss_dict['rcnn_cls_loss']) rcnn_bbox_loss_summary = tf.summary.scalar('rcnn_bbox_loss', loss_dict['rcnn_bbox_loss']) with tf.name_scope('accuracy'): rpn_cls_acc_summary = tf.summary.scalar('rpn_acc', acc_dict['rpn_cls_acc']) rcnn_cls_acc_summary = tf.summary.scalar('rcnn_acc', acc_dict['rcnn_cls_acc']) # with tf.name_scope('train'): # lr_summary = tf.summary.scalar('learning_rate', learning_rate) # summary_op = tf.summary.merge_all() scale_summary = tf.summary.merge([total_loss_summary, rpn_cls_loss_summary, rpn_bbox_loss_summary, rcnn_cls_loss_summary, rcnn_bbox_loss_summary, rpn_cls_acc_summary, rcnn_cls_acc_summary])#, lr_summary]) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) load_weight_name = _load_pre_train_model(frc.PRE_TRAIN_MODEL_PATH) load_variables = {} for name in load_weight_name: load_variables[name] = slim.get_variables_by_name(name)[0] print('From pre-train model load {}.'.format(name), end=' ') print(load_variables[name]) restore = tf.train.Saver(var_list=load_variables) saver = tf.train.Saver(max_to_keep=4) if not os.path.exists(frc.SUMMARY_PATH): os.mkdir(frc.SUMMARY_PATH) with tf.Session() as sess: sess.run(init_op) if frc.PRE_TRAIN_MODEL_PATH: print('Load pre-trained model:', frc.PRE_TRAIN_MODEL_PATH) restore.restore(sess, frc.PRE_TRAIN_MODEL_PATH) start_time = time.strftime('%Y_%m_%d_%H_%M_%S') log_dir = os.path.join(frc.SUMMARY_PATH, start_time) save_model_dir = os.path.join(log_dir, 'model') if not os.path.exists(save_model_dir): os.mkdir(log_dir) os.mkdir(save_model_dir) summary_writer = tf.summary.FileWriter(log_dir, graph=sess.graph) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess, coord) step = 0 try: while step < frc.MAXIMUM_ITERS + 1: images, gt_bboxes, image_shape = batch_generator.__next__() if len(gt_bboxes) == 0: continue feed_dict = {tf_images: images, tf_labels: gt_bboxes, tf_shape: image_shape} step_time = time.time() _, total_loss_, rpn_cls_loss_, rpn_bbox_loss_, rcnn_cls_loss_, rcnn_bbox_loss_, \ rpn_cls_acc_, rcnn_cls_acc_, scale_summary_str, image_summary_str, global_step_ = \ sess.run([train_op, total_loss, loss_dict['rpn_cls_loss'], loss_dict['rpn_bbox_loss'], loss_dict['rcnn_cls_loss'], loss_dict['rcnn_bbox_loss'], acc_dict['rpn_cls_acc'], acc_dict['rcnn_cls_acc'], scale_summary, image_summary, global_step], feed_dict) step_time = time.time() - step_time print(f'Iter: {step}', f'| total_loss: {total_loss_:.3}', f'| rpn_cls_loss: {rpn_cls_loss_:.3}', f'| rpn_bbox_loss: {rpn_bbox_loss_:.3}', f'| rcnn_cls_loss: {rcnn_cls_loss_:.3}', f'| rcnn_bbox_loss: {rcnn_bbox_loss_:.3}', f'| rpn_cls_acc: {rpn_cls_acc_:.3}', f'| rcnn_cls_acc: {rcnn_cls_acc_:.3}', f'| time: {step_time:.3}s') if step % frc.REFRESH_LOGS_ITERS == 0 and step != 0: summary_writer.add_summary(scale_summary_str, step) saver.save(sess, os.path.join(save_model_dir, frc.MODEL_NAME + '.ckpt'), step) if step % 50 == 0: summary_writer.add_summary(image_summary_str, step) summary_writer.flush() step += 1 except tf.errors.OutOfRangeError: print('done') finally: coord.request_stop() coord.join(threads) summary_writer.close()
def model_sample(sen_batch, mask_batch, shp, labels_batch, max_sampling=True, reuse=False): sen_batch, mask_batch, _, seq_len_encoder, _ = proc( sen_batch, mask_batch, shp, labels_batch) sen_batch = tf.cast(sen_batch, tf.int64) if reuse: word_vectors = slim.get_variables_by_name('rnn/word_embedding')[0] else: word_vectors = tf.get_variable( name='rnn/word_embedding', shape=[FLAGS.vocab_size, FLAGS.embedding_size]) embs = tf.nn.embedding_lookup(word_vectors, sen_batch) zero_state = tf.zeros((FLAGS.batch_size, FLAGS.lstm_size)) cnt_embs = embs with tf.variable_scope('cnt_enc', reuse=reuse): cell = tf.contrib.rnn.GRUCell(num_units=FLAGS.lstm_size) cnt_embs_flip = flip_input(cnt_embs, seq_len_encoder) cnt_enc_states, cnt_enc = tf.nn.dynamic_rnn( cell=cell, dtype=tf.float32, inputs=cnt_embs_flip, initial_state=zero_state, sequence_length=seq_len_encoder) with tf.variable_scope('dec', reuse=reuse): cell = tf.contrib.rnn.GRUCell(num_units=FLAGS.dec_lstm_size) all_samples = [] if FLAGS.multi_attr: reuse_flag = False attribs_valid = [] for i in range(FLAGS.Nattr): if FLAGS.attrib_mask[i] == 0: attribs_valid.append([0]) else: attribs_valid.append(range(FLAGS.Nlabels[i])) attribs_valid = itertools.product(*attribs_valid) attribs_valid = [list(attribs) for attribs in attribs_valid] reuse_flag = False for attribs in attribs_valid: labels_attr = tf.tile( tf.reshape(tf.constant(attribs), (1, FLAGS.Nattr)), (FLAGS.batch_size, 1)) attrib_mask = tf.ones((FLAGS.batch_size, FLAGS.Nattr)) labels_oh = one_hot(labels_attr, attrib_mask) cnt_enc_mix = mult_int(cnt_enc, labels_oh, reuse=reuse_flag) samples, _ = decode(cnt_enc_mix, labels_oh, src_states=cnt_enc_states, src_mask=mask_batch, reuse=reuse_flag) all_samples.append(samples) reuse_flag = True else: for l in range(FLAGS.Nlabels): labels = l * tf.ones((FLAGS.batch_size, ), dtype=tf.int32) labels_oh = one_hot(labels) cnt_enc_mix = mult_int(cnt_enc, labels_oh, reuse=l > 0 or reuse) samples, _ = decode(cnt_enc_mix, labels_oh, src_states=cnt_enc_states, src_mask=mask_batch, reuse=l > 0 or reuse, max_sampling=max_sampling) all_samples.append(samples) return all_samples
def decode(cnt, labels_oh, reuse=False, src_states=None, src_mask=None, max_sampling=True, batch_size=None, decode_num_steps=None, pred_logprobs=False): if not batch_size: batch_size = FLAGS.batch_size if not decode_num_steps: decode_num_steps = FLAGS.decode_max_steps if FLAGS.beam_search: return decode_beam(cnt, labels_oh, reuse=reuse, batch_size=batch_size, decode_num_steps=decode_num_steps) word_vectors = slim.get_variables_by_name('rnn/word_embedding')[0] initial_state = tf.zeros([FLAGS.batch_size, FLAGS.dec_lstm_size]) initial_state = cnt states = [] all_predictions = [] all_predictions_mask = [] all_pred_logprobs = [] state = initial_state word_inds = None predictions_embs = None for i in range(decode_num_steps): if i == 0: embedding_input = sos(labels_oh, reuse=reuse) else: embedding_input = tf.nn.embedding_lookup(word_vectors, word_inds) decoder_cell = tf.contrib.rnn.GRUCell(num_units=FLAGS.dec_lstm_size, reuse=(i > 0) or reuse) decoder_cell = tf.nn.rnn_cell.DropoutWrapper( decoder_cell, input_keep_prob=FLAGS.dropout_keep_prob_dec) with tf.variable_scope('rnn', reuse=(i > 0) or reuse): output, state = decoder_cell(embedding_input, state) states.append(tf.expand_dims(state, 1)) predictions_logits = slim.fully_connected(output, FLAGS.vocab_size, activation_fn=None, scope='classifier', reuse=(i > 0) or reuse) predictions = tf.nn.softmax(predictions_logits) if max_sampling: word_inds = tf.argmax(predictions, axis=1) else: word_inds = tf.squeeze(tf.multinomial(predictions_logits, 1)) if pred_logprobs: lp = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=predictions_logits, labels=word_inds) all_pred_logprobs.append(tf.expand_dims(lp, -1)) word_inds_rshp = tf.expand_dims(word_inds, -1) mask = tf.equal(word_inds_rshp, 0) all_predictions.append(word_inds_rshp) all_predictions_mask.append(mask) states = tf.concat(states, axis=1) predictions = tf.concat(all_predictions, axis=1) predictions_mask = tf.concat(all_predictions_mask, axis=1) predictions_mask, predictions_seq_len = construct_eos_mask( predictions_mask, decode_num_steps) predictions = tf.cast( tf.cast(predictions, tf.float32) * predictions_mask, tf.int64) if pred_logprobs: pred_lps = tf.concat(all_pred_logprobs, axis=1) * predictions_mask return predictions, predictions_mask, pred_lps, predictions_seq_len, states return predictions, predictions_mask
def teacher(input_images, keep_prob, lambda_decay=FLAGS.lambda_decay, is_training=True, weight_decay=0.00004, batch_norm_decay=0.99, batch_norm_epsilon=0.001): with tf.variable_scope("Teacher_model"): net, endpoints = resnet.resnet_v2(inputs=input_images, lambda_decay=lambda_decay, num_classes=FLAGS.num_class, is_training=True, scope='resnet_v2_50') # co_trained layers var_scope = 'Teacher_model/resnet_v2_50/' co_list_0 = slim.get_model_variables(var_scope + 'Conv2d_0') # co_list_1 = slim.get_model_variables(var_scope +'InvertedResidual_16_') # co_list_2 = slim.get_model_variables(var_scope +'InvertedResidual_24_') t_co_list = co_list_0 base_var_list = slim.get_variables() # for _ in range(2): # base_var_list.pop() lambda_c_list = slim.get_variables_by_name('lambda_c') lambda_b_list = slim.get_variables_by_name('lambda_b') t_lambda_list = lambda_c_list + lambda_b_list # print(lambda_b_list) # exit() t_net_var_list =[] for v in base_var_list: if v not in t_co_list and v not in t_lambda_list: t_net_var_list.append(v) # feature & attention t_g0 = endpoints["InvertedResidual_{}_{}".format(256, 2)] t_at0 = tf.nn.l2_normalize(tf.reduce_sum(tf.square(t_g0), -1), axis=0, name='t_at0') t_g1 = endpoints["InvertedResidual_{}_{}".format(512, 3)] t_at1 = tf.nn.l2_normalize(tf.reduce_sum(tf.square(t_g1), -1), axis=0, name='t_at1') part_feature = endpoints["InvertedResidual_{}_{}".format(1024, 5)] t_at2 = tf.nn.l2_normalize(tf.reduce_sum(tf.square(part_feature), -1), axis=0, name='t_at2') object_feature = endpoints["InvertedResidual_{}_{}".format(2048, 2)] t_at3 = tf.nn.l2_normalize(tf.reduce_sum(tf.square(object_feature), -1), axis=0, name='t_at3') # print(t_at1.get_shape().as_list()) # exit() t_g = (t_g0, t_g1, part_feature, object_feature) t_at = (t_at0, t_at1, t_at2, t_at3) fc_obj = slim.max_pool2d(object_feature, (6, 8), scope="GMP1") batch_norm_params = { 'center': True, 'scale': True, 'decay': batch_norm_decay, 'epsilon': batch_norm_epsilon, } fc_obj = slim.conv2d(fc_obj, M, [1, 1], activation_fn=None, weights_regularizer=tf.contrib.layers.l2_regularizer(weight_decay), biases_regularizer=tf.contrib.layers.l2_regularizer(weight_decay), scope='fc_obj') fc_obj = tf.nn.dropout(fc_obj, keep_prob=keep_prob) fc_obj = slim.flatten(fc_obj) # fc_part = slim.conv2d(part_feature, M * k, #卷积核个数 [1, 1], #卷积核高宽 activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, # 标准化器设置为BN normalizer_params=batch_norm_params, weights_regularizer=tf.contrib.layers.l2_regularizer(weight_decay), biases_regularizer=tf.contrib.layers.l2_regularizer(weight_decay) ) # print('part',fc_part.get_shape()) fc_part = slim.max_pool2d(fc_part, (12, 16), scope="GMP2") ft_list = tf.split(fc_part, num_or_size_splits=FLAGS.num_class, axis=-1) #最后一维度(C) cls_list = [] for i in range(M): ft = tf.transpose(ft_list[i], [0, 1, 3, 2]) cls = layers_lib.pool(ft, [1, 10], "AVG") cls = layers.flatten(cls) cls_list.append(cls) fc_ccp = tf.concat(cls_list, axis=-1) #cross_channel_pooling (N, M) fc_part = slim.conv2d(fc_part, FLAGS.num_class, [1, 1], activation_fn=None, weights_regularizer=tf.contrib.layers.l2_regularizer(weight_decay), biases_regularizer=tf.contrib.layers.l2_regularizer(weight_decay), scope="fc_part") fc_part = tf.nn.dropout(fc_part, keep_prob=keep_prob) fc_part = slim.flatten(fc_part) t_var_list = slim.get_model_variables() t_fc_var_list = [] for var in t_var_list: if var not in base_var_list: t_fc_var_list.append(var) return t_g, t_at, fc_obj, fc_part, fc_ccp, t_co_list, t_net_var_list, t_fc_var_list, t_lambda_list, t_var_list
def main(args): network = importlib.import_module(args.model_def) subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S') log_dir = os.path.join(os.path.expanduser(args.logs_base_dir), subdir) if not os.path.isdir(log_dir): os.makedirs(log_dir) model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir) if not os.path.isdir(model_dir): os.makedirs(model_dir) np.random.seed(seed=args.seed) random.seed(args.seed) nrof_classes = 8631 print('Model directory: %s' % model_dir) print('Log directory: %s' % log_dir) pretrained_model = None if args.pretrained_model: pretrained_model = os.path.expanduser(args.pretrained_model) print('Pre-trained model: %s' % pretrained_model) if args.lfw_dir: print('LFW directory: %s' % args.lfw_dir) pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs)) lfw_paths, actual_issame = lfw.get_paths( os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext) # images_lfw = np.load(args.lfw_dir+'.npy') with tf.Graph().as_default(): tf.set_random_seed(args.seed) global_step = tf.Variable(0, trainable=False) learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate') phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') tfrecords_list = glob.glob(args.tfrecord_dir) filename_queue = tf.train.string_input_producer(tfrecords_list, shuffle=True) reader = tf.TFRecordReader() key, records = reader.read(filename_queue) features = tf.parse_single_example(records, features={ 'label': tf.FixedLenFeature([], tf.int64), "image_raw": tf.FixedLenFeature([], tf.string) }) label = tf.cast(features['label'], tf.int32) image = tf.cast( tf.image.decode_jpeg(features["image_raw"], channels=3), tf.float32) if args.random_flip: image = tf.image.random_flip_left_right(image) image.set_shape((112, 96, 3)) image = tf.subtract(image, 127.5) * 0.0078125 images_and_labels = [image, label] image_batch, label_batch = tf.train.shuffle_batch( images_and_labels, batch_size=args.batch_size, shapes=[(112, 96, 3), ()], capacity=40000, min_after_dequeue=10000, allow_smaller_final_batch=True) # image_batch.set_shape([args.batch_size, 112, 112, 3]) # label_batch.set_shape([args.batch_size]) image_batch = tf.identity(image_batch, 'input') label_batch = tf.identity(label_batch, 'label') image_batch.set_shape([None, 112, 96, 3]) print('Total number of classes: %d' % nrof_classes) print('Total number of examples: 3000000') print('Building training graph') # Build the inference graph prelogits, _ = network.inference( image_batch, args.keep_probability, phase_train=phase_train_placeholder, bottleneck_layer_size=args.embedding_size, weight_decay=args.weight_decay) prelogits = tf.identity(prelogits, 'prelogits') embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings') cross_entropy_mean, logits = cosSoftmax_loss(embeddings, label_batch, args.batch_size, nrof_classes, m=0.35, s=30, name='softmax') # cross_entropy_mean, accuracy = adaptive_loss(embeddings, label_batch, args.batch_size, nrof_classes, m=0.35, s=30, name='softmax') # AM_logits, logits = AM_logits_compute(embeddings, label_batch, args, nrof_classes) #AM_logits = Arc_logits(embeddings, label_batch, args, nrof_classes) learning_rate = tf.train.exponential_decay( learning_rate_placeholder, global_step, args.learning_rate_decay_epochs * args.epoch_size, args.learning_rate_decay_factor, staircase=True) tf.summary.scalar('learning_rate', learning_rate) # cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( # labels=label_batch, logits=AM_logits, name='cross_entropy_per_example') # cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') correct_prediction = tf.cast( tf.equal(tf.argmax(logits, 1), tf.cast(label_batch, tf.int64)), tf.float32) accuracy = tf.reduce_mean(correct_prediction) for weights in slim.get_variables_by_name('kernel'): kernel_regularization = tf.contrib.layers.l2_regularizer( args.weight_decay)(weights) tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, kernel_regularization) regularization_losses = tf.get_collection( tf.GraphKeys.REGULARIZATION_LOSSES) if args.weight_decay == 0: total_loss = tf.add_n([cross_entropy_mean], name='total_loss') else: total_loss = tf.add_n([cross_entropy_mean] + regularization_losses, name='total_loss') tf.add_to_collection('losses', total_loss) #define two saver in case under 'finetuning on different dataset' situation saver_save = tf.train.Saver(tf.trainable_variables(), max_to_keep=1) train_op = facenet.train(total_loss, global_step, args.optimizer, learning_rate, args.moving_average_decay, tf.trainable_variables(), args.log_histograms) # train_op = tf.train.AdamOptimizer(learning_rate).minimize(total_loss,global_step = global_step,var_list=tf.trainable_variables()) # train_op = tf.train.MomentumOptimizer(learning_rate,momentum=0.9).minimize(total_loss,global_step=global_step,var_list=tf.trainable_variables()) summary_op = tf.summary.merge_all() # Start running operations on the Graph. # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction) gpu_options = tf.GPUOptions(allow_growth=True) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) summary_writer = tf.summary.FileWriter(log_dir, sess.graph) coord = tf.train.Coordinator() tf.train.start_queue_runners(coord=coord, sess=sess) with sess.as_default(): if pretrained_model: exclusions = [] except_exclusions = slim.get_variables_to_restore( exclude=exclusions) restore_variables = [ v for v in tf.trainable_variables() if v in except_exclusions ] saver_load = tf.train.Saver(restore_variables) print('Restoring pretrained model: %s' % pretrained_model) saver_load.restore(sess, pretrained_model) best_accuracy = evaluate_double(sess, phase_train_placeholder, image_batch, embeddings, lfw_paths, actual_issame, log_dir, 0, summary_writer, 0.0, saver_save, model_dir, subdir, args) print('Running training') epoch = 0 best_accuracy = 0.0 while epoch < args.max_nrof_epochs: step = sess.run(global_step, feed_dict=None) epoch = step // args.epoch_size train(args, sess, epoch, learning_rate_placeholder, phase_train_placeholder, global_step, total_loss, train_op, summary_op, summary_writer, regularization_losses, accuracy, learning_rate) print('validation running...') if args.lfw_dir: best_accuracy = evaluate_double( sess, phase_train_placeholder, image_batch, embeddings, lfw_paths, actual_issame, log_dir, step + args.epoch_size, summary_writer, best_accuracy, saver_save, model_dir, subdir, args) return model_dir
sum_of_squares_loss = slim.losses.sum_of_squares(predictions, labels) slim.losses.add_loss(sum_of_squares_loss) total_loss2 = slim.losses.get_total_loss() total_loss = slim.losses.get_total_loss() optimizer = tf.train.GradientDescentOptimizer(0.1) train_op = slim.learning.create_train_op(total_loss, optimizer) logdir = "" slim.learning.train(train_op, logdir, number_of_steps=1000, save_summaries_secs=300, save_interval_secs=600) variables_to_restore = slim.get_variables_by_name("v2") # or variables_to_restore = slim.get_variables_by_suffix("2") # or variables_to_restore = slim.get_variables(scope="nested") # or variables_to_restore = slim.get_variables_to_restore(include=["nested"]) # or variables_to_restore = slim.get_variables_to_restore(exclude=["v1"]) restorer = tf.train.Saver(variables_to_restore) with tf.Session as sess: restorer.restore(sess, "/tmp/model.ckpt") # Load the data images, labels = None, None
reuse=False ) var_new = slim.get_variables_to_restore() # search common count = 0 ommit = 0 all_var = set() restore_list = [] for key in var_new: # 命名改变了 改成了"CenterNet/作为前缀, 需要去掉" all_var.add(change_name(key.name.strip(':0'))) for key in var_ori: if key in all_var: ori_var = reader.get_tensor(key) new_var = slim.get_variables_by_name(restore_name(key))[0] s1 = list(ori_var.shape) s2 = new_var.get_shape().as_list() if s1 == s2: count += 1 restore_list.append(key) else: ommit += 1 else: ommit += 1 print('restore ', count) print('ommit', ommit) print('all', count + ommit) var_list = map_var(restore_list) # loader = tf.train.Saver( # var_list=slim.get_variables_to_restore(