Is there an example on how to generate protobuf files holding trained TensorFlow graphs

前端 未结 6 1856
情话喂你
情话喂你 2020-11-28 19:45

I am looking at Google\'s example on how to deploy and use a pre-trained Tensorflow graph (model) on Android. This example uses a .pb file at:

6条回答
  •  醉梦人生
    2020-11-28 20:05

    I could not figure out how to implement the method described by mrry. But here how I solved it. I'm not sure if that is the best way of solving the problem but at least it solves it.

    As write_graph can also store the values of the constants, I added the following code to the python just before writing the graph with write_graph function:

    for v in tf.trainable_variables():
        vc = tf.constant(v.eval())
        tf.assign(v, vc, name="assign_variables")
    

    This creates constants that store variables' values after being trained and then create tensors "assign_variables" to assign them to the variables. Now, when you call write_graph, it will store the variables' values in the file in form of constants.

    The only remaining part is to call these tensors "assign_variables" in the c code to make sure that your variables are assigned with the constants values that are stored in the file. Here is a one way to do it:

          Status status = NewSession(SessionOptions(), &session);
          std::vector outputs;
          char name[100];
          for(int i = 0;status.ok(); i++) {
            if (i==0)
                sprintf(name, "assign_variables");
            else
                sprintf(name, "assign_variables_%d", i);
    
            status = session->Run({}, {name}, {}, &outputs);
          }
    

提交回复
热议问题