【问题标题】:Output files returned after training a alexnet model...?训练 alexnet 模型后返回的输出文件...?
【发布时间】:2017-06-04 14:04:25
【问题描述】:

代码是用 Python 3.5.X 编写的

请尽量让计算机科学专业三年级学生的答案变得简单

train_model.py 的输出文件似乎是一个 model.meta 文件,但 test_model.py 要求一个 .model 文件。教程用户也有一个 .model 文件我似乎无法理解为什么我得到一个带有 .model.meta 的文件

我正在尝试通过 Python 玩 GTA San Andreas,或者更准确地说,GTA 中的汽车是由模型驱动的。 它将屏幕帧作为输入,并在训练期间记录关键 i Input。此训练数据用于训练模型。

训练模型的代码

import numpy as np
from alexnet import alexnet

WIDTH = 80
HEIGHT = 60
LR = 1e-3
EPOCHS = 8
MODEL_NAME = 'pygta_sa-car-{}-{}-{}-epochs.model'.format(LR, 'alextnetv2', EPOCHS)

model = alexnet(WIDTH, HEIGHT, LR)

train_data = np.load('training_data_v2.npy')

train = train_data[:-500]
test = train_data[-500:]

X = np.array([i[0] for i in train]).reshape(-1,WIDTH,HEIGHT,1)
Y = [i[1] for i in train]

test_x = np.array([i[0] for i in test]).reshape(-1,WIDTH,HEIGHT,1)
test_y = [i[1] for i in test]

model.fit({'input': X}, {'targets': Y}, n_epoch=EPOCHS, validation_set=({'input': test_x}, {'targets': test_y}), 
    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

# tensorboard --logdir=foo:F:\play_gta_sa\log

model.save(MODEL_NAME)

训练成功完成并返回文件

我用来做这个项目的教程视频中返回的文件

sent_dex files returned

检查点文件的内容

model_checkpoint_path: "F:\play_gta_sa\pygta_sa-car-0.001-alextnetv2-8-epochs.model" all_model_checkpoint_paths: "F:\play_gta_sa\pygta_sa-car-0.001-alextnetv2-8-epochs.model"

在游戏中测试模型的代码

import numpy as np
import cv2
import time
from grabscreen import grab_screen
from getkeys import key_check
from directkeys import PressKey, ReleaseKey, W, A, S, D
from alexnet import alexnet


WIDTH = 80
HEIGHT = 60
LR = 1e-3
EPOCHS = 8
MODEL_NAME = 'pygta_sa-car-{}-{}-{}-epochs.model'.format(LR, 'alexnetv2',EPOCHS) 


def straight():
    PressKey(W)
    ReleaseKey(A)
    ReleaseKey(D)

def left():
    PressKey(W)
    PressKey(A)
    ReleaseKey(D)

def right():
    PressKey(W)
    PressKey(D)
    ReleaseKey(A)

model = alexnet(WIDTH, HEIGHT, LR)
model.load(MODEL_NAME)

def main():

    for i in list(range(10))[::-1]:
        print(i+1)
        time.sleep(1)

    last_time = time.time()

    paused = False

    while True:
        if not paused:

            screen = grab_screen(region=(0,40,800,640))
            screen = cv2.cvtColor(screen,cv2.COLOR_BGR2GRAY)
            screen = cv2.resize(screen,(80,60))
            print('Frame took {} seconds'.format(time.time()-last_time))
            last_time = time.time()

            moves = list(np.around(model.predict([screen.reshape(80,60,1)])[0]))
            print(moves, prediction)

            if moves == [1,0,0]:
                left()
            elif moves == [0,1,0]:
                straight()
            elif moves == [0,0,1]:
                right()

        keys = key_check()

    # p pauses game and can get annoying.
        if 'T' in keys:
            if paused:
                paused = False
                time.sleep(1)
            else:
                paused = True
                ReleaseKey(A) 
                ReleaseKey(W)
                ReleaseKey(D)
                time.sleep(1)

main()

运行测试模型的错误信息

Traceback (most recent call last):
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1039, in _do_call
    return fn(*args)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1021, in _run_fn
    status, run_metadata)
  File "C:\Program Files\Python35\lib\contextlib.py", line 66, in __exit__
    next(self.gen)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for F:\play_gta_sa\pygta_sa-car-0.001-alexnetv2-8-epochs.model
     [[Node: save_1/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/RestoreV2/tensor_names, save_1/RestoreV2/shape_and_slices)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:\play_gta_sa\test_model.py", line 33, in <module>
    model.load(MODEL_NAME)
  File "C:\Program Files\Python35\lib\site-packages\tflearn\models\dnn.py", line 282, in load
    self.trainer.restore(model_file, weights_only, **optargs)
  File "C:\Program Files\Python35\lib\site-packages\tflearn\helpers\trainer.py", line 452, in restore
    self.restorer.restore(self.session, model_file)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 1457, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py", line 778, in run
    run_metadata_ptr)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py", line 982, in _run
    feed_dict_string, options, run_metadata)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1032, in _do_run
    target_list, options, run_metadata)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1052, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for F:\play_gta_sa\pygta_sa-car-0.001-alexnetv2-8-epochs.model
     [[Node: save_1/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/RestoreV2/tensor_names, save_1/RestoreV2/shape_and_slices)]]

Caused by op 'save_1/RestoreV2', defined at:
  File "<string>", line 1, in <module>
  File "C:\Program Files\Python35\lib\idlelib\run.py", line 124, in main
    ret = method(*args, **kwargs)
  File "C:\Program Files\Python35\lib\idlelib\run.py", line 351, in runcode
    exec(code, self.locals)
  File "F:\play_gta_sa\test_model.py", line 32, in <module>
    model = alexnet(WIDTH, HEIGHT, LR)
  File "F:\play_gta_sa\alexnet.py", line 40, in alexnet
    max_checkpoints=1, tensorboard_verbose=0, tensorboard_dir='log')
  File "C:\Program Files\Python35\lib\site-packages\tflearn\models\dnn.py", line 64, in __init__
    best_val_accuracy=best_val_accuracy)
  File "C:\Program Files\Python35\lib\site-packages\tflearn\helpers\trainer.py", line 147, in __init__
    allow_empty=True)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 1056, in __init__
    self.build()
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 1086, in build
    restore_sequentially=self._restore_sequentially)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 691, in build
    restore_sequentially, reshape)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 407, in _AddRestoreOps
    tensors = self.restore_op(filename_tensor, saveable, preferred_shard)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 247, in restore_op
    [spec.tensor.dtype])[0])
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\gen_io_ops.py", line 669, in restore_v2
    dtypes=dtypes, name=name)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op
    op_def=op_def)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2336, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1228, in __init__
    self._traceback = _extract_stack()

NotFoundError (see above for traceback): Unsuccessful TensorSliceReader constructor: Failed to find any matching files for F:\play_gta_sa\pygta_sa-car-0.001-alexnetv2-8-epochs.model
     [[Node: save_1/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save_1/Const_0, save_1/RestoreV2/tensor_names, save_1/RestoreV2/shape_and_slices)]]

【问题讨论】:

  • 您使用的是哪个版本的 Tensorflow?训练和测试脚本是否都在同一个目录中?你能确认除了生成的三个文件之外,还有一个叫做checkpoint的文件吗?
  • 你得到的三个文件的文件名没有问题。这就是 TF 保存模型的方式。但是,您收到的错误似乎还有其他一些问题。
  • Tensorflow 1.1.0 尝试了两种安装技术,即 pip install tensorflow 和来自 Windows 站点的非官方二进制文件的 whl 文件......是的,目录中有一个名为 file 的检查点,两个脚本都是 im同一个目录
  • 可以发一下文件检查点的内容吗?
  • 请在您的帖子中更新它以提高可读性。

标签: python tensorflow conv-neural-network tflearn


【解决方案1】:

感谢您的更新。

编辑:

尝试在tf.reset_default_graph() 加载模型之前添加此行。那是

import tensorflow as tf
tf.reset_default_graph()
model = alexnet(WIDTH, HEIGHT, LR)
model.load(MODEL_NAME)

【讨论】:

  • 我用 15 个 epochs 训练了一个新模型,因此检查点文件包含 15 个 epochs 的模型名称。我修改了 test_model.py 并尝试运行它...
  • 哦...你能提供你正在使用的 alexnet 包的链接吗?
  • 这个人正在申请 GTA V 我正在申请 GTA SA 但代码完全是来自 github.com/Sentdex/pygta5/tree/master/Tutorial%20Codes/… 的复制粘贴
  • 在加载之前重置图表怎么样?
  • nope no effect still the same error message will upload the image of the .model file from the video tutorial以证明文件扩展名是问题...
【解决方案2】:

这个错误的症结在于:

Unsuccessful TensorSliceReader constructor: Failed to find any matching files for F:\play_gta_sa\pygta_sa-car-0.001-alexnetv2-8-epochs.model

好的,这是一个典型的“找不到文件”。我们有信心拥有这个文件吗?也许吧,但是,如果它在那里,它就会被发现。我们的第一个猜测应该是我们打错了或犯了错误。让我们看看你的模型文件:

为了训练模型,你有:

MODEL_NAME = 'pygta_sa-car-{}-{}-{}-epochs.model'.format(LR, 'alextnetv2', EPOCHS)

为了测试模型,您有:

MODEL_NAME = 'pygta_sa-car-{}-{}-{}-epochs.model'.format(LR, 'alexnetv2',EPOCHS) 

你看出区别了吗?有一个错字。 alexnetv2 与 alexnetv2

修复它,至少会找到该文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-24
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 2019-10-19
    • 2017-06-15
    相关资源
    最近更新 更多