Esempio n. 1
0
    def test_input_fn_from_parse_example(self):
        """Tests complete flow with input_fn constructed from parse_example."""
        input_dimension = 2
        n_classes = 3
        batch_size = 10
        data = np.linspace(0.,
                           n_classes - 1.,
                           batch_size * input_dimension,
                           dtype=np.float32)
        data = data.reshape(batch_size, input_dimension)

        serialized_examples = []
        for datum in data:
            example = example_pb2.Example(features=feature_pb2.Features(
                feature={
                    'x':
                    feature_pb2.Feature(float_list=feature_pb2.FloatList(
                        value=datum)),
                    'y':
                    feature_pb2.Feature(int64_list=feature_pb2.Int64List(
                        value=self._as_label(datum[:1]))),
                }))
            serialized_examples.append(example.SerializeToString())

        feature_spec = {
            'x': parsing_ops.FixedLenFeature([input_dimension],
                                             dtypes.float32),
            'y': parsing_ops.FixedLenFeature([1], dtypes.int64),
        }

        def _train_input_fn():
            feature_map = parsing_ops.parse_example(serialized_examples,
                                                    feature_spec)
            features = linear_testing_utils.queue_parsed_features(feature_map)
            labels = features.pop('y')
            return features, labels

        def _eval_input_fn():
            feature_map = parsing_ops.parse_example(
                input_lib.limit_epochs(serialized_examples, num_epochs=1),
                feature_spec)
            features = linear_testing_utils.queue_parsed_features(feature_map)
            labels = features.pop('y')
            return features, labels

        def _predict_input_fn():
            feature_map = parsing_ops.parse_example(
                input_lib.limit_epochs(serialized_examples, num_epochs=1),
                feature_spec)
            features = linear_testing_utils.queue_parsed_features(feature_map)
            features.pop('y')
            return features, None

        self._test_complete_flow(train_input_fn=_train_input_fn,
                                 eval_input_fn=_eval_input_fn,
                                 predict_input_fn=_predict_input_fn,
                                 input_dimension=input_dimension,
                                 n_classes=n_classes,
                                 batch_size=batch_size)
    def _test_input_fn_from_parse_example_helper(self, fc_impl, fn_to_run):
        """Tests complete flow with input_fn constructed from parse_example."""
        label_dimension = 2
        batch_size = 10
        data = np.linspace(0.,
                           2.,
                           batch_size * label_dimension,
                           dtype=np.float32)
        data = data.reshape(batch_size, label_dimension)

        serialized_examples = []
        for datum in data:
            example = example_pb2.Example(features=feature_pb2.Features(
                feature={
                    'x':
                    feature_pb2.Feature(float_list=feature_pb2.FloatList(
                        value=datum)),
                    'y':
                    feature_pb2.Feature(float_list=feature_pb2.FloatList(
                        value=datum)),
                }))
            serialized_examples.append(example.SerializeToString())

        feature_spec = {
            'x': tf.io.FixedLenFeature([label_dimension], tf.dtypes.float32),
            'y': tf.io.FixedLenFeature([label_dimension], tf.dtypes.float32),
        }

        def _train_input_fn():
            feature_map = tf.compat.v1.io.parse_example(
                serialized_examples, feature_spec)
            features = linear_testing_utils.queue_parsed_features(feature_map)
            labels = features.pop('y')
            return features, labels

        def _eval_input_fn():
            feature_map = tf.compat.v1.io.parse_example(
                tf.compat.v1.train.limit_epochs(serialized_examples,
                                                num_epochs=1), feature_spec)
            features = linear_testing_utils.queue_parsed_features(feature_map)
            labels = features.pop('y')
            return features, labels

        def _predict_input_fn():
            feature_map = tf.compat.v1.io.parse_example(
                tf.compat.v1.train.limit_epochs(serialized_examples,
                                                num_epochs=1), feature_spec)
            features = linear_testing_utils.queue_parsed_features(feature_map)
            features.pop('y')
            return features, None

        fn_to_run(train_input_fn=_train_input_fn,
                  eval_input_fn=_eval_input_fn,
                  predict_input_fn=_predict_input_fn,
                  input_dimension=label_dimension,
                  label_dimension=label_dimension,
                  batch_size=batch_size,
                  fc_impl=fc_impl)
  def testParseExampleInputFn(self):
    """Tests complete flow with input_fn constructed from parse_example."""
    n_classes = 3
    batch_size = 10
    words = [b'dog', b'cat', b'bird', b'the', b'a', b'sat', b'flew', b'slept']

    _, examples_file = tempfile.mkstemp()
    writer = python_io.TFRecordWriter(examples_file)
    for _ in range(batch_size):
      sequence_length = random.randint(1, len(words))
      sentence = random.sample(words, sequence_length)
      label = random.randint(0, n_classes - 1)
      example = example_pb2.Example(features=feature_pb2.Features(
          feature={
              'tokens':
                  feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
                      value=sentence)),
              'label':
                  feature_pb2.Feature(int64_list=feature_pb2.Int64List(
                      value=[label])),
          }))
      writer.write(example.SerializeToString())
    writer.close()

    col = seq_fc.sequence_categorical_column_with_hash_bucket(
        'tokens', hash_bucket_size=10)
    embed = fc.embedding_column(col, dimension=2)
    feature_columns = [embed]
    feature_spec = parsing_utils.classifier_parse_example_spec(
        feature_columns,
        label_key='label',
        label_dtype=dtypes.int64)

    def _train_input_fn():
      dataset = readers.make_batched_features_dataset(
          examples_file, batch_size, feature_spec)
      return dataset.map(lambda features: (features, features.pop('label')))
    def _eval_input_fn():
      dataset = readers.make_batched_features_dataset(
          examples_file, batch_size, feature_spec, num_epochs=1)
      return dataset.map(lambda features: (features, features.pop('label')))
    def _predict_input_fn():
      dataset = readers.make_batched_features_dataset(
          examples_file, batch_size, feature_spec, num_epochs=1)
      def features_fn(features):
        features.pop('label')
        return features
      return dataset.map(features_fn)

    self._test_complete_flow(
        feature_columns=feature_columns,
        train_input_fn=_train_input_fn,
        eval_input_fn=_eval_input_fn,
        predict_input_fn=_predict_input_fn,
        n_classes=n_classes,
        batch_size=batch_size)
Esempio n. 4
0
  def test_input_fn_from_parse_example(self):
    """Tests complete flow with input_fn constructed from parse_example."""
    label_dimension = 2
    input_dimension = label_dimension
    batch_size = 10
    prediction_length = batch_size
    data = np.linspace(0., 2., batch_size * label_dimension, dtype=np.float32)
    data = data.reshape(batch_size, label_dimension)

    serialized_examples = []
    for datum in data:
      example = example_pb2.Example(features=feature_pb2.Features(
          feature={
              'x': feature_pb2.Feature(
                  float_list=feature_pb2.FloatList(value=datum)),
              'y': feature_pb2.Feature(
                  float_list=feature_pb2.FloatList(
                      value=datum[:label_dimension])),
          }))
      serialized_examples.append(example.SerializeToString())

    feature_spec = {
        'x': parsing_ops.FixedLenFeature([input_dimension], dtypes.float32),
        'y': parsing_ops.FixedLenFeature([label_dimension], dtypes.float32),
    }

    def _train_input_fn():
      feature_map = parsing_ops.parse_example(serialized_examples, feature_spec)
      features = _queue_parsed_features(feature_map)
      labels = features.pop('y')
      return features, labels
    def _eval_input_fn():
      feature_map = parsing_ops.parse_example(
          input_lib.limit_epochs(serialized_examples, num_epochs=1),
          feature_spec)
      features = _queue_parsed_features(feature_map)
      labels = features.pop('y')
      return features, labels
    def _predict_input_fn():
      feature_map = parsing_ops.parse_example(
          input_lib.limit_epochs(serialized_examples, num_epochs=1),
          feature_spec)
      features = _queue_parsed_features(feature_map)
      features.pop('y')
      return features, None

    self._test_complete_flow(
        train_input_fn=_train_input_fn,
        eval_input_fn=_eval_input_fn,
        predict_input_fn=_predict_input_fn,
        input_dimension=input_dimension,
        label_dimension=label_dimension,
        prediction_length=prediction_length,
        batch_size=batch_size)
Esempio n. 5
0
    def testParseExampleInputFn(self):
        """Tests complete flow with input_fn constructed from parse_example."""
        n_classes = 3
        batch_size = 10
        words = [
            b'dog', b'cat', b'bird', b'the', b'a', b'sat', b'flew', b'slept'
        ]

        serialized_examples = []
        for _ in range(batch_size):
            sequence_length = random.randint(1, len(words))
            sentence = random.sample(words, sequence_length)
            label = random.randint(0, n_classes - 1)
            example = example_pb2.Example(features=feature_pb2.Features(
                feature={
                    'tokens':
                    feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
                        value=sentence)),
                    'label':
                    feature_pb2.Feature(int64_list=feature_pb2.Int64List(
                        value=[label])),
                }))
            serialized_examples.append(example.SerializeToString())

        feature_spec = {
            'tokens': parsing_ops.VarLenFeature(dtypes.string),
            'label': parsing_ops.FixedLenFeature([1], dtypes.int64),
        }

        def _train_input_fn():
            features = parsing_ops.parse_example(serialized_examples,
                                                 feature_spec)
            labels = features.pop('label')
            return features, labels

        def _eval_input_fn():
            features = parsing_ops.parse_example(
                input_lib.limit_epochs(serialized_examples, num_epochs=1),
                feature_spec)
            labels = features.pop('label')
            return features, labels

        def _predict_input_fn():
            features = parsing_ops.parse_example(
                input_lib.limit_epochs(serialized_examples, num_epochs=1),
                feature_spec)
            features.pop('label')
            return features, None

        self._test_complete_flow(train_input_fn=_train_input_fn,
                                 eval_input_fn=_eval_input_fn,
                                 predict_input_fn=_predict_input_fn,
                                 n_classes=n_classes,
                                 batch_size=batch_size)
Esempio n. 6
0
    def create_tf_record(self):
        print('\ncreate_tf_record')

        tmp_dir = os.path.join(os.environ['HOME'], 'tmp')
        if not os.path.isdir(tmp_dir):
            os.makedirs(tmp_dir)

        path = os.path.join(tmp_dir, 'tfrecord')
        writer = tf.python_io.TFRecordWriter(path=path)

        image_tensor = np.random.randint(255, size=(4, 5, 3)).astype(np.uint8)
        flat_mask = (4 * 5) * [1.0]
        with self.test_session():
            encoded_jpeg = tf.image.encode_jpeg(
                tf.constant(image_tensor)).eval()

        example = example_pb2.Example(features=feature_pb2.Features(
            feature={
                'image/encoded':
                feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
                    value=[encoded_jpeg])),
                'image/format':
                feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
                    value=['jpeg'.encode('utf-8')])),
                'image/height':
                feature_pb2.Feature(int64_list=feature_pb2.Int64List(
                    value=[4])),
                'image/width':
                feature_pb2.Feature(int64_list=feature_pb2.Int64List(
                    value=[5])),
                'image/object/bbox/xmin':
                feature_pb2.Feature(float_list=feature_pb2.FloatList(
                    value=[0.0])),
                'image/object/bbox/xmax':
                feature_pb2.Feature(float_list=feature_pb2.FloatList(
                    value=[1.0])),
                'image/object/bbox/ymin':
                feature_pb2.Feature(float_list=feature_pb2.FloatList(
                    value=[0.0])),
                'image/object/bbox/ymax':
                feature_pb2.Feature(float_list=feature_pb2.FloatList(
                    value=[1.0])),
                'image/object/class/label':
                feature_pb2.Feature(int64_list=feature_pb2.Int64List(
                    value=[2])),
                'image/object/mask':
                feature_pb2.Feature(float_list=feature_pb2.FloatList(
                    value=flat_mask)),
            }))
        writer.write(example.SerializeToString())
        writer.close()

        return path
    def test_input_fn_from_parse_example(self):
        """Tests complete flow with input_fn constructed from parse_example."""
        input_dim = 4
        batch_size = 6
        data = np.zeros([batch_size, input_dim])

        serialized_examples = []
        for datum in data:
            example = example_pb2.Example(features=feature_pb2.Features(
                feature={
                    'x':
                    feature_pb2.Feature(float_list=feature_pb2.FloatList(
                        value=datum)),
                    'y':
                    feature_pb2.Feature(float_list=feature_pb2.FloatList(
                        value=datum)),
                }))
            serialized_examples.append(example.SerializeToString())

        feature_spec = {
            'x': parsing_ops.FixedLenFeature([input_dim], dtypes.float32),
            'y': parsing_ops.FixedLenFeature([input_dim], dtypes.float32),
        }

        def _train_input_fn():
            feature_map = parsing_ops.parse_example(serialized_examples,
                                                    feature_spec)
            _, features = graph_io.queue_parsed_features(feature_map)
            labels = features.pop('y')
            return features, labels

        def _eval_input_fn():
            feature_map = parsing_ops.parse_example(
                input_lib.limit_epochs(serialized_examples, num_epochs=1),
                feature_spec)
            _, features = graph_io.queue_parsed_features(feature_map)
            labels = features.pop('y')
            return features, labels

        def _predict_input_fn():
            feature_map = parsing_ops.parse_example(
                input_lib.limit_epochs(serialized_examples, num_epochs=1),
                feature_spec)
            _, features = graph_io.queue_parsed_features(feature_map)
            features.pop('y')
            return features, None

        self._test_complete_flow(train_input_fn=_train_input_fn,
                                 eval_input_fn=_eval_input_fn,
                                 predict_input_fn=_predict_input_fn,
                                 prediction_size=[batch_size, input_dim])
Esempio n. 8
0
 def _record(self, f, r):
     example = example_pb2.Example(features=feature_pb2.Features(
         feature={
             "file":
             feature_pb2.Feature(int64_list=feature_pb2.Int64List(
                 value=[f])),
             "record":
             feature_pb2.Feature(int64_list=feature_pb2.Int64List(
                 value=[r])),
             "keywords":
             feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
                 value=self._get_keywords(f, r)))
         }))
     return example.SerializeToString()
  def _make_sequence_example(self):
    example = example_pb2.SequenceExample()
    example.context.feature['int_ctx'].int64_list.value.extend([5])
    example.context.feature['float_ctx'].float_list.value.extend([123.6])
    for val in range(0, 10, 2):
      feat = feature_pb2.Feature()
      feat.int64_list.value.extend([val] * val)
      example.feature_lists.feature_list['int_list'].feature.extend([feat])
    for val in range(1, 11, 2):
      feat = feature_pb2.Feature()
      feat.bytes_list.value.extend([tf.compat.as_bytes(str(val))] * val)
      example.feature_lists.feature_list['str_list'].feature.extend([feat])

    return example
  def testMakeBatchedFeaturesDataset(self):
    # Set up
    fn = os.path.join(self.get_temp_dir(), "tf_record.txt")
    writer = python_io.TFRecordWriter(fn)
    for i in range(1024):
      writer.write(
          example_pb2.Example(
              features=feature_pb2.Features(
                  feature={
                      "value":
                          feature_pb2.Feature(
                              int64_list=feature_pb2.Int64List(value=[i]))
                  })).SerializeToString())
    writer.close()

    dataset = readers.make_batched_features_dataset(
        file_pattern=fn,
        batch_size=32,
        features={"value": parsing_ops.FixedLenFeature([], dtypes.int64)},
        shuffle=False,
        num_epochs=1,
        drop_final_batch=False)

    rebatched_dataset = distribute._RebatchDataset(dataset, num_replicas=4)

    self.assertEqual([[None]],
                     [ts.as_list() for ts in _flat_shapes(rebatched_dataset)])

    expected_output = [{
        "value": [k for k in range(i, i + 8)]
    } for i in range(0, 1024, 8)]  # pylint: disable=g-complex-comprehension
    self.assertDatasetProduces(rebatched_dataset, expected_output)
Esempio n. 11
0
    def _write_test_data():
        schema = FeatureSpecToSchema.apply({
            "f0":
            tf.VarLenFeature(dtype=tf.int64),
            "f1":
            tf.VarLenFeature(dtype=tf.int64),
            "f2":
            tf.VarLenFeature(dtype=tf.int64)
        })
        batches = [
            [1, 4, None],
            [2, None, None],
            [3, 5, None],
            [None, None, None],
        ]

        example_proto = [
            example_pb2.Example(features=feature_pb2.Features(
                feature={
                    "f" + str(i): feature_pb2.Feature(
                        int64_list=feature_pb2.Int64List(value=[f]))
                    for i, f in enumerate(batch) if f is not None
                })) for batch in batches
        ]

        return DataUtil.write_test_data(example_proto, schema)
Esempio n. 12
0
def to_tf_example(ingradients):
    return example_pb2.Example(features=feature_pb2.Features(
    feature={
        'ingredients':
            feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
                value=[bytes(x, 'utf-8') for x in ingradients]))
    }))
Esempio n. 13
0
def _encoded_bytes_feature(tf_encoded):
    encoded = tf_encoded.eval()

    def string_to_bytes(value):
        return feature_pb2.BytesList(value=[value])

    return feature_pb2.Feature(bytes_list=string_to_bytes(encoded))
    def _EncodedBytesFeature(self, tf_encoded):
        with self.test_session():
            encoded = tf_encoded.eval()

        def BytesList(value):
            return feature_pb2.BytesList(value=[value])

        return feature_pb2.Feature(bytes_list=BytesList(encoded))
 def make_record(file_index):
     example = example_pb2.Example(features=feature_pb2.Features(
         feature={
             "file":
             feature_pb2.Feature(int64_list=feature_pb2.Int64List(
                 value=[file_index])),
         }))
     return example.SerializeToString()
Esempio n. 16
0
    def create_tf_record(self):
        path = os.path.join(self.get_temp_dir(), 'tfrecord')
        writer = tf.python_io.TFRecordWriter(path)

        image_tensor = np.random.randint(255, size=(4, 5, 3)).astype(np.uint8)
        with self.test_session():
            encoded_jpeg = tf.image.encode_jpeg(tf.constant(image_tensor)).eval()
        example = example_pb2.Example(features=feature_pb2.Features(feature={
            'image/encoded': feature_pb2.Feature(
                bytes_list=feature_pb2.BytesList(value=[encoded_jpeg])),
            'image/format': feature_pb2.Feature(
                bytes_list=feature_pb2.BytesList(value=['jpeg'.encode('utf-8')])),
            'image/object/bbox/xmin': feature_pb2.Feature(
                float_list=feature_pb2.FloatList(value=[0.0])),
            'image/object/bbox/xmax': feature_pb2.Feature(
                float_list=feature_pb2.FloatList(value=[1.0])),
            'image/object/bbox/ymin': feature_pb2.Feature(
                float_list=feature_pb2.FloatList(value=[0.0])),
            'image/object/bbox/ymax': feature_pb2.Feature(
                float_list=feature_pb2.FloatList(value=[1.0])),
            'image/object/class/label': feature_pb2.Feature(
                int64_list=feature_pb2.Int64List(value=[2])),
        }))
        writer.write(example.SerializeToString())
        writer.close()

        return path
Esempio n. 17
0
 def get_example_proto(cls):
     return [
         example_pb2.Example(features=feature_pb2.Features(
             feature={
                 k: feature_pb2.Feature(int64_list=feature_pb2.Int64List(
                     value=[v]))
                 for k, v in d.items()
             })) for d in cls.values
     ]
 def get_example_proto(values=[{"f1": 1, "f2": 2}]):
     return [
         example_pb2.Example(features=feature_pb2.Features(
             feature={
                 k: feature_pb2.Feature(int64_list=feature_pb2.Int64List(
                     value=[v]))
                 for k, v in d.items()
             })) for d in values
     ]
Esempio n. 19
0
  def testSaveWithSignatures(self):
    model = keras.models.Sequential()
    model.add(keras.layers.Dense(5, input_shape=(3,),
                                 kernel_regularizer=regularizers.get('l2')))
    model.add(keras.layers.Dropout(0.5))
    model.add(keras.layers.Dense(4, kernel_regularizer=regularizers.get('l2')))

    input_arr = np.random.random((2, 3))
    target_arr = np.random.random((2, 4))

    model.compile(
        loss='mse',
        optimizer='rmsprop')
    model.train_on_batch(input_arr, target_arr)

    @tf.function(input_signature=[tf.TensorSpec((None, 3))])
    def predict(inputs):
      return {'predictions': model(inputs)}

    feature_configs = {
        'inputs': tf.io.FixedLenFeature(
            shape=[2, 3], dtype=tf.float32)}

    @tf.function(
        input_signature=[tf.TensorSpec([None], tf.string)])
    def parse_and_predict(examples):
      features = tf.compat.v1.parse_single_example(examples[0], feature_configs)
      return {'predictions': model(features['inputs']),
              'layer_1_outputs': model.layers[0](features['inputs'])}

    saved_model_dir = self._save_model_dir()
    model.save(saved_model_dir, save_format='tf', signatures={
        'predict': predict,
        'parse_and_predict': parse_and_predict})
    model.save('/tmp/saved', save_format='tf', signatures={
        'predict': predict,
        'parse_and_predict': parse_and_predict})

    loaded = keras_load.load(saved_model_dir)

    self.assertAllClose(
        model.predict(input_arr),
        loaded.signatures['predict'](tf.convert_to_tensor(
            input_arr.astype('float32')))['predictions'])

    feature = {
        'inputs': feature_pb2.Feature(
            float_list=feature_pb2.FloatList(
                value=input_arr.astype('float32').flatten()))}
    example = example_pb2.Example(
        features=feature_pb2.Features(feature=feature))
    outputs = loaded.signatures['parse_and_predict'](
        tf.convert_to_tensor([example.SerializeToString()]))
    self.assertAllClose(model.predict(input_arr), outputs['predictions'])
    self.assertAllClose(model.layers[0](input_arr), outputs['layer_1_outputs'])
  def create_tf_record(self):
    path = os.path.join(self.get_temp_dir(), 'tfrecord')
    writer = tf.python_io.TFRecordWriter(path)

    image_tensor = np.random.randint(255, size=(4, 5, 3)).astype(np.uint8)
    with self.test_session():
      encoded_jpeg = tf.image.encode_jpeg(tf.constant(image_tensor)).eval()
    example = example_pb2.Example(features=feature_pb2.Features(feature={
        'image/encoded': feature_pb2.Feature(
            bytes_list=feature_pb2.BytesList(value=[encoded_jpeg])),
        'image/format': feature_pb2.Feature(
            bytes_list=feature_pb2.BytesList(value=['jpeg'.encode('utf-8')])),
        'image/transcript': feature_pb2.Feature(
            bytes_list=feature_pb2.BytesList(value=[
                'hello'.encode('utf-8')]))
    }))
    writer.write(example.SerializeToString())
    writer.close()

    return path
Esempio n. 21
0
    def _write_test_data():
        schema = feature_spec_to_schema({"f1": tf.FixedLenFeature((), tf.int64),
                                         "f2": tf.FixedLenFeature((), tf.int64)})
        values = [{"f1": 1, "f2": 2}]

        example_proto = [example_pb2.Example(features=feature_pb2.Features(feature={
            k: feature_pb2.Feature(int64_list=feature_pb2.Int64List(value=[v]))
            for k, v in d.items()
        })) for d in values]

        return DataUtil.write_test_data(example_proto, schema)
    def _create_feature(feature):
        feature_list = feature if isinstance(feature, list) else [feature]

        # Each feature can be exactly one kind:
        # https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/example/feature.proto#L76

        feature_type = type(feature_list[0])
        if feature_type == int:
            return feature_pb2.Feature(int64_list=feature_pb2.Int64List(value=feature_list))
        elif feature_type == str:
            return feature_pb2.Feature(bytes_list=feature_pb2.BytesList(value=feature_list))
        elif feature_type == unicode:
            return feature_pb2.Feature(bytes_list=feature_pb2.BytesList(value=map(lambda x: str(x), feature_list)))
        elif feature_type == float:
            return feature_pb2.Feature(float_list=feature_pb2.FloatList(value=feature_list))
        else:
            message = """Unsupported request data format: {}, {}.
                            Valid formats: float, int, str any object that implements __iter__
                                           or classification_pb2.ClassificationRequest"""
            raise ValueError(message.format(feature, type(feature)))
Esempio n. 23
0
def test_example_proto():
    image = 'aa'
    example = example_pb2.Example(features=feature_pb2.Features(
        feature={
            'image/encoded':
            feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
                value=[image]))
        }))
    print('aaa')
    print(example)
    example.SerializeToString()
Esempio n. 24
0
 def test_parse_example_with_default_value(self):
     price = fc.numeric_column('price', shape=[2], default_value=11.)
     data = example_pb2.Example(features=feature_pb2.Features(
         feature={
             'price':
             feature_pb2.Feature(float_list=feature_pb2.FloatList(
                 value=[20., 110.]))
         }))
     no_data = example_pb2.Example(features=feature_pb2.Features(
         feature={
             'something_else':
             feature_pb2.Feature(float_list=feature_pb2.FloatList(
                 value=[20., 110.]))
         }))
     features = parsing_ops.parse_example(
         serialized=[data.SerializeToString(),
                     no_data.SerializeToString()],
         features=price._parse_example_config)
     self.assertIn('price', features)
     with self.test_session():
         self.assertAllEqual([[20., 110.], [11., 11.]],
                             features['price'].eval())
Esempio n. 25
0
 def _test_identity_savedmodel(self, export_dir):
   with tf.Graph().as_default() as graph:
     with tf.Session(graph=graph) as sess:
       metagraph_def = tf.saved_model.loader.load(sess, [tf.saved_model.SERVING], export_dir)
       fetch = metagraph_def.signature_def['predictions'].outputs['outputs']
       feed = metagraph_def.signature_def['predictions'].inputs['inputs']
       for x in self._data:
         example = example_pb2.Example(
             features=feature_pb2.Features(
                 feature={
                     'x':
                         feature_pb2.Feature(
                             float_list=feature_pb2.FloatList(
                                 value=np.ravel(x)))
                 })).SerializeToString()
         y = sess.run(fetch.name, feed_dict={feed.name: [example]})
         self.assertAlmostEqual(y, x[0], delta=0.01)
    def testDecodeExampleWithRepeatedImages(self):
        image_shape = (2, 3, 3)
        image_format = 'png'
        image, _ = self.GenerateImage(image_format=image_format,
                                      image_shape=image_shape)
        tf_encoded = self._Encoder(image, image_format)
        with self.test_session():
            tf_string = tf_encoded.eval()

        example = example_pb2.Example(features=feature_pb2.Features(
            feature={
                'image/encoded':
                feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
                    value=[tf_string, tf_string])),
                'image/format':
                self._StringFeature(image_format),
            }))
        serialized_example = example.SerializeToString()

        with self.test_session():
            serialized_example = array_ops.reshape(serialized_example,
                                                   shape=[])

            decoder = tfexample_decoder.TFExampleDecoder(
                keys_to_features={
                    'image/encoded':
                    parsing_ops.FixedLenFeature((2, ), dtypes.string),
                    'image/format':
                    parsing_ops.FixedLenFeature((),
                                                dtypes.string,
                                                default_value=image_format),
                },
                items_to_handlers={
                    'image': tfexample_decoder.Image(repeated=True)
                })
            [tf_image] = decoder.decode(serialized_example, ['image'])

            output_image = tf_image.eval()

            self.assertEqual(output_image.shape, (2, 2, 3, 3))
            self.assertAllEqual(np.squeeze(output_image[0, :, :, :]), image)
            self.assertAllEqual(np.squeeze(output_image[1, :, :, :]), image)
Esempio n. 27
0
def _string_feature(value):
    value = value.encode('utf-8')
    return feature_pb2.Feature(bytes_list=feature_pb2.BytesList(value=[value]))
Esempio n. 28
0
def _encoded_int64_feature(ndarray):
    return feature_pb2.Feature(int64_list=feature_pb2.Int64List(
        value=ndarray.flatten().tolist()))
Esempio n. 29
0
 def _BytesFeatureFromList(self, ndarray):
     values = ndarray.flatten().tolist()
     for i in range(len(values)):
         values[i] = values[i].encode('utf-8')
     return feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
         value=values))
Esempio n. 30
0
 def _Int64FeatureFromList(self, ndarray):
     return feature_pb2.Feature(int64_list=feature_pb2.Int64List(
         value=ndarray.flatten().tolist()))