Exemplo n.º 1
0
def _convert_to_example(image_data, shape, bboxes, labels, labels_text):
    xmin = []
    ymin = []
    xmax = []
    ymax = []
    for b in bboxes:
        assert len(b) == 4
        # pylint: disable=expression-not-assigned
        [l.append(point) for l, point in zip([ymin, xmin, ymax, xmax], b)]
        # pylint: enable=expression-not-assigned

    image_format = b'PNG'
    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': int64_feature(shape[0]),
            'image/width': int64_feature(shape[1]),
            'image/channels': int64_feature(shape[2]),
            'image/shape': int64_feature(shape),
            'image/object/bbox/xmin': float_feature(xmin),
            'image/object/bbox/xmax': float_feature(xmax),
            'image/object/bbox/ymin': float_feature(ymin),
            'image/object/bbox/ymax': float_feature(ymax),
            'image/object/bbox/label': int64_feature(labels),
            'image/object/bbox/label_text': bytes_feature(labels_text),
            'image/format': bytes_feature(image_format),
            'image/encoded': bytes_feature(image_data)
        }))
    return example
Exemplo n.º 2
0
def _convert_to_example(image_data, shape, charbb, bbox, label,imname):
	
	nbbox = np.array(bbox)
	ymin = list(nbbox[:, 0])
	xmin = list(nbbox[:, 1])
	ymax = list(nbbox[:, 2])
	xmax = list(nbbox[:, 3])

	#print 'shape: {}, height:{}, width:{}'.format(shape,shape[0],shape[1])
	example = tf.train.Example(features=tf.train.Features(feature={
			'image/height': int64_feature(shape[0]),
			'image/width': int64_feature(shape[1]),
			'image/channels': int64_feature(shape[2]),
			'image/shape': int64_feature(shape),
			'image/object/bbox/x0': float_feature(charbb[0,0,:].tolist()),
			'image/object/bbox/x1': float_feature(charbb[0,1,:].tolist()),
			'image/object/bbox/x2': float_feature(charbb[0,2,:].tolist()),
			'image/object/bbox/x3': float_feature(charbb[0,3,:].tolist()),
			'image/object/bbox/y0': float_feature(charbb[1,0,:].tolist()),
			'image/object/bbox/y1': float_feature(charbb[1,1,:].tolist()),
			'image/object/bbox/y2': float_feature(charbb[1,2,:].tolist()),
			'image/object/bbox/y3': float_feature(charbb[1,3,:].tolist()),
			'image/object/bbox/ymin': float_feature(ymin),
			'image/object/bbox/xmin': float_feature(xmin),
			'image/object/bbox/ymax': float_feature(ymax),
			'image/object/bbox/xmax': float_feature(xmax),
			'image/object/bbox/label': int64_feature(label),
			'image/format': bytes_feature('jpeg'),
			'image/encoded': bytes_feature(image_data),
			'image/name': bytes_feature(imname.tostring()),
			}))
	return example
Exemplo n.º 3
0
def build_example(user, image, text, label, file):
    return tf.train.Example(features=tf.train.Features(
        feature={
            user_key: dataset_utils.bytes_feature(user),
            image_key: dataset_utils.float_feature(image),
            text_key: dataset_utils.int64_feature(text),
            label_key: dataset_utils.int64_feature(label),
            file_key: dataset_utils.bytes_feature(file),
        }))
Exemplo n.º 4
0
def my_image_to_tfexample(image_data, image_format, filename, height, width):
    return tf.train.Example(features=tf.train.Features(
        feature={
            'image/encoded': bytes_feature(image_data),
            'image/format': bytes_feature(image_format),
            'image/filename': bytes_feature(filename),
            'image/height': int64_feature(height),
            'image/width': int64_feature(width),
        }))
def _process_image(directory, split, name):
    # Read the image file.
    filename = os.path.join(directory, 'image_2', name + '.png')
    image_data = tf.gfile.FastGFile(filename, 'r').read()

    # Get shape
    img = cv2.imread(filename)
    shape = np.shape(img)

    label_list = []
    type_list = []

    bbox_x1_list = []
    bbox_y1_list = []
    bbox_x2_list = []
    bbox_y2_list = []


    # If 'test' split, skip annotations
    if re.findall(r'train', split):
      # Read the txt annotation file.
      filename = os.path.join(directory, 'label_2', name + '.txt')
      with open(filename) as anno_file:
        objects = anno_file.readlines()

      for object in objects:
          obj_anno = object.split(' ')
          type_txt = obj_anno[0].encode('ascii')
          if type_txt in CLASSES:
            label_list.append(CLASSES[type_txt])
            type_list.append(type_txt)

            # Bounding Box
            bbox_x1 = float(obj_anno[4])
            bbox_y1 = float(obj_anno[5])
            bbox_x2 = float(obj_anno[6])
            bbox_y2 = float(obj_anno[7])
            bbox_x1_list.append(bbox_x1)
            bbox_y1_list.append(bbox_y1)
            bbox_x2_list.append(bbox_x2)
            bbox_y2_list.append(bbox_y2)

    image_format = b'PNG'
    example = tf.train.Example(features=tf.train.Features(feature={
            'image/encoded': bytes_feature(image_data),
            'image/height': int64_feature(shape[0]),
            'image/width': int64_feature(shape[1]),
            'image/channels': int64_feature(shape[2]),
            'image/shape': int64_feature(shape),
            'image/object/bbox/xmin': float_feature(bbox_x1_list),
            'image/object/bbox/xmax': float_feature(bbox_x2_list),
            'image/object/bbox/ymin': float_feature(bbox_y1_list),
            'image/object/bbox/ymax': float_feature(bbox_y2_list),
            'image/object/bbox/label': int64_feature(label_list),
            'image/object/bbox/label_text': bytes_feature(type_list),
    }))
    return example
def _convert_to_example(image_data, labels, bboxes, mask_data):
    """Build an Example proto for an image example.

    Args:
      image_data: string, JPEG encoding of RGB image;
      labels: list of integers, identifier for the ground truth;
      labels_text: list of strings, human-readable labels;
      bboxes: list of bounding boxes; each box is a list of integers;
          specifying [xmin, ymin, xmax, ymax]. All boxes are assumed to belong
          to the same label as the image label.
      shape: 3 integers, image shapes in pixels.
    Returns:
      Example proto
    """
    xmin = []
    ymin = []
    xmax = []
    ymax = []
    for b in bboxes:
        assert len(b) == 4
        # pylint: disable=expression-not-assigned
        [l.append(point) for l, point in zip([ymin, xmin, ymax, xmax], b)]
        # pylint: enable=expression-not-assigned

    # image_format = b'JPEG'
    image_format = b'PNG'
    if mask_data is None:
        example = tf.train.Example(features=tf.train.Features(feature={
                'image/height': int64_feature(512),
                'image/width': int64_feature(512),
                'image/channels': int64_feature(3),
                'image/shape': int64_feature([512, 512, 3]),
                'image/object/bbox/xmin': float_feature(xmin),
                'image/object/bbox/xmax': float_feature(xmax),
                'image/object/bbox/ymin': float_feature(ymin),
                'image/object/bbox/ymax': float_feature(ymax),
                'image/object/bbox/label': int64_feature(labels),
                'image/format': bytes_feature(image_format),
                'image/encoded': bytes_feature(image_data)}))
    else:
        example = tf.train.Example(features=tf.train.Features(feature={
            'image/height': int64_feature(512),
            'image/width': int64_feature(512),
            'image/channels': int64_feature(3),
            'image/shape': int64_feature([512, 512, 3]),
            'image/object/bbox/xmin': float_feature(xmin),
            'image/object/bbox/xmax': float_feature(xmax),
            'image/object/bbox/ymin': float_feature(ymin),
            'image/object/bbox/ymax': float_feature(ymax),
            'image/object/bbox/label': int64_feature(labels),
            'image/format': bytes_feature(image_format),
            'image/encoded': bytes_feature(image_data),
            'maskimage/encoded': bytes_feature(mask_data)}))
    return example
Exemplo n.º 7
0
def image_to_tfexample(image_data, image_format, height, width, class_id,
                       fname):
    return tf.train.Example(features=tf.train.Features(
        feature={
            'image/encoded': dataset_utils.bytes_feature(image_data),
            'image/format': dataset_utils.bytes_feature(image_format),
            'image/class/label': dataset_utils.int64_feature(class_id),
            'image/height': dataset_utils.int64_feature(height),
            'image/width': dataset_utils.int64_feature(width),
            'image/name': dataset_utils.bytes_feature(fname),
        }))
def dict_to_tf_example(image_name,
                       dataset_directory,
                       label_map=None,
                       image_subdirectory='train'):
    """
    Args:
      image: a single image name
      dataset_directory: Path to root directory holding PCam dataset
      label_map: A map from string label names to integers ids.
      image_subdirectory: String specifying subdirectory within the
        PCam dataset directory holding the actual image data.

    Returns:
      example: The converted tf.Example.

    Raises:
      ValueError: if the image pointed to by image is not a valid PNG
    """
    # full_path = os.path.join(dataset_directory, image_subdirectory, image_name)
    full_path = os.path.join(dataset_directory, image_name)
    with tf.io.gfile.GFile(full_path, 'rb') as fid:
        encoded = fid.read()
    encoded_io = io.BytesIO(encoded)
    image = Image.open(encoded_io)
    width, height = image.size
    format = image.format
    image_stat = ImageStat.Stat(image)
    mean = image_stat.mean
    std = image_stat.stddev
    key = hashlib.sha256(encoded).hexdigest()
    # if image_subdirectory.lower() == 'test':
    #     label = -1
    # else:
    #     label = int(label_map[image_name])
    label = int(label_map[full_path])

    example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': dataset_utils.int64_feature(height),
        'image/width': dataset_utils.int64_feature(width),
        'image/filename': dataset_utils.bytes_feature(image_name.encode('utf8')),
        'image/fullpath': dataset_utils.bytes_feature(full_path.encode('utf8')),
        'image/source_id': dataset_utils.bytes_feature(image_name.encode('utf8')),
        'image/key/sha256': dataset_utils.bytes_feature(key.encode('utf8')),
        'image/encoded': dataset_utils.bytes_feature(encoded),
        'image/format': dataset_utils.bytes_feature(format.encode('utf8')),
        'image/class/label': dataset_utils.int64_feature(label),
        # 'image/text': dataset_util.bytes_feature('label_text'.encode('utf8'))
        'image/mean': dataset_utils.float_list_feature(mean),
        'image/std': dataset_utils.float_list_feature(std)
    }))
    return example
Exemplo n.º 9
0
  def _convert_to_example(filename, image_data, height, width, current_file_info, common_info):
    colorspace = 'RGB'
    channels = 3
    image_format = 'JPEG'

    example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': dataset_utils.int64_feature(height),
      'image/width': dataset_utils.int64_feature(width),
      'image/colorspace': dataset_utils.bytes_feature(colorspace),
      'image/channels': dataset_utils.int64_feature(channels),
      'image/format': dataset_utils.bytes_feature(image_format),
      'image/filename': dataset_utils.bytes_feature(os.path.basename(filename)),
      'image/encoded': dataset_utils.bytes_feature(image_data)}))
    return example
Exemplo n.º 10
0
def _convert_to_example(img_data, label):
    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/data': bytes_feature(img_data),
            'image/label': int64_feature(label)
        }))
    return example
Exemplo n.º 11
0
 def __convert_to_example(self, image_data, label, name,image_format):
     example = tf.train.Example(features=tf.train.Features(feature={
             'image/label': int64_feature(label),
             'image/filename': bytes_feature(name.encode('utf-8')),
             'image/format': bytes_feature(image_format),
             'image/encoded': bytes_feature(image_data)}))
     return example
Exemplo n.º 12
0
def _convert_to_example(data_example):
    """Build an Example proto for an image example.

    Args:
      image_data: string, JPEG encoding of RGB image;
      labels: list of integers, identifier for the ground truth;
      labels_text: list of strings, human-readable labels;
      bboxes: list of bounding boxes; each box is a list of integers;
          specifying [xmin, ymin, xmax, ymax]. All boxes are assumed to belong
          to the same label as the image label.
      shape: 3 integers, image shapes in pixels.
    Returns:
      Example proto
    """
    xmin = []
    ymin = []
    xmax = []
    ymax = []
    shape = []
    difficult = []
    truncated = []
    label_text =[]
    for i in range(len(data_example['bboxes'])):
        difficult.append(0)
        truncated.append(0)
        label_text.append(b'face')

    for s in data_example['shape']:
        shape.append(s)
    # print(shape)
    image_data = data_example['image']
    filename = data_example['name']
    for bbox in data_example['bboxes']:
        assert len(bbox) == 4
        # pylint: disable=expression-not-assigned
        xmin.append(bbox['xmin'] / shape[1])
        ymin.append(bbox['ymin'] / shape[0])
        xmax.append(bbox['xmax'] / shape[1])
        ymax.append(bbox['ymax'] / shape[0])
        # pylint: enable=expression-not-assigned

    # print(xmin)
    image_format = b'JPEG'
    example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': int64_feature(shape[0]),
        'image/width': int64_feature(shape[1]),
        'image/channels': int64_feature(shape[2]),
        'image/shape': int64_feature(shape),
        'image/object/bbox/xmin': float_feature(xmin),
        'image/object/bbox/xmax': float_feature(xmax),
        'image/object/bbox/ymin': float_feature(ymin),
        'image/object/bbox/ymax': float_feature(ymax),
        'image/object/bbox/label': int64_feature(data_example['labels']),
        'image/object/bbox/label_text': bytes_feature(label_text),
        'image/object/bbox/difficult': int64_feature(difficult),
        'image/object/bbox/truncated': int64_feature(truncated),
        'image/format': bytes_feature(image_format),
        'image/filename': bytes_feature(filename.encode('utf-8')),
        'image/encoded': bytes_feature(image_data)}))
    return example
Exemplo n.º 13
0
def _convert_to_example(image_data, labels, labels_text, bboxes, shape,
                        difficult, truncated):
    """Build an Example proto for an image example.

    :return: Example proto.
    """
    xmin = []
    ymin = []
    xmax = []
    ymax = []
    for b in bboxes:
        assert len(b) == 4
        [l.append(point) for l, point in zip([ymin, xmin, ymax, xmax], b)]

    image_format = b'JPEG'
    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': int64_feature(shape[0]),
            'image/width': int64_feature(shape[1]),
            'image/channels': int64_feature(shape[2]),
            'image/shape': int64_feature(shape),
            'image/object/bbox/xmin': float_feature(xmin),
            'image/object/bbox/xmax': float_feature(xmax),
            'image/object/bbox/ymin': float_feature(ymin),
            'image/object/bbox/ymax': float_feature(ymax),
            'image/object/bbox/label': int64_feature(labels),
            'image/object/bbox/label_text': bytes_feature(labels_text),
            'image/object/bbox/difficult': int64_feature(difficult),
            'image/object/bbox/truncated': int64_feature(truncated),
            'image/format': bytes_feature(image_format),
            'image/encoded': bytes_feature(image_data)
        }))
    return example
Exemplo n.º 14
0
def _convert2example(image_data, shape, bboxes, labels,
                     labels_text, difficult, truncated):

    y_min = []
    x_min = []
    y_max = []
    x_max = []
    for b in bboxes:
        assert len(b) == 4
        [xy.append(point) for xy, point in zip([y_min, x_min, y_max, x_max], b)]

    image_format = b'JPEG'
    example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': int64_feature(shape[0]),
        'image/width': int64_feature(shape[1]),
        'image/channels': int64_feature(shape[2]),
        'image/shape': int64_feature(shape),
        'image/object/bbox/xmin': float_feature(x_min),
        'image/object/bbox/xmax': float_feature(x_max),
        'image/object/bbox/ymin': float_feature(y_min),
        'image/object/bbox/ymax': float_feature(y_max),
        'image/object/bbox/label': int64_feature(labels),
        'image/object/bbox/label_text': bytes_feature(labels_text),
        'image/object/bbox/difficult': int64_feature(difficult),
        'image/object/bbox/truncated': int64_feature(truncated),
        'image/format': bytes_feature(image_format),
        'image/encoded': bytes_feature(image_data)
    }))
    return example
Exemplo n.º 15
0
def _conver_to_example(image_data, label):
    image_format = b'JPEG'
    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/encoded': dataset_utils.bytes_feature(image_data),
            'image/format': dataset_utils.bytes_feature(image_format),
            'image/classes/label': dataset_utils.int64_feature(label)
        }))
    return example
Exemplo n.º 16
0
def _convert_to_example(image_data, shape, bbox, label):
	nbbox = np.array(bbox)
	ymin = list(nbbox[:, 0])
	xmin = list(nbbox[:, 1])
	ymax = list(nbbox[:, 2])
	xmax = list(nbbox[:, 3])

	#print 'shape: {}, height:{}, width:{}'.format(shape,shape[0],shape[1])
	example = tf.train.Example(features=tf.train.Features(feature={
			'image/shape': int64_feature(list(shape)),
			'image/object/bbox/ymin': float_feature(ymin),
			'image/object/bbox/xmin': float_feature(xmin),
			'image/object/bbox/ymax': float_feature(ymax),
			'image/object/bbox/xmax': float_feature(xmax),
			'image/object/bbox/label': int64_feature(label),
			'image/format': bytes_feature('jpeg'),
			'image/encoded': bytes_feature(image_data),
			}))
	return example
    def _convert_to_example(filename, image_buffer, height, width,
                            current_file_info, common_info):
        """Build an Example proto for an example.

    Args:
      filename: string, path to an image file, e.g., '/path/to/example.JPG'
      image_buffer: string, JPEG encoding of RGB image
      height: integer, image height in pixels
      width: integer, image width in pixels
      current_file_info:  equivalent to label: integer, identifier for the ground truth for the network
      common_info: a list of tags with format: ('type', 'ambiguous', 'count', 'name', 'id')

    Returns:
      Example proto
    """
        colorspace = 'RGB'
        channels = 3
        image_format = 'JPEG'
        human_readable_tags = DanbooruDataConverter._tag_to_human_readable(
            current_file_info, common_info)

        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/height':
                dataset_utils.int64_feature(height),
                'image/width':
                dataset_utils.int64_feature(width),
                'image/colorspace':
                dataset_utils.bytes_feature(colorspace),
                'image/channels':
                dataset_utils.int64_feature(channels),
                'image/class/label':
                dataset_utils.int64_feature(current_file_info),
                'image/class/text':
                dataset_utils.bytes_feature(human_readable_tags),
                'image/format':
                dataset_utils.bytes_feature(image_format),
                'image/filename':
                dataset_utils.bytes_feature(os.path.basename(filename)),
                'image/encoded':
                dataset_utils.bytes_feature(image_buffer)
            }))
        return example
Exemplo n.º 18
0
def oneshot_to_tfexample(feature_map, class_id):
    num = len(
        feature_map
    )  # samples num in one train item, including support set and target
    feature = {}

    for i in range(num):
        feature['feature%d' % i] = dataset_utils.bytes_feature(
            feature_map[i].tostring())
        feature['label%d' % i] = dataset_utils.int64_feature(class_id[i])

    return tf.train.Example(features=tf.train.Features(feature=feature))
def _convert_to_example(image_data, labels, labels_text, bboxes, shape):
    """Build an Example proto for an image example.

    Args:
      image_data: string, JPEG encoding of RGB image;
      labels: list of integers, identifier for the ground truth;
      labels_text: list of strings, human-readable labels;
      bboxes: list of bounding boxes; each box is a list of integers;
          specifying [xmin, ymin, xmax, ymax]. All boxes are assumed to belong
          to the same label as the image label.
      shape: 3 integers, image shapes in pixels.
    Returns:
      Example proto
    """
    xmin = []
    ymin = []
    xmax = []
    ymax = []
    for b in bboxes:
        assert len(b) == 4
        # pylint: disable=expression-not-assigned
        [l.append(point) for l, point in zip([xmin, ymin, xmax, ymax], b)]
        # pylint: enable=expression-not-assigned

    image_format = b'JPEG'
    example = tf.train.Example(features=tf.train.Features(feature={
            'image/height': int64_feature(shape[0]),
            'image/width': int64_feature(shape[1]),
            'image/channels': int64_feature(shape[2]),
            'image/shape': int64_feature(shape),
            'image/object/bbox/xmin': float_feature(xmin),
            'image/object/bbox/xmax': float_feature(xmax),
            'image/object/bbox/ymin': float_feature(ymin),
            'image/object/bbox/ymax': float_feature(ymax),
            'image/object/bbox/label': int64_feature(labels),
            'image/object/bbox/label_text': bytes_feature(labels_text),
            'image/format': bytes_feature(image_format),
            'image/encoded': bytes_feature(image_data)}))
    return example
Exemplo n.º 20
0
    def _convert_to_example(filename, image_data, height, width,
                            current_file_info, shared_info):
        colorspace = 'RGB'
        channels = 3
        image_format = 'JPEG'
        (x_expanded, y_expanded, w_expanded, h_expanded, image_w, image_h,
         tags_id, original_image, face_xywh) = current_file_info

        feature = {
            'image/x':
            dataset_utils.int64_feature(x_expanded),
            'image/y':
            dataset_utils.int64_feature(y_expanded),
            'image/height':
            dataset_utils.int64_feature(h_expanded),
            'image/width':
            dataset_utils.int64_feature(w_expanded),
            'image/face_xywh':
            dataset_utils.float_feature(face_xywh),
            # 'image/left_eye_xywh': dataset_utils.float_feature(left_eye_xywh),
            # 'image/right_eye_xywh': dataset_utils.float_feature(right_eye_xywh),
            # 'image/mouth_xywh': dataset_utils.float_feature(mouth_xywh),
            'image/colorspace':
            dataset_utils.bytes_feature(colorspace),
            'image/channels':
            dataset_utils.int64_feature(channels),
            'image/format':
            dataset_utils.bytes_feature(image_format),
            'image/filename':
            dataset_utils.bytes_feature(os.path.basename(filename)),
            'image/encoded':
            dataset_utils.bytes_feature(image_data),
            # Encoding original takes up too much space. Not recommended.
            # 'image/original': dataset_utils.bytes_feature(original_image),
        }
        example = tf.train.Example(features=tf.train.Features(feature=feature))
        return example
Exemplo n.º 21
0
def _convert_to_example(image_data, gt_lanes, y_samples, shape):
    """Build an Example proto for an image example.

    Args:
      image_data: string, JPEG encoding of RGB image;
      labels: list of integers, identifier for the ground truth;
      labels_text: list of strings, human-readable labels;
      bboxes: list of bounding boxes; each box is a list of integers;
          specifying [xmin, ymin, xmax, ymax]. All boxes are assumed to belong
          to the same label as the image label.
      shape: 3 integers, image shapes in pixels.
    Returns:
      Example proto
    """
    if len(gt_lanes) < 5:
        padding_list = [-2] * 56
        padding = 5 - len(gt_lanes)
        for i in range(padding):
            gt_lanes.append(padding_list)

    image_format = b'JPG'
    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': int64_feature(shape[0]),
            'image/width': int64_feature(shape[1]),
            'image/channels': int64_feature(shape[2]),
            'image/shape': int64_feature(shape),
            'image/line1': float_feature(gt_lanes[0]),
            'image/line2': float_feature(gt_lanes[1]),
            'image/line3': float_feature(gt_lanes[2]),
            'image/line4': float_feature(gt_lanes[3]),
            'image/line5': float_feature(gt_lanes[4]),
            'image/ysamples': float_feature(y_samples),
            'image/format': bytes_feature(image_format),
            'image/encoded': bytes_feature(image_data)
        }))
    return example
def _convert_to_example(image_data, shape, labels, labels_text,
                        bboxes):
    """Build an Example proto for an image example.

    Args:
      image_data: string, PNG encoding of RGB image;
      labels: list of integers, identifier for the ground truth;
      labels_text: list of strings, human-readable labels;
      bboxes: list of bounding boxes; each box is a list of integers;
          specifying [xmin, ymin, xmax, ymax]. All boxes are assumed to belong
          to the same label as the image label.
      shape: 3 integers, image shapes in pixels.
    Returns:
      Example proto
    """
    # Transpose bboxes, dimensions and locations.
    bboxes = list(map(list, zip(*bboxes)))
    # Iterators.
    it_bboxes = iter(bboxes)

    image_format = b'JPEG'
    example = tf.train.Example(features=tf.train.Features(feature={
            'image/height': int64_feature(shape[0]),
            'image/width': int64_feature(shape[1]),
            'image/channels': int64_feature(shape[2]),
            'image/shape': int64_feature(shape),
            'image/format': bytes_feature(image_format),
            'image/encoded': bytes_feature(image_data),
            'object/label': int64_feature(labels),
            'object/label_text': bytes_feature(labels_text),
            'object/bbox/xmin': float_feature(next(it_bboxes, [])),
            'object/bbox/ymin': float_feature(next(it_bboxes, [])),
            'object/bbox/xmax': float_feature(next(it_bboxes, [])),
            'object/bbox/ymax': float_feature(next(it_bboxes, [])),
            }))
    return example
Exemplo n.º 23
0
def _convert_to_example(image_data_np, tumor_fully_mask_np, mask_np, liver_mask_np, bbox_np):
    """Build an Example proto for an image example.

    Args:
      image_data: string, JPEG encoding of RGB image;
      labels: list of integers, identifier for the ground truth;
      labels_text: list of strings, human-readable labels;
      bboxes: list of bounding boxes; each box is a list of integers;
          specifying [xmin, ymin, xmax, ymax]. All boxes are assumed to belong
          to the same label as the image label.
      shape: 3 integers, image shapes in pixels.
    Returns:
      Example proto
    """
    bbox_np = np.asarray(np.asarray(bbox_np, np.float32) / 512., np.float32)
    xmin = []
    ymin = []
    xmax = []
    ymax = []
    labels = []
    for b in bbox_np:
        assert len(b) == 4
        # pylint: disable=expression-not-assigned
        [l.append(point) for l, point in zip([ymin, xmin, ymax, xmax], b)]
        labels.append(1)
    image_format = b'PNG'
    example = tf.train.Example(features=tf.train.Features(feature={
        # 'image/height': int64_feature(512),
        # 'image/width': int64_feature(512),
        # 'image/channels': int64_feature(3),
        # 'image/shape': int64_feature([512, 512, 3]),
        'image/format': bytes_feature(image_format),
        # 'image/encoded': bytes_feature(np.asarray(image_data_np, np.float32).tostring()),
        # 'livermask/encoded': bytes_feature(np.asarray(liver_mask_np, np.uint8).tostring()),
        # 'fullAnnTumorMask/encoded': bytes_feature(np.asarray(tumor_fully_mask_np, np.uint8).tostring()),
        # 'maskimage/encoded': bytes_feature(np.asarray(mask_np, np.uint8).tostring())}))
        'image/encoded': EncodedFloatFeature(np.asarray(image_data_np, np.float32)),
        'livermask/encoded': EncodedInt64Feature(np.asarray(liver_mask_np, np.int64)),
        'fullAnnTumorMask/encoded': EncodedInt64Feature(np.asarray(tumor_fully_mask_np, np.int64)),
        'maskimage/encoded': EncodedInt64Feature(np.asarray(mask_np, np.int64)),
        'image/object/bbox/xmin': float_feature(xmin),
        'image/object/bbox/xmax': float_feature(xmax),
        'image/object/bbox/ymin': float_feature(ymin),
        'image/object/bbox/ymax': float_feature(ymax),
        'image/object/bbox/label': int64_feature(labels)}))

    return example
Exemplo n.º 24
0
def _convert_to_example_multiphase_multislice_mask(
        nc_image_data, art_image_data, pv_image_data, mask_image_data, labels,
        labels_text, bboxes, shape, difficult, truncated):
    """Build an Example proto for an image example.

    Args:
      image_data: string, JPEG encoding of RGB image;
      labels: list of integers, identifier for the ground truth;
      labels_text: list of strings, human-readable labels;
      bboxes: list of bounding boxes; each box is a list of integers;
          specifying [xmin, ymin, xmax, ymax]. All boxes are assumed to belong
          to the same label as the image label.
      shape: 3 integers, image shapes in pixels.
    Returns:
      Example proto
    """
    xmin = []
    ymin = []
    xmax = []
    ymax = []
    for b in bboxes:
        assert len(b) == 4
        # pylint: disable=expression-not-assigned
        [l.append(point) for l, point in zip([ymin, xmin, ymax, xmax], b)]
        # pylint: enable=expression-not-assigned

    image_format = suffix_type
    print('image_format is ', image_format)
    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': int64_feature(shape[0]),
            'image/width': int64_feature(shape[1]),
            'image/channels': int64_feature(shape[2]),
            'image/shape': int64_feature(shape),
            'image/object/bbox/xmin': float_feature(xmin),
            'image/object/bbox/xmax': float_feature(xmax),
            'image/object/bbox/ymin': float_feature(ymin),
            'image/object/bbox/ymax': float_feature(ymax),
            'image/object/bbox/label': int64_feature(labels),
            'image/object/bbox/label_text': bytes_feature(labels_text),
            'image/object/bbox/difficult': int64_feature(difficult),
            'image/object/bbox/truncated': int64_feature(truncated),
            'image/format': bytes_feature(image_format),
            'image/mask/encoded': bytes_feature(mask_image_data),
            'image/nc/encoded': bytes_feature(nc_image_data),
            'image/art/encoded': bytes_feature(art_image_data),
            'image/pv/encoded': bytes_feature(pv_image_data)
        }))
    return example
Exemplo n.º 25
0
def create_sequence_examples(question, answer, distractors, word_model):
    """ Creates multiple sequence examples for distractor encoder model
    Args:
    question: string containing question
    answer: string containing answer
    distractors: list of strings containing distractors

    Yields:
    tf.train.SequenceExample
    """
    try:
        sampled_negatives = word_model.most_similar_cosmul(
            positive=[answer], topn=200)
    except KeyError:
        return
    distractors_lower = set([d.lower() for d in distractors])
    sampled_negatives = [
        word_model[w] for w, _ in sampled_negatives
        if w.lower() not in distractors_lower
    ][:100]

    for d in distractors:
        try:
            dist_vector = word_model[d]
        except KeyError:
            continue
        question_feature = bytes_feature(question.encode("utf-8"))
        answer_feature = bytes_feature(answer.encode("utf-8"))
        distractor_feature = float_list_feature(list(dist_vector))
        negative_features = [
            float_list_feature(list(n)) for n in sampled_negatives
        ]
        context = tf.train.Features(
            feature={
                "question": question_feature, "answer": answer_feature,
                "distractor": distractor_feature,
                "negatives_length": int64_feature(len(negative_features)),
            }
        )
        feature_list = {
            "negatives": tf.train.FeatureList(feature=negative_features)
        }
        yield tf.train.SequenceExample(
            feature_lists=tf.train.FeatureLists(feature_list=feature_list),
            context=context)
def _convert_to_example(image_data, shape, labels, labels_text,
                        truncated, occluded, alpha, bboxes,
                        dimensions, locations, rotation_y):
    """Build an Example proto for an image example.

    Args:
      image_data: string, PNG encoding of RGB image;
      labels: list of integers, identifier for the ground truth;
      labels_text: list of strings, human-readable labels;
      bboxes: list of bounding boxes; each box is a list of integers;
          specifying [xmin, ymin, xmax, ymax]. All boxes are assumed to belong
          to the same label as the image label.
      shape: 3 integers, image shapes in pixels.
    Returns:
      Example proto
    """
    # Transpose bboxes, dimensions and locations.
    bboxes = list(map(list, zip(*bboxes)))
    dimensions = list(map(list, zip(*dimensions)))
    locations = list(map(list, zip(*locations)))
    # Iterators.
    it_bboxes = iter(bboxes)
    it_dims = iter(dimensions)
    its_locs = iter(locations)

    image_format = b'PNG'
    example = tf.train.Example(features=tf.train.Features(feature={
            'image/height': int64_feature(shape[0]),
            'image/width': int64_feature(shape[1]),
            'image/channels': int64_feature(shape[2]),
            'image/shape': int64_feature(shape),
            'image/format': bytes_feature(image_format),
            'image/encoded': bytes_feature(image_data),
            'object/label': int64_feature(labels),
            'object/label_text': bytes_feature(labels_text),
            'object/truncated': float_feature(truncated),
            'object/occluded': int64_feature(occluded),
            'object/alpha': float_feature(alpha),
            'object/bbox/xmin': float_feature(next(it_bboxes, [])),
            'object/bbox/ymin': float_feature(next(it_bboxes, [])),
            'object/bbox/xmax': float_feature(next(it_bboxes, [])),
            'object/bbox/ymax': float_feature(next(it_bboxes, [])),
            'object/dimensions/height': float_feature(next(it_dims, [])),
            'object/dimensions/width': float_feature(next(it_dims, [])),
            'object/dimensions/length': float_feature(next(it_dims, [])),
            'object/location/x': float_feature(next(its_locs, [])),
            'object/location/y': float_feature(next(its_locs, [])),
            'object/location/z': float_feature(next(its_locs, [])),
            'object/rotation_y': float_feature(rotation_y),
            }))
    return example
Exemplo n.º 27
0
def _convert_to_example(image_data, shape, bboxes, labels, labels_text,
                        difficult, truncated):
    """
    Build an Example proto for an image example.

    :param image_data:
    :param shape:
    :param bboxes:
    :param labels:
    :param labels_text:
    :param difficult:
    :param truncated:
    :return: Example proto
    """
    xmin = []
    ymin = []
    xmax = []
    ymax = []
    # This three lines convert tuple (which contains a serial of bounding boxes into
    # list format.
    # e.g. [(1, 2, 3, 4), (5, 6, 7, 8)] ==> a list, each element is a bounding box.
    # will be convert to ymin=[1, 5], xmin=[2, 6], ymax=[3, 7] and xmax=[4, 8]
    for b in bboxes:
        assert len(b) == 4
        [l.append(point) for l, point in zip([ymin, xmin, ymax, xmax], b)]

    image_format = b'JPEG'
    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': int64_feature(shape[0]),
            'image/width': int64_feature(shape[1]),
            'image/channels': int64_feature(shape[2]),
            'image/shape': int64_feature(shape),
            'image/object/bbox/xmin': float_feature(xmin),
            'image/object/bbox/ymin': float_feature(ymin),
            'image/object/bbox/xmax': float_feature(xmax),
            'image/object/bbox/ymax': float_feature(ymax),
            'image/object/bbox/label': int64_feature(labels),
            'image/object/bbox/label_text': bytes_feature(labels_text),
            'image/object/bbox/difficult': int64_feature(difficult),
            'image/object/bbox/truncated': int64_feature(truncated),
            'image/format': bytes_feature(image_format),
            'image/encoded': bytes_feature(image_data)
        }))
    return example
Exemplo n.º 28
0
def _convert_to_example(image_data, labels, labels_text, bboxes, shape,
                        difficult, truncated):
    """Build an Example proto for an image example.  对于一张图片样例创建一个样例原型

    Args:
      image_data: string, JPEG encoding of RGB image;     JPEG编码格式的RGB图片
      labels: list of integers, identifier for the ground truth;    类别码标签集合
      labels_text: list of strings, human-readable labels;          类别标签集合
      bboxes: list of bounding boxes; each box is a list of integers;  bbox集合
          specifying [xmin, ymin, xmax, ymax]. All boxes are assumed to belong
          to the same label as the image label.
      shape: 3 integers, image shapes in pixels.                   以像素为单位的图片形状
    Returns:
      Example proto     样本原型
    """
    xmin = []
    ymin = []
    xmax = []
    ymax = []
    for b in bboxes:  #将bbox的值以元组的方式进行打包进以上四个列表中
        assert len(b) == 4
        # pylint: disable=expression-not-assigned
        [l.append(point) for l, point in zip([ymin, xmin, ymax, xmax], b)]
        # pylint: enable=expression-not-assigned

    image_format = b'JPEG'
    example = tf.train.Example(features=tf.train.Features(
        feature={
            'image/height': int64_feature(shape[0]),
            'image/width': int64_feature(shape[1]),
            'image/channels': int64_feature(shape[2]),
            'image/shape': int64_feature(shape),
            'image/object/bbox/xmin': float_feature(xmin),
            'image/object/bbox/xmax': float_feature(xmax),
            'image/object/bbox/ymin': float_feature(ymin),
            'image/object/bbox/ymax': float_feature(ymax),
            'image/object/bbox/label': int64_feature(labels),
            'image/object/bbox/label_text': bytes_feature(labels_text),
            'image/object/bbox/difficult': int64_feature(difficult),
            'image/object/bbox/truncated': int64_feature(truncated),
            'image/format': bytes_feature(image_format),
            'image/encoded': bytes_feature(image_data)
        }))
    return example
Exemplo n.º 29
0
def _convert_to_example(image_data, shape, bboxes, labels,
                        labels_text, difficult, truncated):
    """
    Build an Example proto for an image example.

    :param image_data: string, JPEG encoding of RGB iamge;
    :param shape: list of 3 integers, image shape in pixels
    :param bboxes: list of tuples, each tuple is bounding box os an object
        specifying [xmin, ymin, xmax, ymax]. All boxes are assumed to
        belong to the same label as the image label.
    :param labels: list of integers, identifier for the ground truth
    :param labels_text: list of strings, human-readable labels.
    :return:
        An Example proto.
    """
    xmin = []
    ymin = []
    xmax = []
    ymax = []
    for b in bboxes:
        assert len(b) == 4
        [l.append(point) for l, point in zip([ymin, xmin, ymax, xmax], b)]

    image_format = b'JPEG'

    example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': int64_feature(shape[0]),
        'image/width': int64_feature(shape[1]),
        'image/channels': int64_feature(shape[2]),
        'image/shape': int64_feature(shape),
        'image/object/bbox/xmin': float_feature(xmin),
        'image/object/bbox/xmax': float_feature(xmax),
        'image/object/bbox/ymin': float_feature(ymin),
        'image/object/bbox/ymax': float_feature(ymax),
        'image/object/bbox/label': int64_feature(labels),
        'image/object/bbox/label_text': bytes_feature(labels_text),
        'image/object/bbox/difficult': int64_feature(difficult),
        'image/object/bbox/truncated': int64_feature(truncated),
        'image/format': bytes_feature(image_format),
        'image/encoded': bytes_feature(image_data)
    }))
    return example
Exemplo n.º 30
0
def image_example(image_data_iris, image_format_iris, height_iris, width_iris,
                  class_id_iris, image_data_face, image_format_face,
                  height_face, width_face, class_id_face):
    return tf.train.Example(features=tf.train.Features(
        feature={
            'image_iris/encoded': dataset_utils.bytes_feature(image_data_iris),
            'image_iris/format': dataset_utils.bytes_feature(
                image_format_iris),
            'image_iris/class/label': dataset_utils.int64_feature(
                class_id_iris),
            'image_iris/height': dataset_utils.int64_feature(height_iris),
            'image_iris/width': dataset_utils.int64_feature(width_iris),
            'image_face/encoded': dataset_utils.bytes_feature(image_data_face),
            'image_face/format': dataset_utils.bytes_feature(
                image_format_face),
            'image_face/class/label': dataset_utils.int64_feature(
                class_id_face),
            'image_face/height': dataset_utils.int64_feature(height_face),
            'image_face/width': dataset_utils.int64_feature(width_face),
        }))
Exemplo n.º 31
0
def _create_tf_example(image, annotations_list, image_dir):
  """Converts image and annotations to a tf.Example proto.
  Args:
    image: dict with keys: [u'license', u'file_name', u'coco_url', u'height',
      u'width', u'date_captured', u'flickr_url', u'id']
    annotations_list:
      list of dicts with keys: [u'image_id', u'bbox', u'label',
      object[{"category_id", "area", "bbox" : [x,y,width,height],}]]. Notice
        that bounding box coordinates in the COCO dataset are given as [x, y,
        width, height] tuples using absolute coordinates where x, y represent
        the top-left (0-indexed) corner. This function converts to the format
        that can be used by the Tensorflow Object Detection API (which is [ymin,
        xmin, ymax, xmax] with coordinates normalized relative to image size).
    image_dir: directory containing the image files.
  Returns:
    example: The converted tf.Example
    num_annotations_skipped: Number of (invalid) annotations that were ignored.
  Raises:
    ValueError: if the image pointed to by data['filename'] is not a valid JPEG
  """
  image_height = image['height']
  image_width = image['width']
  filename = image['file_name']
  image_id = image['id']

  full_path = os.path.join(image_dir, filename)
  with tf.gfile.GFile(full_path, 'rb') as fid:
    encoded_jpg = fid.read()
  encoded_jpg_io = io.BytesIO(encoded_jpg)
  image = PIL.Image.open(encoded_jpg_io)
  key = hashlib.sha256(encoded_jpg).hexdigest()

  xmin = []
  xmax = []
  ymin = []
  ymax = []
  category_ids = []
  area = []
  num_annotations_skipped = 0
  label = annotations_list['label']
  for object_annotations in annotations_list['object']:
    (x, y, width, height) = tuple(object_annotations['bbox'])
    if width <= 0 or height <= 0:
      num_annotations_skipped += 1
      continue
    if x + width > image_width or y + height > image_height:
      num_annotations_skipped += 1
      continue
    xmin.append(float(x) / image_width)
    xmax.append(float(x + width) / image_width)
    ymin.append(float(y) / image_height)
    ymax.append(float(y + height) / image_height)
    category_id = int(object_annotations['category_id'])
    category_ids.append(category_id)
    area.append(object_annotations['area'])

  feature_dict = {
      'image/height':
          dataset_utils.int64_feature(image_height),
      'image/width':
          dataset_utils.int64_feature(image_width),
      'image/filename':
          dataset_utils.bytes_feature(filename.encode('utf8')),
      'image/source_id':
          dataset_utils.bytes_feature(str(image_id).encode('utf8')),
      'image/key/sha256':
          dataset_utils.bytes_feature(key.encode('utf8')),
      'image/encoded':
          dataset_utils.bytes_feature(encoded_jpg),
      'image/format':
          dataset_utils.bytes_feature('jpeg'.encode('utf8')),
      'image/class/label':
          dataset_utils.int64_feature(label),
      'image/object/bbox/xmin':
          dataset_utils.float_list_feature(xmin),
      'image/object/bbox/xmax':
          dataset_utils.float_list_feature(xmax),
      'image/object/bbox/ymin':
          dataset_utils.float_list_feature(ymin),
      'image/object/bbox/ymax':
          dataset_utils.float_list_feature(ymax),
      'image/object/class/label':
          dataset_utils.int64_feature(label),
      'image/object/area':
          dataset_utils.float_list_feature(area),
  }
  example = tf.train.Example(features=tf.train.Features(feature=feature_dict))
  return key, example, num_annotations_skipped
Exemplo n.º 32
0
def write_images_from_directory(set_directory_name, set_directory_path,
                                annotations_json, tfrecord_writer):
    sequences = sorted(os.listdir(set_directory_path))
    for sequence in sequences:
        annotations_frames = annotations_json[set_directory_name][sequence][
            'frames']
        image_path = os.path.join(set_directory_path, sequence + '/')
        images = sorted(os.listdir(image_path))

        input_height = 480
        input_width = 640
        input_depth = 3

        bboxes = []
        labels = []
        labels_text = []
        difficult = []
        truncated = []

        for frame in range(len(images)):
            sys.stdout.write('\r>> Annotating image %d/%d' %
                             (frame + 1, len(images)))
            bboxes_f = []
            labels_f = []
            labels_text_f = []
            difficult_f = []
            truncated_f = []

            object_dicts_list = []
            if str(frame) in annotations_frames:
                object_dicts_list = annotations_frames[str(frame)]

            for object_dict in object_dicts_list:
                if object_dict['lbl'] == 'person':
                    #Classify further into person_full and person_occluded
                    label_f = 'person'
                    labels_f.append(int(LABELS[label_f][0]))
                    labels_text_f.append(label_f.encode('ascii'))

                    pos = object_dict['pos']

                    ymin = float(pos[1]) / input_height
                    if ymin < 0.0:
                        ymin = 0.0
                    if float(pos[1]) + float(pos[3]) > input_height:
                        print("FRAME height:", frame, pos[1], pos[3])
                        ymax = 1.0
                    else:
                        ymax = (float(pos[1]) + float(pos[3])) / input_height
                    xmin = float(pos[0]) / input_width
                    if xmin < 0.0:
                        xmin = 0.0
                    if float(pos[0]) + float(pos[2]) > input_width:
                        print("FRAME width:", frame, pos[0], pos[2])
                        xmax = 1.0
                    else:
                        xmax = (float(pos[0]) + float(pos[2])) / input_width

                    bboxes_f.append((ymin, xmin, ymax, xmax))
                    if object_dict['occl'] == 1:
                        truncated_f.append(1)
                    else:
                        truncated_f.append(0)
                    difficult_f.append(0)
                #elif object_dict['lbl'] == 'person?':
                #    truncated_f.append(0)
                #    difficult_f.append(1)
                #else:
                #    truncated_f.append(0)
                #    difficult_f.append(0)
                # Can check whether the object is occluded or not by
                # accessing object_dict['ocl'] == 1, if its 1, then it
                # is occluded.  The associated bbox for the predicted
                # object (predicting stuff thats not occluded) is then
                # object_dict['pos'].  If you just want the bbox for
                # what's visible, do object_dict['posv']
            bboxes.append(bboxes_f)
            labels.append(labels_f)
            labels_text.append(labels_text_f)
            difficult.append(difficult_f)
            truncated.append(truncated_f)

        for i, imagename in enumerate(images):
            sys.stdout.write('\r>> Converting image %d/%d' %
                             (i + 1, len(images)))
            sys.stdout.flush()

            image_file = image_path + imagename
            image_data = tf.gfile.FastGFile(image_file, 'rb').read()

            xmin = []
            ymin = []
            xmax = []
            ymax = []

            for b in bboxes[i]:
                [
                    l.append(point)
                    for l, point in zip([xmin, ymin, xmax, ymax], b)
                ]


#            if len(bboxes[i]) != 0:
            image_format = b'JPEG'
            example = tf.train.Example(features=tf.train.Features(
                feature={
                    'image/height':
                    int64_feature(input_height),
                    'image/width':
                    int64_feature(input_width),
                    'image/channels':
                    int64_feature(input_depth),
                    'image/shape':
                    int64_feature([input_height, input_width, input_depth]),
                    'image/object/bbox/xmin':
                    float_feature(xmin),
                    'image/object/bbox/xmax':
                    float_feature(xmax),
                    'image/object/bbox/ymin':
                    float_feature(ymin),
                    'image/object/bbox/ymax':
                    float_feature(ymax),
                    'image/object/bbox/label':
                    int64_feature(labels[i]),
                    'image/object/bbox/label_text':
                    bytes_feature(labels_text[i]),
                    'image/object/bbox/difficult':
                    int64_feature(difficult[i]),
                    'image/object/bbox/truncated':
                    int64_feature(truncated[i]),
                    'image/format':
                    bytes_feature(image_format),
                    'image/encoded':
                    bytes_feature(image_data)
                }))
            tfrecord_writer.write(example.SerializeToString())