Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加载Tensorflow模型并由其他函数重用_Tensorflow - Fatal编程技术网

加载Tensorflow模型并由其他函数重用

加载Tensorflow模型并由其他函数重用,tensorflow,Tensorflow,我必须在初始化时加载模型,然后通过一些函数重用它。在我的体系结构函数中,通过一个模型在多个数据集上运行predict,我认为每个数据集的硬盘加载模型不是一个好的解决方案 我需要这样的东西,在函数之间共享会话(或模型): def __init__(self): self.graph = tf.Graph() with self.graph.as_default(): self.sess = tf.Session() with sess.as_defa

我必须在初始化时加载模型,然后通过一些函数重用它。在我的体系结构函数中,通过一个模型在多个数据集上运行predict,我认为每个数据集的硬盘加载模型不是一个好的解决方案

我需要这样的东西,在函数之间共享会话(或模型):

def __init__(self):
    self.graph = tf.Graph()
    with self.graph.as_default():
        self.sess = tf.Session()
        with sess.as_default():
           saver.restore(tf.get_default_session(), path_to_checkpoint)

def some_func():
    with self.graph.as_default():
        with self.sess.as_default():
            self.sess.run()

有什么合适的方法可以做到这一点吗?

您可以定义模型体系结构并将其权重作为类加载。将其用于不同功能中的各种数据集

class vgg16:
def __init__(self, imgs, weights=None, sess=None):
    self.imgs = imgs
    self.convlayers()
    self.fc_layers()
    self.probs = tf.nn.softmax(self.fc3l)
    if weights is not None and sess is not None:
        self.load_weights(weights, sess)

def convlayers(self):
    self.parameters = []
      ::
      ::

if __name__ == '__main__':
    sess = tf.Session()
    imgs = tf.placeholder(tf.float32, [None, 224, 224, 3])
    vgg = vgg16(imgs, 'vgg16_weights.npz', sess)

    # run inference here on multiple datasets
请点击此处:

这对你有帮助吗