예제 #1
0
    def _serialize_example(self, image: PIL.Image.Image, label: str) -> str:
        """
         Creates a tf.Example message ready to be written to a file.
         """
        # Create a dictionary mapping the feature name to the tf.Example-compatible
        # data type.
        height = image.height
        width = image.width
        depth = len(image.getbands())
        image_bytes = image.tobytes()
        feature = {
            'height': self._int64_feature(height),
            'width': self._int64_feature(width),
            'depth': self._int64_feature(depth),
            'label': self._bytes_feature(tf.compat.as_bytes(label)),
            'image_raw': self._bytes_feature(image_bytes)
        }

        # Create a Features message using tf.train.Example.
        example_proto = tf.train.Example(features=tf.train.Features(
            feature=feature))
        return example_proto.SerializeToString()
예제 #2
0
    def _serialize_example(self, id: str, image: PIL.Image.Image,
                           **kwargs) -> str:
        """
         Creates a tf.Example message ready to be written to a file.
        """
        # Create a dictionary mapping the feature name to the tf.Example-compatible
        # data type.
        height = image.height
        width = image.width
        depth = len(image.getbands())
        image_bytes = image.tobytes()

        feature = {
            'id': utils.bytes_feature(tf.compat.as_bytes(id)),
            'image_raw': utils.bytes_feature(image_bytes),
            'height': utils.int64_feature(height),
            'width': utils.int64_feature(width),
            'depth': utils.int64_feature(depth),
        }

        if "label" in kwargs:
            feature["label"] = utils.bytes_feature(
                tf.compat.as_bytes(kwargs["label"]))

        for key in set(kwargs.keys()).difference({'label'}):
            value = kwargs[key]
            if isinstance(value, int) or isinstance(value, bool):
                feature[key] = utils.int64_feature(value)
            elif isinstance(value, str):
                feature[key] = utils.bytes_feature(tf.compat.as_bytes(value))
            elif isinstance(value, float):
                feature[key] = utils.float_feature(value)

        # Create a Features message using tf.train.Example.
        example_proto = tf.train.Example(features=tf.train.Features(
            feature=feature))
        return example_proto.SerializeToString()