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
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 _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