Update3:
What did i do?
I wrote a code snippet that reproduces the current problem.And it works.
So i don’t know what happen to my model. Everything fallback to the begining
import numpy as np
import tensorflow as tf
from tensorflow import graph_util
from tensorflow_core.python.saved_model import saved_model
from tensorflow_core.python.saved_model.signature_def_utils_impl import *
# Data settings.
num_words = 20
num_features = 100
num_tags = 5
# Train and evaluate the model.
with tf.Graph().as_default():
with tf.Session() as session:
# Add the data to the TensorFlow graph.
x_t = tf.placeholder(name="x", shape=[None, num_words, num_features], dtype=tf.float32)
y_t = tf.placeholder(name="y", shape=[None, num_words], dtype=tf.int32)
sequence_lengths_t = tf.placeholder(name="seq_len", shape=[None], dtype=tf.int32) # tf.constant(sequence_lengths)
features = {"x": x_t, "y": y_t, "seq_len": sequence_lengths_t}
# Compute unary scores from a linear layer.
weights = tf.get_variable("weights", [num_features, num_tags])
matricized_x_t = tf.reshape(x_t, [-1, num_features])
matricized_unary_scores = tf.matmul(matricized_x_t, weights)
unary_scores = tf.reshape(matricized_unary_scores,
[tf.shape(x_t)[0], num_words, num_tags])
# Compute the log-likelihood of the gold sequences and keep the transition
# params for inference at test time.
from tensorflow_core.contrib.crf.python.ops.crf import crf_log_likelihood, crf_decode
log_likelihood, transition_params = crf_log_likelihood(
unary_scores, y_t, sequence_lengths_t)
# Compute the viterbi sequence and score.
viterbi_sequence, viterbi_score = crf_decode(
unary_scores, transition_params, sequence_lengths_t)
viterbi_sequence = tf.identity(viterbi_sequence, name="output1")
viterbi_score = tf.identity(viterbi_score, name="output2")
log_likelihood = tf.identity(log_likelihood, name="logits")
predictions = {"output1": viterbi_sequence, "output2": viterbi_score, "logits": log_likelihood}
# Add a training op to tune the parameters.
loss = tf.reduce_mean(-log_likelihood)
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
session.run(tf.global_variables_initializer())
# Train for a fixed number of iterations.
for i in range(10):
num_examples = i + 1
# Random features.
x = np.random.rand(num_examples, num_words, num_features).astype(np.float32)
# Random tag indices representing the gold sequence.
y = np.random.randint(num_tags, size=[num_examples, num_words]).astype(np.int32)
# All sequences in this example have the same length, but they can be variable in a real model.
sequence_lengths = np.full(num_examples, num_words - 1, dtype=np.int32)
tf_viterbi_sequence, _ = session.run([viterbi_sequence, train_op], feed_dict={x_t: x, y_t: y, sequence_lengths_t: sequence_lengths})
if i % 10 == 0 or i == 9:
mask = (np.expand_dims(np.arange(num_words), axis=0) <
np.expand_dims(sequence_lengths, axis=1))
total_labels = np.sum(sequence_lengths)
correct_labels = np.sum((y == tf_viterbi_sequence) * mask)
accuracy = 100.0 * correct_labels / float(total_labels)
print("Accuracy: %.2f%%" % accuracy)
predictions_signature = saved_model.signature_def_utils.build_signature_def(
inputs=dict(((k, saved_model.utils.build_tensor_info(v))
for k, v in features.items())),
outputs=dict(((k, saved_model.utils.build_tensor_info(v))
for k, v in predictions.items())),
method_name=saved_model.signature_constants.PREDICT_METHOD_NAME,
)
model_dir = "./crf_test_model"
sm_builder = saved_model.builder.SavedModelBuilder(model_dir)
sm_builder.add_meta_graph_and_variables(
session,
tags=[saved_model.tag_constants.SERVING],
signature_def_map={
'predictions': predictions_signature,
},
clear_devices=True,
)
sm_builder.save()
tf.io.write_graph(tf.get_default_graph(), model_dir, "frozen_graph.pb", as_text=False)
Transfer: TF1.15.5 → Onnx → TensorRT
MODEL=crf_test_model
# Step1: TF.SavedModel -> Onnx
python3 -m tf2onnx.convert \
--saved-model $PWD/crf_test_model \
--opset 15 \
--tag serve \
--signature_def predictions \
--output "${MODEL}.onnx" \
--verbose
# Step2: Onnx -> Trt
trtexec \
--onnx="${MODEL}.onnx"\
--saveEngine="${MODEL}.engine" \
--builderOptimizationLevel=5
New Info
- if i use GraphSurgeon(graph.cleanup),
onnx -> trt
will fail