keras tensorflow load_weights失败

时间:2017-03-15 12:35:59

标签: tensorflow keras

我正在使用keras 1.2和tensorflow 1.0.0后端。

我有一个函数从json加载预先校准的模型,然后从hdf5文件加载它的权重。

ActionController::RoutingError (uninitialized constant Mongoid::Token::Collisions::ProfilesController):

activesupport (5.0.0.1) lib/active_support/inflector/methods.rb:283:in `const_get'
activesupport (5.0.0.1) lib/active_support/inflector/methods.rb:283:in `block in constantize'
activesupport (5.0.0.1) lib/active_support/inflector/methods.rb:266:in `each'
activesupport (5.0.0.1) lib/active_support/inflector/methods.rb:266:in `inject'
activesupport (5.0.0.1) lib/active_support/inflector/methods.rb:266:in `constantize'
actionpack (5.0.0.1) lib/action_dispatch/http/request.rb:93:in `controller_class'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:44:in `controller'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:30:in `serve'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:725:in `call'
rack-attack (5.0.1) lib/rack/attack.rb:147:in `call'
omniauth (1.3.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.3.2) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.3.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.3.2) lib/omniauth/strategy.rb:164:in `call'
rack-tracker (1.1.0) lib/rack/tracker.rb:34:in `call'
rack-utm (0.0.2) lib/rack-utm.rb:59:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (2.0.1) lib/rack/etag.rb:25:in `call'
rack (2.0.1) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.1) lib/rack/head.rb:12:in `call'
rack (2.0.1) lib/rack/session/abstract/id.rb:222:in `context'
rack (2.0.1) lib/rack/session/abstract/id.rb:216:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/cookies.rb:613:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
web-console (3.3.1) lib/web_console/middleware.rb:131:in `call_app'
web-console (3.3.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.3.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.3.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0.1) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `call'
ahoy_matey (1.5.2) lib/ahoy/engine.rb:22:in `call_with_quiet_ahoy'
request_store (1.3.1) lib/request_store/middleware.rb:9:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.1) lib/rack/method_override.rb:22:in `call'
rack (2.0.1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'
railties (5.0.0.1) lib/rails/engine.rb:522:in `call'
puma (3.6.0) lib/puma/configuration.rb:225:in `call'
puma (3.6.0) lib/puma/server.rb:578:in `handle_request'
puma (3.6.0) lib/puma/server.rb:415:in `process_client'
puma (3.6.0) lib/puma/server.rb:275:in `block in run'
puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread'

此函数,更确切地说是对def load(): model = model_from_json(open(model_path).read()) model.load_weights(model_weights_path) 的调用会导致以下异常:

load_weights

我想知道这是否是由于我在模块开头设置张量流种子以获得再现性的这些行:

RuntimeError: The Session graph is empty.  Add operations to the graph before calling run()

似乎keras会话不会自动将加载的模型设置为与会话关联的图形,因此无法初始化权重。

避免异常的任何解释和解决方法?

2 个答案:

答案 0 :(得分:0)

我几乎使用与你相同的代码,它对我有用。

       from keras.models import Sequential
        from keras.models import Model
        from keras.layers import Dense, Dropout, Activation, Flatten, Input, GlobalAveragePooling2D
        from keras.optimizers import RMSprop
        from keras.utils import np_utils
        from keras.models import model_from_json
        from keras.layers import Convolution2D, MaxPooling2D
        from keras.layers.pooling import AveragePooling2D
        from keras.layers.normalization import BatchNormalization
        from keras.layers.convolutional import ZeroPadding2D
        from keras.engine.topology import Merge
        from keras.layers import merge
        from keras.optimizers import Adam
        from keras import backend as K
        from keras.layers.pooling import MaxPooling2D
        from keras.layers.convolutional import ZeroPadding2D

        import PIL
        import inception
        import tensorflow as tf
        import keras
        import glob
        import pandas as pd
        import pickle
        import numpy as np
        import matplotlib.pyplot as plt
        from PIL import Image

     # load json and create model
        json_file = open('model.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        model = model_from_json(loaded_model_json)
        # load weights into new model
        model.load_weights("model.h5")
        print("Loaded model from disk")

model.summary()
model.compile(Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])

score = model.predict(transfer_values_test)

答案 1 :(得分:0)

事实上,在加载模型时,Keras似乎不尊重set_session设置的会话。

尝试强制Keras使用Tensorflow的上下文管理器的特定会话:

def load():
    with sess.as_default():    
        model = model_from_json(open(model_path).read())
        model.load_weights(model_weights_path)''

如果Keras仍然抱怨,请预定义图表(graph=tf.Graph())并强制使用model.load_weights,通过引入额外的with语句来使用它:

def load():
    with graph.as_default():
        with sess.as_default():    
            model = model_from_json(open(model_path).read())
            model.load_weights(model_weights_path)''