首页 > 解决方案 > 在 main() 中初始化一个 tensorflow 模型,将其传递给 __init__ 并在另一个方法中执行

问题描述

我想用烧瓶构建一个 Web 服务,其中多个深度学习模型将应用于某些类型的数据以返回结果。目前,我想在启动时在 main() 本地加载它们,将它们传递给init以在脚本执行开始时初始化它们一次,然后在每次需要执行前向传递以返回某些内容时调用它。到目前为止,这就是我对其余部分所做的,但我不知道如何处理纯 tensorflow 模型初始化。下面的代码工作正常。任何建议,更改表示赞赏:

def evaluate_sample(numpy_array, no_of_frames):
    _IMAGE_SIZE = 224
    _SAMPLE_VIDEO_FRAMES = no_of_frames
    _CHECKPOINT_PATHS = {'flow': 'data/checkpoints/flow_scratch/model.ckpt'}
    NUM_CLASSES = 400

    flow_input = tf.placeholder(
        tf.float32,
        shape=(1, _SAMPLE_VIDEO_FRAMES, _IMAGE_SIZE, _IMAGE_SIZE, 2))
    with tf.variable_scope('Flow', reuse=tf.AUTO_REUSE):
        flow_model = i3d.InceptionI3d(NUM_CLASSES, spatial_squeeze=True, final_endpoint='Logits')
        flow_logits, _ = flow_model(flow_input, is_training=False, dropout_keep_prob=1.0)
    flow_variable_map = {}
    for variable in tf.global_variables():
        if variable.name.split('/')[0] == 'Flow':
            flow_variable_map[variable.name.replace(':0', '')] = variable
    flow_saver = tf.train.Saver(var_list=flow_variable_map, reshape=True)    
    model_logits = flow_logits
    model_predictions = tf.nn.softmax(model_logits)

    with tf.Session() as sess:
        feed_dict = {}
        flow_saver.restore(sess, _CHECKPOINT_PATHS['flow'])
        flow_sample = numpy_array
        feed_dict[flow_input] = flow_sample
        out_logits, out_predictions = sess.run(
            [model_logits, model_predictions],
            feed_dict=feed_dict)
        logits2=np.asarray(out_logits)

    return logits2




def get_flow_features(video_path):
         .....
         aggregated_flow_vector = evaluate_sample(final_np_cropped_flow, len(all_frames_flow))
         .....


    class GetOutOfContext:
        def __init__(self, keras_model, pytorch_model, word2vec_model, max_pooling):
            self.keras_model = keras_model
            self.pytorch_model = pytorch_model
            self.word2vec_model = word2vec_model
            self.max_pooling = max_pooling
            #self.kineticsi3d = kineticsi3d
            print("Similarity Between Video and Text Service Initialized...")

        def get(self):
            dirpath = tempfile.mkdtemp()+"/"
            video_path = download_video(url,dirpath)
            aggregated_audio = get_audio_features(video_path)
            aggregated_flow = get_flow_features(video_path)
            aggregated_video = get_visual_features(video_path, dirpath)
            aggregated_text = get_word_features(text)
            .......

    if __name__ == "__main__":
        """Loading Prediction Model"""
        video_modality_dim = {'face': (128,128), 'audio': (128*16,128),'visual': (2048,2048), 'motion': (1024,1024)}
        the_model = Net(video_modality_dim, 300, audio_cluster=16)
        the_model.load_state_dict(torch.load('/home/estathop/Desktop/journalmodel/msrvttjournal.pt', map_location=lambda storage, loc: storage))
        the_model.eval()
        """Loading Image Feature Extraction Model"""
        model = ResNet152(include_top=False, weights='imagenet', pooling = 'avg') #cons
        """Loading Word2Vec Model"""
        model2 = api.load("word2vec-google-news-300")  
        maxpoolingmodel = keras.layers.pooling.GlobalMaxPooling1D()
        word_vectors = model2.wv
        nltk.download('stopwords')

        x = GetOutOfContext(model,the_model,model2, maxpoolingmodel)
        y = x.get()

标签: pythontensorflowweb-servicesinitialization

解决方案


我会坚持Session多次runsaver.restore应该只发生一次。对于错误检查,您可以tf.get_default_graph().finalize()在指定模型后确保图形不会更改每个请求,这会减慢速度。


推荐阅读