def __batching_data(self, image, glabels, format, filename, gbboxes, gdifficults, gclasses, glocalisations, gscores): #we will want to batch original glabels and gbboxes #this information is still useful even if they are padded after dequeuing dynamic_pad = True batch_shape = [1, 1, 1, 1, 1] + [ len(gclasses), len(glocalisations), len(gscores) ] tensors = [ image, filename, glabels, gbboxes, gdifficults, gclasses, glocalisations, gscores ] #Batch the samples if self.is_training_data: self.num_preprocessing_threads = 1 else: # to make sure data is fectched in sequence during evaluation self.num_preprocessing_threads = 1 #tf.train.batch accepts only list of tensors, this batch shape can used to #flatten the list in list, and later on convet it back to list in list. batch = tf.train.batch(tf_utils.reshape_list(tensors), batch_size=self.batch_size, num_threads=self.num_preprocessing_threads, dynamic_pad=dynamic_pad, capacity=5 * self.batch_size) #convert it back to the list in list format which allows us to easily use later on batch = tf_utils.reshape_list(batch, batch_shape) return batch
def clone_fn(batch_queue): """Allows data parallelism by creating multiple clones of network_fn.""" # Dequeue batch. # 批次的图片、类别、位置与置信度 b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # Construct SSD network. arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) # Add loss function. ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores, match_threshold=FLAGS.match_threshold, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, label_smoothing=FLAGS.label_smoothing) return end_points
def clone_fn(batch_queue): """Allows data parallelism by creating multiple clones of network_fn.""" # Dequeue batch. b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # 重整list # Construct SSD network. # 这个实例方法会返回之前定义的函数ssd_arg_scope(允许修改两个参数) arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): # predictions: (BS, H, W, 4, 21) # localisations: (BS, H, W, 4, 4) # logits: (BS, H, W, 4, 21) predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) # Add loss function. ssd_net.losses( logits, localisations, b_gclasses, b_glocalisations, b_gscores, match_threshold=FLAGS.match_threshold, # .5 negative_ratio=FLAGS.negative_ratio, # 3 alpha=FLAGS.loss_alpha, # 1 label_smoothing=FLAGS.label_smoothing) # .0 return end_points
def clone_fn(batch_queue): #Allows data parallelism by creating multiple #clones of network_fn. # Dequeue batch. batch_shape = [1] + [len(anchors)] * 3 b_image, b_glocalisations, b_glabels, b_glinks = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) if FLAGS.data_format == 'NHWC': b_image = b_image else: b_image = tf.transpose(b_image, perm=(0, 3, 1, 2)) # Construct SSD network. arg_scope = net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=FLAGS.data_format) with slim.arg_scope(arg_scope): localisations, logits, linkslogits, end_points = \ net.net(b_image, is_training=True, use_batch=FLAGS.use_batch) # Add loss function. net.losses(logits, localisations, linkslogits, b_glocalisations, b_glabels, b_glinks, negative_ratio=FLAGS.negative_ratio, alpha1=FLAGS.alpha1, alpha2=FLAGS.alpha2, label_smoothing=FLAGS.label_smoothing) return end_points
def clone_fn(batch_queue): #Allows data parallelism by creating multiple #clones of network_fn. # Dequeue batch. batch_shape = [1] * 3 b_image, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # Construct SSD network. arg_scope = net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=FLAGS.data_format) with slim.arg_scope(arg_scope): localisations, logits, end_points = \ net.net(b_image, is_training=True, use_batch=FLAGS.use_batch) # Add loss function. net.losses(logits, localisations, b_glocalisations, b_gscores, negative_ratio=FLAGS.negative_ratio, use_hard_neg=FLAGS.use_hard_neg, alpha=FLAGS.loss_alpha, label_smoothing=FLAGS.label_smoothing) return end_points
def get_batch(dataset_dir, num_readers, batch_size, out_shape, net, anchors, num_preprocessing_threads, file_pattern = '*.tfrecord', is_training = True): dataset = sythtextprovider.get_datasets(dataset_dir,file_pattern = file_pattern) provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=num_readers, common_queue_capacity=20 * batch_size, common_queue_min=10 * batch_size, shuffle=True) [image, shape, glabels, gbboxes] = provider.get(['image', 'shape', 'object/label', 'object/bbox']) image, glabels, gbboxes,num = \ ssd_vgg_preprocessing.preprocess_image(image, glabels,gbboxes, out_shape,is_training=is_training) gclasses, glocalisations, gscores = \ net.bboxes_encode( glabels, gbboxes, anchors, num) batch_shape = [1] + [len(anchors)] * 3 r = tf.train.batch( tf_utils.reshape_list([image, gclasses, glocalisations, gscores]), batch_size=batch_size, num_threads=num_preprocessing_threads, capacity=5 * batch_size) b_image, b_gclasses, b_glocalisations, b_gscores= \ tf_utils.reshape_list(r, batch_shape) return [b_image, b_gclasses, b_glocalisations, b_gscores]
def clone_fn(batch_queue): """Allows data parallelism by creating multiple clones of network_fn.""" # Dequeue batch. b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # Construct SSD network. arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) # Add loss function. ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores, label_smoothing=FLAGS.label_smoothing) return end_points
def _parser_fn(record, split_name, img_shape): # Features in Pascal VOC TFRecords. # refer to slim.tfexample_decoder.BoundingBox keys_to_features = { 'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), 'image/format': tf.FixedLenFeature((), tf.string, default_value='jpeg'), 'image/height': tf.FixedLenFeature([1], tf.int64), 'image/width': tf.FixedLenFeature([1], tf.int64), 'image/channels': tf.FixedLenFeature([1], tf.int64), 'image/shape': tf.FixedLenFeature([3], tf.int64), 'image/object/bbox/xmin': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/ymin': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/xmax': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/ymax': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/label': tf.VarLenFeature(dtype=tf.int64), 'image/object/bbox/difficult': tf.VarLenFeature(dtype=tf.int64), 'image/object/bbox/truncated': tf.VarLenFeature(dtype=tf.int64), } features = tf.parse_single_example(record, keys_to_features) # image = tf.decode_(features['image/encoded'], tf.float32) # image = tf.reshape(image, img_shape) image = tf.image.decode_jpeg(features['image/encoded'],channels=3) xmin = features['image/object/bbox/xmin'].values # since the original is tf sparse tensor, .value convert to Tensor ymin = features['image/object/bbox/ymin'].values xmax = features['image/object/bbox/xmax'].values ymax = features['image/object/bbox/ymax'].values bboxes = tf.stack([xmin,ymin,xmax,ymax],axis=-1) labels = features['image/object/bbox/label'].values if split_name == 'train': image, labels, bboxes = ssd_vgg_preprocessing.preprocess_for_train(image,labels,bboxes,img_shape) else: image, labels, bboxes = ssd_vgg_preprocessing.preprocess_for_eval(image, labels, bboxes,img_shape) net = model.get_model() arm_anchor_labels, arm_anchor_loc, arm_anchor_scores = net.get_prematched_anchors(img_shape,labels,bboxes) output = tf_utils.reshape_list([image, labels, bboxes, arm_anchor_labels, arm_anchor_loc, arm_anchor_scores]) # print('*'*20) # for i,out in enumerate(output): # print(i,out.get_shape().as_list()) # print('*'*20) return output
def clone_fn(batch_queue): """Allows data parallelism by creating multiple clones of network_fn.""" # Dequeue batch. b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # Construct SSD network. arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) # Add loss function. ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores, match_threshold=FLAGS.match_threshold, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, label_smoothing=FLAGS.label_smoothing) return end_points
def clone_fn(batch_queue): """Allows data parallelism by creating multiple clones of network_fn.""" # Dequeue batch. ''' ssd的核心代码在这一块,我们可以看到 1)编码真实的标签,相当于y_label:gclasses, glocalisations, gscores = \ ssd_net.bboxes_encode(glabels, gbboxes, ssd_anchors) 2) b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) 3)得到输出,相当于得到y_pred: predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) 4)计算损失: predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) ''' b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # Construct SSD network. arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) # Add loss function. ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores, match_threshold=FLAGS.match_threshold, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, label_smoothing=FLAGS.label_smoothing) return end_points
def main(_): if not FLAGS.dataset_dir: raise ValueError( 'You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.DEBUG) with tf.Graph().as_default(): deploy_config = model_deploy.DeploymentConfig( num_clones=FLAGS.num_clones, clone_on_cpu=FLAGS.clone_on_cpu, replica_id=0, num_replicas=1, num_ps_tasks=0) with tf.device(deploy_config.variables_device()): global_step = slim.create_global_step() dataset = dataset_factory.get_dataset(FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) ssd_class = nets_factory.get_network(FLAGS.model_name) ssd_params = ssd_class.default_params._replace( num_classes=FLAGS.num_classes) ssd_net = ssd_class(ssd_params) ssd_shape = ssd_net.params.img_shape ssd_anchors = ssd_net.anchors(ssd_shape) preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) tf_utils.print_configuration(FLAGS.__flags, ssd_params, dataset.data_sources, FLAGS.train_dir) with tf.device(deploy_config.inputs_device()): with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=20 * FLAGS.batch_size, common_queue_min=10 * FLAGS.batch_size, shuffle=True) [image, shape, glabels, gbboxes] = provider.get( ['image', 'shape', 'object/label', 'object/bbox']) image, glabels, gbboxes = \ image_preprocessing_fn(image, glabels, gbboxes, out_shape=ssd_shape, data_format=DATA_FORMAT) gclasses, glocalisations, gscores = \ ssd_net.bboxes_encode(glabels, gbboxes, ssd_anchors) batch_shape = [1] + [len(ssd_anchors)] * 3 r = tf.train.batch(tf_utils.reshape_list( [image, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(r, batch_shape) batch_queue = slim.prefetch_queue.prefetch_queue( tf_utils.reshape_list( [b_image, b_gclasses, b_glocalisations, b_gscores]), capacity=2 * deploy_config.num_clones) def clone_fn(batch_queue): """Allows data parallelism by creating multiple clones of network_fn.""" b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores, match_threshold=FLAGS.match_threshold, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, label_smoothing=FLAGS.label_smoothing) return end_points summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) clones = model_deploy.create_clones(deploy_config, clone_fn, [batch_queue]) first_clone_scope = deploy_config.clone_scope(0) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, first_clone_scope) end_points = clones[0].outputs for end_point in end_points: x = end_points[end_point] summaries.add(tf.summary.histogram('activations/' + end_point, x)) summaries.add( tf.summary.scalar('sparsity/' + end_point, tf.nn.zero_fraction(x))) for loss in tf.get_collection(tf.GraphKeys.LOSSES, first_clone_scope): summaries.add(tf.summary.scalar(loss.op.name, loss)) for loss in tf.get_collection('EXTRA_LOSSES', first_clone_scope): summaries.add(tf.summary.scalar(loss.op.name, loss)) for variable in slim.get_model_variables(): summaries.add(tf.summary.histogram(variable.op.name, variable)) if FLAGS.moving_average_decay: moving_average_variables = slim.get_model_variables() variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, global_step) else: moving_average_variables, variable_averages = None, None with tf.device(deploy_config.optimizer_device()): learning_rate = tf_utils.configure_learning_rate( FLAGS, dataset.num_samples, global_step) optimizer = tf_utils.configure_optimizer(FLAGS, learning_rate) summaries.add(tf.summary.scalar('learning_rate', learning_rate)) if FLAGS.moving_average_decay: update_ops.append( variable_averages.apply(moving_average_variables)) variables_to_train = tf_utils.get_variables_to_train(FLAGS) total_loss, clones_gradients = model_deploy.optimize_clones( clones, optimizer, var_list=variables_to_train) summaries.add(tf.summary.scalar('total_loss', total_loss)) grad_updates = optimizer.apply_gradients(clones_gradients, global_step=global_step) update_ops.append(grad_updates) update_op = tf.group(*update_ops) train_tensor = control_flow_ops.with_dependencies([update_op], total_loss, name='train_op') summaries |= set( tf.get_collection(tf.GraphKeys.SUMMARIES, first_clone_scope)) summary_op = tf.summary.merge(list(summaries), name='summary_op') gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=1.0, write_version=2, pad_step_number=False) slim.learning.train(train_tensor, logdir=FLAGS.train_dir, master='', is_chief=True, init_fn=tf_utils.get_init_fn(FLAGS), summary_op=summary_op, number_of_steps=FLAGS.max_number_of_steps, log_every_n_steps=FLAGS.log_every_n_steps, save_summaries_secs=FLAGS.save_summaries_secs, saver=saver, save_interval_secs=FLAGS.save_interval_secs, session_config=config, sync_optimizer=None)
def main(_): if not FLAGS.dataset_dir: raise ValueError('You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): tf_global_step = slim.get_or_create_global_step() # =================================================================== # # Dataset + SSD model + Pre-processing # =================================================================== # dataset = dataset_factory.get_dataset( FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) # Get the SSD network and its anchors. ssd_class = nets_factory.get_network(FLAGS.model_name) ssd_params = ssd_class.default_params._replace(num_classes=FLAGS.num_classes) ssd_net = ssd_class(ssd_params) # Evaluation shape and associated anchors. # eval_image_size ssd_shape = ssd_net.params.img_shape ssd_anchors = ssd_net.anchors(ssd_shape) # Select the preprocessing function. preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=False) tf_utils.print_configuration(FLAGS.__flags, ssd_params, dataset.data_sources, FLAGS.eval_dir) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # with tf.device('/cpu:0'): with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, common_queue_capacity=2 * FLAGS.batch_size, common_queue_min=FLAGS.batch_size, shuffle=False) # Get for SSD network: image, labels, bboxes. [image, shape, glabels, gbboxes] = provider.get(['image', 'shape', 'object/label', 'object/bbox']) if FLAGS.remove_difficult: [gdifficult] = provider.get(['object/difficult']) else: gdifficult = None # Pre-processing image, labels and bboxes. image, glabels, gbboxes, gbbox_img = \ image_preprocessing_fn(image, glabels, gbboxes, out_shape=ssd_shape, data_format=DATA_FORMAT, resize=FLAGS.eval_resize, difficults=gdifficult) # Encode groundtruth labels and bboxes. gclasses, glocalisations, gscores = \ ssd_net.bboxes_encode(glabels, gbboxes, ssd_anchors) batch_shape = [1] * 4 + [len(ssd_anchors)] * 3 # Evaluation batch. r = tf.train.batch( tf_utils.reshape_list([image, glabels, gbboxes, gbbox_img, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size, dynamic_pad=True) (b_image, b_glabels, b_gbboxes, b_gbbox_img, b_gclasses, b_glocalisations, b_gscores) = tf_utils.reshape_list(r, batch_shape) # =================================================================== # # SSD Network + Ouputs decoding. # =================================================================== # dict_metrics = {} arg_scope = ssd_net.arg_scope(data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=False) # Add losses functions. ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores) # Performing post-processing on CPU: loop-intensive, usually more efficient. with tf.device('/cpu:0'): # Detected objects from SSD output. localisations = ssd_net.bboxes_decode(localisations, ssd_anchors) rclasses, rscores, rbboxes = \ ssd_net.detected_bboxes(predictions, localisations, select_threshold=FLAGS.select_threshold, nms_threshold=FLAGS.nms_threshold, clipping_bbox=b_gbbox_img, top_k=FLAGS.select_top_k) # Compute TP and FP statistics. n_gbboxes, tp_tensor, fp_tensor = \ tfe.bboxes_matching_batch(rclasses, rscores, rbboxes, b_glabels, b_gbboxes, matching_threshold=FLAGS.matching_threshold) # Variables to restore: moving avg. or normal weights. if FLAGS.moving_average_decay: variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, tf_global_step) variables_to_restore = variable_averages.variables_to_restore( slim.get_model_variables()) variables_to_restore[tf_global_step.op.name] = tf_global_step else: variables_to_restore = slim.get_variables_to_restore() # =================================================================== # # Evaluation metrics. # =================================================================== # with tf.device('/cpu:0'): dict_metrics = {} # First add all losses. for loss in tf.get_collection(tf.GraphKeys.LOSSES): dict_metrics[loss.op.name] = slim.metrics.streaming_mean(loss) # Extra losses as well. for loss in tf.get_collection('EXTRA_LOSSES'): dict_metrics[loss.op.name] = slim.metrics.streaming_mean(loss) # Add metrics to summaries and Print on screen. for name, metric in dict_metrics.items(): # summary_name = 'eval/%s' % name summary_name = name op = tf.summary.scalar(summary_name, metric[0], collections=[]) # op = tf.Print(op, [metric[0]], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # Precision / recall arrays metrics. dict_metrics['precision_recall'] = \ tfe.streaming_precision_recall_arrays(n_gbboxes, rclasses, rscores, tp_tensor, fp_tensor) # Add to summaries precision/recall values. metric_val = dict_metrics['precision_recall'][0] l_precisions = tfe.precision_recall_values(LIST_RECALLS, metric_val[0], metric_val[1]) for i, v in enumerate(l_precisions): summary_name = 'eval/precision_at_recall_%.2f' % LIST_RECALLS[i] op = tf.summary.scalar(summary_name, v, collections=[]) op = tf.Print(op, [v], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # Compute Average Precision (Pascal 2012). ap = tfe.average_precision_voc12(metric_val[0], metric_val[1]) summary_name = 'eval/average_precision_voc12' op = tf.summary.scalar(summary_name, ap, collections=[]) op = tf.Print(op, [ap], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # Compute Average Precision (Pascal 2007). ap = tfe.average_precision_voc07(metric_val[0], metric_val[1]) summary_name = 'eval/average_precision_voc07' op = tf.summary.scalar(summary_name, ap, collections=[]) op = tf.Print(op, [ap], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # Split into values and updates ops. names_to_values, names_to_updates = slim.metrics.aggregate_metric_map(dict_metrics) # =================================================================== # # Evaluation loop. # =================================================================== # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) # Number of batches... if FLAGS.max_num_batches: num_batches = FLAGS.max_num_batches else: num_batches = math.ceil(dataset.num_samples / float(FLAGS.batch_size)) if tf.gfile.IsDirectory(FLAGS.checkpoint_path): checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path) else: checkpoint_path = FLAGS.checkpoint_path start = time.clock() tf.logging.info('Evaluating %s' % checkpoint_path) slim.evaluation.evaluate_once( master=FLAGS.master, checkpoint_path=checkpoint_path, logdir=FLAGS.eval_dir, num_evals=num_batches, eval_op=list(names_to_updates.values()), variables_to_restore=variables_to_restore, session_config=config) # Log time spent. elapsed = time.clock() elapsed = elapsed - start print('Time spent : %.3f seconds.' % elapsed) print('Time spent per BATCH: %.3f seconds.' % (elapsed / num_batches))
def main(_): if not FLAGS.data_dir: raise ValueError('You must supply the dataset directory with --data_dir') num_gpus = FLAGS.num_gpus if num_gpus < 1: num_gpus = 1 # ps_spec = FLAGS.ps_hosts.split(",") # worker_spec = FLAGS.worker_hosts.split(",") # num_workers = len(worker_spec) # cluster = tf.train.ClusterSpec({ # "ps": ps_spec, # "worker": worker_spec}) # server = tf.train.Server(cluster, job_name=FLAGS.job_name, task_index=FLAGS.task_index) # if FLAGS.job_name == "ps": # with tf.device("/cpu:0"): # server.join() # return tf.logging.set_verbosity(tf.logging.DEBUG) with tf.device('/cpu:0'): global_step = slim.create_global_step() # Select the dataset. dataset = dataset_factory.get_dataset( FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.data_dir) # Get the RON network and its anchors. ron_class = nets_factory.get_network(FLAGS.model_name) ron_params = ron_class.default_params._replace(num_classes=FLAGS.num_classes) ron_net = ron_class(ron_params) ron_shape = ron_net.params.img_shape ron_anchors = ron_net.anchors(ron_shape) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=120 * FLAGS.batch_size * num_gpus, common_queue_min=80 * FLAGS.batch_size * num_gpus, shuffle=True) # Get for RON network: image, labels, bboxes. # (ymin, xmin, ymax, xmax) fro gbboxes [image, shape, glabels, gbboxes, isdifficult] = provider.get(['image', 'shape', 'object/label', 'object/bbox', 'object/difficult']) isdifficult_mask =tf.cond(tf.reduce_sum(tf.cast(tf.logical_not(tf.equal(tf.ones_like(isdifficult), isdifficult)), tf.float32)) < 1., lambda : tf.one_hot(0, tf.shape(isdifficult)[0], on_value=True, off_value=False, dtype=tf.bool), lambda : isdifficult < tf.ones_like(isdifficult)) glabels = tf.boolean_mask(glabels, isdifficult_mask) gbboxes = tf.boolean_mask(gbboxes, isdifficult_mask) # Select the preprocessing function. preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) # Pre-processing image, labels and bboxes. image, glabels, gbboxes = image_preprocessing_fn(image, glabels, gbboxes, out_shape=ron_shape, data_format=DATA_FORMAT) # Encode groundtruth labels and bboxes. # glocalisations is our regression object # gclasses is the ground_trutuh label # gscores is the the jaccard score with ground_truth gclasses, glocalisations, gscores = \ ron_net.bboxes_encode(glabels, gbboxes, ron_anchors, positive_threshold=FLAGS.match_threshold, ignore_threshold=FLAGS.neg_threshold) # each size of the batch elements # include one image, three others(gclasses, glocalisations, gscores) batch_shape = [1] + [len(ron_anchors)] * 3 # Training batches and queue. r = tf.train.batch( tf_utils.reshape_list([image, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size * num_gpus, num_threads=FLAGS.num_preprocessing_threads, capacity=120 * FLAGS.batch_size * num_gpus) all_batch = tf_utils.reshape_list(r, batch_shape) b_image = tf.split(all_batch[0], num_or_size_splits=num_gpus, axis=0) _b_gclasses = [tf.split(b, num_or_size_splits=num_gpus, axis=0) for b in all_batch[1]] b_gclasses = [_ for _ in zip(*_b_gclasses)] _b_glocalisations = [tf.split(b, num_or_size_splits=num_gpus, axis=0) for b in all_batch[2]] b_glocalisations = [_ for _ in zip(*_b_glocalisations)] _b_gscores = [tf.split(b, num_or_size_splits=num_gpus, axis=0) for b in all_batch[3]] b_gscores = [_ for _ in zip(*_b_gscores)] # Gather initial summaries. summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) # =================================================================== # # Configure the optimization procedure. # =================================================================== # learning_rate = tf_utils.configure_learning_rate(FLAGS, dataset.num_samples, global_step) optimizer = tf_utils.configure_optimizer(FLAGS, learning_rate) summaries.add(tf.summary.scalar('learning_rate', learning_rate)) # Construct RON network. arg_scope = ron_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) reuse_variables = False tower_grads = [] loss_list = [] with slim.arg_scope(arg_scope): for index in range(num_gpus): with tf.device('/gpu:%d' % index): predictions, logits, objness_pred, objness_logits, localisations, end_points = ron_net.net(b_image[index], is_training=True, reuse = reuse_variables) # Add loss function. ron_net.losses(logits, localisations, objness_logits, objness_pred, b_gclasses[index], b_glocalisations[index], b_gscores[index], match_threshold = FLAGS.match_threshold, neg_threshold = FLAGS.neg_threshold, objness_threshold = FLAGS.objectness_thres, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, beta=FLAGS.loss_beta, label_smoothing=FLAGS.label_smoothing) reuse_variables = True # and returns a train_tensor and summary_op loss = tf.losses.get_total_loss() loss_list.append(loss) # Variables to train. variables_to_train = tf_utils.get_variables_to_train(FLAGS) # Create gradient updates. grads = optimizer.compute_gradients(loss, variables_to_train) tower_grads.append(grads) reduce_grads = average_gradients(tower_grads) total_loss = tf.reduce_mean(tf.stack(loss_list, axis=0), axis=0) # Add total_loss to summary. summaries.add(tf.summary.scalar('total_loss', total_loss)) # =================================================================== # # Configure the moving averages. # =================================================================== # if FLAGS.moving_average_decay: moving_average_variables = slim.get_model_variables() variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, global_step) else: moving_average_variables, variable_averages = None, None if FLAGS.moving_average_decay: # Update ops executed locally by trainer. update_ops.append(variable_averages.apply(moving_average_variables)) grad_updates = optimizer.apply_gradients(reduce_grads, global_step=global_step) update_ops.append(grad_updates) update_op = tf.group(*update_ops) train_tensor = control_flow_ops.with_dependencies([update_op], total_loss, name='train_op') # Merge all summaries together. summary_op = tf.summary.merge(list(summaries), name='summary_op') # =================================================================== # # Kicks off the training. # =================================================================== # config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours = FLAGS.save_interval_secs/3600., write_version=2, pad_step_number=False) slim.learning.train( train_tensor, logdir=FLAGS.model_dir, master='', is_chief=True, init_fn=tf_utils.get_init_fn(FLAGS, os.path.join(FLAGS.data_dir, 'vgg_16.ckpt')), summary_op=summary_op, number_of_steps=FLAGS.max_number_of_steps, log_every_n_steps=FLAGS.log_every_n_steps, save_summaries_secs=FLAGS.save_summaries_secs, saver=saver, save_interval_secs=FLAGS.save_interval_secs, session_config=config, session_wrapper=None, sync_optimizer=None)
def main(_): if not FLAGS.dataset_dir: raise ValueError('You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.DEBUG) with tf.Graph().as_default(): # Config model_deploy. Keep TF Slim Models structure. # Useful if want to need multiple GPUs and/or servers in the future. deploy_config = model_deploy.DeploymentConfig( num_clones=FLAGS.num_clones, clone_on_cpu=FLAGS.clone_on_cpu, replica_id=0, num_replicas=1, num_ps_tasks=0) # Create global_step. with tf.device(deploy_config.variables_device()): global_step = slim.create_global_step() # Select the dataset. dataset = dataset_factory.get_dataset( FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) # Get the SSD network and its anchors. ssd_class = nets_factory.get_network(FLAGS.model_name) ssd_params = ssd_class.default_params._replace(num_classes=FLAGS.num_classes) ssd_net = ssd_class(ssd_params) ssd_shape = ssd_net.params.img_shape ssd_anchors = ssd_net.anchors(ssd_shape) # Select the preprocessing function. preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) tf_utils.print_configuration(FLAGS.__flags, ssd_params, dataset.data_sources, FLAGS.train_dir) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # with tf.device(deploy_config.inputs_device()): with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=20 * FLAGS.batch_size, common_queue_min=10 * FLAGS.batch_size, shuffle=True) # Get for SSD network: image, labels, bboxes. [image, shape, glabels, gbboxes] = provider.get(['image', 'shape', 'object/label', 'object/bbox']) # Pre-processing image, labels and bboxes. image, glabels, gbboxes = \ image_preprocessing_fn(image, glabels, gbboxes, out_shape=ssd_shape, data_format=DATA_FORMAT) # Encode groundtruth labels and bboxes. gclasses, glocalisations, gscores = \ ssd_net.bboxes_encode(glabels, gbboxes, ssd_anchors) batch_shape = [1] + [len(ssd_anchors)] * 3 # Training batches and queue. r = tf.train.batch( tf_utils.reshape_list([image, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(r, batch_shape) # Intermediate queueing: unique batch computation pipeline for all # GPUs running the training. batch_queue = slim.prefetch_queue.prefetch_queue( tf_utils.reshape_list([b_image, b_gclasses, b_glocalisations, b_gscores]), capacity=2 * deploy_config.num_clones) # =================================================================== # # Define the model running on every GPU. # =================================================================== # def clone_fn(batch_queue): """Allows data parallelism by creating multiple clones of network_fn.""" # Dequeue batch. b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # Construct SSD network. arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) # Add loss function. ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores, match_threshold=FLAGS.match_threshold, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, label_smoothing=FLAGS.label_smoothing) return end_points # Gather initial summaries. summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) # =================================================================== # # Add summaries from first clone. # =================================================================== # clones = model_deploy.create_clones(deploy_config, clone_fn, [batch_queue]) first_clone_scope = deploy_config.clone_scope(0) # Gather update_ops from the first clone. These contain, for example, # the updates for the batch_norm variables created by network_fn. update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, first_clone_scope) # Add summaries for end_points. end_points = clones[0].outputs for end_point in end_points: x = end_points[end_point] summaries.add(tf.summary.histogram('activations/' + end_point, x)) summaries.add(tf.summary.scalar('sparsity/' + end_point, tf.nn.zero_fraction(x))) # Add summaries for losses and extra losses. for loss in tf.get_collection(tf.GraphKeys.LOSSES, first_clone_scope): summaries.add(tf.summary.scalar(loss.op.name, loss)) for loss in tf.get_collection('EXTRA_LOSSES', first_clone_scope): summaries.add(tf.summary.scalar(loss.op.name, loss)) # Add summaries for variables. for variable in slim.get_model_variables(): summaries.add(tf.summary.histogram(variable.op.name, variable)) # =================================================================== # # Configure the moving averages. # =================================================================== # if FLAGS.moving_average_decay: moving_average_variables = slim.get_model_variables() variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, global_step) else: moving_average_variables, variable_averages = None, None # =================================================================== # # Configure the optimization procedure. # =================================================================== # with tf.device(deploy_config.optimizer_device()): learning_rate = tf_utils.configure_learning_rate(FLAGS, dataset.num_samples, global_step) optimizer = tf_utils.configure_optimizer(FLAGS, learning_rate) summaries.add(tf.summary.scalar('learning_rate', learning_rate)) if FLAGS.moving_average_decay: # Update ops executed locally by trainer. update_ops.append(variable_averages.apply(moving_average_variables)) # Variables to train. variables_to_train = tf_utils.get_variables_to_train(FLAGS) # and returns a train_tensor and summary_op total_loss, clones_gradients = model_deploy.optimize_clones( clones, optimizer, var_list=variables_to_train) # Add total_loss to summary. summaries.add(tf.summary.scalar('total_loss', total_loss)) # Create gradient updates. grad_updates = optimizer.apply_gradients(clones_gradients, global_step=global_step) update_ops.append(grad_updates) update_op = tf.group(*update_ops) train_tensor = control_flow_ops.with_dependencies([update_op], total_loss, name='train_op') # Add the summaries from the first clone. These contain the summaries summaries |= set(tf.get_collection(tf.GraphKeys.SUMMARIES, first_clone_scope)) # Merge all summaries together. summary_op = tf.summary.merge(list(summaries), name='summary_op') # =================================================================== # # Kicks off the training. # =================================================================== # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=1.0, write_version=2, pad_step_number=False) slim.learning.train( train_tensor, logdir=FLAGS.train_dir, master='', is_chief=True, init_fn=tf_utils.get_init_fn(FLAGS), summary_op=summary_op, number_of_steps=FLAGS.max_number_of_steps, log_every_n_steps=FLAGS.log_every_n_steps, save_summaries_secs=FLAGS.save_summaries_secs, saver=saver, save_interval_secs=FLAGS.save_interval_secs, session_config=config, sync_optimizer=None)
def main(_): if FLAGS.train_on_cpu: os.environ["CUDA_VISIBLE_DEVICES"] = "-1" else: os.environ["CUDA_VISIBLE_DEVICES"] = FLAGS.gpu_device if not FLAGS.dataset_dir: raise ValueError( "You must supply the dataset directory with --dataset-dir.") tf.logging.set_verbosity(tf.logging.DEBUG) g = tf.Graph() with g.as_default(): # select the dataset dataset = dataset_factory.get_dataset(FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) # create global step, used for optimizer moving average decay with tf.device("/cpu:0"): global_step = tf.train.create_global_step() # pdb.set_trace() # get the ssd network and its anchors ssd_cls = ssd.SSDnet ssd_params = ssd_cls.default_params._replace( num_classes=FLAGS.num_classes) ssd_net = ssd_cls(ssd_params) image_size = ssd_net.params.img_shape ssd_anchors = ssd_net.anchors(img_shape=image_size) # select the preprocessing function preprocessing_name = FLAGS.preprocessing_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) tf_utils.print_configuration(FLAGS.__flags, ssd_params, dataset.data_sources, FLAGS.train_dir) # create a dataset provider and batches. with tf.device("/cpu:0"): with tf.name_scope(FLAGS.dataset_name + "_data_provider"): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=20 * FLAGS.batch_size, common_queue_min=10 * FLAGS.batch_size, shuffle=True) # get for ssd network: image,labels,bboxes [image, shape, glabels, gbboxes] = provider.get( ["image", "shape", "object/label", "object/bbox"]) # pdb.set_trace() # preprocessing image,glabels,gbboxes = \ image_preprocessing_fn(image, glabels,gbboxes, out_shape=image_size, data_format="NHWC") # encode groundtruth labels and bboxes gclasses,glocalisations,gscores= \ ssd_net.bboxes_encode(glabels,gbboxes,ssd_anchors) batch_shape = [1] + [len(ssd_anchors)] * 3 # training batches and queue r = tf.train.batch(tf_utils.reshape_list( [image, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) b_image,b_gclasses,b_glocalisations,b_gscores = \ tf_utils.reshape_list(r,batch_shape) # prefetch queue batch_queue = slim.prefetch_queue.prefetch_queue( tf_utils.reshape_list( [b_image, b_gclasses, b_glocalisations, b_gscores]), capacity=8) # dequeue batch b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # gather initial summaries summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay) with slim.arg_scope(arg_scope): predictions,localisations,logits,end_points,mobilenet_var_list = \ ssd_net.net(b_image,is_training=True) # add loss function ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores, match_threshold=FLAGS.match_threshold, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, label_smoothing=FLAGS.label_smoothing) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) # add summaries for end_points for end_point in end_points: x = end_points[end_point] summaries.add(tf.summary.histogram("activations/" + end_point, x)) summaries.add( tf.summary.scalar("sparsity/" + end_point, tf.nn.zero_fraction(x))) # add summaries for losses and extra losses for loss in tf.get_collection(tf.GraphKeys.LOSSES): summaries.add(tf.summary.scalar(loss.op.name, loss)) for loss in tf.get_collection("EXTRA_LOSSES"): summaries.add(tf.summary.scalar(loss.op.name, loss)) # add summaries for variables for var in slim.get_model_variables(): summaries.add(tf.summary.histogram(var.op.name, var)) # configure the moving averages if FLAGS.moving_average_decay: # use moving average decay on weights variables moving_average_variables = slim.get_model_variables() variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, global_step) else: moving_average_variables, variable_averages = None, None # configure the optimization procedure with tf.device("/cpu:0"): learning_rate = tf_utils.configure_learning_rate( FLAGS, dataset.num_samples, global_step) optimizer = tf_utils.configure_optimizer(FLAGS, learning_rate) summaries.add(tf.summary.scalar("learning_rate", learning_rate)) if FLAGS.moving_average_decay: # update ops executed by trainer update_ops.append( variable_averages.apply(moving_average_variables)) # get variables to train variables_to_train = tf_utils.get_variables_to_train(FLAGS) # return a train tensor and summary op total_losses = tf.get_collection(tf.GraphKeys.LOSSES) total_loss = tf.add_n(total_losses, name="total_loss") summaries.add(tf.summary.scalar("total_loss", total_loss)) # create gradient updates grads = optimizer.compute_gradients(total_loss, var_list=variables_to_train) grad_updates = optimizer.apply_gradients(grads, global_step=global_step) update_ops.append(grad_updates) # create train op update_op = tf.group(*update_ops) train_tensor = control_flow_ops.with_dependencies([update_op], total_loss, name="train_op") # merge all summaries together summary_op = tf.summary.merge(list(summaries), name="summary_op") # start training gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction, allow_growth=FLAGS.allow_growth) config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) saver = tf.train.Saver(max_to_keep=2, keep_checkpoint_every_n_hours=1.0, write_version=2, pad_step_number=False) # create initial assignment op init_assign_op, init_feed_dict = slim.assign_from_checkpoint( FLAGS.checkpoint_path, mobilenet_var_list, ignore_missing_vars=FLAGS.ignore_missing_vars) # create an initial assignment function for k, v in init_feed_dict.items(): if "global_step" in k.name: g_step = k init_feed_dict[g_step] = 0 # change the global_step to zero. init_fn = lambda sess: sess.run(init_assign_op, init_feed_dict) # run training slim.learning.train( train_tensor, logdir=FLAGS.train_dir, init_fn=init_fn, summary_op=summary_op, number_of_steps=FLAGS.max_number_of_steps, save_summaries_secs=FLAGS.save_summaries_secs, save_interval_secs=FLAGS.save_interval_secs, session_config=config, saver=saver, )
def main(_): if not FLAGS.dataset_dir: raise ValueError( 'You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.DEBUG) with tf.Graph().as_default(): ###################### # Config model_deploy# ###################### deploy_config = model_deploy.DeploymentConfig( num_clones=FLAGS.num_clones, clone_on_cpu=FLAGS.clone_on_cpu, replica_id=FLAGS.task, num_replicas=FLAGS.worker_replicas, num_ps_tasks=FLAGS.num_ps_tasks) # Create global_step with tf.device(deploy_config.variables_device()): global_step = slim.create_global_step() network_fn = nets_factory.get_network(FLAGS.model_name) params = network_fn.default_params params = params._replace(match_threshold=FLAGS.match_threshold) # initalize the net net = network_fn(params) out_shape = net.params.img_shape anchors = net.anchors(out_shape) # create batch dataset with tf.device(deploy_config.inputs_device()): b_image, b_glocalisations, b_gscores = \ load_batch.get_batch(FLAGS.dataset_dir, FLAGS.num_readers, FLAGS.batch_size, out_shape, net, anchors, FLAGS, file_pattern = FLAGS.file_pattern, is_training = True, shuffe = FLAGS.shuffle_data) allgscores = [] allglocalization = [] for i in range(len(anchors)): allgscores.append(tf.reshape(b_gscores[i], [-1])) allglocalization.append( tf.reshape(b_glocalisations[i], [-1, 4])) b_gscores = tf.concat(allgscores, 0) b_glocalisations = tf.concat(allglocalization, 0) batch_queue = slim.prefetch_queue.prefetch_queue( tf_utils.reshape_list([b_image, b_glocalisations, b_gscores]), num_threads=8, capacity=16 * deploy_config.num_clones) # =================================================================== # # Define the model running on every GPU. # =================================================================== # def clone_fn(batch_queue): #Allows data parallelism by creating multiple #clones of network_fn. # Dequeue batch. batch_shape = [1] * 3 b_image, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # Construct SSD network. arg_scope = net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=FLAGS.data_format) with slim.arg_scope(arg_scope): localisations, logits, end_points = \ net.net(b_image, is_training=True, use_batch=FLAGS.use_batch) # Add loss function. net.losses(logits, localisations, b_glocalisations, b_gscores, negative_ratio=FLAGS.negative_ratio, use_hard_neg=FLAGS.use_hard_neg, alpha=FLAGS.loss_alpha, label_smoothing=FLAGS.label_smoothing) return end_points summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) clones = model_deploy.create_clones(deploy_config, clone_fn, [batch_queue]) first_clone_scope = deploy_config.clone_scope(0) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, first_clone_scope) end_points = clones[0].outputs for end_point in end_points: x = end_points[end_point] summaries.add(tf.summary.histogram('activations/' + end_point, x)) for loss in tf.get_collection('EXTRA_LOSSES', first_clone_scope): summaries.add(tf.summary.scalar(loss.op.name, loss)) for variable in slim.get_model_variables(): summaries.add(tf.summary.histogram(variable.op.name, variable)) ################################# # Configure the moving averages # ################################# if FLAGS.moving_average_decay: moving_average_variables = slim.get_model_variables() variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, global_step) else: moving_average_variables, variable_averages = None, None ######################################### # Configure the optimization procedure. # ######################################### with tf.device(deploy_config.optimizer_device()): learning_rate = tf_utils.configure_learning_rate( FLAGS, FLAGS.num_samples, global_step) optimizer = tf_utils.configure_optimizer(FLAGS, learning_rate) summaries.add(tf.summary.scalar('learning_rate', learning_rate)) if FLAGS.fine_tune: gradient_multipliers = pickle.load( open('nets/multiplier_300.pkl', 'rb')) else: gradient_multipliers = None if FLAGS.moving_average_decay: # Update ops executed locally by trainer. update_ops.append( variable_averages.apply(moving_average_variables)) # Variables to train. variables_to_train = tf_utils.get_variables_to_train(FLAGS) # and returns a train_tensor and summary_op total_loss, clones_gradients = model_deploy.optimize_clones( clones, optimizer, var_list=variables_to_train) # Add total_loss to summary. summaries.add(tf.summary.scalar('total_loss', total_loss)) # Create gradient updates. grad_updates = optimizer.apply_gradients(clones_gradients, global_step=global_step) update_ops.append(grad_updates) update_op = tf.group(*update_ops) train_tensor = control_flow_ops.with_dependencies([update_op], total_loss, name='train_op') #train_tensor = slim.learning.create_train_op(total_loss, optimizer, gradient_multipliers=gradient_multipliers) # Add the summaries from the first clone. These contain the summaries # created by model_fn and either optimize_clones() or _gather_clone_loss(). summaries |= set( tf.get_collection(tf.GraphKeys.SUMMARIES, first_clone_scope)) # Merge all summaries together. summary_op = tf.summary.merge(list(summaries), name='summary_op') # =================================================================== # # Kicks off the training. # =================================================================== # gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction, allocator_type="BFC") config = tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False, allow_soft_placement=True, inter_op_parallelism_threads=0, intra_op_parallelism_threads=1, ) saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=1.0, write_version=2, pad_step_number=False) slim.learning.train(train_tensor, logdir=FLAGS.train_dir, master='', is_chief=True, init_fn=tf_utils.get_init_fn(FLAGS), summary_op=summary_op, number_of_steps=FLAGS.max_number_of_steps, log_every_n_steps=FLAGS.log_every_n_steps, save_summaries_secs=FLAGS.save_summaries_secs, saver=saver, save_interval_secs=FLAGS.save_interval_secs, session_config=config, sync_optimizer=None)
def get_batch(dataset_dir, num_readers, batch_size, out_shape, net, anchors, FLAGS, file_pattern='*.tfrecord', is_training=True, shuffe=False): dataset = sythtextprovider.get_datasets(dataset_dir, file_pattern=file_pattern) provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=num_readers, common_queue_capacity=512 * 16 + 20 * batch_size, common_queue_min=512 * 16, shuffle=shuffe) [image, shape, glabels, gbboxes, height, width] = provider.get( ['image', 'shape', 'object/label', 'object/bbox', 'height', 'width']) if is_training: image, glabels, gbboxes,num = \ txt_preprocessing.preprocess_image(image, glabels, gbboxes, height, width, out_shape,use_whiten=FLAGS.use_whiten,is_training=is_training) glocalisations, gscores = \ net.bboxes_encode( gbboxes, anchors, num) batch_shape = [1] + [len(anchors)] * 2 r = tf.train.shuffle_batch(tf_utils.reshape_list( [image, glocalisations, gscores]), batch_size=batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=100 * batch_size, min_after_dequeue=50 * batch_size) b_image, b_glocalisations, b_gscores= \ tf_utils.reshape_list(r, batch_shape) return b_image, b_glocalisations, b_gscores else: image, glabels, gbboxes,bbox_img, num = \ txt_preprocessing.preprocess_image(image, glabels,gbboxes, height,width, out_shape,use_whiten=FLAGS.use_whiten,is_training=is_training) glocalisations, gscores = \ net.bboxes_encode( gbboxes, anchors, num) batch_shape = [1] * 4 + [len(anchors)] * 2 r = tf.train.batch(tf_utils.reshape_list( [image, glabels, gbboxes, bbox_img, glocalisations, gscores]), batch_size=batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=50 * batch_size, dynamic_pad=True) image, glabels, gbboxes,g_bbox_img,glocalisations, gscores = \ tf_utils.reshape_list(r, batch_shape) return image, glabels, gbboxes, g_bbox_img, glocalisations, gscores
def main(_): if not FLAGS.dataset_dir: raise ValueError( 'You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.DEBUG) with tf.Graph().as_default(): # Config model_deploy. Keep TF Slim Models structure. # Useful if want to need multiple GPUs and/or servers in the future. deploy_config = model_deploy.DeploymentConfig( num_clones=FLAGS.num_clones, clone_on_cpu=FLAGS.clone_on_cpu, replica_id=0, num_replicas=1, num_ps_tasks=0) # Create global_step. with tf.device(deploy_config.variables_device()): global_step = slim.create_global_step() # Select the dataset. dataset = dataset_factory.get_dataset(FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) # Get the SSD network and its anchors. ssd_class = nets_factory.get_network(FLAGS.model_name) ssd_params = ssd_class.default_params._replace( num_classes=FLAGS.num_classes) ssd_net = ssd_class(ssd_params) ssd_shape = ssd_net.params.img_shape ssd_anchors = ssd_net.anchors(ssd_shape) # Select the preprocessing function. preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) tf_utils.print_configuration(FLAGS.__flags, ssd_params, dataset.data_sources, FLAGS.train_dir) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # with tf.device(deploy_config.inputs_device()): with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=20 * FLAGS.batch_size, common_queue_min=10 * FLAGS.batch_size, shuffle=True) # Get for SSD network: image, labels, bboxes. [image, shape, glabels, gbboxes] = provider.get( ['image', 'shape', 'object/label', 'object/bbox']) # Pre-processing image, labels and bboxes. image, glabels, gbboxes = \ image_preprocessing_fn(image, glabels, gbboxes, out_shape=ssd_shape, data_format=DATA_FORMAT) # Encode groundtruth labels and bboxes. gclasses, glocalisations, gscores = \ ssd_net.bboxes_encode(glabels, gbboxes, ssd_anchors) batch_shape = [1] + [len(ssd_anchors)] * 3 # # Training batches and queue. r = tf.train.batch(tf_utils.reshape_list( [image, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(r, batch_shape) # Intermediate queueing: unique batch computation pipeline for all # GPUs running the training. batch_queue = slim.prefetch_queue.prefetch_queue( tf_utils.reshape_list( [b_image, b_gclasses, b_glocalisations, b_gscores]), capacity=2 * deploy_config.num_clones) # # # =================================================================== # # # Define the model running on every GPU. # # =================================================================== # def clone_fn(batch_queue): """Allows data parallelism by creating multiple clones of network_fn.""" # Dequeue batch. b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # Construct SSD network. arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) # # Add loss function. ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores, match_threshold=FLAGS.match_threshold, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, label_smoothing=FLAGS.label_smoothing) return end_points
def main(_): if not FLAGS.dataset_dir: raise ValueError( 'You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.DEBUG) with tf.Graph().as_default(): # Create global_step. # with tf.device('/gpu:0'): global_step = slim.create_global_step() # ckpt = tf.train.get_checkpoint_state(os.path.dirname('./logs/checkpoint')) #os.path.dirname('./logs/') ckpt_filename = os.path.dirname( './logs/') + '/mobilenet_v1_1.0_224.ckpt' sess = tf.InteractiveSession() # Select the dataset. dataset = dataset_factory.get_dataset(FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) dataset_kitti = dataset_factory.get_dataset('kitti', FLAGS.dataset_split_name, FLAGS.dataset_dir) # Get the SSD network and its anchors. ssd_class = nets_factory.get_network(FLAGS.model_name) ssd_params = ssd_class.default_params._replace( num_classes=FLAGS.num_classes) ssd_net = ssd_class(ssd_params) ssd_shape = ssd_net.params.img_shape ssd_anchors = ssd_net.anchors(ssd_shape) preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=20 * FLAGS.batch_size, common_queue_min=10 * FLAGS.batch_size, shuffle=True) [image, shape, glabels, gbboxes ] = provider.get(['image', 'shape', 'object/label', 'object/bbox']) image, glabels, gbboxes = \ image_preprocessing_fn(image, glabels, gbboxes, out_shape = ssd_shape, data_format = DATA_FORMAT) gclasses, glocalisations, gscores = \ ssd_net.bboxes_encode(glabels, gbboxes, ssd_anchors) batch_shape = [1] + [len(ssd_anchors)] * 3 r = tf.train.batch(tf_utils.reshape_list( [image, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(r, batch_shape) summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) summaries.add(tf.summary.image("imgs", tf.cast(b_image, tf.float32))) f_i = 0 for gt_map in b_gscores: gt_features = tf.reduce_max(gt_map, axis=3) gt_features = tf.expand_dims(gt_features, -1) summaries.add( tf.summary.image("gt_map_%d" % f_i, tf.cast(gt_features, tf.float32))) f_i += 1 # for festures in gt_list: # summaries.add(tf.summary.image("gt_map_%d" % f_i, tf.cast(festures, tf.float32))) # f_i += 1 arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) f_i = 0 for predict_map in predictions: predict_map = predict_map[:, :, :, :, 1:] predict_map = tf.reduce_max(predict_map, axis=4) predict_map = tf.reduce_max(predict_map, axis=3) predict_map = tf.expand_dims(predict_map, -1) summaries.add( tf.summary.image("predicte_map_%d" % f_i, tf.cast(predict_map, tf.float32))) f_i += 1 ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores, 0, match_threshold=FLAGS.match_threshold, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, label_smoothing=FLAGS.label_smoothing) # with tf.name_scope('kitti' + '_data_provider'): # provider_k = slim.dataset_data_provider.DatasetDataProvider( # dataset_kitti, # num_readers = FLAGS.num_readers, # common_queue_capacity = 20 * FLAGS.batch_size, # common_queue_min = 10 * FLAGS.batch_size, # shuffle = True # ) # [image_k, shape_k, glabels_k, gbboxes_k] = provider_k.get(['image', 'shape', 'object/label', 'object/bbox']) # # image_preprocessing_fn_k = preprocessing_factory.get_preprocessing('kitti', is_training=True) # image_k, glabels_k, gbboxes_k = \ # image_preprocessing_fn_k(image_k, glabels_k, gbboxes_k, out_shape = ssd_shape, data_format = DATA_FORMAT) # # gclasses_k, glocalisations_k, gscores_k = \ # ssd_net.bboxes_encode(glabels_k, gbboxes_k, ssd_anchors) # #batch_shape = [1] + [len(ssd_anchors)] * 3 # # r_k = tf.train.batch( # tf_utils.reshape_list([image_k, gclasses_k, glocalisations_k, gscores_k]), # batch_size=FLAGS.batch_size, # num_threads=FLAGS.num_preprocessing_threads, # capacity= 5 * FLAGS.batch_size # ) # # b_image_k, b_gclasses_k, b_glocalisations_k, b_gscores_k = \ # tf_utils.reshape_list(r_k, batch_shape) # # summaries.add(tf.summary.image("k_imgs", tf.cast(b_image_k, tf.float32))) # # f_i = 0 # for gt_map in b_gscores_k: # gt_features = tf.reduce_max(gt_map, axis=3) # gt_features = tf.expand_dims(gt_features, -1) # summaries.add(tf.summary.image("k_gt_map_%d" % f_i, tf.cast(gt_features, tf.float32))) # f_i += 1 # # for festures in gt_list: # # summaries.add(tf.summary.image("gt_map_%d" % f_i, tf.cast(festures, tf.float32))) # # f_i += 1 # # arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) # with slim.arg_scope(arg_scope): # predictions_k, localisations_k, logits_k, end_points_k = \ # ssd_net.net(b_image_k, is_training=True, reuse=True) # # f_i = 0 # for predict_map in predictions_k: # predict_map = predict_map[:, :, :, :, 1:] # predict_map = tf.reduce_max(predict_map, axis=4) # predict_map = tf.reduce_max(predict_map, axis=3) # predict_map = tf.expand_dims(predict_map, -1) # summaries.add(tf.summary.image("k_predicte_map_%d" % f_i, tf.cast(predict_map, tf.float32))) # f_i += 1 # # ssd_net.losses(logits_k, localisations_k, b_gclasses_k, b_glocalisations_k, b_gscores_k, 2, # match_threshold=FLAGS.match_threshold, # negative_ratio=FLAGS.negative_ratio, # alpha=FLAGS.loss_alpha, # label_smoothing=FLAGS.label_smoothing) #total_loss = slim.losses.get_total_loss() total_loss = tf.losses.get_total_loss() summaries.add(tf.summary.scalar('loss', total_loss)) for loss in tf.get_collection(tf.GraphKeys.LOSSES): summaries.add(tf.summary.scalar(loss.op.name, loss)) # for variable in slim.get_model_variables(): # summaries.add(tf.summary.histogram(variable.op.name, variable)) for variable in tf.trainable_variables(): summaries.add(tf.summary.histogram(variable.op.name, variable)) learning_rate = tf_utils.configure_learning_rate( FLAGS, dataset.num_samples, global_step) optimizer = tf_utils.configure_optimizer(FLAGS, learning_rate) # optimizer = tf.train.AdamOptimizer(learning_rate, beta1=FLAGS.adam_beta1, # beta2=FLAGS.adam_beta2, epsilon=FLAGS.opt_epsilon) #optimizer = tf.train.GradientDescentOptimizer(learning_rate) summaries.add(tf.summary.scalar('learning_rate', learning_rate)) extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(extra_update_ops): train_op = slim.learning.create_train_op(total_loss, optimizer, summarize_gradients=False) summary_op = tf.summary.merge(list(summaries), name='summary_op') train_writer = tf.summary.FileWriter('./logs/', sess.graph) gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) #variables_to_exclude = slim.get_variables_by_suffix("Adam") variables_to_restore = slim.get_variables_to_restore( exclude=["MobilenetV1/Logits", "MobilenetV1/Box", "global_step"]) restorer = tf.train.Saver(variables_to_restore) saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=1.0, write_version=2, pad_step_number=False) sess.run(tf.global_variables_initializer()) restorer.restore(sess, ckpt_filename) # if ckpt and ckpt.model_checkpoint_path: # saver.restore(sess, ckpt.model_checkpoint_path) i = 0 with slim.queues.QueueRunners(sess): while (i < FLAGS.max_number_of_steps): _, summary_str = sess.run([train_op, summary_op]) if i % 50 == 0: global_step_str = global_step.eval() print('%diteraton' % (global_step_str)) train_writer.add_summary(summary_str, global_step_str) if i % 100 == 0: global_step_str = global_step.eval() saver.save(sess, "./logs/", global_step=global_step_str) i += 1
def main(_): if not FLAGS.dataset_dir: raise ValueError( 'You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.DEBUG) with tf.Graph().as_default(): # Create global_step. global_step = slim.create_global_step() sess = tf.InteractiveSession() # Select the dataset. dataset = dataset_factory.get_dataset(FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) # Get the SSD network and its anchors. ssd_class = nets_factory.get_network(FLAGS.model_name) ssd_params = ssd_class.default_params._replace( num_classes=FLAGS.num_classes) ssd_net = ssd_class(ssd_params) ssd_shape = ssd_net.params.img_shape ssd_anchors = ssd_net.anchors(ssd_shape) # Select the preprocessing function. preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=20 * FLAGS.batch_size, common_queue_min=10 * FLAGS.batch_size, shuffle=True) # Get for SSD network: image, labels, bboxes. [image, shape, glabels, gbboxes ] = provider.get(['image', 'shape', 'object/label', 'object/bbox']) # Pre-processing image, labels and bboxes. image, glabels, gbboxes = \ image_preprocessing_fn(image, glabels, gbboxes, out_shape=ssd_shape, data_format=DATA_FORMAT) # Encode groundtruth labels and bboxes. gclasses, glocalisations, gscores = \ ssd_net.bboxes_encode(glabels, gbboxes, ssd_anchors) batch_shape = [1] + [len(ssd_anchors)] * 3 # Training batches and queue. r = tf.train.batch(tf_utils.reshape_list( [image, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(r, batch_shape) # Gather initial summaries. summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) summaries.add( tf.summary.image("input_image", tf.cast(b_image, tf.float32))) f_i = 0 for gt_map in b_gscores: gt_features = tf.reduce_max(gt_map, axis=3) gt_features = tf.expand_dims(gt_features, -1) summaries.add( tf.summary.image("gt_map_%d" % f_i, tf.cast(gt_features, tf.float32))) f_i += 1 arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) f_i = 0 for predict_map in predictions: predict_map = predict_map[:, :, :, :, 1:] predict_map = tf.reduce_max(predict_map, axis=4) predict_map = tf.reduce_max(predict_map, axis=3) predict_map = tf.expand_dims(predict_map, -1) summaries.add( tf.summary.image("predicte_map_%d" % f_i, tf.cast(predict_map, tf.float32))) f_i += 1 ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores, match_threshold=FLAGS.match_threshold, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, label_smoothing=FLAGS.label_smoothing) total_loss = tf.losses.get_total_loss() summaries.add(tf.summary.scalar('loss', total_loss)) for loss in tf.get_collection(tf.GraphKeys.LOSSES): summaries.add(tf.summary.scalar(loss.op.name, loss)) for variable in tf.trainable_variables(): summaries.add(tf.summary.histogram(variable.op.name, variable)) with tf.name_scope('Optimizer'): learning_rate = tf_utils.configure_learning_rate( FLAGS, dataset.num_samples, global_step) optimizer = tf_utils.configure_optimizer(FLAGS, learning_rate) summaries.add(tf.summary.scalar('learning_rate', learning_rate)) extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(extra_update_ops): train_op = slim.learning.create_train_op(total_loss, optimizer, summarize_gradients=False) summary_op = tf.summary.merge(list(summaries), name='summary_op') train_writer = tf.summary.FileWriter('./logs/', sess.graph) gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) variables_to_train = tf.trainable_variables() variables_to_restore = \ tf.contrib.framework.filter_variables(variables_to_train, exclude_patterns=['_box']) restorer = tf.train.Saver(variables_to_restore) saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=1.0, write_version=2, pad_step_number=False) sess.run(tf.global_variables_initializer()) restorer.restore(sess, FLAGS.checkpoint_path) # get_init_fn i = 0 with slim.queues.QueueRunners(sess): while (i < FLAGS.max_number_of_steps): _, summary_str = sess.run([train_op, summary_op]) if i % 50 == 0: global_step_str = global_step.eval() print('%diteraton' % (global_step_str)) train_writer.add_summary(summary_str, global_step_str) if i % 100 == 0: global_step_str = global_step.eval() saver.save(sess, "./logs/", global_step=global_step_str) i += 1
def main(_): tf.logging.set_verbosity(tf.logging.DEBUG) with tf.Graph().as_default(): # Create global_step. global_step = slim.create_global_step() # Select the dataset. dataset = pascalvoc_2012.get_split('train', FLAGS.dataset_dir) # Get the SSD network and its anchors. ssd_class = ssd_vgg_300.SSDNet ssd_params = ssd_class.default_params._replace( num_classes=FLAGS.num_classes) ssd_net = ssd_class(ssd_params) ssd_shape = ssd_net.params.img_shape # 计算所有先验框位置和大小[anchor=(x,y,h,w)....] ssd_anchors = ssd_net.anchors(ssd_shape) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # with tf.name_scope('pascalvoc_2012_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=20 * FLAGS.batch_size, common_queue_min=10 * FLAGS.batch_size, shuffle=True) # Get for SSD network: image, labels, bboxes. [image, shape, glabels, gbboxes ] = provider.get(['image', 'shape', 'object/label', 'object/bbox']) # Pre-processing image, labels and bboxes. image, glabels, gbboxes = \ ssd_vgg_preprocessing.preprocess_image(image, glabels, gbboxes, out_shape=ssd_shape, data_format=DATA_FORMAT, is_training=True) # Encode groundtruth labels and bboxes. gclasses, glocalisations, gscores = \ ssd_net.bboxes_encode(glabels, gbboxes, ssd_anchors) batch_shape = [1] + [len(ssd_anchors)] * 3 # Training batches and queue. r = tf.train.batch(tf_utils.reshape_list( [image, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(r, batch_shape) # Intermediate queueing batch_queue = slim.prefetch_queue.prefetch_queue(tf_utils.reshape_list( [b_image, b_gclasses, b_glocalisations, b_gscores]), capacity=2) # Dequeue batch. b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # Construct SSD network. # 读取网络中的默认参数 arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) # Add loss function. ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores, match_threshold=FLAGS.match_threshold, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, label_smoothing=0.0) # Gather initial summaries. summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) # =================================================================== # # Add summaries. # =================================================================== # update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) # Add summaries for end_points. for end_point in end_points: x = end_points[end_point] summaries.add(tf.summary.histogram('activations/' + end_point, x)) summaries.add( tf.summary.scalar('sparsity/' + end_point, tf.nn.zero_fraction(x))) # Add summaries for losses and extra losses. for loss in tf.get_collection(tf.GraphKeys.LOSSES): summaries.add(tf.summary.scalar(loss.op.name, loss)) for loss in tf.get_collection('EXTRA_LOSSES'): summaries.add(tf.summary.scalar(loss.op.name, loss)) # Add summaries for variables. for variable in slim.get_model_variables(): summaries.add(tf.summary.histogram(variable.op.name, variable)) # =================================================================== # # Configure the optimization procedure. # =================================================================== # learning_rate = tf_utils.configure_learning_rate( FLAGS, dataset.num_samples, global_step) optimizer = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=1.0) summaries.add(tf.summary.scalar('learning_rate', learning_rate)) # Variables to train. variables_to_train = tf_utils.get_variables_to_train(FLAGS) # and returns a train_tensor and summary_op total_loss = tf.add_n(tf.get_collection(tf.GraphKeys.LOSSES)) gradients = optimizer.compute_gradients(total_loss, var_list=variables_to_train) # Add total_loss to summary. summaries.add(tf.summary.scalar('total_loss', total_loss)) # Create gradient updates. grad_updates = optimizer.apply_gradients(gradients, global_step=global_step) update_ops.append(grad_updates) # 将所有的更新操作组合成一个operation update_op = tf.group(*update_ops) # 保证所有的更新操作执行后,才获取total_loss train_tensor = control_flow_ops.with_dependencies([update_op], total_loss, name='train_op') # Add the summaries from the first clone. These contain the summaries summaries |= set(tf.get_collection(tf.GraphKeys.SUMMARIES)) # Merge all summaries together. summary_op = tf.summary.merge(list(summaries), name='summary_op') saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=1.0, write_version=2, pad_step_number=False) slim.learning.train(train_tensor, logdir=FLAGS.train_dir, init_fn=tf_utils.get_init_fn(FLAGS), summary_op=summary_op, number_of_steps=FLAGS.max_number_of_steps, log_every_n_steps=FLAGS.log_every_n_steps, save_summaries_secs=FLAGS.save_summaries_secs, saver=saver, save_interval_secs=FLAGS.save_interval_secs)
def main(_): if not FLAGS.data_dir: raise ValueError('You must supply the dataset directory with --data_dir') tf.logging.set_verbosity(tf.logging.DEBUG) #with tf.Graph().as_default(): # with tf.Graph().as_default(), tf.device('/cpu:0'): with tf.device('/cpu:0'): global_step = slim.create_global_step() # Select the dataset. dataset = dataset_factory.get_dataset( FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.data_dir) # Get the RON network and its anchors. ron_class = nets_factory.get_network(FLAGS.model_name) ron_params = ron_class.default_params._replace(num_classes=FLAGS.num_classes) ron_net = ron_class(ron_params) ron_shape = ron_net.params.img_shape ron_anchors = ron_net.anchors(ron_shape) # for i in range(len(ron_anchors)): # for j in range(len(ron_anchors[i])): # print(ron_anchors[i][j].shape) tf_utils.print_configuration(FLAGS.__flags, ron_params, dataset.data_sources, FLAGS.model_dir) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=120 * FLAGS.batch_size, common_queue_min=80 * FLAGS.batch_size, shuffle=True) # Get for RON network: image, labels, bboxes. # (ymin, xmin, ymax, xmax) fro gbboxes [image, shape, glabels, gbboxes, isdifficult] = provider.get(['image', 'shape', 'object/label', 'object/bbox', 'object/difficult']) #glabels = tf.cast(isdifficult < tf.ones_like(isdifficult), glabels.dtype) * glabels isdifficult_mask =tf.cond(tf.reduce_sum(tf.cast(tf.logical_not(tf.equal(tf.ones_like(isdifficult), isdifficult)), tf.float32)) < 1., lambda : tf.one_hot(0, tf.shape(isdifficult)[0], on_value=True, off_value=False, dtype=tf.bool), lambda : isdifficult < tf.ones_like(isdifficult)) glabels = tf.boolean_mask(glabels, isdifficult_mask) gbboxes = tf.boolean_mask(gbboxes, isdifficult_mask) #glabels = tf.Print(glabels, [glabels,isdifficult], message='glabels: ', summarize=200) #### DEBUG #### #image = tf.Print(image, [shape, glabels, gbboxes], message='before preprocess: ', summarize=20) # Select the preprocessing function. preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) # Pre-processing image, labels and bboxes. image, glabels, gbboxes = image_preprocessing_fn(image, glabels, gbboxes, out_shape=ron_shape, data_format=DATA_FORMAT) #### DEBUG #### #image = tf.Print(image, [shape, glabels, gbboxes], message='after preprocess: ', summarize=20) #glabels = tf.Print(glabels, [glabels,isdifficult], message='glabels: ', summarize=200) # save_image_op = tf.py_func(save_image_with_bbox, # [image, # tf.reshape(tf.clip_by_value(glabels, 0, 22), [-1]), # #tf.convert_to_tensor(list(rscores.keys()), dtype=tf.int64), # tf.reshape(tf.ones_like(gbboxes), [-1]), # tf.reshape(gbboxes, [-1, 4])], # tf.int64, stateful=True) # Encode groundtruth labels and bboxes. # glocalisations is our regression object # gclasses is the ground_trutuh label # gscores is the the jaccard score with ground_truth gclasses, glocalisations, gscores, gbboxes = \ ron_net.bboxes_encode(glabels, gbboxes, ron_anchors, positive_threshold=FLAGS.match_threshold, ignore_threshold=FLAGS.neg_threshold) #gclasses[1] = tf.Print(gclasses[1], [gclasses[1]], message='gclasses[1]: ', summarize=200) # save_image_op = tf.py_func(save_image_with_bbox, # [image, # tf.reshape(tf.clip_by_value(gclasses[3], 0, 22), [-1]), # #tf.convert_to_tensor(list(rscores.keys()), dtype=tf.int64), # tf.reshape(gscores[3], [-1]), # tf.reshape(gbboxes[3], [-1, 4])], # tf.int64, stateful=True) # save_image_op = tf.py_func(save_image_with_bbox, # [image, # tf.clip_by_value(tf.concat([tf.reshape(_, [-1]) for _ in gclasses], axis=0), 0, 22), # tf.concat([tf.reshape(_, [-1]) for _ in gscores], axis=0), # tf.concat([tf.reshape(_, [-1, 4]) for _ in gbboxes], axis=0)], # tf.int64, stateful=True) # each size of the batch elements # include one image, three others(gclasses, glocalisations, gscores) batch_shape = [1] + [len(ron_anchors)] * 3 #with tf.control_dependencies([save_image_op]): # Training batches and queue. r = tf.train.batch( tf_utils.reshape_list([image, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=120 * FLAGS.batch_size) b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(r, batch_shape) with tf.device('/gpu:0'): # Construct RON network. arg_scope = ron_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, logits, objness_pred, objness_logits, localisations, end_points = \ ron_net.net(b_image, is_training=True) # Add loss function. ron_net.losses(logits, localisations, objness_logits, objness_pred, b_gclasses, b_glocalisations, b_gscores, match_threshold = FLAGS.match_threshold, neg_threshold = FLAGS.neg_threshold, objness_threshold = FLAGS.objectness_thres, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, beta=FLAGS.loss_beta, label_smoothing=FLAGS.label_smoothing) # Gather initial summaries. summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) # Add summaries for losses and extra losses. for loss in tf.get_collection(tf.GraphKeys.LOSSES): summaries.add(tf.summary.scalar(loss.op.name, loss)) for loss in tf.get_collection('EXTRA_LOSSES'): summaries.add(tf.summary.scalar(loss.op.name, loss)) # =================================================================== # # Configure the moving averages. # =================================================================== # if FLAGS.moving_average_decay: moving_average_variables = slim.get_model_variables() variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, global_step) else: moving_average_variables, variable_averages = None, None # =================================================================== # # Configure the optimization procedure. # =================================================================== # # learning_rate = tf_utils.configure_learning_rate(FLAGS, # dataset.num_samples, # global_step) lr_values = [FLAGS.learning_rate * decay for decay in [1., 0.1, 0.001]] learning_rate_ = tf.train.piecewise_constant(tf.cast(global_step, tf.int32), [90000, 115000], lr_values) learning_rate = tf.maximum(learning_rate_, tf.constant(FLAGS.end_learning_rate, dtype=learning_rate_.dtype)) optimizer = tf_utils.configure_optimizer(FLAGS, learning_rate) if FLAGS.moving_average_decay: # Update ops executed locally by trainer. update_ops.append(variable_averages.apply(moving_average_variables)) summaries.add(tf.summary.scalar('learning_rate', learning_rate)) # Variables to train. variables_to_train = tf_utils.get_variables_to_train(FLAGS) # and returns a train_tensor and summary_op total_loss = tf.losses.get_total_loss() # Add total_loss to summary. summaries.add(tf.summary.scalar('total_loss', total_loss)) # Create gradient updates. grads = optimizer.compute_gradients( total_loss, variables_to_train) grad_updates = optimizer.apply_gradients(grads, global_step=global_step) update_ops.append(grad_updates) update_op = tf.group(*update_ops) train_tensor = control_flow_ops.with_dependencies([update_op], total_loss, name='train_op') # Merge all summaries together. summary_op = tf.summary.merge(list(summaries), name='summary_op') # =================================================================== # # Kicks off the training. # =================================================================== # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction = FLAGS.gpu_memory_fraction) config = tf.ConfigProto(allow_soft_placement=True, log_device_placement = False, intra_op_parallelism_threads = FLAGS.num_cpu_threads, inter_op_parallelism_threads = FLAGS.num_cpu_threads, gpu_options = gpu_options) saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours = FLAGS.save_interval_secs/3600., write_version=2, pad_step_number=False) def wrapper_debug(sess): sess = tf_debug.LocalCLIDebugWrapperSession(sess, thread_name_filter="MainThread$") sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan) return sess slim.learning.train( train_tensor, logdir=FLAGS.model_dir, master='', is_chief=True, init_fn=tf_utils.get_init_fn(FLAGS, os.path.join(FLAGS.data_dir, 'vgg_model/vgg16_reducedfc.ckpt')),#'vgg_model/vgg16_reducedfc.ckpt' summary_op=summary_op, number_of_steps=FLAGS.max_number_of_steps, log_every_n_steps=FLAGS.log_every_n_steps, save_summaries_secs=FLAGS.save_summaries_secs, saver=saver, save_interval_secs=FLAGS.save_interval_secs, session_config=config, session_wrapper=None,#wrapper_debug,# sync_optimizer=None)
def main(_): if not FLAGS.dataset_dir: raise ValueError( 'You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): tf_global_step = slim.get_or_create_global_step() # =================================================================== # # Dataset + SSD model + Pre-processing # =================================================================== # dataset = dataset_factory.get_dataset(FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) # get the ssd network and its anchors ssd_cls = ssd.SSDnet ssd_params = ssd_cls.default_params._replace( num_classes=FLAGS.num_classes) ssd_net = ssd_cls(ssd_params) image_size = ssd_net.params.img_shape # Evaluation shape and associated anchors: eval_image_size ssd_shape = ssd_net.params.img_shape ssd_anchors = ssd_net.anchors(ssd_shape) # Select the preprocessing function. preprocessing_name = FLAGS.preprocessing_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=False) tf_utils.print_configuration(FLAGS.__flags, ssd_params, dataset.data_sources, FLAGS.eval_dir) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # with tf.device('/cpu:0'): with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, common_queue_capacity=2 * FLAGS.batch_size, common_queue_min=FLAGS.batch_size, shuffle=False) # Get for SSD network: image, labels, bboxes. [image, shape, glabels, gbboxes] = provider.get( ['image', 'shape', 'object/label', 'object/bbox']) if FLAGS.remove_difficult: [gdifficults] = provider.get(['object/difficult']) else: gdifficults = tf.zeros(tf.shape(glabels), dtype=tf.int64) # Pre-processing image, labels and bboxes. image, glabels, gbboxes, gbbox_img = \ image_preprocessing_fn(image, glabels, gbboxes, out_shape=ssd_shape, data_format=DATA_FORMAT, resize=FLAGS.eval_resize, difficults=None) # Encode groundtruth labels and bboxes. gclasses, glocalisations, gscores = \ ssd_net.bboxes_encode(glabels, gbboxes, ssd_anchors) batch_shape = [1] * 5 + [len(ssd_anchors)] * 3 # Evaluation batch. r = tf.train.batch(tf_utils.reshape_list([ image, glabels, gbboxes, gdifficults, gbbox_img, gclasses, glocalisations, gscores ]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size, dynamic_pad=True) (b_image, b_glabels, b_gbboxes, b_gdifficults, b_gbbox_img, b_gclasses, b_glocalisations, b_gscores) = tf_utils.reshape_list(r, batch_shape) # =================================================================== # # SSD Network + Ouputs decoding. # =================================================================== # dict_metrics = {} arg_scope = ssd_net.arg_scope(data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=False) # Add losses functions. # pdb.set_trace() ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores) # Performing post-processing on CPU: loop-intensive, usually more efficient. with tf.device('/device:CPU:0'): # Detected objects from SSD output. localisations = ssd_net.bboxes_decode(localisations, ssd_anchors) rscores, rbboxes = \ ssd_net.detected_bboxes(predictions, localisations, select_threshold=FLAGS.select_threshold, nms_threshold=FLAGS.nms_threshold, clipping_bbox=None, top_k=FLAGS.select_top_k, keep_top_k=FLAGS.keep_top_k) # Compute TP and FP statistics. num_gbboxes, tp, fp, rscores = \ tfe.bboxes_matching_batch(rscores.keys(), rscores, rbboxes, b_glabels, b_gbboxes, b_gdifficults, matching_threshold=FLAGS.matching_threshold) # Variables to restore: moving avg. or normal weights. if FLAGS.moving_average_decay: variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, tf_global_step) variables_to_restore = variable_averages.variables_to_restore( slim.get_model_variables()) variables_to_restore[tf_global_step.op.name] = tf_global_step else: variables_to_restore = slim.get_variables_to_restore() # =================================================================== # # Evaluation metrics. # =================================================================== # with tf.device('/device:CPU:0'): dict_metrics = {} # First add all losses. for loss in tf.get_collection(tf.GraphKeys.LOSSES): dict_metrics[loss.op.name] = slim.metrics.streaming_mean(loss) # Extra losses as well. for loss in tf.get_collection('EXTRA_LOSSES'): dict_metrics[loss.op.name] = slim.metrics.streaming_mean(loss) # Add metrics to summaries and Print on screen. for name, metric in dict_metrics.items(): # summary_name = 'eval/%s' % name summary_name = name op = tf.summary.scalar(summary_name, metric[0], collections=[]) # op = tf.Print(op, [metric[0]], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # FP and TP metrics. tp_fp_metric = tfe.streaming_tp_fp_arrays(num_gbboxes, tp, fp, rscores) for c in tp_fp_metric[0].keys(): dict_metrics['tp_fp_%s' % c] = (tp_fp_metric[0][c], tp_fp_metric[1][c]) # Add to summaries precision/recall values. aps_voc07 = {} aps_voc12 = {} for c in tp_fp_metric[0].keys(): # Precison and recall values. prec, rec = tfe.precision_recall(*tp_fp_metric[0][c]) # Average precision VOC07. v = tfe.average_precision_voc07(prec, rec) summary_name = 'AP_VOC07/%s' % c op = tf.summary.scalar(summary_name, v, collections=[]) # op = tf.Print(op, [v], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) aps_voc07[c] = v # Average precision VOC12. v = tfe.average_precision_voc12(prec, rec) summary_name = 'AP_VOC12/%s' % c op = tf.summary.scalar(summary_name, v, collections=[]) # op = tf.Print(op, [v], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) aps_voc12[c] = v # Mean average precision VOC07. summary_name = 'AP_VOC07/mAP' mAP = tf.add_n(list(aps_voc07.values())) / len(aps_voc07) op = tf.summary.scalar(summary_name, mAP, collections=[]) op = tf.Print(op, [mAP], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # Mean average precision VOC12. summary_name = 'AP_VOC12/mAP' mAP = tf.add_n(list(aps_voc12.values())) / len(aps_voc12) op = tf.summary.scalar(summary_name, mAP, collections=[]) op = tf.Print(op, [mAP], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # for i, v in enumerate(l_precisions): # summary_name = 'eval/precision_at_recall_%.2f' % LIST_RECALLS[i] # op = tf.summary.scalar(summary_name, v, collections=[]) # op = tf.Print(op, [v], summary_name) # tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # Split into values and updates ops. names_to_values, names_to_updates = slim.metrics.aggregate_metric_map( dict_metrics) # =================================================================== # # Evaluation loop. # =================================================================== # gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) # config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1 # Number of batches... if FLAGS.max_num_batches: num_batches = FLAGS.max_num_batches else: num_batches = math.ceil(dataset.num_samples / float(FLAGS.batch_size)) if not FLAGS.wait_for_checkpoints: if tf.gfile.IsDirectory(FLAGS.checkpoint_path): checkpoint_path = tf.train.latest_checkpoint( FLAGS.checkpoint_path) else: checkpoint_path = os.path.join(os.getcwd(), FLAGS.checkpoint_path) tf.logging.info('Evaluating %s' % checkpoint_path) # Standard evaluation loop. start = time.time() # pdb.set_trace() slim.evaluation.evaluate_once( master=FLAGS.master, checkpoint_path=checkpoint_path, logdir=FLAGS.eval_dir, num_evals=num_batches, eval_op=flatten(list(names_to_updates.values())), variables_to_restore=variables_to_restore, session_config=config) # Log time spent. elapsed = time.time() elapsed = elapsed - start print('Time spent : %.3f seconds.' % elapsed) print('Time spent per BATCH: %.3f seconds.' % (elapsed / num_batches)) else: checkpoint_path = FLAGS.checkpoint_path tf.logging.info('Evaluating %s' % checkpoint_path) # Waiting loop. slim.evaluation.evaluation_loop( master=FLAGS.master, checkpoint_dir=checkpoint_path, logdir=FLAGS.eval_dir, num_evals=num_batches, eval_op=flatten(list(names_to_updates.values())), variables_to_restore=variables_to_restore, eval_interval_secs=60, max_number_of_evaluations=np.inf, session_config=config, timeout=None)
def get_batch(dataset_dir, num_readers, batch_size, out_shape, net, anchors, FLAGS, file_pattern='*.tfrecord', is_training=True, shuffe=False): dataset = sythtextprovider.get_datasets(dataset_dir, file_pattern=file_pattern) provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=num_readers, common_queue_capacity=20 * batch_size, common_queue_min=10 * batch_size, shuffle=shuffe) [image, shape, glabels, gbboxes, corx, cory] = provider.get([ 'image', 'shape', 'object/label', 'object/bbox', 'object/corx', 'object/cory' ]) corx = tf.expand_dims(corx, -1) cory = tf.expand_dims(cory, -1) cord = tf.concat([corx, cory], -1) if is_training: image, glabels, gbboxes, cord, num = \ txt_preprocessing.preprocess_image(image, glabels,gbboxes, cord, out_shape,is_training=is_training) glocalisations, glabels, glinks = \ net.bboxes_encode(cord, anchors ,num) batch_shape = [1] + [len(anchors)] * 3 r = tf.train.batch( tf_utils.reshape_list([image, glocalisations, glabels, glinks]), batch_size=batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * batch_size, ) b_image, b_glocalisations, b_glabels, b_glinks= \ tf_utils.reshape_list(r, batch_shape) return b_image, b_glocalisations, b_glabels, b_glinks else: image, labels, bboxes, cord, num = \ txt_preprocessing.preprocess_image(image, glabels,gbboxes, cord, out_shape,is_training=is_training) glocalisations, glabels, glinks = \ net.bboxes_encode(cord, anchors ,num) batch_shape = [1] * 3 + [len(anchors)] * 3 r = tf.train.batch( tf_utils.reshape_list( [image, labels, cord, glocalisations, glabels, glinks]), batch_size=batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * batch_size, ) b_image, b_labels, b_cord, b_glocalisations, b_glabels, b_glinks= \ tf_utils.reshape_list(r, batch_shape) return b_image, b_labels, b_cord, b_glocalisations, b_glabels, b_glinks
def main(_): if not FLAGS.dataset_dir: raise ValueError('You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): tf_global_step = slim.get_or_create_global_step() dataset = dataset_factory.get_dataset( FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) # Get the RON network and its anchors. ron_class = nets_factory.get_network(FLAGS.model_name) ron_params = ron_class.default_params._replace(num_classes=FLAGS.num_classes) ron_net = ron_class(ron_params) ron_shape = ron_net.params.img_shape ron_anchors = ron_net.anchors(ron_shape) # Select the preprocessing function. preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=False) tf_utils.print_configuration(FLAGS.__flags, ron_params, dataset.data_sources, FLAGS.eval_dir) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # with tf.device('/cpu:0'): with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, common_queue_capacity=2 * FLAGS.batch_size, common_queue_min=FLAGS.batch_size, shuffle=False) # Get for SSD network: image, labels, bboxes. [image_, shape, glabels, gbboxes, gdifficults] = provider.get(['image', 'shape', 'object/label', 'object/bbox', 'object/difficult']) # Pre-processing image, labels and bboxes. image, glabels, gbboxes, gbbox_img = \ image_preprocessing_fn(image_, glabels, gbboxes, out_shape=ron_shape, data_format=DATA_FORMAT, difficults=None) # Encode groundtruth labels and bboxes. gclasses, glocalisations, gscores, _ = \ ron_net.bboxes_encode(glabels, gbboxes, ron_anchors) batch_shape = [1] * 5 + [len(ron_anchors)] * 3 # Evaluation batch. r = tf.train.batch( tf_utils.reshape_list([image, glabels, gbboxes, gdifficults, gbbox_img, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size, dynamic_pad=True) (b_image, b_glabels, b_gbboxes, b_gdifficults, b_gbbox_img, b_gclasses, b_glocalisations, b_gscores) = tf_utils.reshape_list(r, batch_shape) # =================================================================== # # SSD Network + Ouputs decoding. # =================================================================== # dict_metrics = {} arg_scope = ron_net.arg_scope(weight_decay=FLAGS.weight_decay, is_training=False, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, logits, objness_pred, objness_logits, localisations, end_points = \ ron_net.net(b_image, is_training=False) # Add loss function. ron_net.losses(logits, localisations, objness_logits, objness_pred, b_gclasses, b_glocalisations, b_gscores, match_threshold = FLAGS.match_threshold, neg_threshold = FLAGS.neg_threshold, objness_threshold = FLAGS.objectness_thres, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, beta=FLAGS.loss_beta, label_smoothing=FLAGS.label_smoothing) variables_to_restore = slim.get_variables_to_restore() # Performing post-processing on CPU: loop-intensive, usually more efficient. with tf.device('/device:CPU:0'): # Detected objects from SSD output. localisations = ron_net.bboxes_decode(localisations, ron_anchors) filtered_predictions = [] for i, objness in enumerate(objness_pred): filtered_predictions.append(tf.cast(tf.greater(objness, FLAGS.objectness_thres), tf.float32) * predictions[i]) rscores, rbboxes = \ ron_net.detected_bboxes(filtered_predictions, localisations, select_threshold=FLAGS.select_threshold, nms_threshold=FLAGS.nms_threshold, clipping_bbox=[0., 0., 1., 1.], top_k=FLAGS.select_top_k, keep_top_k=FLAGS.keep_top_k) labels_list = [] for k, v in rscores.items(): labels_list.append(tf.ones_like(v, tf.int32) * k) save_image_op = tf.py_func(save_image_with_bbox, [tf.cast(tf.squeeze(b_image, 0), tf.float32), tf.squeeze(tf.concat(labels_list, axis=1), 0), #tf.convert_to_tensor(list(rscores.keys()), dtype=tf.int64), tf.squeeze(tf.concat(list(rscores.values()), axis=1), 0), tf.squeeze(tf.concat(list(rbboxes.values()), axis=1), 0)], tf.int64, stateful=True) with tf.control_dependencies([save_image_op]): # Compute TP and FP statistics. num_gbboxes, tp, fp, rscores = \ tfe.bboxes_matching_batch(rscores.keys(), rscores, rbboxes, b_glabels, b_gbboxes, b_gdifficults, matching_threshold=0.5) # =================================================================== # # Evaluation metrics. # =================================================================== # with tf.device('/device:CPU:0'): dict_metrics = {} # First add all losses. for loss in tf.get_collection(tf.GraphKeys.LOSSES): dict_metrics[loss.op.name] = slim.metrics.streaming_mean(loss) # Extra losses as well. for loss in tf.get_collection('EXTRA_LOSSES'): dict_metrics[loss.op.name] = slim.metrics.streaming_mean(loss) # Add metrics to summaries and Print on screen. for name, metric in dict_metrics.items(): # summary_name = 'eval/%s' % name summary_name = name op = tf.summary.scalar(summary_name, metric[0], collections=[]) # op = tf.Print(op, [metric[0]], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # FP and TP metrics. tp_fp_metric = tfe.streaming_tp_fp_arrays(num_gbboxes, tp, fp, rscores) metrics_name = ('nobjects', 'ndetections', 'tp', 'fp', 'scores') for c in tp_fp_metric[0].keys(): for _ in range(len(tp_fp_metric[0][c])): dict_metrics['tp_fp_%s_%s' % (c, metrics_name[_])] = (tp_fp_metric[0][c][_], tp_fp_metric[1][c][_]) # for c in tp_fp_metric[0].keys(): # dict_metrics['tp_fp_%s' % c] = (tp_fp_metric[0][c], # tp_fp_metric[1][c]) # Add to summaries precision/recall values. aps_voc07 = {} aps_voc12 = {} for c in tp_fp_metric[0].keys(): # Precison and recall values. prec, rec = tfe.precision_recall(*tp_fp_metric[0][c]) # Average precision VOC07. v = tfe.average_precision_voc07(prec, rec) summary_name = 'AP_VOC07/%s' % c op = tf.summary.scalar(summary_name, v, collections=[]) # op = tf.Print(op, [v], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) aps_voc07[c] = v # Average precision VOC12. v = tfe.average_precision_voc12(prec, rec) summary_name = 'AP_VOC12/%s' % c op = tf.summary.scalar(summary_name, v, collections=[]) # op = tf.Print(op, [v], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) aps_voc12[c] = v # Mean average precision VOC07. summary_name = 'AP_VOC07/mAP' mAP = tf.add_n(list(aps_voc07.values())) / len(aps_voc07) op = tf.summary.scalar(summary_name, mAP, collections=[]) op = tf.Print(op, [mAP], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # Mean average precision VOC12. summary_name = 'AP_VOC12/mAP' mAP = tf.add_n(list(aps_voc12.values())) / len(aps_voc12) op = tf.summary.scalar(summary_name, mAP, collections=[]) op = tf.Print(op, [mAP], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # for i, v in enumerate(l_precisions): # summary_name = 'eval/precision_at_recall_%.2f' % LIST_RECALLS[i] # op = tf.summary.scalar(summary_name, v, collections=[]) # op = tf.Print(op, [v], summary_name) # tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # Split into values and updates ops. names_to_values, names_to_updates = slim.metrics.aggregate_metric_map(dict_metrics) # =================================================================== # # Evaluation loop. # =================================================================== # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) # config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1 # Number of batches... num_batches = math.ceil(dataset.num_samples / float(FLAGS.batch_size)) if tf.gfile.IsDirectory(FLAGS.checkpoint_path): checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path) else: checkpoint_path = FLAGS.checkpoint_path tf.logging.info('Evaluating %s' % checkpoint_path) # Standard evaluation loop. start = time.time() slim.evaluation.evaluate_once( master=FLAGS.master, checkpoint_path=checkpoint_path, logdir=FLAGS.eval_dir, num_evals=num_batches, eval_op=list(names_to_updates.values()), variables_to_restore=variables_to_restore, session_config=config) # Log time spent. elapsed = time.time() elapsed = elapsed - start print('Time spent : %.3f seconds.' % elapsed) print('Time spent per BATCH: %.3f seconds.' % (elapsed / num_batches))
def main(_): if not FLAGS.dataset_dir: raise ValueError( 'You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.DEBUG) with tf.Graph().as_default(): # Config model_deploy. Keep TF Slim Models structure. # Useful if want to need multiple GPUs and/or servers in the future. deploy_config = model_deploy.DeploymentConfig( num_clones=FLAGS.num_clones, # 1 clone_on_cpu=FLAGS.clone_on_cpu, # False replica_id=0, num_replicas=1, num_ps_tasks=0) # Create global_step. with tf.device(deploy_config.variables_device()): global_step = slim.create_global_step() # Select the dataset. # 'pascalvoc_2012', 'train', tfr文件存储位置 # TFR文件命名格式:'voc_2012_%s_*.tfrecord',%s使用train或者test dataset = dataset_factory.get_dataset(FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) # Get the SSD network and its anchors. ssd_class = nets_factory.get_network(FLAGS.model_name) # 'ssd_300_vgg' ssd_params = ssd_class.default_params._replace( num_classes=FLAGS.num_classes) # 替换类属性为21 ssd_net = ssd_class(ssd_params) # 创建类实例 ssd_shape = ssd_net.params.img_shape # 获取类属性(300,300) ssd_anchors = ssd_net.anchors(ssd_shape) # 调用类方法,创建搜素框 # Select the preprocessing function. # 'ssd_300_vgg', 如果 preprocessing_name 是 None 就使用 model_name preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) tf_utils.print_configuration(FLAGS.__flags, ssd_params, dataset.data_sources, FLAGS.train_dir) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # # '/job:ps/device:CPU:0' 或者 '/device:CPU:0' with tf.device(deploy_config.inputs_device()): with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, # DatasetDataProvider 需要 slim.dataset.Dataset 做参数 num_readers=FLAGS.num_readers, common_queue_capacity=20 * FLAGS.batch_size, common_queue_min=10 * FLAGS.batch_size, shuffle=True) # Get for SSD network: image, labels, bboxes.c # DatasetDataProvider可以通过TFR字段获取batch size数据 [image, shape, glabels, gbboxes] = provider.get( ['image', 'shape', 'object/label', 'object/bbox']) # Pre-processing image, labels and bboxes. # 'CHW' (n,) (n, 4) image, glabels, gbboxes = \ image_preprocessing_fn(image, glabels, gbboxes, out_shape=ssd_shape, # (300,300) data_format=DATA_FORMAT) # 'NCHW' # Encode groundtruth labels and bboxes. # f层个(m,m,k),f层个(m,m,k,4xywh),f层个(m,m,k) f层表示提取ssd特征的层的数目 # 0-20数字,方便loss的坐标记录,IOU值 gclasses, glocalisations, gscores = \ ssd_net.bboxes_encode(glabels, gbboxes, ssd_anchors) batch_shape = [1] + [len(ssd_anchors)] * 3 # (1,f层,f层,f层) # Training batches and queue. r = tf.train.batch( # 图片,中心点类别,真实框坐标,得分 tf_utils.reshape_list( [image, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, # 32 num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) # pp.pprint([image, gclasses, glocalisations, gscores]) # pp.pprint(r) b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(r, batch_shape) # pp.pprint([b_image, b_gclasses, b_glocalisations, b_gscores]) # Intermediate queueing: unique batch computation pipeline for all # GPUs running the training. batch_queue = slim.prefetch_queue.prefetch_queue( tf_utils.reshape_list( [b_image, b_gclasses, b_glocalisations, b_gscores]), capacity=2 * deploy_config.num_clones) # =================================================================== # # Define the model running on every GPU. # =================================================================== # def clone_fn(batch_queue): """Allows data parallelism by creating multiple clones of network_fn.""" # Dequeue batch. b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # 重整list # Construct SSD network. # 这个实例方法会返回之前定义的函数ssd_arg_scope(允许修改两个参数) arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): # predictions: (BS, H, W, 4, 21) # localisations: (BS, H, W, 4, 4) # logits: (BS, H, W, 4, 21) predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) # Add loss function. ssd_net.losses( logits, localisations, b_gclasses, b_glocalisations, b_gscores, match_threshold=FLAGS.match_threshold, # .5 negative_ratio=FLAGS.negative_ratio, # 3 alpha=FLAGS.loss_alpha, # 1 label_smoothing=FLAGS.label_smoothing) # .0 return end_points # Gather initial summaries. summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) # =================================================================== # # Add summaries from first clone. # =================================================================== # clones = model_deploy.create_clones(deploy_config, clone_fn, [batch_queue]) first_clone_scope = deploy_config.clone_scope(0) # Gather update_ops from the first clone. These contain, for example, # the updates for the batch_norm variables created by network_fn. update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, first_clone_scope) # Add summaries for end_points. end_points = clones[0].outputs for end_point in end_points: x = end_points[end_point] summaries.add(tf.summary.histogram('activations/' + end_point, x)) summaries.add( tf.summary.scalar('sparsity/' + end_point, tf.nn.zero_fraction(x))) # Add summaries for losses and extra losses. for loss in tf.get_collection(tf.GraphKeys.LOSSES, first_clone_scope): summaries.add(tf.summary.scalar(loss.op.name, loss)) for loss in tf.get_collection('EXTRA_LOSSES', first_clone_scope): summaries.add(tf.summary.scalar(loss.op.name, loss)) # Add summaries for variables. for variable in slim.get_model_variables(): summaries.add(tf.summary.histogram(variable.op.name, variable)) # =================================================================== # # Configure the moving averages. # =================================================================== # if FLAGS.moving_average_decay: moving_average_variables = slim.get_model_variables() variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, global_step) else: moving_average_variables, variable_averages = None, None # =================================================================== # # Configure the optimization procedure. # =================================================================== # with tf.device(deploy_config.optimizer_device()): learning_rate = tf_utils.configure_learning_rate( FLAGS, dataset.num_samples, global_step) optimizer = tf_utils.configure_optimizer(FLAGS, learning_rate) summaries.add(tf.summary.scalar('learning_rate', learning_rate)) if FLAGS.moving_average_decay: # Update ops executed locally by trainer. update_ops.append( variable_averages.apply(moving_average_variables)) # Variables to train. variables_to_train = tf_utils.get_variables_to_train(FLAGS) # and returns a train_tensor and summary_op# # total_loss 并不参与优化,仅记录用 total_loss, clones_gradients = model_deploy.optimize_clones( clones, optimizer, var_list=variables_to_train) # Add total_loss to summary. summaries.add(tf.summary.scalar('total_loss', total_loss)) # Create gradient updates. grad_updates = optimizer.apply_gradients(clones_gradients, global_step=global_step) update_ops.append(grad_updates) update_op = tf.group(*update_ops) train_tensor = control_flow_ops.with_dependencies([update_op], total_loss, name='train_op') # Add the summaries from the first clone. These contain the summaries summaries |= set( tf.get_collection(tf.GraphKeys.SUMMARIES, first_clone_scope)) # Merge all summaries together. summary_op = tf.summary.merge(list(summaries), name='summary_op') # =================================================================== # # Kicks off the training. # =================================================================== # gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=1.0, write_version=2, pad_step_number=False) slim.learning.train( train_tensor, logdir=FLAGS.train_dir, master='', is_chief=True, init_fn=tf_utils.get_init_fn(FLAGS), # 看函数实现就明白了 summary_op=summary_op, # tf.summary.merge节点 number_of_steps=FLAGS.max_number_of_steps, # 训练step log_every_n_steps=FLAGS.log_every_n_steps, # 每次model保存step间隔 save_summaries_secs=FLAGS.save_summaries_secs, # 每次summary时间间隔 saver=saver, # tf.train.Saver节点 save_interval_secs=FLAGS.save_interval_secs, session_config=config, # sess参数 sync_optimizer=None)
def main(_): if not FLAGS.dataset_dir: raise ValueError( 'You must supply the dataset directory with --dataset_dir') tf.logging.set_verbosity(tf.logging.DEBUG) with tf.Graph().as_default(): # Config model_deploy. Keep TF Slim Models structure. # Useful if want to need multiple GPUs and/or servers in the future. deploy_config = model_deploy.DeploymentConfig( num_clones=FLAGS.num_clones, clone_on_cpu=FLAGS.clone_on_cpu, replica_id=0, num_replicas=1, num_ps_tasks=0) # Create global_step. with tf.device(deploy_config.variables_device()): global_step = slim.create_global_step() # Select the dataset. dataset = dataset_factory.get_dataset(FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir) # Get the SSD network and its anchors. ssd_class = nets_factory.get_network(FLAGS.model_name) ssd_params = ssd_class.default_params._replace( num_classes=FLAGS.num_classes) ssd_net = ssd_class(ssd_params) ssd_shape = ssd_net.params.img_shape ssd_anchors = ssd_net.anchors(ssd_shape) # Select the preprocessing function. preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) tf_utils.print_configuration(FLAGS.__flags, ssd_params, dataset.data_sources, FLAGS.train_dir) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # with tf.device(deploy_config.inputs_device()): with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=20 * FLAGS.batch_size, common_queue_min=10 * FLAGS.batch_size, shuffle=True) # Get for SSD network: image, labels, bboxes. [image, shape, glabels, gbboxes] = provider.get( ['image', 'shape', 'object/label', 'object/bbox']) # Pre-processing image, labels and bboxes. image, glabels, gbboxes = \ image_preprocessing_fn(image, glabels, gbboxes, out_shape=ssd_shape, data_format=DATA_FORMAT) # Encode groundtruth labels and bboxes. gclasses, glocalisations, gscores = \ ssd_net.bboxes_encode(glabels, gbboxes, ssd_anchors) batch_shape = [1] + [len(ssd_anchors)] * 3 # Training batches and queue. r = tf.train.batch(tf_utils.reshape_list( [image, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(r, batch_shape) # Intermediate queueing: unique batch computation pipeline for all # GPUs running the training. batch_queue = slim.prefetch_queue.prefetch_queue( tf_utils.reshape_list( [b_image, b_gclasses, b_glocalisations, b_gscores]), capacity=2 * deploy_config.num_clones) # =================================================================== # # Define the model running on every GPU. # =================================================================== # def clone_fn(batch_queue): """Allows data parallelism by creating multiple clones of network_fn.""" # Dequeue batch. b_image, b_gclasses, b_glocalisations, b_gscores = \ tf_utils.reshape_list(batch_queue.dequeue(), batch_shape) # Construct SSD network. arg_scope = ssd_net.arg_scope(weight_decay=FLAGS.weight_decay, data_format=DATA_FORMAT) with slim.arg_scope(arg_scope): predictions, localisations, logits, end_points = \ ssd_net.net(b_image, is_training=True) # Add loss function. ssd_net.losses(logits, localisations, b_gclasses, b_glocalisations, b_gscores, match_threshold=FLAGS.match_threshold, negative_ratio=FLAGS.negative_ratio, alpha=FLAGS.loss_alpha, label_smoothing=FLAGS.label_smoothing) return end_points # Gather initial summaries. summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) # =================================================================== # # Add summaries from first clone. # =================================================================== # clones = model_deploy.create_clones(deploy_config, clone_fn, [batch_queue]) first_clone_scope = deploy_config.clone_scope(0) # Gather update_ops from the first clone. These contain, for example, # the updates for the batch_norm variables created by network_fn. update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, first_clone_scope) # Add summaries for end_points. end_points = clones[0].outputs for end_point in end_points: x = end_points[end_point] summaries.add(tf.summary.histogram('activations/' + end_point, x)) summaries.add( tf.summary.scalar('sparsity/' + end_point, tf.nn.zero_fraction(x))) # Add summaries for losses and extra losses. for loss in tf.get_collection(tf.GraphKeys.LOSSES, first_clone_scope): summaries.add(tf.summary.scalar(loss.op.name, loss)) for loss in tf.get_collection('EXTRA_LOSSES', first_clone_scope): summaries.add(tf.summary.scalar(loss.op.name, loss)) # Add summaries for variables. for variable in slim.get_model_variables(): summaries.add(tf.summary.histogram(variable.op.name, variable)) # =================================================================== # # Configure the moving averages. # =================================================================== # if FLAGS.moving_average_decay: moving_average_variables = slim.get_model_variables() variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, global_step) else: moving_average_variables, variable_averages = None, None # =================================================================== # # Configure the optimization procedure. # =================================================================== # with tf.device(deploy_config.optimizer_device()): learning_rate = tf_utils.configure_learning_rate( FLAGS, dataset.num_samples, global_step) optimizer = tf_utils.configure_optimizer(FLAGS, learning_rate) summaries.add(tf.summary.scalar('learning_rate', learning_rate)) if FLAGS.moving_average_decay: # Update ops executed locally by trainer. update_ops.append( variable_averages.apply(moving_average_variables)) # Variables to train. variables_to_train = tf_utils.get_variables_to_train(FLAGS) # and returns a train_tensor and summary_op total_loss, clones_gradients = model_deploy.optimize_clones( clones, optimizer, var_list=variables_to_train) # Add total_loss to summary. summaries.add(tf.summary.scalar('total_loss', total_loss)) # Create gradient updates. grad_updates = optimizer.apply_gradients(clones_gradients, global_step=global_step) update_ops.append(grad_updates) update_op = tf.group(*update_ops) train_tensor = control_flow_ops.with_dependencies([update_op], total_loss, name='train_op') # Add the summaries from the first clone. These contain the summaries summaries |= set( tf.get_collection(tf.GraphKeys.SUMMARIES, first_clone_scope)) # Merge all summaries together. summary_op = tf.summary.merge(list(summaries), name='summary_op') # =================================================================== # # Kicks off the training. # =================================================================== # gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction) config = tf.ConfigProto(log_device_placement=False, gpu_options=gpu_options) saver = tf.train.Saver(max_to_keep=5, keep_checkpoint_every_n_hours=1.0, write_version=2, pad_step_number=False) slim.learning.train(train_tensor, logdir=FLAGS.train_dir, master='', is_chief=True, init_fn=tf_utils.get_init_fn(FLAGS), summary_op=summary_op, number_of_steps=FLAGS.max_number_of_steps, log_every_n_steps=FLAGS.log_every_n_steps, save_summaries_secs=FLAGS.save_summaries_secs, saver=saver, save_interval_secs=FLAGS.save_interval_secs, session_config=config, sync_optimizer=None)
def train_input_fn(): # Select the dataset. dataset = dataset_factory.get_dataset(FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.data_dir) tf_utils.print_configuration(FLAGS.__flags, ron_params, dataset.data_sources, FLAGS.model_dir) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # with tf.name_scope(FLAGS.dataset_name + '_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=120 * FLAGS.batch_size, common_queue_min=80 * FLAGS.batch_size, shuffle=True) # Get for RON network: image, labels, bboxes. # (ymin, xmin, ymax, xmax) fro gbboxes [image, shape, glabels, gbboxes, isdifficult] = provider.get([ 'image', 'shape', 'object/label', 'object/bbox', 'object/difficult' ]) isdifficult_mask = tf.cond( tf.reduce_sum( tf.cast( tf.logical_not( tf.equal(tf.ones_like(isdifficult), isdifficult)), tf.float32)) < 1., lambda: tf.one_hot(0, tf.shape(isdifficult)[0], on_value=True, off_value=False, dtype=tf.bool), lambda: isdifficult < tf.ones_like(isdifficult)) glabels = tf.boolean_mask(glabels, isdifficult_mask) gbboxes = tf.boolean_mask(gbboxes, isdifficult_mask) # Select the preprocessing function. preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) # Pre-processing image, labels and bboxes. image, glabels, gbboxes = image_preprocessing_fn( image, glabels, gbboxes, out_shape=ron_shape, data_format=DATA_FORMAT) # Encode groundtruth labels and bboxes. # glocalisations is our regression object # gclasses is the ground_trutuh label # gscores is the the jaccard score with ground_truth gclasses, glocalisations, gscores = ron_net.bboxes_encode( glabels, gbboxes, ron_anchors, positive_threshold=FLAGS.match_threshold, ignore_threshold=FLAGS.neg_threshold) # each size of the batch elements # include one image, three others(gclasses, glocalisations, gscores) batch_shape = [1] + [len(ron_anchors)] * 3 # Training batches and queue. r = tf.train.batch(tf_utils.reshape_list( [image, gclasses, glocalisations, gscores]), batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=120 * FLAGS.batch_size, shared_name=None) b_image, b_gclasses, b_glocalisations, b_gscores = tf_utils.reshape_list( r, batch_shape) return b_image, { 'b_gclasses': b_gclasses, 'b_glocalisations': b_glocalisations, 'b_gscores': b_gscores }