def main(tfrecords_filename, label_map=None): """ Visualize the image and label stored in tf record Blatantly stole from https://stackoverflow.com/questions/50391967/how-to-visualize-a-tfrecord :param tfrecords_filename: full path to the tfrecord file :param label_map: None by default :return: """ matplotlib.use('TkAgg') if label_map is not None: label_map_proto = pb.StringIntLabelMap() with tf.gfile.GFile(label_map, 'r') as f: text_format.Merge(f.read(), label_map_proto) class_dict = {} for entry in label_map_proto.item: class_dict[entry.id] = {'name': entry.display_name} sess = tf.Session() decoder = TfDecoder(label_map_proto_file=label_map, use_display_name=False) sess.run(tf.tables_initializer()) for record in tf.python_io.tf_record_iterator(tfrecords_filename): example = decoder.decode(record) host_example = sess.run(example) scores = np.ones(host_example['groundtruth_boxes'].shape[0]) vu.visualize_boxes_and_labels_on_image_array( host_example['image'], host_example['groundtruth_boxes'], host_example['groundtruth_classes'], scores, class_dict, max_boxes_to_draw=None, use_normalized_coordinates=True) plt.imshow(host_example['image']) plt.show()
def read_and_decode(filename_queue): decoder = TfExampleDecoder() reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) tensorDict = decoder.decode(serialized_example) image = tensorDict["image"] boxes = tensorDict["groundtruth_boxes"] #depth = tf.cast(features['depth'], tf.int32) return image, boxes
def __init__(self, data_inputs=None, validation_inputs=None, batch_size=1): """ Constructor :param data_inputs: List of input ops for the model :param validation_inputs: List of validation ops for the model :param batch_size: Batch size for the data """ self._validation_inputs = validation_inputs self._data_inputs = data_inputs self._batch_size = batch_size if data_inputs is None: self._data_inputs = ['image_tensor'] else: self._data_inputs = data_inputs self.keys_to_features = TfExampleDecoder().keys_to_features self.items_to_handlers = { fields.InputDataFields.image: (slim_example_decoder.Image(image_key='image/encoded', format_key='image/format', channels=3)), fields.InputDataFields.source_id: (slim_example_decoder.Tensor('image/source_id')), }
def draw_tfrecord(tfrecords_filename, label_map=None): """ Draw the image and bounding box indicated in the groundtruth tf record file. :param tfrecords_filename: :param label_map: :return: """ if label_map is not None: label_map_proto = pb.StringIntLabelMap() with tf.gfile.GFile(label_map, 'r') as f: text_format.Merge(f.read(), label_map_proto) class_dict = {} for entry in label_map_proto.item: class_dict[entry.id] = {'name': entry.display_name} sess = tf.Session() decoder = TfDecoder(label_map_proto_file=label_map, use_display_name=False) sess.run(tf.tables_initializer()) iter_num = 0 for record in tf.python_io.tf_record_iterator(tfrecords_filename): iter_num += 1 print("This is the {} example".format(iter_num)) example = decoder.decode(record) host_example = sess.run(example) # Set the score of the groundth truth bounding box as 1 scores = np.ones(host_example['groundtruth_boxes'].shape[0]) vu.visualize_boxes_and_labels_on_image_array( host_example['image'], host_example['groundtruth_boxes'], host_example['groundtruth_classes'], scores, class_dict, max_boxes_to_draw=None, use_normalized_coordinates=True) plt.imshow(host_example['image']) plt.show(block=False) plt.pause(.1) plt.close("all")
from object_detection.data_decoders.tf_example_decoder import TfExampleDecoder from object_detection.utils.flow_util import flow_to_color, flow_error_image, flow_error_avg from object_detection.utils.np_motion_util import dense_flow_from_motion, _rotation_angle from object_detection.utils.visualization_utils import visualize_flow with tf.Graph().as_default(): #file_pattern = 'object_detection/data/records/vkitti_train/00000-of-00020.record' file_pattern = 'object_detection/data/records/kitti_train/00000-of-00002.record' tfrecords = glob.glob(file_pattern) with tf.device('/cpu:0'): filename_queue = tf.train.string_input_producer( tfrecords, capacity=len(tfrecords)) reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) example = TfExampleDecoder().decode(serialized_example) flow_color = flow_to_color( tf.expand_dims(example['groundtruth_flow'], 0))[0, :, :, :] print(example.keys()) sess = tf.Session() init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) tf.train.start_queue_runners(sess=sess) out_dir = 'object_detection/output/tests/vkitti/' if os.path.isdir(out_dir): shutil.rmtree(out_dir) os.makedirs(out_dir)
def process_detections(detections_record, categories): record_iterator = tf.python_io.tf_record_iterator(path=detections_record) data_parser = tf_example_parser.TfExampleDetectionAndGTParser() confusion_matrix = np.zeros(shape=(len(categories) + 1, len(categories) + 1)) num_shown = 0 image_index = 0 os.makedirs(detectionDirName, exist_ok=True) for string_record in record_iterator: example = tf.train.Example() example.ParseFromString(string_record) decoded_dict = data_parser.parse(example) image_index += 1 if decoded_dict: groundtruth_boxes = decoded_dict[ standard_fields.InputDataFields.groundtruth_boxes] groundtruth_classes = decoded_dict[ standard_fields.InputDataFields.groundtruth_classes] detection_scores = decoded_dict[ standard_fields.DetectionResultFields.detection_scores] detection_classes = decoded_dict[ standard_fields.DetectionResultFields.detection_classes][ detection_scores >= CONFIDENCE_THRESHOLD] detection_boxes = decoded_dict[ standard_fields.DetectionResultFields.detection_boxes][ detection_scores >= CONFIDENCE_THRESHOLD] if num_shown < NUM_TO_SHOW: # Convert encoded image in example TF Record to image testing = TfDecoder() features = testing.decode(string_record) image = features['image'] with tf.Session() as sess: image = image.eval() im = PIL.Image.fromarray(image) for box in groundtruth_boxes: vis_util.draw_bounding_box_on_image( im, box[0] * IMAGE_HEIGHT, box[1] * IMAGE_WIDTH, box[2] * IMAGE_HEIGHT, box[3] * IMAGE_WIDTH, color='red', thickness=1, use_normalized_coordinates=False) for box in detection_boxes: vis_util.draw_bounding_box_on_image( im, box[0] * IMAGE_HEIGHT, box[1] * IMAGE_WIDTH, box[2] * IMAGE_HEIGHT, box[3] * IMAGE_WIDTH, color='blue', thickness=1, use_normalized_coordinates=False) # UNCOMMENT TO DISPLAY IMAGES W/ BoundingBox #plt.imshow(np.asarray(im)) #plt.show() # Code to create directory & save images w/ bounding boxes filename = decoded_dict['key'] im.save(detectionDirName + "/" + filename) num_shown += 1 matches = [] if image_index % 100 == 0: print("Processed %d images" % (image_index)) for i in range(len(groundtruth_boxes)): for j in range(len(detection_boxes)): iou = compute_iou(groundtruth_boxes[i], detection_boxes[j]) if iou > IOU_THRESHOLD: matches.append([i, j, iou]) matches = np.array(matches) if matches.shape[0] > 0: # Sort list of matches by descending IOU so we can remove duplicate detections # while keeping the highest IOU entry. matches = matches[matches[:, 2].argsort()[::-1][:len(matches)]] # Remove duplicate detections from the list. matches = matches[np.unique(matches[:, 1], return_index=True)[1]] # Sort the list again by descending IOU. Removing duplicates doesn't preserve # our previous sort. matches = matches[matches[:, 2].argsort()[::-1][:len(matches)]] # Remove duplicate ground truths from the list. matches = matches[np.unique(matches[:, 0], return_index=True)[1]] for i in range(len(groundtruth_boxes)): if matches.shape[0] > 0 and matches[matches[:, 0] == i].shape[0] == 1: confusion_matrix[groundtruth_classes[i] - 1][ detection_classes[int(matches[matches[:, 0] == i, 1][0])] - 1] += 1 else: confusion_matrix[groundtruth_classes[i] - 1][confusion_matrix.shape[1] - 1] += 1 for i in range(len(detection_boxes)): if matches.shape[0] > 0 and matches[matches[:, 1] == i].shape[0] == 0: confusion_matrix[confusion_matrix.shape[0] - 1][detection_classes[i] - 1] += 1 else: print("Skipped image %d" % (image_index)) print("Processed %d images" % (image_index)) return confusion_matrix