将Inception V3自定义模型移植到Android Tensor Flow Camera演示中的崩溃

时间:2017-05-04 08:19:40

标签: android tensorflow

我的目标是:

  1. 使用初始V3拱形为我的自定义类(一个类)创建检查点文件:Inception in TensorFlow
  2. 使用freeze_graph
  3. 将它们冻结到protobuf(.pb)
  4. 使用optimize_for_inference优化冻结图
  5. 在Android TF相机演示中使用pb文件进行分类:TensorFlow Android Camera Demo
  6. 在步骤1中,在训练时,批量大小设置为1。 还添加了images = tf.identity(images, name='Inputs_layer')来命名张量网络,如问题所示 No Operation named [input] in the Graph" error while fine tuning/retraining inceptionV1 slim model

    在第3步之前,

    >> bazel-bin/tensorflow/tools/graph_transforms/summarize_graph --
       in_graph=frozen_graph.pb
       No inputs spotted.
       No variables spotted.
       Found 1 possible outputs: (name=tower_0/logits/predictions, op=Softmax)
       Found 21781804 (21.78M) const parameters, 0 (0) variable parameters, and 188 
       control_edges
       Op types used: 777 Const, 378 Mul, 284 Add, 283 Sub, 190 Identity, 188 Sum, 
       96 Reshape, 94
       Conv2D, 94 StopGradient, 94 SquaredDifference, 94 Square, 94 Mean, 94 Rsqrt, 
       94 Relu, 94
       Reciprocal, 15 ConcatV2, 10 AvgPool, 4 MaxPool, 1 RealDiv, 1 RandomUniform, 1
       QueueDequeueManyV2, 1 Softmax, 1 Split, 1 MatMul, 1 Floor, 1 FIFOQueueV2, 1 
       BiasAdd
    

    在第3步中,

    bazel-bin/tensorflow/python/tools/optimize_for_inference \
       --input=tensorflow/examples/android/assets/frozen_graph.pb \
       --output=tensorflow/examples/android/assets/stripped_graph.pb \
       --input_names=inputs_layer \
       --output_names=tower_0/logits/predictions
    

    在第3步之后,

     >>> bazel-bin/tensorflow/tools/graph_transforms/summarize_graph --
       in_graph=stripped_graph.pb
       No inputs spotted.
       No variables spotted.
       Found 1 possible outputs: (name=tower_0/logits/predictions, op=Softmax) 
       Found 21781804 (21.78M) const parameters, 0 (0) variable parameters, and 188 
       control_edges
       Op types used: 777 Const, 378 Mul, 284 Add, 283 Sub, 188 Sum, 96 Reshape, 94 
       Conv2D, 94 StopGradient, 94 SquaredDifference, 94 Square, 94 Mean, 94 Rsqrt, 
       94 Relu, 94 Reciprocal, 15 ConcatV2, 10 AvgPool, 4 MaxPool, 1 RealDiv, 1 
       RandomUniform, 1 QueueDequeueManyV2, 1 Softmax, 1 Split, 1 MatMul, 1 Floor, 1 
       FIFOQueueV2, 1 BiasAdd
       To use with tensorflow/tools/benchmark:benchmark_model try these arguments:
       run tensorflow/tools/benchmark:benchmark_model -- --
       graph=stripped_graph.pb --show_flops --logtostderr --input_layer= --
       input_layer_type= --input_layer_shape= --
       output_layer=tower_0/logits/predictions
    

    在ClassifierActivity.java中,

    private static final int INPUT_SIZE = 224;//299; //224;  
    private static final int IMAGE_MEAN = 117;
    private static final float IMAGE_STD = 1;
    private static final String INPUT_NAME = "inputs_layer";
    private static final String OUTPUT_NAME = "tower_0/logits/predictions";
    private static final String MODEL_FILE = 
                           "file:///android_asset/stripped_graph.pb";
    private static final String LABEL_FILE =
          "file:///android_asset/custom_label.txt";
    

    按照上述4个步骤操作后,APK崩溃登录Android设备:

    E/AndroidRuntime( 8558): FATAL EXCEPTION: inference
       E/AndroidRuntime( 8558): Process: org.tensorflow.demo, PID: 8558
       E/AndroidRun time( 8558): java.lang.IllegalArgumentException: No Operation 
       named [inputs_layer] in the Graph
    

    如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

在优化推理时,它没有正确的输入节点名称。您刚刚提供了inputs_layer,因此在Android中无法正确识别optimized.pb文件。

它说输入节点是server { listen 443; server_name localhost; #access_log logs/access.log #error_log logs/error.log ssl_certificate example-com.cert.pem; ssl_certificate_key example-com.key.pem; root C:\Work\Code\OptimumTrunk; charset utf-8; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://localhost:3005/; } } 无处可去。给出正确的输入节点名称,它应该可以工作。