def testForwardpassDeepLabv3plus(self): crop_size = [33, 33] outputs_to_num_classes = {'semantic': 3} model_options = common.ModelOptions( outputs_to_num_classes, crop_size, output_stride=16)._replace(add_image_level_feature=True, aspp_with_batch_norm=True, logits_kernel_size=1, model_variant='mobilenet_v2' ) # Employ MobileNetv2 for fast test. g = tf.Graph() with g.as_default(): with self.test_session(graph=g) as sess: inputs = tf.random_uniform((1, crop_size[0], crop_size[1], 3)) outputs_to_scales_to_logits = model.multi_scale_logits( inputs, model_options, image_pyramid=[1.0]) sess.run(tf.global_variables_initializer()) outputs_to_scales_to_logits = sess.run( outputs_to_scales_to_logits) # Check computed results for each output type. for output in outputs_to_num_classes: scales_to_logits = outputs_to_scales_to_logits[output] # Expect only one output. self.assertEqual(len(scales_to_logits), 1) for logits in scales_to_logits.values(): self.assertTrue(logits.any())
def main(unused_argv): tf.logging.set_verbosity(tf.logging.INFO) tf.logging.info('Prepare to export model to: %s', FLAGS.export_path) with tf.Graph().as_default(): image, image_size, resized_image_size = _create_input_tensors() model_options = common.ModelOptions( outputs_to_num_classes={common.OUTPUT_TYPE: FLAGS.num_classes}, crop_size=FLAGS.crop_size, atrous_rates=FLAGS.atrous_rates, output_stride=FLAGS.output_stride) if tuple(FLAGS.inference_scales) == (1.0, ): tf.logging.info('Exported model performs single-scale inference.') predictions = model.predict_labels( image, model_options=model_options, image_pyramid=FLAGS.image_pyramid) else: tf.logging.info('Exported model performs multi-scale inference.') predictions = model.predict_labels_multi_scale( image, model_options=model_options, eval_scales=FLAGS.inference_scales, add_flipped_images=FLAGS.add_flipped_images) # Crop the valid regions from the predictions. semantic_predictions = tf.slice( predictions[common.OUTPUT_TYPE], [0, 0, 0], [1, resized_image_size[0], resized_image_size[1]]) # Resize back the prediction to the original image size. def _resize_label(label, label_size): # Expand dimension of label to [1, height, width, 1] for resize operation. label = tf.expand_dims(label, 3) resized_label = tf.image.resize_images( label, label_size, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR, align_corners=True) return tf.squeeze(resized_label, 3) semantic_predictions = _resize_label(semantic_predictions, image_size) semantic_predictions = tf.identity(semantic_predictions, name=_OUTPUT_NAME) saver = tf.train.Saver(tf.model_variables()) tf.gfile.MakeDirs(os.path.dirname(FLAGS.export_path)) freeze_graph.freeze_graph_with_def_protos( tf.get_default_graph().as_graph_def(add_shapes=True), saver.as_saver_def(), FLAGS.checkpoint_path, _OUTPUT_NAME, restore_op_name=None, filename_tensor_name=None, output_graph=FLAGS.export_path, clear_devices=True, initializer_nodes=None)
def _build_deeplab(inputs_queue, outputs_to_num_classes, ignore_label): """Builds a clone of DeepLab. Args: inputs_queue: A prefetch queue for images and labels. outputs_to_num_classes: A map from output type to the number of classes. For example, for the task of semantic segmentation with 21 semantic classes, we would have outputs_to_num_classes['semantic'] = 21. ignore_label: Ignore label. Returns: A map of maps from output_type (e.g., semantic prediction) to a dictionary of multi-scale logits names to logits. For each output_type, the dictionary has keys which correspond to the scales and values which correspond to the logits. For example, if `scales` equals [1.0, 1.5], then the keys would include 'merged_logits', 'logits_1.00' and 'logits_1.50'. """ samples = inputs_queue.dequeue() # Add name to input and label nodes so we can add to summary. samples[common.IMAGE] = tf.identity(samples[common.IMAGE], name=common.IMAGE) samples[common.LABEL] = tf.identity(samples[common.LABEL], name=common.LABEL) model_options = common.ModelOptions( outputs_to_num_classes=outputs_to_num_classes, crop_size=FLAGS.train_crop_size, atrous_rates=FLAGS.atrous_rates, output_stride=FLAGS.output_stride) outputs_to_scales_to_logits = model.multi_scale_logits( samples[common.IMAGE], model_options=model_options, image_pyramid=FLAGS.image_pyramid, weight_decay=FLAGS.weight_decay, is_training=True, fine_tune_batch_norm=FLAGS.fine_tune_batch_norm) # Add name to graph node so we can add to summary. output_type_dict = outputs_to_scales_to_logits[common.OUTPUT_TYPE] output_type_dict[model.MERGED_LOGITS_SCOPE] = tf.identity( output_type_dict[model.MERGED_LOGITS_SCOPE], name=common.OUTPUT_TYPE) for output, num_classes in six.iteritems(outputs_to_num_classes): train_utils.add_softmax_cross_entropy_loss_for_each_scale( outputs_to_scales_to_logits[output], samples[common.LABEL], num_classes, ignore_label, loss_weight=1.0, upsample_logits=FLAGS.upsample_logits, scope=output) return outputs_to_scales_to_logits
def testDeepcopy(self): num_classes = 21 model_options = common.ModelOptions( outputs_to_num_classes={common.OUTPUT_TYPE: num_classes}) model_options_new = copy.deepcopy(model_options) self.assertEqual((model_options_new. outputs_to_num_classes[common.OUTPUT_TYPE]), num_classes) num_classes_new = 22 model_options_new.outputs_to_num_classes[common.OUTPUT_TYPE] = ( num_classes_new) self.assertEqual(model_options.outputs_to_num_classes[common.OUTPUT_TYPE], num_classes) self.assertEqual((model_options_new. outputs_to_num_classes[common.OUTPUT_TYPE]), num_classes_new)
def testBuildDeepLabv2(self): batch_size = 2 crop_size = [41, 41] # Test with two image_pyramids. image_pyramids = [[1], [0.5, 1]] # Test two model variants. model_variants = ['xception_65', 'mobilenet_v2'] # Test with two output_types. outputs_to_num_classes = {'semantic': 3, 'direction': 2} expected_endpoints = [['merged_logits'], ['merged_logits', 'logits_0.50', 'logits_1.00']] expected_num_logits = [1, 3] for model_variant in model_variants: model_options = common.ModelOptions( outputs_to_num_classes)._replace( add_image_level_feature=False, aspp_with_batch_norm=False, aspp_with_separable_conv=False, model_variant=model_variant) for i, image_pyramid in enumerate(image_pyramids): g = tf.Graph() with g.as_default(): with self.test_session(graph=g): inputs = tf.random_uniform( (batch_size, crop_size[0], crop_size[1], 3)) outputs_to_scales_to_logits = model.multi_scale_logits( inputs, model_options, image_pyramid=image_pyramid) # Check computed results for each output type. for output in outputs_to_num_classes: scales_to_logits = outputs_to_scales_to_logits[ output] self.assertListEqual( sorted(scales_to_logits.keys()), sorted(expected_endpoints[i])) # Expected number of logits = len(image_pyramid) + 1, since the # last logits is merged from all the scales. self.assertEqual(len(scales_to_logits), expected_num_logits[i])
def testBuildDeepLabWithDensePredictionCell(self): batch_size = 1 crop_size = [33, 33] outputs_to_num_classes = {'semantic': 2} #expected_endpoints = ['merged_logits'] dense_prediction_cell_config = [ { 'kernel': 3, 'rate': [1, 6], 'op': 'conv', 'input': -1 }, { 'kernel': 3, 'rate': [18, 15], 'op': 'conv', 'input': 0 }, ] model_options = common.ModelOptions( outputs_to_num_classes, crop_size, output_stride=16)._replace( aspp_with_batch_norm=True, model_variant='mobilenet_v2', dense_prediction_cell_config=dense_prediction_cell_config) g = tf.Graph() with g.as_default(): with self.test_session(graph=g): inputs = tf.random_uniform( (batch_size, crop_size[0], crop_size[1], 3)) outputs_to_scales_to_model_results = model.multi_scale_logits( inputs, model_options, image_pyramid=[1.0]) for output in outputs_to_num_classes: scales_to_model_results = outputs_to_scales_to_model_results[ output] #self.assertListEqual(scales_to_model_results.keys(),expected_endpoints) self.assertEqual(len(scales_to_model_results), 1)
def main(unused_argv): tf.logging.set_verbosity(tf.logging.INFO) # Get dataset-dependent information. dataset = segmentation_dataset.get_dataset( FLAGS.dataset, FLAGS.vis_split, dataset_dir=FLAGS.dataset_dir) train_id_to_eval_id = None if dataset.name == segmentation_dataset.get_cityscapes_dataset_name(): tf.logging.info('Cityscapes requires converting train_id to eval_id.') train_id_to_eval_id = _CITYSCAPES_TRAIN_ID_TO_EVAL_ID # Prepare for visualization. tf.gfile.MakeDirs(FLAGS.vis_logdir) save_dir = os.path.join(FLAGS.vis_logdir, _SEMANTIC_PREDICTION_SAVE_FOLDER) tf.gfile.MakeDirs(save_dir) raw_save_dir = os.path.join( FLAGS.vis_logdir, _RAW_SEMANTIC_PREDICTION_SAVE_FOLDER) tf.gfile.MakeDirs(raw_save_dir) tf.logging.info('Visualizing on %s set', FLAGS.vis_split) g = tf.Graph() with g.as_default(): samples = input_generator.get(dataset, FLAGS.vis_crop_size, FLAGS.vis_batch_size, min_resize_value=FLAGS.min_resize_value, max_resize_value=FLAGS.max_resize_value, resize_factor=FLAGS.resize_factor, dataset_split=FLAGS.vis_split, is_training=False, model_variant=FLAGS.model_variant) model_options = common.ModelOptions( outputs_to_num_classes={common.OUTPUT_TYPE: dataset.num_classes}, crop_size=FLAGS.vis_crop_size, atrous_rates=FLAGS.atrous_rates, output_stride=FLAGS.output_stride) if tuple(FLAGS.eval_scales) == (1.0,): tf.logging.info('Performing single-scale test.') predictions = model.predict_labels( samples[common.IMAGE], model_options=model_options, image_pyramid=FLAGS.image_pyramid) else: tf.logging.info('Performing multi-scale test.') predictions = model.predict_labels_multi_scale( samples[common.IMAGE], model_options=model_options, eval_scales=FLAGS.eval_scales, add_flipped_images=FLAGS.add_flipped_images) predictions = predictions[common.OUTPUT_TYPE] if FLAGS.min_resize_value and FLAGS.max_resize_value: # Only support batch_size = 1, since we assume the dimensions of original # image after tf.squeeze is [height, width, 3]. assert FLAGS.vis_batch_size == 1 # Reverse the resizing and padding operations performed in preprocessing. # First, we slice the valid regions (i.e., remove padded region) and then # we reisze the predictions back. original_image = tf.squeeze(samples[common.ORIGINAL_IMAGE]) original_image_shape = tf.shape(original_image) predictions = tf.slice( predictions, [0, 0, 0], [1, original_image_shape[0], original_image_shape[1]]) resized_shape = tf.to_int32([tf.squeeze(samples[common.HEIGHT]), tf.squeeze(samples[common.WIDTH])]) predictions = tf.squeeze( tf.image.resize_images(tf.expand_dims(predictions, 3), resized_shape, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR, align_corners=True), 3) tf.train.get_or_create_global_step() saver = tf.train.Saver(slim.get_variables_to_restore()) sv = tf.train.Supervisor(graph=g, logdir=FLAGS.vis_logdir, init_op=tf.global_variables_initializer(), summary_op=None, summary_writer=None, global_step=None, saver=saver) num_batches = int(math.ceil( dataset.num_samples / float(FLAGS.vis_batch_size))) last_checkpoint = None # Loop to visualize the results when new checkpoint is created. num_iters = 0 while (FLAGS.max_number_of_iterations <= 0 or num_iters < FLAGS.max_number_of_iterations): num_iters += 1 last_checkpoint = slim.evaluation.wait_for_new_checkpoint( FLAGS.checkpoint_dir, last_checkpoint) start = time.time() tf.logging.info( 'Starting visualization at ' + time.strftime('%Y-%m-%d-%H:%M:%S', time.gmtime())) tf.logging.info('Visualizing with model %s', last_checkpoint) with sv.managed_session(FLAGS.master, start_standard_services=False) as sess: sv.start_queue_runners(sess) sv.saver.restore(sess, last_checkpoint) image_id_offset = 0 for batch in range(num_batches): tf.logging.info('Visualizing batch %d / %d', batch + 1, num_batches) _process_batch(sess=sess, original_images=samples[common.ORIGINAL_IMAGE], semantic_predictions=predictions, image_names=samples[common.IMAGE_NAME], image_heights=samples[common.HEIGHT], image_widths=samples[common.WIDTH], image_id_offset=image_id_offset, save_dir=save_dir, raw_save_dir=raw_save_dir, train_id_to_eval_id=train_id_to_eval_id) image_id_offset += FLAGS.vis_batch_size tf.logging.info( 'Finished visualization at ' + time.strftime('%Y-%m-%d-%H:%M:%S', time.gmtime())) time_to_next_eval = start + FLAGS.eval_interval_secs - time.time() if time_to_next_eval > 0: time.sleep(time_to_next_eval)
def testOutputsToNumClasses(self): num_classes = 21 model_options = common.ModelOptions( outputs_to_num_classes={common.OUTPUT_TYPE: num_classes}) self.assertEqual(model_options.outputs_to_num_classes[common.OUTPUT_TYPE], num_classes)
def testWrongDeepLabVariant(self): model_options = common.ModelOptions( [])._replace(model_variant='no_such_variant') with self.assertRaises(ValueError): model._get_logits(images=[], model_options=model_options)
def main(unused_argv): tf.logging.set_verbosity(tf.logging.INFO) # Get dataset-dependent information. dataset = segmentation_dataset.get_dataset(FLAGS.dataset, FLAGS.eval_split, dataset_dir=FLAGS.dataset_dir) tf.gfile.MakeDirs(FLAGS.eval_logdir) tf.logging.info('Evaluating on %s set', FLAGS.eval_split) with tf.Graph().as_default(): samples = input_generator.get(dataset, FLAGS.eval_crop_size, FLAGS.eval_batch_size, min_resize_value=FLAGS.min_resize_value, max_resize_value=FLAGS.max_resize_value, resize_factor=FLAGS.resize_factor, dataset_split=FLAGS.eval_split, is_training=False, model_variant=FLAGS.model_variant) model_options = common.ModelOptions( outputs_to_num_classes={common.OUTPUT_TYPE: dataset.num_classes}, crop_size=FLAGS.eval_crop_size, atrous_rates=FLAGS.atrous_rates, output_stride=FLAGS.output_stride) if tuple(FLAGS.eval_scales) == (1.0, ): tf.logging.info('Performing single-scale test.') predictions = model.predict_labels( samples[common.IMAGE], model_options, image_pyramid=FLAGS.image_pyramid) else: tf.logging.info('Performing multi-scale test.') predictions = model.predict_labels_multi_scale( samples[common.IMAGE], model_options=model_options, eval_scales=FLAGS.eval_scales, add_flipped_images=FLAGS.add_flipped_images) predictions = predictions[common.OUTPUT_TYPE] predictions = tf.reshape(predictions, shape=[-1]) labels = tf.reshape(samples[common.LABEL], shape=[-1]) weights = tf.to_float(tf.not_equal(labels, dataset.ignore_label)) # Set ignore_label regions to label 0, because metrics.mean_iou requires # range of labels = [0, dataset.num_classes). Note the ignore_label regions # are not evaluated since the corresponding regions contain weights = 0. labels = tf.where(tf.equal(labels, dataset.ignore_label), tf.zeros_like(labels), labels) predictions_tag = 'miou' for eval_scale in FLAGS.eval_scales: predictions_tag += '_' + str(eval_scale) if FLAGS.add_flipped_images: predictions_tag += '_flipped' # Define the evaluation metric. metric_map = {} metric_map[predictions_tag] = tf.metrics.mean_iou(predictions, labels, dataset.num_classes, weights=weights) metrics_to_values, metrics_to_updates = ( tf.contrib.metrics.aggregate_metric_map(metric_map)) for metric_name, metric_value in six.iteritems(metrics_to_values): slim.summaries.add_scalar_summary(metric_value, metric_name, print_summary=True) num_batches = int( math.ceil(dataset.num_samples / float(FLAGS.eval_batch_size))) tf.logging.info('Eval num images %d', dataset.num_samples) tf.logging.info('Eval batch size %d and num batch %d', FLAGS.eval_batch_size, num_batches) num_eval_iters = None if FLAGS.max_number_of_evaluations > 0: num_eval_iters = FLAGS.max_number_of_evaluations slim.evaluation.evaluation_loop( master=FLAGS.master, checkpoint_dir=FLAGS.checkpoint_dir, logdir=FLAGS.eval_logdir, num_evals=num_batches, eval_op=list(metrics_to_updates.values()), max_number_of_evaluations=num_eval_iters, eval_interval_secs=FLAGS.eval_interval_secs)