tflearn model.load(path)不加载

时间:2018-03-01 13:59:18

标签: python conv-neural-network tflearn

这个问题会让我发疯:我有一个使用tflearn的卷积网络代码。我想保存我的模型并加载它以对不同的图像进行预测。我是这样做的:

def predictValues(image_path):
    ...
    model = tflearn.DNN(convnet)

    if not os.path.exists("emotionPrediction.tfl.meta"):
        X = images.reshape(-1, 1, 268, 1) 
        Y = lables.reshape(-1, 7)
        model.fit({'input': X}, {'targets': Y}, n_epoch=2, snapshot_step=500, show_metric=True) 
        model.save("emotionPrediction.tfl")

    model.load(".\\emotionPrediction.tfl", weights_only=True)

    print("Emotion values for %s: ", file):
    ....

images = [path1, path2, path3]

for x in images:
    predictValues(x)

不幸的是,无论我如何更改路径,代码都无法正常工作。我得到了第一张图片的预测,但之后对于其他图片我得到了这个错误:

2018-03-01 14:43:58.334231: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key Conv2D_11/W not found in checkpoint
2018-03-01 14:43:58.334617: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key FullyConnected_3/b not found in checkpoint
2018-03-01 14:43:58.337374: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key Conv2D_10/b not found in checkpoint
2018-03-01 14:43:58.337411: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key Conv2D_10/W not found in checkpoint
2018-03-01 14:43:58.342332: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key Conv2D_11/b not found in checkpoint
2018-03-01 14:43:58.350840: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key Conv2D_6/W not found in checkpoint
2018-03-01 14:43:58.354507: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key Conv2D_6/b not found in checkpoint
2018-03-01 14:43:58.355432: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key Conv2D_7/W not found in checkpoint
2018-03-01 14:43:58.535684: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key FullyConnected_3/W not found in checkpoint
2018-03-01 14:43:58.543666: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key Conv2D_7/b not found in checkpoint
2018-03-01 14:43:58.548686: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key Conv2D_8/W not found in checkpoint
2018-03-01 14:43:58.551316: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key Conv2D_8/b not found in checkpoint
2018-03-01 14:43:58.559867: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key Conv2D_9/W not found in checkpoint
2018-03-01 14:43:58.567227: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key Conv2D_9/b not found in checkpoint
2018-03-01 14:43:58.588178: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key FullyConnected_2/b not found in checkpoint
2018-03-01 14:43:58.588249: W C:\tf_jenkins\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1198] Not found: Key FullyConnected_2/W not found in checkpoint
Traceback (most recent call last):
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1350, in _do_call
    return fn(*args)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1329, in _run_fn
    status, run_metadata)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.NotFoundError: Key Conv2D_11/W not found in checkpoint
         [[Node: save_7/RestoreV2_6 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save_7/Const_0_0, save_7/RestoreV2_6/tensor_names, save_7/RestoreV2_6/shape_and_slices)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\xx\Desktop\Prediction.py", line 128, in <module>
    predictEmotions(x)
  File "C:\Users\xx\Desktop\Prediction.py", line 113, in predictEmotions
    model.load(".\\emotionPrediction.tfl", weights_only=True)
  File "C:\Users\xx\Python35\lib\site-packages\tflearn\models\dnn.py", line 308, in load
    self.trainer.restore(model_file, weights_only, **optargs)
  File "C:\Users\xx\Python35\lib\site-packages\tflearn\helpers\trainer.py", line 492, in restore
    self.restorer_trainvars.restore(self.session, model_file)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 1686, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
    run_metadata_ptr)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1128, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1344, in _do_run
    options, run_metadata)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1363, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Key Conv2D_11/W not found in checkpoint
         [[Node: save_7/RestoreV2_6 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save_7/Const_0_0, save_7/RestoreV2_6/tensor_names, save_7/RestoreV2_6/shape_and_slices)]]

Caused by op 'save_7/RestoreV2_6', defined at:
  File "C:\Users\xx\Desktop\Prediction.py", line 128, in <module>
    predictEmotions(x)
  File "C:\Users\xx\Desktop\Prediction.py", line 105, in predictEmotions
    model = tflearn.DNN(convnet)
  File "C:\Users\xx\Python35\lib\site-packages\tflearn\models\dnn.py", line 65, in __init__
    best_val_accuracy=best_val_accuracy)
  File "C:\Users\xx\Python35\lib\site-packages\tflearn\helpers\trainer.py", line 155, in __init__
    allow_empty=True)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 1239, in __init__
    self.build()
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 1248, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 1284, in _build
    build_save=build_save, build_restore=build_restore)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 765, in _build_internal
    restore_sequentially, reshape)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 428, in _AddRestoreOps
    tensors = self.restore_op(filename_tensor, saveable, preferred_shard)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 268, in restore_op
    [spec.tensor.dtype])[0])
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\ops\gen_io_ops.py", line 1113, in restore_v2
    shape_and_slices=shape_and_slices, dtypes=dtypes, name=name)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3160, in create_op
    op_def=op_def)
  File "C:\Users\xx\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1625, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

NotFoundError (see above for traceback): Key Conv2D_11/W not found in checkpoint
         [[Node: save_7/RestoreV2_6 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save_7/Const_0_0, save_7/RestoreV2_6/tensor_names, save_7/RestoreV2_6/shape_and_slices)]]

有人可以帮忙吗?我已经谷歌搜索了几个小时来解决这个问题但是徒劳无功..

1 个答案:

答案 0 :(得分:0)

你必须使用

model.predict(input)

model.load()只会加载预先保存的模型文件。它不会马上给你预测。