load multiple models in Tensorflow

后端 未结 5 1859
北荒
北荒 2021-02-04 09:08

I have written the following convolutional neural network (CNN) class in Tensorflow [I have tried to omit some lines of code for clarity.]

class CNN:
de         


        
5条回答
  •  佛祖请我去吃肉
    2021-02-04 09:38

    This should be a comment to the most up-voted answer. But I do not have enough reputation to do that.

    Anyway. If you(anyone searched and got to this point) still having trouble with the solution provided by lpp AND you are using Keras, check following quote from github.

    This is because the keras share a global session if no default tf session provided

    When the model1 created, it is on graph1 When the model1 loads weight, the weight is on a keras global session which is associated with graph1

    When the model2 created, it is on graph2 When the model2 loads weight, the global session does not know the graph2

    A solution below may help,

    graph1 = Graph()
    with graph1.as_default():
        session1 = Session()
        with session1.as_default():
            with open('model1_arch.json') as arch_file:
                model1 = model_from_json(arch_file.read())
            model1.load_weights('model1_weights.h5')
            # K.get_session() is session1
    
    # do the same for graph2, session2, model2
    

提交回复
热议问题