示例#1
0
def _run_function_for_calibration_graph_mode(
        sess: session.Session, signature_def: meta_graph_pb2.SignatureDef,
        representative_dataset: repr_dataset.RepresentativeDataset) -> None:
    """Runs the representative dataset through a function for calibration.

  NOTE: This is intended to be run in graph mode (TF1).

  The function is identified by the SignatureDef.

  Args:
    sess: The Session object to run the function in.
    signature_def: A SignatureDef that identifies a function by specifying the
      inputs and outputs.
    representative_dataset: The representative dataset to run through the
      function.
  """
    output_tensor_names = [
        output_tensor_info.name
        for output_tensor_info in signature_def.outputs.values()
    ]

    sample_validator = _create_sample_validator(
        expected_input_keys=signature_def.inputs.keys())
    for sample in map(sample_validator, representative_dataset):
        # Create a mapping from input tensor name to the input tensor value.
        # ex) "Placeholder:0" -> [0, 1, 2]
        feed_dict = _create_feed_dict_from_input_data(sample, signature_def)
        sess.run(output_tensor_names, feed_dict=feed_dict)
def create_resource_split_graph(
        sess: Session,
        input_value: Any,
        input_dtype: Any,
        num_outputs: int,
        num_splits: List[int],
        paddings: Optional[List[int]] = None) -> List[Tensor]:
    variable = resource_variable_ops.ResourceVariable(
        initial_value=input_value, dtype=input_dtype)
    sess.run(variables.variables_initializer([variable]))
    return gen_tpu_ops.read_variable_xla_split_nd(variable.handle,
                                                  input_dtype,
                                                  num_outputs,
                                                  num_splits,
                                                  paddings=paddings)
示例#3
0
def copy_variable_to_graph(org_instance, to_graph, scope=''):
  """Given a `Variable` instance from one `Graph`, initializes and returns
  a copy of it from another `Graph`, under the specified scope
  (default `""`).

  Args:
    org_instance: A `Variable` from some `Graph`.
    to_graph: The `Graph` to copy the `Variable` to.
    scope: A scope for the new `Variable` (default `""`).

  Returns:
    The copied `Variable` from `to_graph`.

  Raises:
    TypeError: If `org_instance` is not a `Variable`.
  """

  if not isinstance(org_instance, Variable):
    raise TypeError(str(org_instance) + ' is not a Variable')

  #The name of the new variable
  if scope != '':
    new_name = (scope + '/' + org_instance.name[:org_instance.name.index(':')])
  else:
    new_name = org_instance.name[:org_instance.name.index(':')]

  #Get the collections that the new instance needs to be added to.
  #The new collections will also be a part of the given scope,
  #except the special ones required for variable initialization and
  #training.
  collections = []
  for name, collection in org_instance.graph._collections.items():
    if org_instance in collection:
      if (name == ops.GraphKeys.GLOBAL_VARIABLES or
          name == ops.GraphKeys.TRAINABLE_VARIABLES or scope == ''):
        collections.append(name)
      else:
        collections.append(scope + '/' + name)

  #See if it's trainable.
  trainable = (
      org_instance in org_instance.graph.get_collection(
          ops.GraphKeys.TRAINABLE_VARIABLES))
  #Get the initial value
  with org_instance.graph.as_default():
    temp_session = Session()
    init_value = temp_session.run(org_instance.initialized_value())

  #Initialize the new variable
  with to_graph.as_default():
    new_var = Variable(
        init_value,
        trainable,
        name=new_name,
        collections=collections,
        validate_shape=False)

  return new_var
def copy_variable_to_graph(org_instance, to_graph, scope=""):
    """Given a `Variable` instance from one `Graph`, initializes and returns
    a copy of it from another `Graph`, under the specified scope
    (default `""`).

    Args:
    org_instance: A `Variable` from some `Graph`.
    to_graph: The `Graph` to copy the `Variable` to.
    scope: A scope for the new `Variable` (default `""`).

    Returns:
        The copied `Variable` from `to_graph`.

    Raises:
        TypeError: If `org_instance` is not a `Variable`.
    """

    if not isinstance(org_instance, Variable):
        raise TypeError(str(org_instance) + " is not a Variable")

    #The name of the new variable
    if scope != "":
        new_name = (scope + '/' +
                    org_instance.name[:org_instance.name.index(':')])
    else:
        new_name = org_instance.name[:org_instance.name.index(':')]

    #Get the collections that the new instance needs to be added to.
    #The new collections will also be a part of the given scope,
    #except the special ones required for variable initialization and
    #training.
    collections = []
    for name, collection in org_instance.graph._collections.items():
        if org_instance in collection:
            if (name == ops.GraphKeys.VARIABLES
                    or name == ops.GraphKeys.TRAINABLE_VARIABLES
                    or scope == ''):
                collections.append(name)
            else:
                collections.append(scope + '/' + name)

    #See if its trainable.
    trainable = (org_instance in org_instance.graph.get_collection(
        ops.GraphKeys.TRAINABLE_VARIABLES))
    #Get the initial value
    with org_instance.graph.as_default():
        temp_session = Session()
        init_value = temp_session.run(org_instance.initialized_value())

    #Initialize the new variable
    with to_graph.as_default():
        new_var = Variable(init_value,
                           trainable,
                           name=new_name,
                           collections=collections,
                           validate_shape=False)

    return new_var
def create_resource_roundtrip_graph(
        sess: Session,
        value: Any,
        dtype: Any,
        num_partitions: List[int],
        paddings: Optional[List[int]] = None) -> Tensor:
    variable = resource_variable_ops.ResourceVariable(initial_value=value,
                                                      dtype=dtype)
    sess.run(variables.variables_initializer([variable]))
    split = gen_tpu_ops.read_variable_xla_split_nd(variable.handle,
                                                   dtype,
                                                   np.prod(num_partitions),
                                                   num_partitions,
                                                   paddings=paddings)
    concat = gen_tpu_ops.assign_variable_xla_concat_nd(variable.handle, split,
                                                       num_partitions,
                                                       paddings)
    with control_dependencies([concat]):
        return math_ops.equal(variable.read_value(),
                              constant_op.constant(value, dtype=dtype))
def create_resource_concat_graph(
        sess: Session,
        input_values: List[Any],
        input_dtype: Any,
        num_concats: List[int],
        paddings: Optional[List[int]] = None,
        output_shape: Optional[List[int]] = None) -> Tensor:
    variable_shape = [] if output_shape is None else output_shape
    variable = resource_variable_ops.ResourceVariable(initial_value=np.zeros(
        variable_shape, dtype=input_dtype),
                                                      dtype=input_dtype)
    sess.run(variables.variables_initializer([variable]))
    const_input_ops = [
        constant_op.constant(i, dtype=input_dtype) for i in input_values
    ]
    concat = gen_tpu_ops.assign_variable_xla_concat_nd(variable.handle,
                                                       const_input_ops,
                                                       num_concats, paddings)
    with control_dependencies([concat]):
        return variable.read_value()
class DetectionApiNmsPerformer(NmsPerformerBase):
    def __init__(self):
        self.sess = None
        self.config = ConfigProto()
        self.config.gpu_options.allow_growth = True

    def create_session(self):
        """
        create tensorflow session
        """

        self.sess = Session(config=self.config)

    def nms_single_class(self, boxes, scores, input_metadata):
        """ see NmsPerformerBase """
        # define nms function params
        # This is not supposed to be in the request! we insert a default value for safety.
        score_thresh = SCORE_THRESH
        iou_thresh = input_metadata.get('nmsThresh', 0.6)
        max_output_size = boxes.shape[0]

        # expand to adapt to expected input dimensions
        boxes = boxes[:, np.newaxis, :]
        scores = scores[:, np.newaxis]

        # convert to tensors
        boxes = tf.constant(boxes, tf.float32)
        scores = tf.constant(scores, tf.float32)

        # create nms graph node
        nms_node = multiclass_non_max_suppression(boxes, scores, score_thresh,
                                                  iou_thresh, max_output_size)

        # run nms evaluation
        self.sess.run(nms_node.get())
        nms_boxes = nms_node.data['boxes'].eval(session=self.sess)
        nms_scores = nms_node.data['scores'].eval(session=self.sess)

        return nms_boxes, nms_scores
class DeepLabModel(object):
    """Class to load deeplab model and run inference."""

    INPUT_TENSOR_NAME = 'ImageTensor:0'
    OUTPUT_TENSOR_NAME = 'SemanticPredictions:0'
    INPUT_SIZE = 513
    FROZEN_GRAPH_NAME = 'frozen_inference_graph'

    def __init__(self, tarball_path):
        # """Creates and loads pretrained deeplab model."""
        self.graph = tf.Graph()
        graph_def = None
        # Extract frozen graph from tar archive.
        tar_file = tarfile.open(tarball_path)
        for tar_info in tar_file.getmembers():
            if self.FROZEN_GRAPH_NAME in os.path.basename(tar_info.name):
                file_handle = tar_file.extractfile(tar_info)
                graph_def = GraphDef.FromString(file_handle.read())
                break

        tar_file.close()

        if graph_def is None:
            raise RuntimeError('Cannot find inference graph in tar archive.')

        with self.graph.as_default():
            tf.import_graph_def(graph_def, name = '')

        self.sess = Session(graph = self.graph)

    def run(self, image):
        """Runs inference on a single image.

        Args:
          image: A PIL.Image object, raw input image.

        Returns:
          resized_image: RGB image resized from original input image.
          seg_map: Segmentation map of `resized_image`.
        """
        width, height = image.size
        resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height)
        target_size = (int(resize_ratio * width), int(resize_ratio * height))
        resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)
        batch_seg_map = self.sess.run(
            self.OUTPUT_TENSOR_NAME,
            feed_dict = {self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]})
        seg_map = batch_seg_map[0]
        return resized_image, seg_map
示例#9
0
    def end(self, session: session_lib.Session):
        if self._save_thread:
            logging.info("Waiting for any pending checkpoints to finish.")
            self._save_thread.join()
        if self._write_graph_thread:
            logging.info("Waiting for any pending write_graph to finish.")
            self._write_graph_thread.join()

        last_step = session.run(self._global_step_tensor)

        if self._last_checkpoint_step != last_step:
            self._save(session, last_step, asynchronous=False)

        for l in self._listeners:
            l.end(session, last_step)
示例#10
0
def test_hash(hash_func):
    # random_str = generate_random_ascii_str()
    random_str = []
    with open(RANDOM_STRING_FILE, 'rb') as f:
        for line in f:
            random_str.append(line.strip())
    nums = len(random_str)
    num_batches = int(nums / BATCH_SIZE)

    # result = hash_method(input=random_str, num_buckets=HASH_SIZE, name="test_hash")
    print("Num of random string is {}".format(len(random_str)))
    print("Hash size is {}".format(HASH_SIZE))
    print("HASH_SIZE / NUMS_RANDOM_STR is {}".format(HASH_SIZE / nums))

    x_input_str = placeholder(dtype=tf_string,
                              shape=[
                                  None,
                              ],
                              name='input_str')
    result = hash_func(input=x_input_str,
                       num_buckets=HASH_SIZE,
                       name="test_hash")

    RESULT = []
    sess = Session()
    t0 = time.time()
    for i in xrange(num_batches):
        if i != num_batches - 1:
            batch_i = random_str[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]
        else:
            batch_i = random_str[i * BATCH_SIZE:]
        result_i = sess.run(result, feed_dict={x_input_str: batch_i})
        RESULT += result_i.tolist()

    cost_t = time.time() - t0
    conflict_nums = len(RESULT) - len(set(RESULT))
    print("Calculate time is {}s".format(cost_t))
    print("Conflict nums is {}".format(conflict_nums))
    print("Conflict rate is {}".format(conflict_nums / len(RESULT)))
    sess.close()
示例#11
0
    def after_create_session(self, session: session_lib.Session, coord: Any):
        global_step = session.run(self._global_step_tensor)

        # We do write graph and saver_def at the first call of before_run.
        # We cannot do this in begin, since we let other hooks to change graph and
        # add variables in begin. Graph is finalized after all begin calls.
        def _write_graph_fn(self):
            training_util.write_graph(
                ops.get_default_graph().as_graph_def(add_shapes=True),
                self._checkpoint_dir, "graph.pbtxt")

        self._write_graph_thread = threading.Thread(target=_write_graph_fn,
                                                    args=[self])
        self._write_graph_thread.start()

        saver_def = self._get_saver().saver_def if self._get_saver() else None
        graph = ops.get_default_graph()
        meta_graph_def = meta_graph.create_meta_graph_def(
            graph_def=graph.as_graph_def(add_shapes=True), saver_def=saver_def)
        self._summary_writer.add_graph(graph)
        self._summary_writer.add_meta_graph(meta_graph_def)
        # The checkpoint saved here is the state at step "global_step".
        self._save(session, global_step)
        self._timer.update_last_triggered_step(global_step)