Ejemplo n.º 1
0
def translate(stub, model_name, sentence_processor,
              input_string, timeout=5.0):
    """Translates a sequence of tokens.

    Args:
        stub: The prediction service stub.
        model_name: The model to request.
        sentence_processor: A `sentencepience.SentenceProcessor`
        input_string: The input string
        timeout: Timeout after this many seconds.

    Returns:
        A future.
    """
    tokens = sentence_processor.EncodeAsPieces(input_string)
    length = len(tokens)

    request = predict_pb2.PredictRequest()
    request.model_spec.name = model_name
    request.inputs["tokens"].CopyFrom(
            tf.make_tensor_proto([tokens], shape=(1, length)))
    request.inputs["length"].CopyFrom(
            tf.make_tensor_proto([length], shape=(1,)))

    return stub.Predict.future(request, timeout)
def test_pb_request():
    data = [x for x in xrange(784)]
    tensor_proto = tf.make_tensor_proto(values=np.asarray(data), shape=[1, len(data)], dtype=tf.float32)

    request = predict_pb2.PredictRequest()
    request.model_spec.name = "generic_model"
    request.model_spec.signature_name = DEFAULT_SERVING_SIGNATURE_DEF_KEY

    request.inputs[PREDICT_INPUTS].CopyFrom(tensor_proto)

    serialized_output = requests.post("http://localhost:8080/invocations",
                                      data=request.SerializeToString(),
                                      headers={
                                          'Content-type': 'application/octet-stream',
                                          'Accept': 'application/octet-stream'
                                      }).content

    predict_response = predict_pb2.PredictResponse()
    predict_response.ParseFromString(serialized_output)

    probabilities = predict_response.outputs['probabilities']
    assert len(probabilities.float_val) == 10
    for v in probabilities.float_val:
        assert v <= 1.
        assert v >= 0.
Ejemplo n.º 3
0
  def _CreateEventWithDebugNumericSummary(
      self, device_name, op_name, output_slot, wall_time, step, list_of_values):
    """Creates event with a health pill summary.

    Args:
      device_name: The name of the op's device.
      op_name: The name of the op to which a DebugNumericSummary was attached.
      output_slot: The numeric output slot for the tensor.
      wall_time: The numeric wall time of the event.
      step: The step of the event.
      list_of_values: A python list of values within the tensor.

    Returns:
      A `tf.Event` with a health pill summary.
    """
    event = tf.Event(step=step, wall_time=wall_time)
    value = event.summary.value.add(
        tag=op_name,
        node_name='%s:%d:DebugNumericSummary' % (op_name, output_slot),
        tensor=tf.make_tensor_proto(
            list_of_values, dtype=tf.float64, shape=[len(list_of_values)]))
    content_proto = debugger_event_metadata_pb2.DebuggerEventMetadata(
        device=device_name, output_slot=output_slot)
    value.metadata.plugin_data.plugin_name = constants.DEBUGGER_PLUGIN_NAME
    value.metadata.plugin_data.content = tf.compat.as_bytes(
        json_format.MessageToJson(
            content_proto, including_default_value_fields=True))
    return event
Ejemplo n.º 4
0
def pb(name, data, display_name=None, description=None):
  """Create a scalar summary protobuf.

  Arguments:
    name: A unique name for the generated summary, including any desired
      name scopes.
    data: A rank-0 `np.array` or array-like form (so raw `int`s and
      `float`s are fine, too).
    display_name: Optional name for this summary in TensorBoard, as a
      `str`. Defaults to `name`.
    description: Optional long-form description for this summary, as a
      `str`. Markdown is supported. Defaults to empty.

  Returns:
    A `tf.Summary` protobuf object.
  """
  data = np.array(data)
  if data.shape != ():
    raise ValueError('Expected scalar shape for data, saw shape: %s.'
                     % data.shape)
  if data.dtype.kind not in ('b', 'i', 'u', 'f'):  # bool, int, uint, float
    raise ValueError('Cast %s to float is not supported' % data.dtype.name)
  tensor = tf.make_tensor_proto(data.astype(np.float32))

  if display_name is None:
    display_name = name
  summary_metadata = metadata.create_summary_metadata(
      display_name=display_name, description=description)
  summary = tf.Summary()
  summary.value.add(tag='%s/scalar_summary' % name,
                    metadata=summary_metadata,
                    tensor=tensor)
  return summary
Ejemplo n.º 5
0
    def post(self, model, version=None):
        request_key = self.settings['request_key']
        request_data = tornado.escape.json_decode(self.request.body)
        instances = request_data.get(request_key)
        if not instances:
            self.send_error('Request json object have to use the key: %s'%request_key)

        if len(instances) < 1 or not isinstance(instances, (list, tuple)):
            self.send_error('Request instances object have to use be a list')

        instances = decode_b64_if_needed(instances)

        input_columns = instances[0].keys()

        request = predict_pb2.PredictRequest()
        request.model_spec.name = model

        if version is not None:
            request.model_spec.version = version
        
        for input_column in input_columns:
            values = [instance[input_column] for instance in instances]
            request.inputs[input_column].CopyFrom(tf.make_tensor_proto(values, shape=[len(values)]))

        stub = self.settings['stub']
        result = yield fwrap(stub.Predict.future(request, self.settings['rpc_timeout']))
        output_keys = result.outputs.keys()
        predictions = zip(*[tf.make_ndarray(result.outputs[output_key]).tolist() for output_key in output_keys])
        predictions = [dict(zip(*t)) for t in zip(repeat(output_keys), predictions)]
        self.write(dict(predictions=predictions))
Ejemplo n.º 6
0
def pb(name, data, display_name=None, description=None):
  """Create a text summary protobuf.

  Arguments:
    name: A name for the generated node. Will also serve as a series name in
      TensorBoard.
    data: A Python bytestring (of type bytes), or Unicode string. Or a numpy
      data array of those types.
    display_name: Optional name for this summary in TensorBoard, as a
      `str`. Defaults to `name`.
    description: Optional long-form description for this summary, as a
      `str`. Markdown is supported. Defaults to empty.

  Raises:
    ValueError: If the type of the data is unsupported.

  Returns:
    A `tf.Summary` protobuf object.
  """
  try:
    tensor = tf.make_tensor_proto(data, dtype=tf.string)
  except TypeError as e:
    raise ValueError(e)

  if display_name is None:
    display_name = name
  summary_metadata = metadata.create_summary_metadata(
      display_name=display_name, description=description)
  summary = tf.Summary()
  summary.value.add(tag='%s/text_summary' % name,
                    metadata=summary_metadata,
                    tensor=tensor)
  return summary
Ejemplo n.º 7
0
  def _create_event_with_float_tensor(self, node_name, output_slot, debug_op,
                                      list_of_values):
    """Creates event with float64 (double) tensors.

    Args:
      node_name: The string name of the op. This lacks both the output slot as
        well as the name of the debug op.
      output_slot: The number that is the output slot.
      debug_op: The name of the debug op to use.
      list_of_values: A python list of values within the tensor.
    Returns:
      A `tf.Event` with a summary containing that node name and a float64
      tensor with those values.
    """
    event = tf.Event()
    value = event.summary.value.add(
        tag=node_name,
        node_name="%s:%d:%s" % (node_name, output_slot, debug_op),
        tensor=tf.make_tensor_proto(
            list_of_values, dtype=tf.float64, shape=[len(list_of_values)]))
    plugin_content = debugger_event_metadata_pb2.DebuggerEventMetadata(
        device="/job:localhost/replica:0/task:0/cpu:0", output_slot=output_slot)
    value.metadata.plugin_data.plugin_name = constants.DEBUGGER_PLUGIN_NAME
    value.metadata.plugin_data.content = tf.compat.as_bytes(
        json_format.MessageToJson(
            plugin_content, including_default_value_fields=True))
    return event
Ejemplo n.º 8
0
def pb(name, data, bucket_count=None, display_name=None, description=None):
  """Create a histogram summary protobuf.

  Arguments:
    name: A unique name for the generated summary, including any desired
      name scopes.
    data: A `np.array` or array-like form of any shape. Must have type
      castable to `float`.
    bucket_count: Optional positive `int`. The output will have this
      many buckets, except in two edge cases. If there is no data, then
      there are no buckets. If there is data but all points have the
      same value, then there is one bucket whose left and right
      endpoints are the same.
    display_name: Optional name for this summary in TensorBoard, as a
      `str`. Defaults to `name`.
    description: Optional long-form description for this summary, as a
      `str`. Markdown is supported. Defaults to empty.

  Returns:
    A `tf.Summary` protobuf object.
  """
  if bucket_count is None:
    bucket_count = DEFAULT_BUCKET_COUNT
  data = np.array(data).flatten().astype(float)
  if data.size == 0:
    buckets = np.array([]).reshape((0, 3))
  else:
    min_ = np.min(data)
    max_ = np.max(data)
    range_ = max_ - min_
    if range_ == 0:
      center = min_
      buckets = np.array([[center - 0.5, center + 0.5, float(data.size)]])
    else:
      bucket_width = range_ / bucket_count
      offsets = data - min_
      bucket_indices = np.floor(offsets / bucket_width).astype(int)
      clamped_indices = np.minimum(bucket_indices, bucket_count - 1)
      one_hots = (np.array([clamped_indices]).transpose()
                  == np.arange(0, bucket_count))  # broadcast
      assert one_hots.shape == (data.size, bucket_count), (
          one_hots.shape, (data.size, bucket_count))
      bucket_counts = np.sum(one_hots, axis=0)
      edges = np.linspace(min_, max_, bucket_count + 1)
      left_edges = edges[:-1]
      right_edges = edges[1:]
      buckets = np.array([left_edges, right_edges, bucket_counts]).transpose()
  tensor = tf.make_tensor_proto(buckets, dtype=tf.float64)

  if display_name is None:
    display_name = name
  summary_metadata = metadata.create_summary_metadata(
      display_name=display_name, description=description)

  summary = tf.Summary()
  summary.value.add(tag='%s/histogram_summary' % name,
                    metadata=summary_metadata,
                    tensor=tensor)
  return summary
Ejemplo n.º 9
0
def raw_data_pb(
    name,
    true_positive_counts,
    false_positive_counts,
    true_negative_counts,
    false_negative_counts,
    precision,
    recall,
    num_thresholds=None,
    display_name=None,
    description=None):
  """Create a PR curves summary protobuf from raw data values.

  Args:
    name: A tag attached to the summary. Used by TensorBoard for organization.
    true_positive_counts: A rank-1 numpy array of true positive counts. Must
        contain `num_thresholds` elements and be castable to float32.
    false_positive_counts: A rank-1 numpy array of false positive counts. Must
        contain `num_thresholds` elements and be castable to float32.
    true_negative_counts: A rank-1 numpy array of true negative counts. Must
        contain `num_thresholds` elements and be castable to float32.
    false_negative_counts: A rank-1 numpy array of false negative counts. Must
        contain `num_thresholds` elements and be castable to float32.
    precision: A rank-1 numpy array of precision values. Must contain
        `num_thresholds` elements and be castable to float32.
    recall: A rank-1 numpy array of recall values. Must contain `num_thresholds`
        elements and be castable to float32.
    num_thresholds: Number of thresholds, evenly distributed in `[0, 1]`, to
        compute PR metrics for. Should be an int `>= 2`.
    display_name: Optional name for this summary in TensorBoard, as a `str`.
        Defaults to `name`.
    description: Optional long-form description for this summary, as a `str`.
        Markdown is supported. Defaults to empty.

  Returns:
    A summary operation for use in a TensorFlow graph. See docs for the `op`
    method for details on the float32 tensor produced by this summary.
  """
  if display_name is None:
    display_name = name
  summary_metadata = metadata.create_summary_metadata(
      display_name=display_name if display_name is not None else name,
      description=description or '',
      num_thresholds=num_thresholds)
  summary = tf.Summary()
  data = np.stack(
      (true_positive_counts,
       false_positive_counts,
       true_negative_counts,
       false_negative_counts,
       precision,
       recall))
  tensor = tf.make_tensor_proto(np.float32(data), dtype=tf.float32)
  summary.value.add(tag='%s/pr_curves' % name,
                    metadata=summary_metadata,
                    tensor=tensor)
  return summary
Ejemplo n.º 10
0
def _migrate_scalar_value(value):
  scalar_value = value.simple_value
  tensor_proto = tf.make_tensor_proto(scalar_value)
  summary_metadata = scalar_metadata.create_summary_metadata(
      display_name=value.metadata.display_name or value.tag,
      description=value.metadata.summary_description)
  return tf.Summary.Value(tag=value.tag,
                          metadata=summary_metadata,
                          tensor=tensor_proto)
Ejemplo n.º 11
0
def _migrate_audio_value(value):
  audio_value = value.audio
  data = [[audio_value.encoded_audio_string, b'']]  # empty label
  tensor_proto = tf.make_tensor_proto(data)
  summary_metadata = audio_metadata.create_summary_metadata(
      display_name=value.metadata.display_name or value.tag,
      description=value.metadata.summary_description,
      encoding=audio_metadata.Encoding.Value('WAV'))
  return tf.Summary.Value(tag=value.tag,
                          metadata=summary_metadata,
                          tensor=tensor_proto)
Ejemplo n.º 12
0
def send_request(stub, model_name, batch_tokens, timeout=5.0):
    """Sends a translation request.

  Args:
    stub: The prediction service stub.
    model_name: The model to request.
    tokens: A list of tokens.
    timeout: Timeout after this many seconds.

  Returns:
    A future.
  """
    batch_tokens, lengths, max_length = pad_batch(batch_tokens)
    batch_size = len(lengths)
    request = predict_pb2.PredictRequest()
    request.model_spec.name = model_name
    request.inputs["tokens"].CopyFrom(
        tf.make_tensor_proto(batch_tokens, shape=(batch_size, max_length)))
    request.inputs["length"].CopyFrom(
        tf.make_tensor_proto(lengths, shape=(batch_size, )))
    return stub.Predict.future(request, timeout)
def test_json_request():
    data = [x for x in xrange(784)]
    tensor_proto = tf.make_tensor_proto(values=np.asarray(data), shape=[1, len(data)], dtype=tf.float32)

    url = "http://localhost:8080/invocations"
    serialized_output = requests.post(url,
                                      MessageToJson(tensor_proto),
                                      headers={'Content-type': 'application/json'}).content

    prediction_result = json.loads(serialized_output)

    assert len(prediction_result['outputs']['probabilities']['floatVal']) == 10
Ejemplo n.º 14
0
def grpc_request(stub, data_sample, model_name=config.MODEL_NAME, \
                 signature_name='serving_default'):
    request = predict_pb2.PredictRequest()
    request.model_spec.name = model_name
    request.model_spec.signature_name = signature_name
    shp = [dim for dim in data_sample.shape]
    request.inputs['input_tensor'].CopyFrom(tf.make_tensor_proto(data_sample,
                                                                 shape=shp))
    # I think I need to increase this waiting time to something like 20sec? done
    result = stub.Predict(request, 100.0)

    return result
Ejemplo n.º 15
0
 def add_code_summary(self):
     code_string = "\n".join(open('LSTM.py', 'r').readlines())
     text_tensor = tf.make_tensor_proto(self.DESCRIPTION + "\n\n" +
                                        code_string,
                                        dtype=tf.string)
     meta = tf.SummaryMetadata()
     meta.plugin_data.plugin_name = "text"
     summary = tf.Summary()
     summary.value.add(tag="Hyper parameters",
                       metadata=meta,
                       tensor=text_tensor)
     self.summary_writer.add_summary(summary)
Ejemplo n.º 16
0
    def forward_request(self, batch_inputs, info, timeout=None):
        max_message_length = int(os.getenv('GRPC_MAX_MESSAGE_LENGTH', 100 * 1024 * 1024))  # 100MB
        channel = grpc.insecure_channel("localhost:%s" % info['port'], options=[
            ('grpc.max_send_message_length', max_message_length),
            ('grpc.max_receive_message_length', max_message_length)])
        stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

        max_length = max(len(src) for src in batch_inputs)
        tokens, lengths = utils.pad_lists(batch_inputs, padding_value='', max_length=max_length)
        batch_size = len(lengths)

        predict_request = predict_pb2.PredictRequest()
        predict_request.model_spec.name = info['model_name']
        predict_request.inputs['tokens'].CopyFrom(
            tf.make_tensor_proto(tokens, dtype=tf.string, shape=(batch_size, max_length)))
        predict_request.inputs['length'].CopyFrom(
            tf.make_tensor_proto(lengths, dtype=tf.int32, shape=(batch_size,)))

        try:
            future = stub.Predict.future(predict_request, timeout)
            result = future.result()
        except ExpirationError as e:
            logger.error('%s', e)
            return None

        lengths = tf.make_ndarray(result.outputs['length'])
        predictions = tf.make_ndarray(result.outputs['tokens'])
        log_probs = tf.make_ndarray(result.outputs['log_probs'])

        batch_outputs = []
        for hypotheses, length, log_prob in zip(predictions, lengths, log_probs):
            outputs = []
            for i, prediction in enumerate(hypotheses):
                prediction_length = length[i] - 1  # Ignore </s>.
                prediction = prediction[0:prediction_length].tolist()
                prediction = [tf.compat.as_text(pred) for pred in prediction]
                score = float(log_prob[i])
                outputs.append(serving.TranslationOutput(prediction, score=score))
            batch_outputs.append(outputs)
        return batch_outputs
Ejemplo n.º 17
0
  def test_model_predict(self, keyed_input: bool):
    predictions = [{
        'output_1': [0.901],
        'output_2': [0.997]
    }] * len(self._predict_examples)
    builder = http.RequestMockBuilder({
        'ml.projects.predict':
            (None, self._make_response_body(predictions, successful=True))
    })
    resource = discovery.build(
        'ml',
        'v1',
        http=http.HttpMock(self._discovery_testdata_dir,
                           {'status': http_client.OK}),
        requestBuilder=builder)
    with mock.patch('googleapiclient.discovery.' 'build') as response_mock:
      response_mock.side_effect = lambda service, version: resource
      inference_spec_type = model_spec_pb2.InferenceSpecType(
          ai_platform_prediction_model_spec=model_spec_pb2
          .AIPlatformPredictionModelSpec(
              project_id='test-project',
              model_name='test-model',
          ))
      expected = []
      for example in self._predict_examples:
        prediction_log = prediction_log_pb2.PredictionLog()
        predict_log = prediction_log.predict_log
        input_tensor_proto = predict_log.request.inputs['inputs']
        input_tensor_proto.dtype = tf.string.as_datatype_enum
        input_tensor_proto.tensor_shape.dim.add().size = 1
        input_tensor_proto.string_val.append(example.SerializeToString())
        predict_log.response.outputs['output_1'].CopyFrom(
            tf.make_tensor_proto(values=[0.901], dtype=tf.double, shape=(1, 1)))
        predict_log.response.outputs['output_2'].CopyFrom(
            tf.make_tensor_proto(values=[0.997], dtype=tf.double, shape=(1, 1)))
        expected.append(prediction_log)

      self._set_up_pipeline(inference_spec_type, keyed_input)
      assert_that(self.pcoll, equal_to(expected))
      self._run_inference_with_beam()
Ejemplo n.º 18
0
def fasttext_nce_loss(weights, biases, labels, inputs, params):
    """Fasttext nce loss. [batch, num_sampled] sampled ids."""

    num_classes = params['num_classes']
    opts = params['opts']

    dict_word_counts_path = os.path.join(opts.dict_dir,
                                         model_keys.DICT_WORD_COUNTS)
    unigrams = [int(word.strip()) for word in open(dict_word_counts_path)]
    unigrams = tf.make_tensor_proto(unigrams, dtype=tf.int64)

    # [batch, num_sampled]
    sampled_ids = custom_ops.fasttext_negative_sampler(
        true_classes=labels,
        num_true=opts.ntargets,
        num_sampled=opts.num_sampled,
        unique=True,
        range_max=num_classes,
        num_reserved_ids=1,
        unigrams=unigrams,
        seed=np.random.randint(1000000))

    # Weights for labels: [batch_size, emb_dim]
    true_w = tf.nn.embedding_lookup(weights, tf.reshape(labels, [-1]))
    # Biases for labels: [batch_size, emb_dim]
    true_b = tf.nn.embedding_lookup(biases, tf.reshape(labels, [-1]))

    # Weights for sampled ids: [batch, num_sampled, emb_dim]
    sampled_w = tf.nn.embedding_lookup(weights, sampled_ids)
    # Biases for sampled ids: [batch, num_sampled, 1]
    sampled_b = tf.nn.embedding_lookup(biases, sampled_ids)

    # True logits: [batch_size, 1]
    true_logits = tf.reduce_sum(tf.multiply(inputs, true_w), 1) + true_b

    # Sampled logits: [batch_size, num_sampled]
    sampled_b_vec = tf.reshape(sampled_b, [-1, opts.num_sampled])
    broadcast_inputs = tf.reshape(inputs, [-1, 1, opts.nce_dim])
    sampled_logits = tf.multiply(broadcast_inputs, sampled_w)
    sampled_logits = tf.reduce_sum(sampled_logits, -1) + sampled_b_vec

    # cross-entropy(logits, labels)
    true_xent = tf.nn.sigmoid_cross_entropy_with_logits(
        labels=tf.ones_like(true_logits), logits=true_logits)
    sampled_xent = tf.nn.sigmoid_cross_entropy_with_logits(
        labels=tf.zeros_like(sampled_logits), logits=sampled_logits)

    # NCE-loss is the sum of the true and noise (sampled words)
    # contributions, averaged over the batch.
    nce_loss_tensor = (tf.reduce_sum(true_xent) +
                       tf.reduce_sum(sampled_xent)) / opts.batch_size
    return nce_loss_tensor
Ejemplo n.º 19
0
def raw_data_pb(name,
                true_positive_counts,
                false_positive_counts,
                true_negative_counts,
                false_negative_counts,
                precision,
                recall,
                num_thresholds=None,
                display_name=None,
                description=None):
    """Create a PR curves summary protobuf from raw data values.

  Args:
    name: A tag attached to the summary. Used by TensorBoard for organization.
    true_positive_counts: A rank-1 numpy array of true positive counts. Must
        contain `num_thresholds` elements and be castable to float32.
    false_positive_counts: A rank-1 numpy array of false positive counts. Must
        contain `num_thresholds` elements and be castable to float32.
    true_negative_counts: A rank-1 numpy array of true negative counts. Must
        contain `num_thresholds` elements and be castable to float32.
    false_negative_counts: A rank-1 numpy array of false negative counts. Must
        contain `num_thresholds` elements and be castable to float32.
    precision: A rank-1 numpy array of precision values. Must contain
        `num_thresholds` elements and be castable to float32.
    recall: A rank-1 numpy array of recall values. Must contain `num_thresholds`
        elements and be castable to float32.
    num_thresholds: Number of thresholds, evenly distributed in `[0, 1]`, to
        compute PR metrics for. Should be an int `>= 2`.
    display_name: Optional name for this summary in TensorBoard, as a `str`.
        Defaults to `name`.
    description: Optional long-form description for this summary, as a `str`.
        Markdown is supported. Defaults to empty.

  Returns:
    A summary operation for use in a TensorFlow graph. See docs for the `op`
    method for details on the float32 tensor produced by this summary.
  """
    if display_name is None:
        display_name = name
    summary_metadata = metadata.create_summary_metadata(
        display_name=display_name if display_name is not None else name,
        description=description or '',
        num_thresholds=num_thresholds)
    summary = tf.Summary()
    data = np.stack(
        (true_positive_counts, false_positive_counts, true_negative_counts,
         false_negative_counts, precision, recall))
    tensor = tf.make_tensor_proto(np.float32(data), dtype=tf.float32)
    summary.value.add(tag='%s/pr_curves' % name,
                      metadata=summary_metadata,
                      tensor=tensor)
    return summary
Ejemplo n.º 20
0
    def PostProcess(self, grpc_flag):
        if (grpc_flag):
            tt = self.encoded_src.encode('utf-8')

            next_request = predict_pb2.PredictRequest()
            next_request.inputs['encoder_output'].CopyFrom(
              tf.make_tensor_proto(tt))

            return next_request
        else:
            result = dict()
            result["encoder_output"] = self.encoded_src
            return result
Ejemplo n.º 21
0
def main(_):
  assets_dir = make_assets_dir(tf.flags.FLAGS.export_dir)
  with tf.Session() as session:
    random_tensors = load_saved_model(session, tf.flags.FLAGS.export_dir)
    with tf.python_io.TFRecordWriter(os.path.join(assets_dir, 'tf_serving_warmup_requests')) as writer:
      for _ in range(tf.flags.FLAGS.batch_size):
        request = predict_pb2.PredictRequest(
          model_spec=model_pb2.ModelSpec(name=tf.flags.FLAGS.name),
          inputs={k: tf.make_tensor_proto(v) for k, v in session.run(random_tensors).items()}
        )
        log = prediction_log_pb2.PredictionLog(
          predict_log=prediction_log_pb2.PredictLog(request=request))
        writer.write(log.SerializeToString())
Ejemplo n.º 22
0
def _migrate_image_value(value):
  image_value = value.image
  data = [tf.compat.as_bytes(str(image_value.width)),
          tf.compat.as_bytes(str(image_value.height)),
          tf.compat.as_bytes(image_value.encoded_image_string)]

  tensor_proto = tf.make_tensor_proto(data)
  summary_metadata = image_metadata.create_summary_metadata(
      display_name=value.metadata.display_name or value.tag,
      description=value.metadata.summary_description)
  return tf.Summary.Value(tag=value.tag,
                          metadata=summary_metadata,
                          tensor=tensor_proto)
Ejemplo n.º 23
0
 def prepare_request(self, input_data):
     request = PredictRequest()
     request.model_spec.name = self.name
     request.model_spec.signature_name = self.signature_name
     request.inputs[self.input_name].CopyFrom(
         tf.make_tensor_proto(
             tf.convert_to_tensor(
                 input_data,
                 dtype = tf.string
             )
         )
     )
     return request
Ejemplo n.º 24
0
    def beam_search(self, logits):
        assert len(logits.shape
                   ) == 2, f"Logits should be rank 2, got shape {logits.shape}"
        f = concurrent.futures.Future()

        request = predict_pb2.PredictRequest()
        request.CopyFrom(self.req)
        request.inputs['logits'].CopyFrom(
            tf.make_tensor_proto(logits[np.newaxis, :, :]), )

        result = self.stub.Predict(request, 120.0)  # 120 secs timeout
        f.set_result(np.array(result.outputs['path'].int64_val))
        return f
Ejemplo n.º 25
0
    def AddScalarTensor(self, tag, wall_time=0, step=0, value=0):
        """Add a rank-0 tensor event.

    Note: This is not related to the scalar plugin; it's just a
    convenience function to add an event whose contents aren't
    important.
    """
        tensor = tf.make_tensor_proto(float(value))
        event = tf.Event(wall_time=wall_time,
                         step=step,
                         summary=tf.Summary(
                             value=[tf.Summary.Value(tag=tag, tensor=tensor)]))
        self.AddEvent(event)
Ejemplo n.º 26
0
def resize_data(image):
    """
    Use bi-linear interpolation to skrink the image.
    """
    image = tf.reshape(image, shape=[128, 128, 1])
    image = tf.image.resize(image,
                            size=[64, 64],
                            preserve_aspect_ratio=False,
                            antialias=False,
                            name=None)
    image = tf.reshape(image, shape=[64, 64])
    proto_tensor = tf.make_tensor_proto(image)
    return tf.make_ndarray(proto_tensor)
Ejemplo n.º 27
0
    def _WriteToLog(self, text, logdir, filename):
        """Logs `text` and saves it under `logdir/filename`."""
        with tf.gfile.FastGFile(os.path.join(logdir, filename), 'w') as f:
            f.write(text)

        if self._summary_writer is not None:
            # Force newlines to be rendered correctly by Markdown.
            text = text.replace('\n', '  \n')
            self._summary_writer.add_summary(
                tf.Summary(value=[
                    tf.Summary.Value(tag=filename,
                                     tensor=tf.make_tensor_proto([text]))
                ]))
 def PostProcess(self, grpc_flag):
     if (grpc_flag):
         next_request = predict_pb2.PredictRequest()
         # next_request.inputs['client_input'].CopyFrom(
         #   tf.make_tensor_proto(self.image))
         next_request.inputs['FINAL'].CopyFrom(
           tf.make_tensor_proto(self.output))
     else:
         next_request = dict()
         # next_request['client_input'] = self.image
         next_request['FINAL'] = self.output
         # print("[tracker] size of result = %s" % str(sys.getsizeof(result)))
     return next_request
def translate(stub, model_name, tokens, timeout=5.0):
  """Translates a sequence of tokens.

  Args:
    stub: The prediction service stub.
    model_name: The model to request.
    tokens: A list of tokens.
    timeout: Timeout after this many seconds.

  Returns:
    A future.
  """
  length = len(tokens)

  request = predict_pb2.PredictRequest()
  request.model_spec.name = model_name
  request.inputs["tokens"].CopyFrom(
      tf.make_tensor_proto([tokens], shape=(1, length)))
  request.inputs["length"].CopyFrom(
      tf.make_tensor_proto([length], shape=(1,)))

  return stub.Predict.future(request, timeout)
    def PostProcess(self, grpc_flag):
        if (grpc_flag):
            # tt = self.final_result.encode('utf-8')
            next_request = predict_pb2.PredictRequest()
            next_request.inputs['FINAL'].CopyFrom(
                tf.make_tensor_proto(self.final_result,
                                     shape=self.final_result.shape))

            return next_request
        else:
            result = dict()
            result["FINAL"] = self.final_result
            return result
Ejemplo n.º 31
0
 def save_history(self, game_history):
     request = replay_buffer_pb2.GameHistory()
     request.observations.extend([tf.make_tensor_proto(observation) for observation in game_history.observations])
     request.actions.extend([action.index for action in game_history.actions])
     request.rewards.extend(game_history.rewards)
     request.to_plays.extend([player.player_id for player in game_history.to_plays])
     request.root_values.extend(game_history.root_values)
     for probabilities in game_history.policies:
         policy = replay_buffer_pb2.Policy()
         policy.probabilities.extend(probabilities)
         request.policies.append(policy)
     response = self.remote_replay_buffer.SaveGame(request)
     return response.success
Ejemplo n.º 32
0
    def post(self, model_name, version_name=None):
        if not self.settings['signature_map'].get(model_name):
            self.settings['signature_map'][model_name] = get_signature_map(
                self.settings['stub'], model_name)

        request_key = self.settings['request_key']
        request_data = tornado.escape.json_decode(self.request.body)
        instances = request_data.get(request_key)
        if not instances:
            self.send_error('Request json object have to use the key: %s' %
                            request_key)
        if len(instances) < 1 or not isinstance(instances, (list, tuple)):
            self.send_error('Request instances object have to use be a list')
        instances = decode_b64_if_needed(instances)

        signature_name = request_data.get("signature_name")
        signature_name_used, signature = get_signature(
            self.settings['signature_map'][model_name], signature_name)
        input_columns = signature.inputs.keys()

        request = predict_pb2.PredictRequest()
        request.model_spec.name = model_name
        request.model_spec.signature_name = signature_name_used

        if version_name is not None:
            request.model_spec.version = version_name

        inputs_type_map = signature.inputs
        for input_column in input_columns:
            values = [instance[input_column] for instance in instances]
            request.inputs[input_column].CopyFrom(
                tf.make_tensor_proto(values,
                                     inputs_type_map[input_column].dtype))

        stub = self.settings['stub']
        result = yield fwrap(
            stub.Predict.future(request, self.settings['rpc_timeout']))
        output_keys = result.outputs.keys()
        predictions = zip(*[
            tf.make_ndarray(result.outputs[output_key]).tolist()
            for output_key in output_keys
        ])
        predictions = [
            dict(zip(*t)) for t in zip(repeat(output_keys), predictions)
        ]
        self.write(dict(predictions=predictions))

        if self.settings['request_logger'] is not None:
            for instance in instances:
                if random.random() < self.settings['request_log_prob']:
                    self.settings['request_logger'].info(json.dumps(instance))
Ejemplo n.º 33
0
def detect_mask_single_image_using_grpc(image):
    images = np.expand_dims(image, axis=0)
    molded_images, image_metas, windows = preprocess_obj.mold_inputs(images)
    molded_images = molded_images.astype(np.float32)
    image_metas = image_metas.astype(np.float32)
    # Validate image sizes
    # All images in a batch MUST be of the same size
    image_shape = molded_images[0].shape
    for g in molded_images[1:]:
        assert g.shape == image_shape, \
            "After resizing, all images must have the same size. Check IMAGE_RESIZE_MODE and image sizes."

    # Anchors
    anchors = preprocess_obj.get_anchors(image_shape)
    anchors = np.broadcast_to(anchors, (molded_images.shape[0],) + anchors.shape)


    if int(tf.__version__[0]) >= 2 :
        request.inputs[saved_model_config.INPUT_IMAGE].CopyFrom(
            tf.make_tensor_proto(molded_images, shape=molded_images.shape))
        request.inputs[saved_model_config.INPUT_IMAGE_META].CopyFrom(
            tf.make_tensor_proto(image_metas, shape=image_metas.shape))
        request.inputs[saved_model_config.INPUT_ANCHORS].CopyFrom(
            tf.make_tensor_proto(anchors, shape=anchors.shape))
    else:
        request.inputs[saved_model_config.INPUT_IMAGE].CopyFrom(
            tf.contrib.util.make_tensor_proto(molded_images, shape=molded_images.shape))
        request.inputs[saved_model_config.INPUT_IMAGE_META].CopyFrom(
            tf.contrib.util.make_tensor_proto(image_metas, shape=image_metas.shape))
        request.inputs[saved_model_config.INPUT_ANCHORS].CopyFrom(
            tf.contrib.util.make_tensor_proto(anchors, shape=anchors.shape))

    try:
        result = stub.Predict(request,30)#(request,10.0)
    except:
        return None
    result_dict = preprocess_obj.result_to_dict(images, molded_images, windows, result)[0]
    return result_dict
Ejemplo n.º 34
0
def _serialize_tensor_value(
        value: Any,
        type_spec: computation_types.TensorType) -> _SerializeReturnType:
    """Serializes a tensor value into `executor_pb2.Value`.

  Args:
    value: A Numpy array or other object understood by `tf.make_tensor_proto`.
    type_spec: A `tff.TensorType`.

  Returns:
    A tuple `(value_proto, ret_type_spec)` in which `value_proto` is an instance
    of `executor_pb2.Value` with the serialized content of `value`, and
    `ret_type_spec` is the type of the serialized value. The `ret_type_spec` is
    the same as the argument `type_spec` if that argument was not `None`. If
    the argument was `None`, `ret_type_spec` is a type determined from `value`.

  Raises:
    TypeError: If the arguments are of the wrong types.
    ValueError: If the value is malformed.
  """
    if isinstance(value, tf.Tensor):
        value = value.numpy()
    if isinstance(value, np.ndarray):
        tensor_proto = tf.make_tensor_proto(value,
                                            dtype=type_spec.dtype,
                                            verify_shape=False)
    else:
        tensor_proto = tf.make_tensor_proto(value,
                                            dtype=type_spec.dtype,
                                            shape=type_spec.shape,
                                            verify_shape=True)
    type_spec.check_assignable_from(
        computation_types.TensorType(
            dtype=tf.dtypes.as_dtype(tensor_proto.dtype),
            shape=tf.TensorShape(tensor_proto.tensor_shape)))
    any_pb = any_pb2.Any()
    any_pb.Pack(tensor_proto)
    return executor_pb2.Value(tensor=any_pb), type_spec
Ejemplo n.º 35
0
def grpc_yolov4_client(host, model_name, image, shape_size=416):
    channel = grpc.insecure_channel(host)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    request = predict_pb2.PredictRequest()
    request.model_spec.name = model_name
    request.model_spec.signature_name = 'serving_default'
    request.inputs['input_1'].CopyFrom(
        tf.make_tensor_proto(image, shape=[1, shape_size, shape_size, 3]))
    pred_bbox = stub.Predict(request, 30.0)
    channel.close()
    result = [
        tf.make_ndarray(pred_bbox.outputs[a]) for a in list(pred_bbox.outputs)
    ]
    return result
Ejemplo n.º 36
0
    def PostProcess(self):
        if not self.can_output:
            frames_output = "None"
            temporal_rois_output = "None"
            norm_rois_output = "None"
            actor_boxes_output = "None"
        else:
            frames_output = pickle.dumps(self.frames)
            temporal_rois_output = pickle.dumps(self.temporal_rois)
            norm_rois_output = pickle.dumps(self.norm_rois)
            actor_boxes_output = pickle.dumps(self.actor_boxes)

        next_request = predict_pb2.PredictRequest()
        next_request.inputs['frames_output'].CopyFrom(
            tf.make_tensor_proto(frames_output))
        next_request.inputs['temporal_rois_output'].CopyFrom(
            tf.make_tensor_proto(temporal_rois_output))
        next_request.inputs['norm_rois_output'].CopyFrom(
            tf.make_tensor_proto(norm_rois_output))
        next_request.inputs['actor_boxes_output'].CopyFrom(
            tf.make_tensor_proto(actor_boxes_output))

        return next_request
Ejemplo n.º 37
0
def main():
    # Create request for a model server
    request = predict_pb2.PredictRequest()
    request.model_spec.name = FLAGS.model
    request.model_spec.signature_name = 'serving_default'

    # Create prediction service stub
    channel = grpc.insecure_channel("{}:{}".format(FLAGS.host, FLAGS.port))
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)

    # Concurrency defines how many max requests are in a queue for processing
    state = _State(0, int(FLAGS.concurrency))

    # Read webcam or video file
    try:
        video = cv2.VideoCapture(int(FLAGS.video))
        # video.set(cv2.CAP_PROP_FPS, 1) Does not work as expected, device driver must be capable to set it (for Linux)
    except:
        video = cv2.VideoCapture(FLAGS.video)

    while True:
        success, raw_frame = video.read()

        if success is False:
            print("Video read failed!")
            break

        state.inc_total()

        # Convert frame to Tensor
        float_frame = raw_frame.astype(numpy.float32)

        image = cv2.resize(float_frame, (int(FLAGS.size), int(FLAGS.size)))
        tensor = tensorflow.make_tensor_proto(image,
                                              shape=[1] + list(image.shape))

        # Prepare model input
        request.inputs['input_1'].CopyFrom(tensor)

        state.throttle()
        print('predict')
        # Predict
        predict = stub.Predict.future(request)
        predict.add_done_callback(_create_rpc_callback(state))

    print("Error ration: {}".format(state.get_error_rate()))
    print("Mean: {}".format(state.get_percentile(95)))
    print("Jitter: {}".format(state.get_execution_time_jitter()))

    state.save_measurements()
Ejemplo n.º 38
0
def send_image_ovms(address, port, image, model_name, input_name, output_name,
                    channels, height, width):
    channel = grpc.insecure_channel("{}:{}".format(address, port))
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    shape = (1, channels, height, width)
    image.reshape(shape)
    request = predict_pb2.PredictRequest()
    request.model_spec.name = model_name
    request.inputs[input_name].CopyFrom(make_tensor_proto(image, shape=shape))
    result = stub.Predict(request, 10.0)
    output = {}
    for out in output_name:
        output[out] = make_ndarray(result.outputs[out])
    return output
Ejemplo n.º 39
0
def _migrate_histogram_value(value):
  histogram_value = value.histo
  bucket_lefts = [histogram_value.min] + histogram_value.bucket_limit[:-1]
  bucket_rights = histogram_value.bucket_limit[:-1] + [histogram_value.max]
  bucket_counts = histogram_value.bucket
  buckets = np.array([bucket_lefts, bucket_rights, bucket_counts]).transpose()

  tensor_proto = tf.make_tensor_proto(buckets)
  summary_metadata = histogram_metadata.create_summary_metadata(
      display_name=value.metadata.display_name or value.tag,
      description=value.metadata.summary_description)
  return tf.Summary.Value(tag=value.tag,
                          metadata=summary_metadata,
                          tensor=tensor_proto)
Ejemplo n.º 40
0
def infer_batch(batch_input, input_tensor, grpc_stub, model_spec_name,
                model_spec_version, output_tensors):
    request = predict_pb2.PredictRequest()
    request.model_spec.name = model_spec_name
    if model_spec_version is not None:
        request.model_spec.version.value = model_spec_version
    print("input shape", list(batch_input.shape))
    request.inputs[input_tensor].CopyFrom(
        make_tensor_proto(batch_input, shape=list(batch_input.shape)))
    result = grpc_stub.Predict(request, 10.0)
    data = {}
    for output_tensor in output_tensors:
        data[output_tensor] = make_ndarray(result.outputs[output_tensor])
    return data
Ejemplo n.º 41
0
    def pre_process(self, dictionnaire: dict) -> TensorProto:
        """[summary]

        Args:
            dictionnaire (dict): [Dictionary containing the id of the images]

        Returns:
            TensorProto: [description]
        """

        idPT = str(dictionnaire['id'][0])
        idCT = str(dictionnaire['id'][1])
        idserie_dicom = str(dictionnaire['id'][2])

        data_path = settings.STORAGE_DIR
        path_ct = data_path + '/image/image_' + idCT + '_CT.nii'
        path_pt = data_path + '/image/image_' + idPT + '_PT.nii'
        img_ct = sitk.ReadImage(path_ct)
        img_pt = sitk.ReadImage(path_pt)

        #save original direction, spacing and origin
        self.spacing = img_pt.GetSpacing()
        self.direction = img_pt.GetDirection()
        self.origin = img_pt.GetOrigin()
        self.size = img_pt.GetSize()
        fusion_object = Fusion()
        fusion_object.set_origin_image(img_pt)
        fusion_object.set_target_volume((128, 128, 256), (4.0, 4.0, 4.0),
                                        (1, 0, 0, 0, 1, 0, 0, 0, 1))
        ct_resampled = fusion_object.resample(img_ct, -1000.0)
        pt_resampled = fusion_object.resample(img_pt, 0)
        self.pt_resampled_origin = pt_resampled.GetOrigin()
        self.pt_resampled_spacing = pt_resampled.GetSpacing()
        self.pt_resampled_direction = pt_resampled.GetDirection()

        ct_array = sitk.GetArrayFromImage(ct_resampled)
        pt_array = sitk.GetArrayFromImage(pt_resampled)

        #Normalize PET
        pt_array[np.where(pt_array < 0)] = 0  #0 SUV
        pt_array[np.where(pt_array > 25)] = 25  #25 SUV
        pt_array = pt_array[:, :, ] / 25

        #Normalize CT
        ct_array[np.where(ct_array < -1000)] = -1000  #-1000 SUV
        ct_array[np.where(ct_array > 1000)] = 1000  #1000 SUV
        ct_array = ct_array + 1000
        ct_array = ct_array[:, :, ] / 2000
        data = np.stack((pt_array, ct_array), axis=-1).astype('float32')
        return tf.make_tensor_proto(data, shape=[1, 256, 128, 128, 2])
    def _value_to_tensor(self, value):
        """Converts the given value to a tensor_pb2.TensorProto. Used on code path for creating PredictRequests."""
        if isinstance(value, tensor_pb2.TensorProto):
            return value

        msg = """Unable to convert value to TensorProto: {}. 
        Valid formats: tensor_pb2.TensorProto, list, numpy.ndarray"""
        try:
            # TODO: tensorflow container supports prediction requests with ONLY one tensor as input
            input_type = self.input_type_map.values()[0]
            ndarray = np.asarray(value)
            return make_tensor_proto(values=ndarray, dtype=input_type, shape=ndarray.shape)
        except Exception:
            raise ValueError(msg.format(value))
    def _value_to_tensor(self, value):
        """Converts the given value to a tensor_pb2.TensorProto. Used on code path for creating PredictRequests."""
        if isinstance(value, tensor_pb2.TensorProto):
            return value

        msg = """Unable to convert value to TensorProto: {}. 
        Valid formats: tensor_pb2.TensorProto, list, numpy.ndarray"""
        try:
            # TODO: tensorflow container supports prediction requests with ONLY one tensor as input
            input_type = self.input_type_map.values()[0]
            ndarray = np.asarray(value)
            return make_tensor_proto(values=ndarray, dtype=input_type, shape=ndarray.shape)
        except Exception:
            raise ValueError(msg.format(value))
Ejemplo n.º 44
0
def _transform_request(request: bytes) -> dict:
    # Convert from bytes to tf.tensor, np.array, etc.
    # This needs to be a JPEG
    request_str = request.decode('utf-8')
    request_json = json.loads(request_str)

    #    image_url = request_json['image_url']
    #    image = Image.open(requests.get(image_url, stream=True).raw)

    image_response = requests.get(request_json['image_url'])
    #    with BytesIO(image_response.content) as f:
    #        with Image.open(f) as img:
    #            print(img)
    #            image = img

    #    image_file_path = '%s/images/fregly_avatar.jpg' % os.environ['PIPELINE_INPUT_PATH']

    from datetime import datetime
    version = int(datetime.now().strftime("%s"))

    image_file_path = 'blah-%s.jpg' % version

    with open(image_file_path, 'wb') as f:
        f.write(image_response.content)

    with open(image_file_path, 'rb') as f:
        image = f.read()


# TODO:  https://towardsdatascience.com/tensorflow-serving-client-make-it-slimmer-and-faster-b3e5f71208fb
#        https://github.com/Vetal1977/tf_serving_example/tree/master/tensorflow/core/framework
#image_tensor = tf.make_tensor_proto(image)
#                                    shape=[1])
# NEW STUFF - pipeline_model==1.10+
# Replacement for tf.make_tensor_proto(image, shape=[1])
# Create TensorProto object for a request
#
#from tensorflow.core.framework import tensor_pb2
#from tensorflow.core.framework import tensor_shape_pb2
#from tensorflow.core.framework import types_pb2
#
#dims = [tensor_shape_pb2.TensorShapeProto.Dim(size=1)]
#tensor_shape_proto = tensor_shape_pb2.TensorShapeProto(dim=dims)
#image_tensor_proto = tensor_pb2.TensorProto(dtype=types_pb2.DT_STRING,
#                                            tensor_shape=tensor_shape_proto,
#                                            string_val=[image])
#
    image_tensor_proto = tf.make_tensor_proto(image, shape=[1])

    return {"images": image_tensor_proto}
  def AddScalarTensor(self, tag, wall_time=0, step=0, value=0):
    """Add a rank-0 tensor event.

    Note: This is not related to the scalar plugin; it's just a
    convenience function to add an event whose contents aren't
    important.
    """
    tensor = tf.make_tensor_proto(float(value))
    event = tf.Event(
        wall_time=wall_time,
        step=step,
        summary=tf.Summary(
            value=[tf.Summary.Value(tag=tag, tensor=tensor)]))
    self.AddEvent(event)
    def PostProcess(self, grpc_flag):
        if (grpc_flag):
            tt = self.final_result.encode('utf-8')
            next_request = predict_pb2.PredictRequest()
            # next_request.inputs['general_transformer_output'].CopyFrom(
            #   tf.make_tensor_proto(tt))
            next_request.inputs['FINAL'].CopyFrom(tf.make_tensor_proto(tt))

            return next_request
        else:
            result = dict()
            # result["general_transformer_output"] = self.final_result
            result["FINAL"] = self.final_result
            return result
Ejemplo n.º 47
0
  def normalize_summary_pb(self, pb):
    """Pass `pb`'s `TensorProto` through a marshalling roundtrip.

    `TensorProto`s can be equal in value even if they are not identical
    in representation, because data can be stored in either the
    `tensor_content` field or the `${dtype}_value` field. This
    normalization ensures a canonical form, and should be used before
    comparing two `Summary`s for equality.
    """
    result = tf.Summary()
    result.MergeFrom(pb)
    for value in result.value:
      if value.HasField('tensor'):
        new_tensor = tf.make_tensor_proto(tf.make_ndarray(value.tensor))
        value.ClearField('tensor')
        value.tensor.MergeFrom(new_tensor)
    return result
Ejemplo n.º 48
0
def pb(name, images, max_outputs=3, display_name=None, description=None):
  """Create an image summary protobuf.

  This behaves as if you were to create an `op` with the same arguments
  (wrapped with constant tensors where appropriate) and then execute
  that summary op in a TensorFlow session.

  Arguments:
    name: A unique name for the generated summary, including any desired
      name scopes.
    images: An `np.array` representing pixel data with shape
      `[k, w, h, c]`, where `k` is the number of images, `w` and `h` are
      the width and height of the images, and `c` is the number of
      channels, which should be 1, 3, or 4.
    max_outputs: Optional `int`. At most this many images will be
      emitted. If more than this many images are provided, the first
      `max_outputs` many images will be used and the rest silently
      discarded.
    display_name: Optional name for this summary in TensorBoard, as a
      `str`. Defaults to `name`.
    description: Optional long-form description for this summary, as a
      `str`. Markdown is supported. Defaults to empty.

  Returns:
    A `tf.Summary` protobuf object.
  """
  images = np.array(images).astype(np.uint8)
  if images.ndim != 4:
    raise ValueError('Shape %r must have rank 4' % (images.shape, ))

  limited_images = images[:max_outputs]
  encoded_images = [util.encode_png(image) for image in limited_images]
  (width, height) = (images.shape[1], images.shape[2])
  content = [str(width), str(height)] + encoded_images
  tensor = tf.make_tensor_proto(content, dtype=tf.string)

  if display_name is None:
    display_name = name
  summary_metadata = metadata.create_summary_metadata(
      display_name=display_name, description=description)

  summary = tf.Summary()
  summary.value.add(tag='%s/image_summary' % name,
                    metadata=summary_metadata,
                    tensor=tensor)
  return summary
Ejemplo n.º 49
0
def main(image_paths, server, port):
  channel = implementations.insecure_channel(server, port)
  stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

  raw_images = []
  for path in image_paths:
    with tf.gfile.Open(path) as img:
      raw_images.append(img.read())

  # Send request
  # See prediction_service.proto for gRPC request/response details.
  request = predict_pb2.PredictRequest()
  request.model_spec.name = 'inception'
  request.model_spec.signature_name = 'predict_images'
  request.inputs['images'].CopyFrom(
      tf.make_tensor_proto(raw_images, shape=[len(raw_images)]))
  result = stub.Predict(request, 10.0)  # 10 secs timeout
  print(result)
  def _writeMetadata(self, logdir, summary_metadata, nonce=''):
    """Write to disk a summary with the given metadata.

    Arguments:
      logdir: a string
      summary_metadata: a `SummaryMetadata` protobuf object
      nonce: optional; will be added to the end of the event file name
        to guarantee that multiple calls to this function do not stomp the
        same file
    """

    summary = tf.Summary()
    summary.value.add(
        tensor=tf.make_tensor_proto(['po', 'ta', 'to'], dtype=tf.string),
        tag='you_are_it',
        metadata=summary_metadata)
    writer = tf.summary.FileWriter(logdir, filename_suffix=nonce)
    writer.add_summary(summary.SerializeToString())
    writer.close()
def test_predict_tensor_request_csv(sagemaker_session):
    data = [6.4, 3.2, .5, 1.5]
    tensor_proto = tf.make_tensor_proto(values=np.asarray(data), shape=[1, len(data)], dtype=tf.float32)
    predictor = RealTimePredictor(serializer=tf_csv_serializer,
                                  deserializer=tf_json_deserializer,
                                  sagemaker_session=sagemaker_session,
                                  endpoint=ENDPOINT)

    mock_response(json.dumps(CLASSIFICATION_RESPONSE).encode('utf-8'), sagemaker_session, JSON_CONTENT_TYPE)

    result = predictor.predict(tensor_proto)

    sagemaker_session.sagemaker_runtime_client.invoke_endpoint.assert_called_once_with(
        Accept=JSON_CONTENT_TYPE,
        Body='6.4,3.2,0.5,1.5',
        ContentType=CSV_CONTENT_TYPE,
        EndpointName='myendpoint'
    )

    assert result == CLASSIFICATION_RESPONSE
Ejemplo n.º 52
0
def pb(scalars_layout):
  """Creates a summary that contains a layout.

  When users navigate to the custom scalars dashboard, they will see a layout
  based on the proto provided to this function.

  Args:
    scalars_layout: The scalars_layout_pb2.Layout proto that specifies the
        layout.

  Returns:
    A summary proto containing the layout.
  """
  assert isinstance(scalars_layout, layout_pb2.Layout)
  tensor = tf.make_tensor_proto(
      scalars_layout.SerializeToString(), dtype=tf.string)
  summary = tf.Summary()
  summary.value.add(tag=metadata.CONFIG_SUMMARY_TAG,
                    metadata=_create_summary_metadata(),
                    tensor=tensor)
  return summary
    def _create_input_map(self, data):
        """
        Parses the input data and returns a dict<string, TensorProto> which will be used to create the predict request.
        If the input data is not a dict, a dictionary will be created with the default predict key PREDICT_INPUTS

        input.

        Examples:
            input                                   => output
            {'inputs': tensor_proto}                => {'inputs': tensor_proto}
            tensor_proto                            => {PREDICT_INPUTS: tensor_proto}
            [1,2,3]                                 => {PREDICT_INPUTS: tensor_proto(1,2,3)}
        Args:
            data: request data. Can be any instance of dict<string, tensor_proto>, tensor_proto or any array like data.

        Returns:
            dict<string, tensor_proto>


        """
        msg = """Unsupported request data format: {}.
Valid formats: tensor_pb2.TensorProto, dict<string,  tensor_pb2.TensorProto> and predict_pb2.PredictRequest"""

        if isinstance(data, dict):
            if all(isinstance(v, tensor_pb2.TensorProto) for k, v in data.items()):
                return data
            raise ValueError(msg.format(data))

        if isinstance(data, tensor_pb2.TensorProto):
            return {self.input_tensor_name: data}

        try:
            # TODO: tensorflow container supports prediction requests with ONLY one tensor as input
            input_type = self.input_type_map.values()[0]
            ndarray = np.asarray(data)
            tensor_proto = make_tensor_proto(values=ndarray, dtype=input_type, shape=ndarray.shape)
            return {self.input_tensor_name: tensor_proto}
        except:
            raise ValueError(msg.format(data))
Ejemplo n.º 54
0
def pb(name,
       audio,
       sample_rate,
       labels=None,
       max_outputs=3,
       encoding=None,
       display_name=None,
       description=None):
  """Create an audio summary protobuf.

  This behaves as if you were to create an `op` with the same arguments
  (wrapped with constant tensors where appropriate) and then execute
  that summary op in a TensorFlow session.

  Arguments:
    name: A unique name for the generated summary node.
    audio: An `np.array` representing audio data with shape `[k, t, c]`,
      where `k` is the number of audio clips, `t` is the number of
      frames, and `c` is the number of channels. Elements should be
      floating-point values in `[-1.0, 1.0]`.
    sample_rate: An `int` that represents the sample rate, in Hz.
      Must be positive.
    labels: Optional list (or rank-1 `np.array`) of textstrings or UTF-8
      bytestrings whose length is the first dimension of `audio`, where
      `labels[i]` contains arbitrary textual information about
      `audio[i]`. (For instance, this could be some text that a TTS
      system was supposed to produce.) Markdown is supported.
    max_outputs: Optional `int`. At most this many audio clips will be
      emitted. When more than `max_outputs` many clips are provided, the
      first `max_outputs` many clips will be used and the rest silently
      discarded.
    encoding: A constant `str` indicating the desired encoding. You
      can choose any format you like, as long as it's "wav". Please see
      the "API compatibility note" below.
    display_name: Optional name for this summary in TensorBoard, as a
      `str`. Defaults to `name`.
    description: Optional long-form description for this summary, as a
      `str`. Markdown is supported. Defaults to empty.

  Returns:
    A `tf.Summary` protobuf object.

  API compatibility note: The default value of the `encoding`
  argument is _not_ guaranteed to remain unchanged across TensorBoard
  versions. In the future, we will by default encode as FLAC instead of
  as WAV. If the specific format is important to you, please provide a
  file format explicitly.
  """
  audio = np.array(audio)
  if audio.ndim != 3:
    raise ValueError('Shape %r must have rank 3' % (audio.shape,))
  if encoding is None:
    encoding = 'wav'

  if encoding == 'wav':
    encoding = metadata.Encoding.Value('WAV')
    encoder = functools.partial(util.encode_wav,
                                samples_per_second=sample_rate)
  else:
    raise ValueError('Unknown encoding: %r' % encoding)

  limited_audio = audio[:max_outputs]
  if labels is None:
    limited_labels = [b''] * len(limited_audio)
  else:
    limited_labels = [tf.compat.as_bytes(label)
                      for label in labels[:max_outputs]]

  encoded_audio = [encoder(a) for a in limited_audio]
  content = np.array([encoded_audio, limited_labels]).transpose()
  tensor = tf.make_tensor_proto(content, dtype=tf.string)

  if display_name is None:
    display_name = name
  summary_metadata = metadata.create_summary_metadata(
      display_name=display_name,
      description=description,
      encoding=encoding)

  summary = tf.Summary()
  summary.value.add(tag='%s/audio_summary' % name,
                    metadata=summary_metadata,
                    tensor=tensor)
  return summary
 def _mock_tensors(self, run, tag):
   result_dict = {
       'session_1': {
           'current_temp': [
               TensorEvent(
                   wall_time=1, step=1,
                   tensor_proto=tf.make_tensor_proto(10.0))
           ],
           'delta_temp': [
               TensorEvent(
                   wall_time=1, step=1,
                   tensor_proto=tf.make_tensor_proto(20.0)),
               TensorEvent(
                   wall_time=10, step=2,
                   tensor_proto=tf.make_tensor_proto(15.0))
           ],
           'optional_metric' : [
               TensorEvent(
                   wall_time=1, step=1,
                   tensor_proto=tf.make_tensor_proto(20.0)),
               TensorEvent(
                   wall_time=2, step=20,
                   tensor_proto=tf.make_tensor_proto(33.0))
           ]
       },
       'session_2': {
           'current_temp': [
               TensorEvent(
                   wall_time=1, step=1,
                   tensor_proto=tf.make_tensor_proto(100.0)),
           ],
           'delta_temp': [
               TensorEvent(
                   wall_time=1, step=1,
                   tensor_proto=tf.make_tensor_proto(200.0)),
               TensorEvent(
                   wall_time=10, step=2,
                   tensor_proto=tf.make_tensor_proto(150.0))
           ]
       },
       'session_3': {
           'current_temp': [
               TensorEvent(
                   wall_time=1, step=1,
                   tensor_proto=tf.make_tensor_proto(1.0)),
           ],
           'delta_temp': [
               TensorEvent(
                   wall_time=1, step=1,
                   tensor_proto=tf.make_tensor_proto(2.0)),
               TensorEvent(
                   wall_time=10, step=2,
                   tensor_proto=tf.make_tensor_proto(1.5))
           ]
       },
       'session_4': {
           'current_temp': [
               TensorEvent(
                   wall_time=1, step=1,
                   tensor_proto=tf.make_tensor_proto(101.0)),
           ],
           'delta_temp': [
               TensorEvent(
                   wall_time=1, step=1,
                   tensor_proto=tf.make_tensor_proto(201.0)),
               TensorEvent(
                   wall_time=10, step=2,
                   tensor_proto=tf.make_tensor_proto(-151.0))
           ]
       },
   }
   return result_dict[run][tag]