Python tensorflow.make_ndarray() Examples

The following are 30 code examples of tensorflow.make_ndarray(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow , or try the search function .
Example #1
Source File: serving_utils.py    From BERT with Apache License 2.0 6 votes vote down vote up
def make_grpc_request_fn(servable_name, server, timeout_secs):
  """Wraps function to make grpc requests with runtime args."""
  stub = _create_stub(server)

  def _make_grpc_request(examples):
    """Builds and sends request to TensorFlow model server."""
    request = predict_pb2.PredictRequest()
    request.model_spec.name = servable_name
    request.inputs["input"].CopyFrom(
        tf.make_tensor_proto(
            [ex.SerializeToString() for ex in examples], shape=[len(examples)]))
    response = stub.Predict(request, timeout_secs)
    outputs = tf.make_ndarray(response.outputs["outputs"])
    scores = tf.make_ndarray(response.outputs["scores"])
    assert len(outputs) == len(scores)
    return [{  # pylint: disable=g-complex-comprehension
        "outputs": output,
        "scores": score
    } for output, score in zip(outputs, scores)]

  return _make_grpc_request 
Example #2
Source File: _tf_utils.py    From keras-onnx with MIT License 6 votes vote down vote up
def cal_tensor_value(tensor):  # type: (tensorflow.Tensor)->Union[np.ndarray, None]
    if _count_input_nodes(tensor) < 0:
        return None

    node = tensor.op
    if node.type in ["Const", "ConstV2"]:
        make_ndarray = tensorflow.make_ndarray
        np_arr = make_ndarray(node.get_attr("value"))
        return np_arr
    else:
        try:
            cls_sess = tensorflow.Session if hasattr(tensorflow, 'Session') else tensorflow.compat.v1.Session
            with cls_sess(graph=node.graph) as sess:
                np_arr = sess.run(tensor)
                return np_arr
        except (ValueError, tensorflow.errors.InvalidArgumentError, tensorflow.errors.OpError):
            return None 
Example #3
Source File: histograms_plugin.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 6 votes vote down vote up
def histograms_impl(self, tag, run, downsample_to=50):
    """Result of the form `(body, mime_type)`, or `ValueError`.

    At most `downsample_to` events will be returned. If this value is
    `None`, then no downsampling will be performed.
    """
    try:
      tensor_events = self._multiplexer.Tensors(run, tag)
    except KeyError:
      raise ValueError('No histogram tag %r for run %r' % (tag, run))
    events = [[ev.wall_time, ev.step, tf.make_ndarray(ev.tensor_proto).tolist()]
              for ev in tensor_events]
    if downsample_to is not None and len(events) > downsample_to:
      indices = sorted(random.Random(0).sample(list(range(len(events))),
                                               downsample_to))
      events = [events[i] for i in indices]
    return (events, 'application/json') 
Example #4
Source File: test_predict_utils.py    From model_server with Apache License 2.0 6 votes vote down vote up
def test_prepare_output_as_list(serialization_function, outputs_names, shapes,
                                types):
    outputs = {}
    x = 0
    for key, value in outputs_names.items():
        outputs[value] = np.ones(shape=shapes[x], dtype=types[x])
        x += 1

    output = SERIALIZATION_FUNCTIONS[serialization_function](
        inference_output=outputs, model_available_outputs=outputs_names)

    x = 0
    for key, value in outputs_names.items():
        temp_output = make_ndarray(output.outputs[key])
        assert temp_output.shape == shapes[x]
        assert temp_output.dtype == types[x]
        x += 1 
Example #5
Source File: test_predict_grpc.py    From model_server with Apache License 2.0 6 votes vote down vote up
def test_predict_successful_version(mocker, get_grpc_service_for_predict):
    results_mock = mocker.patch(
        'ie_serving.server.request.Request.wait_for_result')
    expected_response = np.ones(shape=(2, 2))
    results_mock.return_value = ({'output': expected_response}, None)
    requested_version = 1
    request = get_fake_request(model_name='test', data_shape=(1, 1, 1),
                               input_blob='input', version=requested_version)
    grpc_server = get_grpc_service_for_predict
    rpc = grpc_server.invoke_unary_unary(
        PREDICT_SERVICE.methods_by_name['Predict'],
        (),
        request, None)
    rpc.initial_metadata()
    response, trailing_metadata, code, details = rpc.termination()

    encoded_response = make_ndarray(response.outputs['output'])
    assert requested_version == response.model_spec.version.value
    assert grpc.StatusCode.OK == code
    assert expected_response.shape == encoded_response.shape 
Example #6
Source File: test_predict_grpc.py    From model_server with Apache License 2.0 6 votes vote down vote up
def test_predict_successful(mocker, get_grpc_service_for_predict,
                            get_fake_model):
    results_mock = mocker.patch(
        'ie_serving.server.request.Request.wait_for_result')
    expected_response = np.ones(shape=(2, 2))
    results_mock.return_value = ({'output': expected_response}, 0)

    request = get_fake_request(model_name='test',
                               data_shape=(1, 1, 1), input_blob='input')
    grpc_server = get_grpc_service_for_predict
    rpc = grpc_server.invoke_unary_unary(
            PREDICT_SERVICE.methods_by_name['Predict'],
            (),
            request, None)
    rpc.initial_metadata()
    response, trailing_metadata, code, details = rpc.termination()

    encoded_response = make_ndarray(response.outputs['output'])
    assert get_fake_model.default_version == response.model_spec.version.value
    assert grpc.StatusCode.OK == code
    assert expected_response.shape == encoded_response.shape 
Example #7
Source File: serving_utils.py    From training_results_v0.5 with Apache License 2.0 6 votes vote down vote up
def make_grpc_request_fn(servable_name, server, timeout_secs):
  """Wraps function to make grpc requests with runtime args."""
  stub = _create_stub(server)

  def _make_grpc_request(examples):
    """Builds and sends request to TensorFlow model server."""
    request = predict_pb2.PredictRequest()
    request.model_spec.name = servable_name
    request.inputs["input"].CopyFrom(
        tf.contrib.util.make_tensor_proto(
            [ex.SerializeToString() for ex in examples], shape=[len(examples)]))
    response = stub.Predict(request, timeout_secs)
    outputs = tf.make_ndarray(response.outputs["outputs"])
    scores = tf.make_ndarray(response.outputs["scores"])
    assert len(outputs) == len(scores)
    return [{
        "outputs": outputs[i],
        "scores": scores[i]
    } for i in range(len(outputs))]

  return _make_grpc_request 
Example #8
Source File: serving_utils.py    From fine-lm with MIT License 6 votes vote down vote up
def make_grpc_request_fn(servable_name, server, timeout_secs):
  """Wraps function to make grpc requests with runtime args."""
  stub = _create_stub(server)

  def _make_grpc_request(examples):
    """Builds and sends request to TensorFlow model server."""
    request = predict_pb2.PredictRequest()
    request.model_spec.name = servable_name
    request.inputs["input"].CopyFrom(
        tf.contrib.util.make_tensor_proto(
            [ex.SerializeToString() for ex in examples], shape=[len(examples)]))
    response = stub.Predict(request, timeout_secs)
    outputs = tf.make_ndarray(response.outputs["outputs"])
    scores = tf.make_ndarray(response.outputs["scores"])
    assert len(outputs) == len(scores)
    return [{
        "outputs": outputs[i],
        "scores": scores[i]
    } for i in range(len(outputs))]

  return _make_grpc_request 
Example #9
Source File: ende_client.py    From OpenNMT-tf with MIT License 6 votes vote down vote up
def extract_prediction(result):
  """Parses a translation result.

  Args:
    result: A `PredictResponse` proto.

  Returns:
    A generator over the hypotheses.
  """
  batch_lengths = tf.make_ndarray(result.outputs["length"])
  batch_predictions = tf.make_ndarray(result.outputs["tokens"])
  for hypotheses, lengths in zip(batch_predictions, batch_lengths):
    # Only consider the first hypothesis (the best one).
    best_hypothesis = hypotheses[0].tolist()
    best_length = lengths[0]
    if best_hypothesis[best_length - 1] == b"</s>":
      best_length -= 1
    yield best_hypothesis[:best_length] 
Example #10
Source File: audio_plugin.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def _audio_response_for_run(self, tensor_events, run, tag, sample):
    """Builds a JSON-serializable object with information about audio.

    Args:
      tensor_events: A list of image event_accumulator.TensorEvent objects.
      run: The name of the run.
      tag: The name of the tag the audio entries all belong to.
      sample: The zero-indexed sample of the audio sample for which to
      retrieve information. For instance, setting `sample` to `2` will
        fetch information about only the third audio clip of each batch,
        and steps with fewer than three audio clips will be omitted from
        the results.

    Returns:
      A list of dictionaries containing the wall time, step, URL, width, and
      height for each audio entry.
    """
    response = []
    index = 0
    filtered_events = self._filter_by_sample(tensor_events, sample)
    content_type = self._get_mime_type(run, tag)
    for (index, tensor_event) in enumerate(filtered_events):
      data = tf.make_ndarray(tensor_event.tensor_proto)
      label = data[sample, 1]
      response.append({
          'wall_time': tensor_event.wall_time,
          'step': tensor_event.step,
          'label': plugin_util.markdown_to_safe_html(label),
          'contentType': content_type,
          'query': self._query_for_individual_audio(run, tag, sample, index)
      })
    return response 
Example #11
Source File: audio_plugin.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def _serve_individual_audio(self, request):
    """Serve encoded audio data."""
    tag = request.args.get('tag')
    run = request.args.get('run')
    index = int(request.args.get('index'))
    sample = int(request.args.get('sample', 0))
    events = self._filter_by_sample(self._multiplexer.Tensors(run, tag), sample)
    data = tf.make_ndarray(events[index].tensor_proto)[sample, 0]
    mime_type = self._get_mime_type(run, tag)
    return http_util.Respond(request, data, mime_type) 
Example #12
Source File: pr_curves_plugin.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def _process_tensor_event(self, event, thresholds):
    """Converts a TensorEvent into a dict that encapsulates information on it.

    Args:
      event: The TensorEvent to convert.
      thresholds: An array of floats that ranges from 0 to 1 (in that
        direction and inclusive of 0 and 1).

    Returns:
      A JSON-able dictionary of PR curve data for 1 step.
    """
    data_array = tf.make_ndarray(event.tensor_proto)

    # Trim entries for which TP + FP = 0 (precision is undefined) at the tail of
    # the data.
    true_positives = [int(v) for v in data_array[metadata.TRUE_POSITIVES_INDEX]]
    false_positives = [
        int(v) for v in data_array[metadata.FALSE_POSITIVES_INDEX]]
    tp_index = metadata.TRUE_POSITIVES_INDEX
    fp_index = metadata.FALSE_POSITIVES_INDEX
    positives = data_array[[tp_index, fp_index], :].astype(int).sum(axis=0)
    end_index_inclusive = len(positives) - 1
    while end_index_inclusive > 0 and positives[end_index_inclusive] == 0:
      end_index_inclusive -= 1
    end_index = end_index_inclusive + 1

    return {
        'wall_time': event.wall_time,
        'step': event.step,
        'precision': data_array[metadata.PRECISION_INDEX, :end_index].tolist(),
        'recall': data_array[metadata.RECALL_INDEX, :end_index].tolist(),
        'true_positives': true_positives[:end_index],
        'false_positives': false_positives[:end_index],
        'true_negatives':
            [int(v) for v in
             data_array[metadata.TRUE_NEGATIVES_INDEX][:end_index]],
        'false_negatives':
            [int(v) for v in
             data_array[metadata.FALSE_NEGATIVES_INDEX][:end_index]],
        'thresholds': thresholds[:end_index],
    } 
Example #13
Source File: text_plugin.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def process_string_tensor_event(event):
  """Convert a TensorEvent into a JSON-compatible response."""
  string_arr = tf.make_ndarray(event.tensor_proto)
  html = text_array_to_html(string_arr)
  return {
      'wall_time': event.wall_time,
      'step': event.step,
      'text': html,
  } 
Example #14
Source File: debugger_plugin.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def _process_health_pill_value(self,
                                 wall_time,
                                 step,
                                 device_name,
                                 output_slot,
                                 node_name,
                                 tensor_proto,
                                 node_name_set=None):
    """Creates a HealthPillEvent containing various properties of a health pill.

    Args:
      wall_time: The wall time in seconds.
      step: The session run step of the event.
      device_name: The name of the node's device.
      output_slot: The numeric output slot.
      node_name: The name of the node (without the output slot).
      tensor_proto: A tensor proto of data.
      node_name_set: An optional set of node names that are relevant. If not
        provided, no filtering by relevance occurs.

    Returns:
      An event_accumulator.HealthPillEvent. Or None if one could not be created.
    """
    if node_name_set and node_name not in node_name_set:
      # This event is not relevant.
      return None

    # Since we seek health pills for a specific step, this function
    # returns 1 health pill per node per step. The wall time is the
    # seconds since the epoch.
    elements = list(tf.make_ndarray(tensor_proto))
    return HealthPillEvent(
        wall_time=wall_time,
        step=step,
        device_name=device_name,
        output_slot=output_slot,
        node_name=node_name,
        dtype=repr(tf.as_dtype(elements[12])),
        shape=elements[14:],
        value=elements) 
Example #15
Source File: parse_layer_parameters.py    From receptive_field with Apache License 2.0 5 votes vote down vote up
def _padding_size_pad_layer(node, name_to_node):
  """Computes padding size given a TF padding node.

  Args:
    node: Tensorflow node (NodeDef proto).
    name_to_node: Dict keyed by node name, each entry containing the node's
      NodeDef.

  Returns:
    total_padding_x: Total padding size for horizontal direction (integer).
    padding_x: Padding size for horizontal direction, left side (integer).
    total_padding_y: Total padding size for vertical direction (integer).
    padding_y: Padding size for vertical direction, top side (integer).

  Raises:
    ValueError: If padding layer is invalid.
  """
  paddings_layer_name = node.input[1]
  if not paddings_layer_name.endswith("/paddings"):
    raise ValueError("Padding layer name does not end with '/paddings'")
  paddings_node = name_to_node[paddings_layer_name]
  if paddings_node.op != "Const":
    raise ValueError("Padding op is not Const")
  value = paddings_node.attr["value"]
  t = tf.make_ndarray(value.tensor)
  padding_y = t[1][0]
  padding_x = t[2][0]
  total_padding_y = padding_y + t[1][1]
  total_padding_x = padding_x + t[2][1]
  if (t[0][0] != 0) or (t[0][1] != 0):
    raise ValueError("padding is not zero for first tensor dim")
  if (t[3][0] != 0) or (t[3][1] != 0):
    raise ValueError("padding is not zero for last tensor dim")
  return total_padding_x, padding_x, total_padding_y, padding_y 
Example #16
Source File: scalars_plugin.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def scalars_impl(self, tag, run, output_format):
    """Result of the form `(body, mime_type)`."""
    tensor_events = self._multiplexer.Tensors(run, tag)
    values = [[tensor_event.wall_time,
               tensor_event.step,
               tf.make_ndarray(tensor_event.tensor_proto).item()]
              for tensor_event in tensor_events]
    if output_format == OutputFormat.CSV:
      string_io = StringIO()
      writer = csv.writer(string_io)
      writer.writerow(['Wall time', 'Step', 'Value'])
      writer.writerows(values)
      return (string_io.getvalue(), 'text/csv')
    else:
      return (values, 'application/json') 
Example #17
Source File: parse_layer_parameters.py    From receptive_field with Apache License 2.0 5 votes vote down vote up
def _pool_kernel_size(node, name_to_node):
  """Computes kernel size given a TF pooling node.

  Args:
    node: Tensorflow node (NodeDef proto).
    name_to_node: For MaxPoolV2, mapping from node name to NodeDef.

  Returns:
    kernel_size_x: Kernel size for horizontal direction (integer).
    kernel_size_y: Kernel size for vertical direction (integer).

  Raises:
    ValueError: If pooling is invalid.
  """
  if node.op == "MaxPoolV2":
    ksize_input_name = node.input[1]
    if not ksize_input_name.endswith("/ksize"):
      raise ValueError("Kernel size name does not end with '/ksize'")
    ksize_node = name_to_node[ksize_input_name]
    value = ksize_node.attr["value"]
    t = tf.make_ndarray(value.tensor)
    kernel_size_y = t[1]
    kernel_size_x = t[2]
    if t[0] != 1:
      raise ValueError("pool ksize for first dim is not 1")
    if t[3] != 1:
      raise ValueError("pool ksize for last dim is not 1")
  else:
    ksize = node.attr["ksize"]
    kernel_size_y = ksize.list.i[1]
    kernel_size_x = ksize.list.i[2]
    if ksize.list.i[0] != 1:
      raise ValueError("pool ksize for first dim is not 1")
    if ksize.list.i[3] != 1:
      raise ValueError("pool ksize for last dim is not 1")
  return kernel_size_x, kernel_size_y 
Example #18
Source File: parse_layer_parameters.py    From receptive_field with Apache License 2.0 5 votes vote down vote up
def _stride_size(node, name_to_node):
  """Computes stride size given a TF node.

  Args:
    node: Tensorflow node (NodeDef proto).
    name_to_node: For MaxPoolV2, mapping from variable name Tensorflow node.

  Returns:
    stride_x: Stride size for horizontal direction (integer).
    stride_y: Stride size for vertical direction (integer).

  Raises:
    ValueError: If stride input cannot be found in `name_to_node`.
  """
  if node.op == "MaxPoolV2":
    strides_input_name = node.input[2]
    if not strides_input_name.endswith("/strides"):
      raise ValueError("Strides name does not end with '/strides'")
    strides_node = name_to_node[strides_input_name]
    value = strides_node.attr["value"]
    t = tf.make_ndarray(value.tensor)
    stride_y = t[1]
    stride_x = t[2]
  else:
    strides_attr = node.attr["strides"]
    logging.vlog(4, "strides_attr = %s", strides_attr)
    stride_y = strides_attr.list.i[1]
    stride_x = strides_attr.list.i[2]
  return stride_x, stride_y 
Example #19
Source File: data_loading.py    From rl-reliability-metrics with Apache License 2.0 5 votes vote down vote up
def get_summary_value(summary, load_tensors):
  if load_tensors:
    proto = summary.tensor_proto
    value = tf.make_ndarray(proto)
    return value.item()
  else:
    return summary.value 
Example #20
Source File: pipeline_invoke_tfserving.py    From models with Apache License 2.0 5 votes vote down vote up
def _transform_response(response):
    # Convert from tf.tensor, np.array, etc. to bytes

    # TODO:  Optimize this to avoid tf.make_ndarray similar to _transform_request() above
    class_list = tf.make_ndarray(response['classes']).tolist()[0]
    class_list_str = [clazz.decode('utf-8') for clazz in class_list]
    score_list = tf.make_ndarray(response['scores']).tolist()[0]

    return {"classes": class_list_str,
            "scores": score_list} 
Example #21
Source File: converter.py    From utensor_cgen with Apache License 2.0 5 votes vote down vote up
def get_generic_value(cls, value):
    """quantized numpy array is mapped to np.uint8 typed

    FIXME: I'm not sure if it's a good idea
    """
    np_array = make_ndarray(value)
    dtype = np_array.dtype
    if dtype.fields is None:
      pass
    elif dtype[0] in [np.uint8, np.int8]:
      np_array = np_array.astype(dtype[0])
    else:
      raise ValueError('Unsupported numpy dtype: %s' % dtype)
    return cls.__utensor_generic_type__(np_array=np_array,
                                        dtype=dtype) 
Example #22
Source File: greeter_plugin.py    From tensorboard-plugin-example with Apache License 2.0 5 votes vote down vote up
def _process_string_tensor_event(self, event):
    """Convert a TensorEvent into a JSON-compatible response."""
    string_arr = tf.make_ndarray(event.tensor_proto)
    text = string_arr.astype(np.dtype(str)).tostring()
    return {
        'wall_time': event.wall_time,
        'step': event.step,
        'text': text,
    } 
Example #23
Source File: tf_utils.py    From video_prediction with MIT License 5 votes vote down vote up
def convert_tensor_to_gif_summary(summ):
    if isinstance(summ, bytes):
        summary_proto = tf.Summary()
        summary_proto.ParseFromString(summ)
        summ = summary_proto

    summary = tf.Summary()
    for value in summ.value:
        tag = value.tag
        try:
            images_arr = tf.make_ndarray(value.tensor)
        except TypeError:
            summary.value.add(tag=tag, image=value.image)
            continue

        if len(images_arr.shape) == 5:
            images_arr = np.concatenate(list(images_arr), axis=-2)
        if len(images_arr.shape) != 4:
            raise ValueError('Tensors must be 4-D or 5-D for gif summary.')
        channels = images_arr.shape[-1]
        if channels < 1 or channels > 4:
            raise ValueError('Tensors must have 1, 2, 3, or 4 color channels for gif summary.')

        encoded_image_string = ffmpeg_gif.encode_gif(images_arr, fps=4)

        image = tf.Summary.Image()
        image.height = images_arr.shape[-3]
        image.width = images_arr.shape[-2]
        image.colorspace = channels  # 1: grayscale, 2: grayscale + alpha, 3: RGB, 4: RGBA
        image.encoded_image_string = encoded_image_string
        summary.value.add(tag=tag, image=image)
    return summary 
Example #24
Source File: tf2_summary.py    From RLs with Apache License 2.0 5 votes vote down vote up
def tf2summary2dict(path, tags=[]):
    serialized_examples = tf.data.TFRecordDataset(path)
    data = {}
    for serialized_example in serialized_examples:
        event = event_pb2.Event.FromString(serialized_example.numpy())
        for value in event.summary.value:
            if value.tag in tags or tags == []:
                t = tf.make_ndarray(value.tensor)
                t = float(t)
                try:
                    data[f'{value.tag}'].append([t, event.step])
                except:
                    data[f'{value.tag}'] = [[t, event.step]]
                    pass
    return data 
Example #25
Source File: extract_tensorboard.py    From deeprl_signal_control with MIT License 5 votes vote down vote up
def extract_scalar(multiplexer, run_name, tag):
    tensor_events = multiplexer.Tensors(run_name, tag)
    data = {'wall_time': [], 'step': [], 'value': []}
    for event in tensor_events:
        data['wall_time'].append(event.wall_time)
        data['step'].append(event.step)
        data['value'].append(tf.make_ndarray(event.tensor_proto).item())
    return pd.DataFrame(data) 
Example #26
Source File: fetch_events.py    From planet with Apache License 2.0 5 votes vote down vote up
def extract_values(reader, tag):
  events = reader.Tensors('run', tag)
  steps = [event.step for event in events]
  times = [event.wall_time for event in events]
  values = [tf.make_ndarray(event.tensor_proto) for event in events]
  return steps, times, values 
Example #27
Source File: grpc.py    From model_server with Apache License 2.0 5 votes vote down vote up
def infer(img, 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 ", img.shape)
    request.inputs[input_tensor].CopyFrom(
        make_tensor_proto(img, shape=list(img.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 
Example #28
Source File: executor_service_utils.py    From federated with Apache License 2.0 5 votes vote down vote up
def deserialize_tensor_value(value_proto):
  """Deserializes a tensor value from `executor_pb2.Value`.

  Args:
    value_proto: An instance of `executor_pb2.Value`.

  Returns:
    A tuple `(value, type_spec)`, where `value` is a Numpy array that represents
    the deserialized value, and `type_spec` is an instance of `tff.TensorType`
    that represents its type.

  Raises:
    TypeError: If the arguments are of the wrong types.
    ValueError: If the value is malformed.
  """
  py_typecheck.check_type(value_proto, executor_pb2.Value)
  which_value = value_proto.WhichOneof('value')
  if which_value != 'tensor':
    raise ValueError('Not a tensor value: {}'.format(which_value))

  # TODO(b/134543154): Find some way of creating the `TensorProto` using a
  # proper public interface rather than creating a dummy value that we will
  # overwrite right away.
  tensor_proto = tf.make_tensor_proto(values=0)
  if not value_proto.tensor.Unpack(tensor_proto):
    raise ValueError('Unable to unpack the received tensor value.')

  tensor_value = tf.make_ndarray(tensor_proto)
  value_type = computation_types.TensorType(
      dtype=tf.dtypes.as_dtype(tensor_proto.dtype),
      shape=tf.TensorShape(tensor_proto.tensor_shape))

  return tensor_value, value_type 
Example #29
Source File: client.py    From hncynic with MIT License 4 votes vote down vote up
def __call__(self, title, n=8, timeout=50.0):
    if self.preprocessor:
      # FIXME: Tried to reuse the process, but something seems to be buffering
      preprocessor = subprocess.Popen([self.preprocessor],
                                      stdout=subprocess.PIPE,
                                      stdin=subprocess.PIPE,
                                      stderr=subprocess.DEVNULL)

      title_pp = preprocessor.communicate((title.strip() + '\n').encode())[0].decode('utf-8')
    else:
      title_pp = title

    title_bpe = self.bpe.segment_tokens(title_pp.strip().lower().split(' '))
    #print(title_bpe)

    request = predict_pb2.PredictRequest()
    request.model_spec.name = self.model_name
    request.inputs['tokens'].CopyFrom(
      tf.make_tensor_proto([title_bpe] * n, shape=(n, len(title_bpe))))
    request.inputs['length'].CopyFrom(
      tf.make_tensor_proto([len(title_bpe)] * n, shape=(n,)))
    future = self.stub.Predict.future(request, timeout)

    result = future.result()

    batch_predictions = tf.make_ndarray(result.outputs["tokens"])
    batch_lengths = tf.make_ndarray(result.outputs["length"])
    batch_scores = tf.make_ndarray(result.outputs["log_probs"])

    hyps = []
    for (predictions, lengths, scores) in zip(batch_predictions, batch_lengths, batch_scores):
      # ignore </s>
      prediction = predictions[0][:lengths[0]-1]

      comment = ' '.join([token.decode('utf-8') for token in prediction])
      comment = comment.replace('@@ ', '')
      #comment = comment.replace('<NL>', '\n')

      # FIXME: Tried to reuse the process, but something seems to be buffering
      if self.postprocessor:
        postprocessor = subprocess.Popen([self.postprocessor],
                                         stdout=subprocess.PIPE,
                                         stdin=subprocess.PIPE,
                                         stderr=subprocess.DEVNULL)
        prediction_ready = postprocessor.communicate((comment + '\n').encode())[0].decode('utf-8')
      else:
        prediction_ready = comment

      hyps.append((prediction_ready.strip(), float(scores[0])))

    #hyps.sort(key=lambda hyp: hyp[1] / len(hyp), reverse=True)

    return hyps 
Example #30
Source File: tensorboard_output.py    From gym-sawyer with MIT License 4 votes vote down vote up
def _dump_tensors(self):
        if not self._has_recorded_tensor:
            return

        layout_categories = []

        for scope in self._scope_tensor:
            chart = []
            for name in self._scope_tensor[scope]:
                chart.append(
                    layout_pb2.Chart(
                        title=name,
                        multiline=layout_pb2.MultilineChartContent(
                            tag=[r'name(?!.*margin.*)'.replace('name', name)
                                 ])))
            category = layout_pb2.Category(title=scope, chart=chart)
            layout_categories.append(category)

        if layout_categories:
            layout_proto_to_write = layout_pb2.Layout(
                category=layout_categories)

            try:
                # Load former layout_proto from self._layout_writer_dir.
                multiplexer = event_multiplexer.EventMultiplexer()
                multiplexer.AddRunsFromDirectory(self._layout_writer_dir)
                multiplexer.Reload()
                tensor_events = multiplexer.Tensors(
                    '.', metadata.CONFIG_SUMMARY_TAG)
                shutil.rmtree(self._layout_writer_dir)

                # Parse layout proto from disk.
                string_array = tf.make_ndarray(tensor_events[0].tensor_proto)
                content = np.asscalar(string_array)
                layout_proto_from_disk = layout_pb2.Layout()
                layout_proto_from_disk.ParseFromString(
                    tf.compat.as_bytes(content))

                # Merge two layout proto.
                merged_layout_json = merge(
                    json_format.MessageToJson(layout_proto_from_disk),
                    json_format.MessageToJson(layout_proto_to_write))
                merged_layout_proto = layout_pb2.Layout()
                json_format.Parse(str(merged_layout_json), merged_layout_proto)

                self._layout_writer = tf.summary.FileWriter(
                    self._layout_writer_dir)
                layout_summary = summary_lib.custom_scalar_pb(
                    merged_layout_proto)
                self._layout_writer.add_summary(layout_summary)
                self._layout_writer.close()
            except KeyError:
                # Write the current layout proto into disk
                # when there is no layout.
                self._layout_writer = tf.summary.FileWriter(
                    self._layout_writer_dir)
                layout_summary = summary_lib.custom_scalar_pb(
                    layout_proto_to_write)
                self._layout_writer.add_summary(layout_summary)
                self._layout_writer.close()