def save_annotation(label, save_dir, filename, add_colormap=True, colormap_type=get_dataset_colormap.get_pascal_name()): """Saves the given label to image on disk. Args: label: The numpy array to be saved. The data will be converted to uint8 and saved as png image. save_dir: The directory to which the results will be saved. filename: The image filename. add_colormap: Add color map to the label or not. colormap_type: Colormap type for visualization. """ # Add colormap for visualizing the prediction. if add_colormap: colored_label = get_dataset_colormap.label_to_color_image( label, colormap_type) else: colored_label = label pil_image = img.fromarray(colored_label.astype(dtype=np.uint8)) with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f: pil_image.save(f, 'PNG')
def testFirstColorInADE20KColorMap(self): label = np.array([[1, 3], [10, 20]]) expected_result = np.array([[[120, 120, 120], [6, 230, 230]], [[4, 250, 7], [204, 70, 3]]]) colored_label = get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_ade20k_name()) self.assertTrue(np.array_equal(colored_label, expected_result))
def vis_segmentation(image, seg_map): plt.figure() plt.subplot(221) plt.imshow(image) plt.axis('off') plt.title('input image') plt.subplot(222) seg_image = get_dataset_colormap.label_to_color_image( seg_map, get_dataset_colormap.get_pascal_name()).astype(np.uint8) plt.imshow(seg_image) plt.axis('off') plt.title('segmentation map') plt.subplot(223) plt.imshow(image) plt.imshow(seg_image, alpha=0.7) plt.axis('off') plt.title('segmentation overlay') unique_labels = np.unique(seg_map) ax = plt.subplot(224) plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation='nearest') ax.yaxis.tick_right() plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels]) plt.xticks([], []) ax.tick_params(width=0) plt.show()
def save_annotation(label, save_dir, filename, add_colormap=True, colormap_type=get_dataset_colormap.get_osm_name()): """Saves the given label to image on disk. Args: label: The numpy array to be saved. The data will be converted to uint8 and saved as png image. save_dir: The directory to which the results will be saved. filename: The image filename. add_colormap: Add color map to the label or not. colormap_type: Colormap type for visualization. """ # Add colormap for visualizing the prediction. if add_colormap: colored_label = get_dataset_colormap.label_to_color_image( label, colormap_type) else: colored_label = label pil_image = img.fromarray(colored_label.astype(dtype=np.uint8)) with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f: pil_image.save(f, 'PNG')
def testFirstColorInADE20KColorMap(self): label = np.array([[1, 3], [10, 20]]) expected_result = np.array([ [[120, 120, 120], [6, 230, 230]], [[4, 250, 7], [204, 70, 3]] ]) colored_label = get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_ade20k_name()) self.assertTrue(np.array_equal(colored_label, expected_result))
def testLabelToPASCALColorImage(self): """Test the value of the converted label value.""" label = np.array([[0, 16, 16], [52, 7, 52]]) expected_result = np.array([[[0, 0, 0], [0, 64, 0], [0, 64, 0]], [[0, 64, 192], [128, 128, 128], [0, 64, 192]]]) colored_label = get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_pascal_name()) self.assertTrue(np.array_equal(expected_result, colored_label))
def testLabelToPASCALColorImage(self): """Test the value of the converted label value.""" label = np.array([[0, 16, 16], [52, 7, 52]]) expected_result = np.array([ [[0, 0, 0], [0, 64, 0], [0, 64, 0]], [[0, 64, 192], [128, 128, 128], [0, 64, 192]] ]) colored_label = get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_pascal_name()) self.assertTrue(np.array_equal(expected_result, colored_label))
def decode_labels(mask, num_images=1): """Decode batch of segmentation masks. Args: mask: result of inference after taking argmax. num_images: number of images to decode from the batch. """ colored_label = get_dataset_colormap.label_to_color_image(mask) pil_image = img.fromarray(colored_label.astype(dtype=np.uint8)) return pil_image
def silhouettes_from_images(input_path, output_path, deeplab_path): sys.path.append(deeplab_path) from deeplab.utils import get_dataset_colormap FULL_COLOR_MAP = get_dataset_colormap.label_to_color_image(FULL_LABEL_MAP) input_path_images = [ f for f in glob.glob(input_path + "/**/*.jpg", recursive=True) ] for image_path in input_path_images: image = Image.open(image_path) resized_im, seg_map = model.run(image) seg_image = get_dataset_colormap.label_to_color_image( seg_map, get_dataset_colormap.get_pascal_name()).astype(np.uint8) basename = image_path.replace(input_path, "") open_cv_image = np.array(seg_image) open_cv_image = open_cv_image[:, :, ::-1].copy() os.makedirs(output_path + basename.replace(ntpath.basename(image_path), ""), exist_ok=True) cv2.imwrite(output_path + basename, open_cv_image)
def silhouettes_from_videos(input_path, output_path, deeplab_path): sys.path.append(deeplab_path) from deeplab.utils import get_dataset_colormap FULL_COLOR_MAP = get_dataset_colormap.label_to_color_image(FULL_LABEL_MAP) input_path_videos = [ f for f in glob.glob(input_path + "*.avi", recursive=True) ] for video_path in input_path_videos: basename = video_path.replace(input_path, "") os.makedirs(output_path + basename[:-4], exist_ok=True) counter = 0 vidcap = cv2.VideoCapture(video_path) success, image = vidcap.read() while success: image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) pil_im = Image.fromarray(image) resized_im, seg_map = model.run(pil_im) seg_image = get_dataset_colormap.label_to_color_image( seg_map, get_dataset_colormap.get_pascal_name()).astype(np.uint8) open_cv_image = np.array(seg_image) open_cv_image = open_cv_image[:, :, ::-1].copy() if np.all(open_cv_image == 0): # Esto es lo nuevo success, image = vidcap.read() counter = counter + 1 continue cv2.imwrite( output_path + basename[:-4] + "/" + str(counter).zfill(3) + ".jpg", open_cv_image) success, image = vidcap.read() counter = counter + 1
def save_annotation_overlayed( label, original_image, save_dir, filename, add_colormap=True, normalize_to_unit_values=False, scale_values=False, colormap_type=get_dataset_colormap.get_pascal_name()): """Saves the given label to image on disk. Args: label: The numpy array to be saved. The data will be converted to uint8 and saved as png image. save_dir: String, the directory to which the results will be saved. filename: String, the image filename. add_colormap: Boolean, add color map to the label or not. normalize_to_unit_values: Boolean, normalize the input values to [0, 1]. scale_values: Boolean, scale the input values to [0, 255] for visualization. colormap_type: String, colormap type for visualization. """ # Add colormap for visualizing the prediction. if add_colormap: colored_label = get_dataset_colormap.label_to_color_image( label, colormap_type) else: colored_label = label if normalize_to_unit_values: min_value = np.amin(colored_label) max_value = np.amax(colored_label) range_value = max_value - min_value if range_value != 0: colored_label = (colored_label - min_value) / range_value if scale_values: colored_label = 255. * colored_label pil_image = img.blend(img.fromarray(original_image.astype(dtype=np.uint8)), img.fromarray(colored_label.astype(dtype=np.uint8)), 0.5) with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f: pil_image.save(f, 'PNG')
def save_annotation(label, save_dir, filename, add_colormap=True, normalize_to_unit_values=False, scale_values=False, colormap_type=get_dataset_colormap.get_pascal_name()): """Saves the given label to image on disk. Args: label: The numpy array to be saved. The data will be converted to uint8 and saved as png image. save_dir: String, the directory to which the results will be saved. filename: String, the image filename. add_colormap: Boolean, add color map to the label or not. normalize_to_unit_values: Boolean, normalize the input values to [0, 1]. scale_values: Boolean, scale the input values to [0, 255] for visualization. colormap_type: String, colormap type for visualization. """ # Add colormap for visualizing the prediction. if add_colormap: colored_label = get_dataset_colormap.label_to_color_image( label, colormap_type) else: colored_label = label if normalize_to_unit_values: min_value = np.amin(colored_label) max_value = np.amax(colored_label) range_value = max_value - min_value if range_value != 0: colored_label = (colored_label - min_value) / range_value if scale_values: colored_label = 255. * colored_label pil_image = img.fromarray(colored_label.astype(dtype=np.uint8)) with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f: pil_image.save(f, 'PNG')
def testUnExpectedLabelValueForLabelToPASCALColorImage(self): """Raise ValueError when input value exceeds range.""" label = np.array([[120], [300]]) with self.assertRaises(ValueError): get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_pascal_name())
def testUnExpectedLabelDimensionForLabelToADE20KColorImage(self): label = np.array([250]) with self.assertRaises(ValueError): get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_ade20k_name())
def testUnExpectedLabelDimensionForLabelToPASCALColorImage(self): """Raise ValueError if input dimension is not correct.""" label = np.array([120]) with self.assertRaises(ValueError): get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_pascal_name())
def testUnExpectedLabelValueForLabelToPASCALColorImage(self): """Raise ValueError when input value exceeds range.""" label = np.array([[120], [300]]) with self.assertRaises(ValueError): get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_pascal_name())
type=int, default=640, help='the input image will be scaled to this size') parser.add_argument('--report', '-r', default='report.csv', help='the report file') args = parser.parse_args() LABEL_NAMES = np.asarray([ 'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tv' ]) FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1) FULL_COLOR_MAP = get_dataset_colormap.label_to_color_image(FULL_LABEL_MAP) class DeepLabModel(object): """Class to load deeplab model and run inference.""" INPUT_TENSOR_NAME = 'ImageTensor:0' OUTPUT_TENSOR_NAME = 'SemanticPredictions:0' def __init__(self, model_path): """Creates and loads pretrained deeplab model.""" self.graph = tf.Graph() with open(model_path, 'rb') as fd: graph_def = tf.GraphDef.FromString(fd.read()) with self.graph.as_default(): tf.import_graph_def(graph_def, name='') self.sess = tf.Session(graph=self.graph)
def _process_batch(sess, original_images, semantic_predictions, instance_predictions, regression_predictions, panoptic_prediction, image_names, image_heights, image_widths, image_id_offset, save_dir, instance_save_dir, regression_save_dir, panoptic_save_dir, raw_save_dir, train_id_to_eval_id=None): """Evaluates one single batch qualitatively. Args: sess: TensorFlow session. original_images: One batch of original images. semantic_predictions: One batch of semantic segmentation predictions. instance_predictions: One batch of instance predictions. image_names: Image names. image_heights: Image heights. image_widths: Image widths. image_id_offset: Image id offset for indexing images. save_dir: The directory where the predictions will be saved. instance_save_dir : The directory where the instance predictions will be saved. raw_save_dir: The directory where the raw predictions will be saved. train_id_to_eval_id: A list mapping from train id to eval id. """ (original_images, semantic_predictions, instance_predictions, regression_predictions, instance_segmentation, image_names, image_heights, image_widths) = sess.run([ original_images, semantic_predictions, instance_predictions, regression_predictions, panoptic_prediction, image_names, image_heights, image_widths ]) num_image = semantic_predictions.shape[0] for i in range(num_image): image_height = np.squeeze(image_heights[i]) image_width = np.squeeze(image_widths[i]) original_image = np.squeeze(original_images[i]) semantic_prediction = np.squeeze(semantic_predictions[i]) instance_predictions = np.squeeze(instance_predictions[i]) regression_predictions = np.squeeze(regression_predictions[i]) crop_semantic_prediction = semantic_prediction[:image_height, : image_width] crop_instance_prediction = instance_predictions[:image_height, : image_width] crop_regression_prediction = regression_predictions[:image_height, : image_width, :] instance_segmentation = np.squeeze(instance_segmentation) unique_elements = np.unique(instance_segmentation) instance_segmentation_scaled = np.array(instance_segmentation) * ( 255 // len(unique_elements)) ########## VIS INSTANCE SEG OUTPUT ################## inst_color = cv2.applyColorMap( instance_segmentation_scaled.astype('uint8'), cv2.COLORMAP_JET) instance_segmentation_coloured = Image.blend( Image.fromarray(original_image), Image.fromarray(inst_color), 0.4) ###################################################### # For Creating boundries around instances # Add boundry to Image colormap_type = FLAGS.colormap_type instance_boundry = np.zeros_like(semantic_prediction) instances = np.delete(unique_elements, 0) for index, i in enumerate(instances): local_instance_mask = instance_segmentation == i kernel = np.ones((5, 5), np.uint8) dilation = cv2.dilate(local_instance_mask.astype('uint8'), kernel, iterations=1) erosion = cv2.erode(local_instance_mask.astype('uint8'), kernel, iterations=1) boundry = (dilation - erosion) * 255 instance_boundry += boundry colored_label = get_dataset_colormap.label_to_color_image( semantic_prediction.astype('uint8'), colormap_type) colored_label = colored_label + np.dstack( (instance_boundry, instance_boundry, instance_boundry)) colored_label = Image.fromarray(colored_label.astype(dtype=np.uint8)) panoptic_output = Image.blend(Image.fromarray(original_image), colored_label, 0.7) ###################################################################################### # Save image. save_annotation.save_annotation(original_image, save_dir, _IMAGE_FORMAT % (image_id_offset + i), add_colormap=False) # Save instance heatmap prediction. save_annotation.save_annotation(crop_instance_prediction, instance_save_dir, _PREDICTION_FORMAT % (image_id_offset + i), scale_values=True, add_colormap=False, colormap_type=FLAGS.colormap_type) # Save regression prediction. save_annotation.save_annotation_instance_regression( instance_segmentation_coloured, regression_save_dir, _PREDICTION_FORMAT % (image_id_offset + i), normalize_values=True, add_colormap=False, colormap_type=FLAGS.colormap_type) # Save prediction. save_annotation.save_annotation(crop_semantic_prediction, save_dir, _PREDICTION_FORMAT % (image_id_offset + i), add_colormap=True, colormap_type=FLAGS.colormap_type) # Save panoptic prediction. save_annotation.save_annotation_panoptic( panoptic_output, panoptic_save_dir, _PREDICTION_FORMAT % (image_id_offset + i), add_colormap=False, colormap_type=FLAGS.colormap_type) if FLAGS.also_save_raw_predictions: image_filename = os.path.basename(image_names[i]) if train_id_to_eval_id is not None: crop_semantic_prediction = _convert_train_id_to_eval_id( crop_semantic_prediction, train_id_to_eval_id) save_annotation.save_annotation(crop_semantic_prediction, raw_save_dir, image_filename, add_colormap=False)
import sys from matplotlib import pyplot as plt import numpy as np from PIL import Image import tensorflow as tf from deeplab.utils import get_dataset_colormap LABEL_NAMES = np.asarray([ 'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tv' ]) FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1) FULL_COLOR_MAP = get_dataset_colormap.label_to_color_image(FULL_LABEL_MAP) class DeepLabModel(object): """Class to load deeplab model and run inference.""" INPUT_TENSOR_NAME = 'ImageTensor:0' OUTPUT_TENSOR_NAME = 'SemanticPredictions:0' INPUT_SIZE = 513 def __init__(self, model_path): """Creates and loads pretrained deeplab model.""" self.graph = tf.Graph() with open(model_path) as fd: graph_def = tf.GraphDef.FromString(fd.read()) with self.graph.as_default(): tf.import_graph_def(graph_def, name='') self.sess = tf.Session(graph=self.graph)
def save_output_samples(checkpoint_dir, best_chekpnt, eval_preprocess_threads=8, eval_crop_size=[1024, 2048]): eval_batch_size = 1 compressed_reconstructed_dir = os.path.join( checkpoint_dir, 'compressed_reconstructed_images') if not os.path.exists(compressed_reconstructed_dir): os.makedirs(compressed_reconstructed_dir) logger.info('Creating directory ' + compressed_reconstructed_dir + '/') eval_split = 'val' num_sample_output = 20 dataset = data_generator.Dataset( dataset_name='cityscapes', split_name=eval_split, dataset_dir= '/datatmp/Experiments/belbarashy/datasets/Cityscapes/tfrecord/', batch_size=eval_batch_size, crop_size=[int(sz) for sz in eval_crop_size], min_resize_value=None, max_resize_value=None, resize_factor=None, model_variant=None, num_readers=eval_preprocess_threads, is_training=False, should_shuffle=False, should_repeat=False) samples = dataset.get_one_shot_iterator().get_next() in_imgs = samples['image'] / 255 depth = samples['depth'] / 255 labels = samples['label'] num_classes = dataset.num_of_classes # =================================== arch _, _, _, _, _, _, _, _, logits, _ = \ build_model(in_imgs, depth, None, num_classes, mode='testing') # =================================== predictions = tf.argmax(logits, 3) # batch*H*W*1 with tf.Session() as sess: if best_chekpnt is None: latest = tf.train.latest_checkpoint(checkpoint_dir=checkpoint_dir) best_chekpnt = latest tf.train.Saver().restore(sess, save_path=best_chekpnt) for i in range(num_sample_output): test_file_name = str(i) depth_path = os.path.join(compressed_reconstructed_dir, test_file_name + '_depth' + '.png') orig_path = os.path.join(compressed_reconstructed_dir, test_file_name + '_orig' + '.png') map_gt_path = os.path.join(compressed_reconstructed_dir, test_file_name + '_map_gt' + '.png') map_pred_path = os.path.join(compressed_reconstructed_dir, test_file_name + '_map_pred' + '.png') p, l, input_img, dep = sess.run( [predictions, labels, in_imgs, depth]) l = np.squeeze(l) p = np.squeeze(p) input_img = np.squeeze(input_img) dep = np.squeeze(dep) p[l == 255] = 255 colored_label = get_dataset_colormap.label_to_color_image( l, 'cityscapes') colored_pred = get_dataset_colormap.label_to_color_image( p, 'cityscapes') dep_jet = cv2.applyColorMap(np.uint8(dep * (255 * 2)), cv2.COLORMAP_JET) cv2.imwrite(depth_path, dep_jet) colored_pred = np.uint8(colored_pred[:, :, ::-1]) cv2.imwrite(map_pred_path, colored_pred) colored_label = np.uint8(colored_label[:, :, ::-1]) cv2.imwrite(map_gt_path, colored_label) input_img = np.uint8(input_img[:, :, ::-1] * 255) cv2.imwrite(orig_path, input_img)
def _process_batch(sess, original_images, semantic_predictions, instance_predictions, regression_predictions, panoptic_prediction, image_names, image_heights, image_widths, image_id_offset, save_dir, instance_save_dir, instance_seg_save_dir, panoptic_save_dir, instance_offset_save_dir): """Evaluates one single batch qualitatively. Args: sess: TensorFlow session. original_images: One batch of original images. semantic_predictions: One batch of semantic segmentation predictions. instance_predictions: One batch of instance predictions. image_names: Image names. image_heights: Image heights. image_widths: Image widths. image_id_offset: Image id offset for indexing images. save_dir: The directory where the predictions will be saved. instance_save_dir : The directory where the instance predictions will be saved. raw_save_dir: The directory where the raw predictions will be saved. train_id_to_eval_id: A list mapping from train id to eval id. """ (original_images, semantic_predictions, instance_predictions, regression_predictions, instance_segmentation, image_names, image_heights, image_widths) = sess.run([ original_images, semantic_predictions, instance_predictions, regression_predictions, panoptic_prediction, image_names, image_heights, image_widths ]) num_image = semantic_predictions.shape[0] for i in range(num_image): image_height = np.squeeze(image_heights[i]) image_width = np.squeeze(image_widths[i]) original_image = np.squeeze(original_images[i]) semantic_prediction = np.squeeze(semantic_predictions[i]) instance_predictions = np.squeeze(instance_predictions[i]) regression_predictions = np.squeeze(regression_predictions[i]) crop_semantic_prediction = semantic_prediction[:image_height, : image_width] crop_instance_prediction = instance_predictions[:image_height, : image_width] crop_regression_prediction = regression_predictions[:image_height, : image_width, :] instance_segmentation = np.squeeze(instance_segmentation) unique_elements = np.unique(instance_segmentation) instance_segmentation_scaled = np.array(instance_segmentation) * ( 255 // len(unique_elements)) #################### Heatmaps ############################ crop_instance_prediction = np.multiply( crop_instance_prediction / np.amax(crop_instance_prediction), 255) #crop_instance_prediction_mask = np.greater_equal(crop_instance_prediction, 100) crop_instance_prediction_mask = np.greater_equal( crop_instance_prediction, 10) crop_instance_prediction = crop_instance_prediction * crop_instance_prediction_mask mask_single_channel = np.not_equal(crop_instance_prediction, 0) mask_rgb = np.dstack( (mask_single_channel, mask_single_channel, mask_single_channel)) instance_heatmap = cv2.applyColorMap( crop_instance_prediction.astype('uint8'), cv2.COLORMAP_HSV) #instance_heatmap = Image.blend(Image.fromarray(original_image), Image.fromarray(instance_heatmap), 0.2) instance_heatmap = Image.fromarray( np.where(mask_rgb, instance_heatmap, original_image)) ##################### Offset Vectors Prediction ######################### red, green = np.dsplit(crop_regression_prediction, 2) blue = np.zeros_like(red) crop_regression_prediction = np.dstack((red, green, blue)) crop_regression_prediction = np.multiply( np.divide(crop_regression_prediction, np.amax(crop_regression_prediction)), 255) ########## VIS PANOPTIC OUTPUT ################## inst_color = cv2.applyColorMap( instance_segmentation_scaled.astype('uint8'), cv2.COLORMAP_JET) instance_segmentation_coloured = Image.blend( Image.fromarray(original_image), Image.fromarray(inst_color), 0.4) # For Creating boundries around instances # Add boundry to Image colormap_type = FLAGS.colormap_type instance_boundry = np.zeros_like(semantic_prediction) instances = np.delete(unique_elements, 0) for index, i in enumerate(instances): local_instance_mask = instance_segmentation == i kernel = np.ones((5, 5), np.uint8) dilation = cv2.dilate(local_instance_mask.astype('uint8'), kernel, iterations=1) erosion = cv2.erode(local_instance_mask.astype('uint8'), kernel, iterations=1) boundry = (dilation - erosion) * 255 instance_boundry += boundry colored_label = get_dataset_colormap.label_to_color_image( semantic_prediction.astype('uint8'), colormap_type) colored_label = colored_label + np.dstack( (instance_boundry, instance_boundry, instance_boundry)) colored_label = Image.fromarray(colored_label.astype(dtype=np.uint8)) panoptic_output = Image.blend(Image.fromarray(original_image), colored_label, 0.7) # Save image. save_annotation.save_annotation(original_image, save_dir, _IMAGE_FORMAT % (image_id_offset + i), add_colormap=False) # Save instance heatmap prediction. save_annotation.save_annotation_heatmaps( instance_heatmap, instance_save_dir, _PREDICTION_FORMAT % (image_id_offset + i), scale_values=False, add_colormap=False, colormap_type=FLAGS.colormap_type) # Save regression prediction. save_annotation.save_annotation_instance_segmentation( instance_segmentation_coloured, instance_seg_save_dir, _PREDICTION_FORMAT % (image_id_offset + i), normalize_values=False, add_colormap=False, colormap_type=FLAGS.colormap_type) # Save prediction. save_annotation.save_annotation_overlayed( crop_semantic_prediction, original_image, save_dir, _PREDICTION_FORMAT % (image_id_offset + i), add_colormap=True, colormap_type=FLAGS.colormap_type) # Save panoptic prediction. save_annotation.save_annotation_panoptic( panoptic_output, panoptic_save_dir, _PREDICTION_FORMAT % (image_id_offset + i), add_colormap=False, colormap_type=FLAGS.colormap_type) # Save instance_segmentation prediction. save_annotation.save_annotation_offset_vectors( crop_regression_prediction, instance_offset_save_dir, _PREDICTION_FORMAT % (image_id_offset + i))
def testUnExpectedLabelDimensionForLabelToADE20KColorImage(self): label = np.array([250]) with self.assertRaises(ValueError): get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_ade20k_name())
def testUnExpectedLabelDimensionForLabelToPASCALColorImage(self): """Raise ValueError if input dimension is not correct.""" label = np.array([120]) with self.assertRaises(ValueError): get_dataset_colormap.label_to_color_image( label, get_dataset_colormap.get_pascal_name())
return resized_image, seg_map model = DeepLabModel(download_path) ## Helper methods LABEL_NAMES = np.asarray([ 'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tv' ]) FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1) FULL_COLOR_MAP = get_dataset_colormap.label_to_color_image(FULL_LABEL_MAP) TEST_IMAGE_PATHS = [f for f in glob.glob(PATH_TO_TEST_IMAGES_DIR + "/**/*.jpg", recursive=True)] for image_path in TEST_IMAGE_PATHS: image = Image.open(image_path) resized_im, seg_map = model.run(image) seg_image = get_dataset_colormap.label_to_color_image( seg_map, get_dataset_colormap.get_pascal_name()).astype(np.uint8) basename = image_path.replace(PATH_TO_TEST_IMAGES_DIR,"") open_cv_image = np.array(seg_image) open_cv_image = open_cv_image[:, :, ::-1].copy()
def main(unused_argv): tf.logging.set_verbosity(tf.logging.INFO) # Set up deployment (i.e., multi-GPUs and/or multi-replicas). config = model_deploy.DeploymentConfig(num_clones=FLAGS.num_clones, clone_on_cpu=FLAGS.clone_on_cpu, replica_id=FLAGS.task, num_replicas=FLAGS.num_replicas, num_ps_tasks=FLAGS.num_ps_tasks) # Split the batch across GPUs. assert FLAGS.train_batch_size % config.num_clones == 0, ( 'Training batch size not divisble by number of clones (GPUs).') clone_batch_size = FLAGS.train_batch_size // config.num_clones # Get dataset-dependent information. dataset = segmentation_dataset.get_dataset(FLAGS.dataset, FLAGS.train_split, dataset_dir=FLAGS.dataset_dir) with tf.Graph().as_default() as graph: with tf.device(config.inputs_device()): samples = input_generator.get( dataset, FLAGS.train_crop_size, clone_batch_size, min_resize_value=FLAGS.min_resize_value, max_resize_value=FLAGS.max_resize_value, resize_factor=FLAGS.resize_factor, min_scale_factor=FLAGS.min_scale_factor, max_scale_factor=FLAGS.max_scale_factor, scale_factor_step_size=FLAGS.scale_factor_step_size, dataset_split=FLAGS.train_split, is_training=True, model_variant=FLAGS.model_variant) inputs_queue = prefetch_queue.prefetch_queue(samples, capacity=128 * config.num_clones) 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) print(samples) # Create the global step on the device storing the variables. with tf.device(config.variables_device()): global_step = tf.train.get_or_create_global_step() init = tf.global_variables_initializer() with tf.Session() as session: session.run(init) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) print('Start verification process...') try: while True: out_image, out_label = session.run( [samples[common.IMAGE], samples[common.LABEL]]) #write_file("out_label.csv",np.squeeze(out_label[0], axis=2)) cv2.imshow( 'out_image', cv2.cvtColor(out_image[0] / 255, cv2.COLOR_RGB2BGR)) cv2.imshow('out_label', np.asarray(out_label[0] * 100, dtype=np.uint8)) colored_label = get_dataset_colormap.label_to_color_image( np.squeeze(out_label[0]), dataset=get_dataset_colormap.get_pascal_name()) cv2.imshow( "colored_label", cv2.cvtColor(colored_label.astype(np.uint8), cv2.COLOR_RGB2BGR)) alpha = 0.5 img_add = cv2.addWeighted(out_image[0], alpha, colored_label.astype(np.float32), 1 - alpha, 0) cv2.imshow("colored_overlap", cv2.cvtColor(img_add, cv2.COLOR_RGB2BGR) / 255) cv2.waitKey(0) except tf.errors.OutOfRangeError: print("end!") coord.request_stop() coord.join(threads)
def save_annotation(label, save_dir, filename, add_colormap=True, normalize_to_unit_values=False, scale_values=False, flag='N', save_dir_label='./vis/segmentation_labels', colormap_type=get_dataset_colormap.get_pascal_name()): """Saves the given label to image on disk. Args: label: The numpy array to be saved. The data will be converted to uint8 and saved as png image. save_dir: String, the directory to which the results will be saved. filename: String, the image filename. add_colormap: Boolean, add color map to the label or not. normalize_to_unit_values: Boolean, normalize the input values to [0, 1]. scale_values: Boolean, scale the input values to [0, 255] for visualization. colormap_type: String, colormap type for visualization. """ # Add colormap for visualizing the prediction. if add_colormap: colored_label = get_dataset_colormap.label_to_color_image( label, colormap_type) else: colored_label = label if normalize_to_unit_values: min_value = np.amin(colored_label) max_value = np.amax(colored_label) range_value = max_value - min_value if range_value != 0: colored_label = (colored_label - min_value) / range_value if flag == 'Y': from PIL import Image save_color = [ [0, 0, 0], # 0-background [128, 0, 0], # 1-stem [0, 0, 128], # 2-callus [0, 128, 0], # 3-shoot ] palette = list(np.reshape(np.asarray(save_color), (-1))) colored_label = colored_label.astype(np.uint8) colored_label = cv2.resize(colored_label, (new_h, new_w), interpolation=cv2.INTER_NEAREST) save_labelI = Image.new( 'P', (colored_label.shape[1], colored_label.shape[0])) save_labelI.putpalette(palette) save_labelI.paste(Image.fromarray(colored_label), (0, 0)) if scale_values: colored_label = 255. * colored_label pil_image = img.fromarray(colored_label.astype(dtype=np.uint8)) with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f: pil_image.save(f, 'PNG') if not add_colormap and flag == 'Y': with tf.gfile.Open('%s/%s.png' % (save_dir_label, filename), mode='w') as f: # print(os.path.join(save_dir, filename, '.png')) save_labelI.save(f, 'PNG')