Esempio n. 1
0
def assert_equal_graph_def(actual, expected, checkpoint_v2=False):
    """Asserts that two `GraphDef`s are (mostly) the same.

  Compares two `GraphDef` protos for equality, ignoring versions and ordering of
  nodes, attrs, and control inputs.  Node names are used to match up nodes
  between the graphs, so the naming of nodes must be consistent.

  Args:
    actual: The `GraphDef` we have.
    expected: The `GraphDef` we expected.
    checkpoint_v2: boolean determining whether to ignore randomized attribute
        values that appear in V2 checkpoints.

  Raises:
    AssertionError: If the `GraphDef`s do not match.
    TypeError: If either argument is not a `GraphDef`.
  """
    if not isinstance(actual, graph_pb2.GraphDef):
        raise TypeError("Expected tf.GraphDef for actual, got %s" %
                        type(actual).__name__)
    if not isinstance(expected, graph_pb2.GraphDef):
        raise TypeError("Expected tf.GraphDef for expected, got %s" %
                        type(expected).__name__)

    if checkpoint_v2:
        _strip_checkpoint_v2_randomized(actual)
        _strip_checkpoint_v2_randomized(expected)

    diff = pywrap_tensorflow.EqualGraphDefWrapper(actual.SerializeToString(),
                                                  expected.SerializeToString())
    if diff:
        raise AssertionError(compat.as_str(diff))
Esempio n. 2
0
    def test_diff_in_tf_graph(self):
        g1 = tf.Graph()
        with g1.as_default() as g:
            with g.name_scope("g1") as g1_scope:
                matrix1 = tf.constant([[3., 3.]])
                matrix2 = tf.constant([[2.], [2.]])
                product = tf.matmul(matrix1, matrix2, name="product")
                first_graph = str(g.as_graph_def())

        g2 = tf.Graph()
        with g2.as_default() as g:
            with g.name_scope("g1") as g1_scope:
                matrix1 = tf.constant([[3., 3.]])
                matrix2 = tf.constant([[2.], [2.]])
                #product = tf.matmul(matrix1, tf.matmul(matrix2,matrix1), name="product")
                product = tf.matmul(matrix1, matrix2, name="product")
                second_graph = str(g.as_graph_def())

        print(first_graph)
        print(second_graph)
        diff = pywrap_tensorflow.EqualGraphDefWrapper(
            g1.as_graph_def().SerializeToString(),
            g2.as_graph_def().SerializeToString())
        print('diff', diff)
        assert not diff
Esempio n. 3
0
    def test_refactoring_ecn_sum(self):
        # Test the refactoring of ECN

        node_dim = 29
        edge_dim = 42
        nb_class = 18

        nb_layers = 3
        lr = 0.001
        nb_conv = 10

        g1 = tf.Graph()
        with g1.as_default() as g:
            with g.name_scope("g1") as g1_scope:
                print('Old')
                gcn_model = EdgeConvNet(
                    node_dim,
                    edge_dim,
                    nb_class,
                    num_layers=nb_layers,
                    learning_rate=lr,
                    mu=0.0,
                    node_indim=-1,
                    nconv_edge=nb_conv,
                )
                gcn_model.stack_instead_add = False
                gcn_model.create_model_old()
                first_graph = str(g.as_graph_def())

        g2 = tf.Graph()
        with g2.as_default() as g:
            with g.name_scope("g1") as g1_scope:
                print('New')
                gcn_model = EdgeConvNet(
                    node_dim,
                    edge_dim,
                    nb_class,
                    num_layers=nb_layers,
                    learning_rate=lr,
                    mu=0.0,
                    node_indim=-1,
                    nconv_edge=nb_conv,
                )
                gcn_model.stack_instead_add = False
                gcn_model.create_model()
                second_graph = str(g.as_graph_def())

        diff = pywrap_tensorflow.EqualGraphDefWrapper(
            g1.as_graph_def().SerializeToString(),
            g2.as_graph_def().SerializeToString())
        print('diff', diff)
        print(
            'Failure due to change of nonlinearty in the first layer. old model apply non lineartiye on nodes and then convolves'
        )

        assert not diff
Esempio n. 4
0
    def _evaluate_test_case(self, name, graph, ops_to_eval,
                            correctness_function):
        """Determine if a graph agrees with the reference data.

        Args:
          name: String defining the run. This will be used to define folder names
            and will be used for random seed construction.
          graph: The graph in which the test is conducted.
          ops_to_eval: Ops which the user wishes to be evaluated under a controlled
            session.
          correctness_function: This function accepts the evaluated results of
            ops_to_eval, and returns a list of values. This list must be JSON
            serializable; in particular it is up to the user to convert numpy
            dtypes into builtin dtypes.
        """
        data_dir = os.path.join(self.data_root, name)

        # Serialize graph for comparison.
        graph_bytes = graph.as_graph_def().SerializeToString()
        expected_file = os.path.join(data_dir, "expected_graph")
        with tf.io.gfile.GFile(expected_file, "rb") as f:
            expected_graph_bytes = f.read()
            # The serialization is non-deterministic byte-for-byte. Instead there is
            # a utility which evaluates the semantics of the two graphs to test for
            # equality. This has the added benefit of providing some information on
            # what changed.
            #   Note: The summary only show the first difference detected. It is not
            #         an exhaustive summary of differences.
        differences = pywrap_tensorflow.EqualGraphDefWrapper(
            graph_bytes, expected_graph_bytes).decode("utf-8")

        with graph.as_default():
            init = tf.compat.v1.global_variables_initializer()
            saver = tf.compat.v1.train.Saver()

        with tf.io.gfile.GFile(os.path.join(data_dir, "tf_version.json"),
                               "r") as f:
            tf_version_reference, tf_git_version_reference = json.load(f)  # pylint: disable=unpacking-non-sequence

        tf_version_comparison = ""
        if tf.version.GIT_VERSION != tf_git_version_reference:
            tf_version_comparison = (
                "Test was built using:     {} (git = {})\n"
                "Local TensorFlow version: {} (git = {})".format(
                    tf_version_reference, tf_git_version_reference,
                    tf.version.VERSION, tf.version.GIT_VERSION))

        with self.session(graph=graph) as sess:
            sess.run(init)
            try:
                saver.restore(sess=sess,
                              save_path=os.path.join(data_dir,
                                                     self.ckpt_prefix))
                if differences:
                    tf.compat.v1.logging.warn(
                        "The provided graph is different than expected:\n  {}\n"
                        "However the weights were still able to be loaded.\n{}"
                        .format(differences, tf_version_comparison))
            except:  # pylint: disable=bare-except
                raise self.failureException(
                    "Weight load failed. Graph comparison:\n  {}{}".format(
                        differences, tf_version_comparison))

            eval_results = [op.eval() for op in ops_to_eval]
            if correctness_function is not None:
                results = correctness_function(*eval_results)
                result_json = os.path.join(data_dir, "results.json")
                with tf.io.gfile.GFile(result_json, "r") as f:
                    expected_results = json.load(f)
                self.assertAllClose(results, expected_results)