def _convert_to_example(image_data, 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 """ 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(image_data), 'maskimage/encoded': bytes_feature(mask_data) })) return example
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
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
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
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
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
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
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 _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
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), }))
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 _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
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 _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
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
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
def _convert_to_example_multiphase_multislice(nc_image_data, art_image_data, pv_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 = 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/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
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
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, 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
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
def _add_to_tfrecord(image_path, mask_path, tfrecord_writer): img_data, mask_data = _process_image(image_path, mask_path) shape = np.shape(img_data)[:2] image_format = b'PNG' # print([*shape, 3], np.unique(mask_data)) example = tf.train.Example(features=tf.train.Features( feature={ 'format': bytes_feature(image_format), 'image': bytes_feature(img_data), 'mask': bytes_feature(mask_data), 'image_name': bytes_feature( str.encode(os.path.basename(image_path))), 'mask_name': bytes_feature(str.encode(os.path.basename(mask_path))) # 'image': np_array_feature(img_data), # 'mask': np_array_feature(mask_data), # 'image/shape': int64_feature([*shape, 3]), # 'mask/shape': int64_feature([*shape, 1]) })) tfrecord_writer.write(example.SerializeToString())
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
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(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
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; 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' 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
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), }))
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, 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(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
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
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