Use decoded string as input produce unexpected result
ArtificialNotImbecile opened this issue · 0 comments
ArtificialNotImbecile commented
I have a frozen graph optimized by tftrt, the outputs are exactly the same as using the un-optimized graph. But, when I use base64 encoded strings as input, the result is totally wrong, here is how I produce the graph with ase64 encoded string as input. (I've tested frozen graph without tftrt optimization using base64 encoded string as input and it works exactly as I expected. This is totally unreasonable since the behavior of tf.io.decode_base64()
and tf.image.decode_jpeg()
should be the same regardless whether I use tftrt optimized graph or not)
# load yolo frozen graph def
frozen_graph = tf.GraphDef()
with open('./trt_optimized_frozen_graph.pb', 'rb') as f:
frozen_graph.ParseFromString(f.read())
with tf.Graph().as_default() as g_yolo:
tf.import_graph_def(frozen_graph, name='')
gdef_yolo = g_yolo.as_graph_def()
# load string->image tensor graph def
def decode_string(string_image):
byte_im = tf.io.decode_base64(string_image)
image = tf.image.decode_jpeg(byte_im, channels=3,dct_method='INTEGER_ACCURATE')
return image
with tf.Graph().as_default() as g_bs64:
raw_bytes_batch = tf.placeholder(shape=[None], dtype=tf.string, name='input')
data = tf.map_fn(decode_string, raw_bytes_batch, dtype=tf.uint8,
back_prop=False, parallel_iterations=10)
data = tf.cast(data, tf.float32)
input_data = tf.truediv(data, 255., name='output')
gdef_bs64 = g_bs64.as_graph_def()
# combine graph
with tf.Graph().as_default() as g_combined:
x = tf.placeholder(shape=[None], dtype=tf.string, name='input')
y, = tf.import_graph_def(gdef_bs64, input_map={"input:0": x},
return_elements=["output:0"], name='')
tf.import_graph_def(gdef_yolo, input_map={"input:0": y}, name='')
with tf.gfile.GFile('./frozen_graph_bs64_input.pb', "wb") as f:
f.write(g_combined.as_graph_def().SerializeToString())
print('Done')