0

在张量流中,我想尽快从我训练的模型中获得预测结果

因此,我尝试了之前建议的几种组合,速度结果看起来像这样(左边是最快的)

import tf1 result = model(input)> import tf2 result = model(input)>=import tf1 result = model.predict(input)

特别是,import tf1 result = model(input)比其他方法超级快但是,tf1转换result tensornumpy array使用(sess.run()eval()其他方式,如 No.1)非常耗时

因此,如果你们提供一些意见或提示来解决这个问题,我将不胜感激。

我附上了我的代码名为“AI_simulation”的代码函数是回调函数

在代码中,速度结果是,,

No.1 -> 4.1 FPS ~3.6 FPS getting slow (I think there is some memory leak)
No.2 -> 1.6 FPS (sess.run() is very time consuming )
No.3 -> 3.9 FPS
No.4 -> 3.8 FPS

1号

import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
tf.disable_v2_behavior()

sess = tf.Session()
with sess.as_default():

    G_A2B = model_from_json(loded_model_json, custom_objects={'ReflectionPadding3D':ReflectionPadding3D, 'InstanceNormalization':InstanceNormalization})
    G_A2B.load_weights(model_path+'G_A2B_model_epoch_'+str(model_num)+'.hdf5')
    place_holder = tf.compat.v1.placeholder(tf.float32, shape=(1,170,110,110,1))
    G_A2B = G_A2B(place_holder, training=False)

def AI_simulation(AI_input):

    result = sess.run(G_A2B, feed_dict={place_holder: AI_input[np.newaxis, :, :, :, np.newaxis]})
    result = np.squeeze(result)

    return result

2号

import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
tf.disable_v2_behavior()

sess = tf.Session()
with sess.as_default():

    G_A2B = model_from_json(loded_model_json, custom_objects={'ReflectionPadding3D':ReflectionPadding3D, 'InstanceNormalization':InstanceNormalization})
    G_A2B.load_weights(model_path+'G_A2B_model_epoch_'+str(model_num)+'.hdf5')


def AI_simulation(AI_input):

    result = G_A2B(AI_input[np.newaxis, :, :, :, np.newaxis])
    result = sess.run(result )
    result = np.squeeze(result)

    return result

3号

import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
tf.disable_v2_behavior()

G_A2B = model_from_json(loded_model_json, custom_objects={'ReflectionPadding3D':ReflectionPadding3D, 'InstanceNormalization':InstanceNormalization})
G_A2B.load_weights(model_path+'G_A2B_model_epoch_'+str(model_num)+'.hdf5')
G_A2B.call = tf.function(G_A2B.call, experimental_relax_shapes = True)

def AI_simulation(AI_input):

    result = G_A2B.predict(AI_input[np.newaxis, :, :, :, np.newaxis])
    result = np.squeeze(result)
    return result

4号

import tensorflow as tf  #tf2

G_A2B = model_from_json(loded_model_json, custom_objects={'ReflectionPadding3D':ReflectionPadding3D, 'InstanceNormalization':InstanceNormalization})
G_A2B.load_weights(model_path+'G_A2B_model_epoch_'+str(model_num)+'.hdf5')
G_A2B.call = tf.function(G_A2B.call, experimental_relax_shapes = True)

def AI_simulation(AI_input):
    result = G_A2B(AI_input[np.newaxis, :, :, :, np.newaxis])
    result = np.squeeze(result.numpy())
    return result
4

0 回答 0