コード例 #1
0
    def _record_to_tf(self, record):
        """Creates tf.train.SequenceExample object from records.
        """
        try:
            self._validate_record(record)
        except InvalidRecord as e:
            # Pop image before displaying record.
            record.pop('image_raw')
            tf.logging.warning('Invalid record: {} - {}'.format(
                e, record
            ))
            return

        sequence_vals = {
            'label': [],
            'xmin': [],
            'ymin': [],
            'xmax': [],
            'ymax': [],
        }

        for b in record['gt_boxes']:
            sequence_vals['label'].append(to_int64(b['label']))
            sequence_vals['xmin'].append(to_int64(b['xmin']))
            sequence_vals['ymin'].append(to_int64(b['ymin']))
            sequence_vals['xmax'].append(to_int64(b['xmax']))
            sequence_vals['ymax'].append(to_int64(b['ymax']))

        object_feature_lists = {
            'label': tf.train.FeatureList(feature=sequence_vals['label']),
            'xmin': tf.train.FeatureList(feature=sequence_vals['xmin']),
            'ymin': tf.train.FeatureList(feature=sequence_vals['ymin']),
            'xmax': tf.train.FeatureList(feature=sequence_vals['xmax']),
            'ymax': tf.train.FeatureList(feature=sequence_vals['ymax']),
        }

        object_features = tf.train.FeatureLists(
            feature_list=object_feature_lists
        )

        feature = {
            'width': to_int64(record['width']),
            'height': to_int64(record['height']),
            'depth': to_int64(record['depth']),
            'filename': to_string(record['filename']),
            'image_raw': to_bytes(record['image_raw']),
        }

        # Now build an `Example` protobuf object and save with the writer.
        context = tf.train.Features(feature=feature)
        example = tf.train.SequenceExample(
            feature_lists=object_features, context=context
        )

        return example
コード例 #2
0
    def _record_to_tf(self, record):
        """Creates tf.train.SequenceExample object from records.
        """
        try:
            self._validate_record(record)
        except InvalidRecord as e:
            # Pop image before displaying record.
            record.pop('image_raw')
            tf.logging.warning('Invalid record: {} - {}'.format(
                e, record
            ))
            return

        sequence_vals = {
            'label': [],
            'xmin': [],
            'ymin': [],
            'xmax': [],
            'ymax': [],
        }

        for b in record['gt_boxes']:
            sequence_vals['label'].append(to_int64(b['label']))
            sequence_vals['xmin'].append(to_int64(b['xmin']))
            sequence_vals['ymin'].append(to_int64(b['ymin']))
            sequence_vals['xmax'].append(to_int64(b['xmax']))
            sequence_vals['ymax'].append(to_int64(b['ymax']))

        object_feature_lists = {
            'label': tf.train.FeatureList(feature=sequence_vals['label']),
            'xmin': tf.train.FeatureList(feature=sequence_vals['xmin']),
            'ymin': tf.train.FeatureList(feature=sequence_vals['ymin']),
            'xmax': tf.train.FeatureList(feature=sequence_vals['xmax']),
            'ymax': tf.train.FeatureList(feature=sequence_vals['ymax']),
        }

        object_features = tf.train.FeatureLists(
            feature_list=object_feature_lists
        )

        feature = {
            'width': to_int64(record['width']),
            'height': to_int64(record['height']),
            'depth': to_int64(record['depth']),
            'filename': to_string(record['filename']),
            'image_raw': to_bytes(record['image_raw']),
        }

        # Now build an `Example` protobuf object and save with the writer.
        context = tf.train.Features(feature=feature)
        example = tf.train.SequenceExample(
            feature_lists=object_features, context=context
        )

        return example
コード例 #3
0
    def _record_to_tf(self, record):
        """Creates tf.train.SequenceExample object from records."""
        try:
            self._validate_record(record)
        except InvalidRecord as e:
            # Pop image before displaying record.
            record.pop("image_raw")
            tf.logging.warning("Invalid record: {} - {}".format(e, record))
            return

        sequence_vals = {
            "label": [],
            "xmin": [],
            "ymin": [],
            "xmax": [],
            "ymax": [],
        }

        for b in record["gt_boxes"]:
            sequence_vals["label"].append(to_int64(b["label"]))
            sequence_vals["xmin"].append(to_int64(b["xmin"]))
            sequence_vals["ymin"].append(to_int64(b["ymin"]))
            sequence_vals["xmax"].append(to_int64(b["xmax"]))
            sequence_vals["ymax"].append(to_int64(b["ymax"]))

        object_feature_lists = {
            "label": tf.train.FeatureList(feature=sequence_vals["label"]),
            "xmin": tf.train.FeatureList(feature=sequence_vals["xmin"]),
            "ymin": tf.train.FeatureList(feature=sequence_vals["ymin"]),
            "xmax": tf.train.FeatureList(feature=sequence_vals["xmax"]),
            "ymax": tf.train.FeatureList(feature=sequence_vals["ymax"]),
        }

        object_features = tf.train.FeatureLists(
            feature_list=object_feature_lists)

        feature = {
            "width": to_int64(record["width"]),
            "height": to_int64(record["height"]),
            "depth": to_int64(record["depth"]),
            "filename": to_string(record["filename"]),
            "image_raw": to_bytes(record["image_raw"]),
        }

        # Now build an `Example` protobuf object and save with the writer.
        context = tf.train.Features(feature=feature)
        example = tf.train.SequenceExample(feature_lists=object_features,
                                           context=context)

        return example
コード例 #4
0
    def image_to_example(self, classes, image_id):
        annotation_path = self.get_image_annotation(image_id)
        image_path = self.get_image_path(image_id)

        # Read both the image and the annotation into memory.
        annotation = read_xml(annotation_path)
        image = read_image(image_path)

        # TODO: consider alternatives to using Pillow here.
        image_pil = Image.open(image_path)
        width = image_pil.width
        height = image_pil.height
        image_pil.close()

        obj_vals = {
            'label': [],
            'xmin': [],
            'ymin': [],
            'xmax': [],
            'ymax': [],
        }

        objects = annotation.get('object')
        if objects is None:
            # If there's no bounding boxes, we don't want it
            return
        for b in annotation['object']:
            try:
                label_id = classes.index(self._wnids[b['name']])
            except ValueError:
                continue

            (xmin, ymin, xmax, ymax) = adjust_bbox(
                xmin=int(b['bndbox']['xmin']), ymin=int(b['bndbox']['ymin']),
                xmax=int(b['bndbox']['xmax']), ymax=int(b['bndbox']['ymax']),
                old_width=int(annotation['size']['width']),
                old_height=int(annotation['size']['height']),
                new_width=width, new_height=height
            )
            obj_vals['label'].append(to_int64(label_id))
            obj_vals['xmin'].append(to_int64(xmin))
            obj_vals['ymin'].append(to_int64(ymin))
            obj_vals['xmax'].append(to_int64(xmax))
            obj_vals['ymax'].append(to_int64(ymax))

        if len(obj_vals['label']) == 0:
            # No bounding box matches the available classes.
            return

        object_feature_lists = {
            'label': tf.train.FeatureList(feature=obj_vals['label']),
            'xmin': tf.train.FeatureList(feature=obj_vals['xmin']),
            'ymin': tf.train.FeatureList(feature=obj_vals['ymin']),
            'xmax': tf.train.FeatureList(feature=obj_vals['xmax']),
            'ymax': tf.train.FeatureList(feature=obj_vals['ymax']),
        }

        object_features = tf.train.FeatureLists(
            feature_list=object_feature_lists
        )

        sample = {
            'width': to_int64(width),
            'height': to_int64(height),
            'depth': to_int64(3),
            'filename': to_string(annotation['filename']),
            'image_raw': to_bytes(image),
        }

        # Now build an `Example` protobuf object and save with the writer.
        context = tf.train.Features(feature=sample)
        example = tf.train.SequenceExample(
            feature_lists=object_features, context=context
        )

        return example
コード例 #5
0
    def image_to_example(self, classes, image_id):
        annotation_path = self.get_image_annotation(image_id)
        image_path = self.get_image_path(image_id)

        # Read both the image and the annotation into memory.
        annotation = read_xml(annotation_path)
        image = read_image(image_path)

        object_features_values = {
            'label': [],
            'xmin': [],
            'ymin': [],
            'xmax': [],
            'ymax': [],
        }

        for b in annotation['object']:
            try:
                label_id = classes.index(b['name'])
            except ValueError:
                continue

            object_features_values['label'].append(to_int64(label_id))
            object_features_values['xmin'].append(to_int64(
                b['bndbox']['xmin']))
            object_features_values['ymin'].append(to_int64(
                b['bndbox']['ymin']))
            object_features_values['xmax'].append(to_int64(
                b['bndbox']['xmax']))
            object_features_values['ymax'].append(to_int64(
                b['bndbox']['ymax']))

        if len(object_features_values['label']) == 0:
            # No bounding box matches the available classes.
            return

        object_feature_lists = {
            'label':
            tf.train.FeatureList(feature=object_features_values['label']),
            'xmin':
            tf.train.FeatureList(feature=object_features_values['xmin']),
            'ymin':
            tf.train.FeatureList(feature=object_features_values['ymin']),
            'xmax':
            tf.train.FeatureList(feature=object_features_values['xmax']),
            'ymax':
            tf.train.FeatureList(feature=object_features_values['ymax']),
        }

        object_features = tf.train.FeatureLists(
            feature_list=object_feature_lists)

        sample = {
            'width': to_int64(int(annotation['size']['width'])),
            'height': to_int64(int(annotation['size']['height'])),
            'depth': to_int64(int(annotation['size']['depth'])),
            'filename': to_string(annotation['filename']),
            'image_raw': to_bytes(image),
        }

        # Now build an `Example` protobuf object and save with the writer.
        context = tf.train.Features(feature=sample)
        example = tf.train.SequenceExample(feature_lists=object_features,
                                           context=context)

        return example