无法编译tensorflow Lite文件在珊瑚TPU上工作

时间:2019-05-28 21:55:08

标签: python tensorflow tensorflow-lite tpu

我正试图获得反复简单的模型进行编译,以使其能够在珊瑚tpu上运行。到目前为止,我已经冻结并将文件转换为tflite文件,但是当我通过Edge TPU Model Compiler运行文件时,它给了我一个相对无益的错误消息。

COMPILING FAILED
Something went wrong. Couldn't compile model.
Please make sure your model meets the requirements.
See the log below for more compilation details.
If you believe your model meets the requirements but you still receive this error,
email support at coral‑support@google.com.

我给他们发了电子邮件,他们说要使用/ tensorflow / lite / tools:visualization来查看模型出了什么问题。 (我在使它也无法正常工作时遇到麻烦,但似乎我应该问一个单独的问题以获取有关淡褐色东西的帮助)

this site之后,我通过量化感知训练对模型进行了训练,并且使用随机输入运行了tflite文件,它似乎可以正常工作。我担心TPU模型编译器的部分问题是我在代理后面,所以我通过它运行了其他人的文件并成功编译了该文件。)

以下是评估图:

import pandas as pd
import sys
import tensorflow as tf
import numpy as np
from tensorflow.python.tools import inspect_checkpoint as chkp
from sklearn.model_selection import train_test_split


#test data
seed = np.random.seed(141)

features = pd.read_csv(sys.argv[1], sep=',', index_col=0)
labels = pd.read_csv(sys.argv[2], sep=',', index_col=0)
train_input, test_input, train_labels, test_labels = train_test_split(features, labels, test_size=0.2, random_state=seed)

def neuron_layer(X, n_neurons, name, activation=None):
    with tf.name_scope(name):
        n_inputs = int(X.get_shape()[1])
        W = tf.Variable(tf.zeros([n_inputs, n_neurons]), name="kernal")
        b = tf.Variable(tf.zeros([n_neurons]), name="bias")
        Z = tf.matmul(X, W) + b
        if activation is not None:
            return activation(Z)
        else:
            return Z

X = tf.placeholder(tf.float32, (1, 701), name="X")
n_outputs = 2
n_hidden1 = 700
n_hidden2 = 701
with tf.name_scope("dnn"):
    hidden1 = neuron_layer(X, n_hidden1, name="hidden1", activation=tf.nn.relu)
    # hidden2 = neuron_layer(hidden1, n_hidden2, name="hidden2", activation=tf.nn.relu)
    # trying only one layer
    logits = neuron_layer(hidden1, n_outputs, name="outputs")

with tf.name_scope("final_eval"):
    output = tf.argmax(logits, axis=1, name="output")


# Call the eval rewrite which rewrites the graph in-place with
# FakeQuantization nodes and fold batchnorm for eval.
g = tf.get_default_graph()
tf.contrib.quantize.create_eval_graph(input_graph=g)

# Add ops to save and restore all the variables.
saver = tf.train.Saver()
eval_graph_file = "eval_graph.pb"

#handles different tensorboard runs
from datetime import datetime
now = datetime.utcnow().strftime("%Y%m%d%H%M%S")
root_logdir = "tf_logs"
logdir = "eval/{}/run-{}".format(root_logdir, now)

file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph())
with tf.Session() as sess:
    saver.restore(sess, "./nbtf/nothing_but_tf_model.ckpt")

    # Save the checkpoint and eval graph proto to disk for freezing
    # and providing to TFLite.
    with open(eval_graph_file, 'w+') as f:
        f.write(str(g.as_graph_def()))
    saver.save(sess, "./nbtf/eval/eval.ckpt")
    pred = output.eval(feed_dict={X: [test_input.values[45]]})
    print(pred, test_labels.values[45])

然后我冻结:

 freeze_graph --input_graph=eval_graph.pb --input_checkpoint=nbtf\eval\eval.ckpt --output_graph=frozen_eval_graph.pb --output_node_names=final_eval/output

然后用以下命令进行转换:

toco --graph_def_file=frozen_eval_graph.pb --output_file=tflite_model.tflite --input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE --inference_type=QUANTIZED_UINT8 --input_array=X --output_array=final_eval/output --std_dev_value=127 --mean_value=127

tensorboard image

我只是想编译这个文件,它并不一定是完美的。

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

好的,我能够编译文件,我只需要使用offline compiler就可以了。似乎可以完全解决它。