代码不工作:尝试训练 alexnet 模型

Code not working : Trying a train an alexnet model

我正在尝试根据游戏输入训练神经网络,更准确地说,GTA 5 中的自行车是由模型驱动的。它以屏幕帧作为输入,并记录了我在训练时输入的键。

不断收到此错误消息,我不知道如何解决它。

错误信息如下:

>Traceback (most recent call last):
      File "training_model.py", line 27, in <module>
        show_metric =True, run_id = MODEL_NAME)
      File "C:\Users\Aman\Anaconda2\envs\tensorflow\lib\site-packages\tflearn\models\dnn.py", line 184, in fit
        self.targets)
      File "C:\Users\Aman\Anaconda2\envs\tensorflow\lib\site-packages\tflearn\utils.py", line 331, in feed_dict_builder
        "such variable is known to exist" % key)
    Exception: Feed dict asks for variable named 'target' but no such variable is known to exist

模型训练代码:

import numpy as np
from alexnet import alexnet

WIDTH = 80
HEIGHT = 60
EPOCHS = 2
LR = 1e-3

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

model = alexnet(WIDTH, HEIGHT, LR)

train_data = np.load('final_data.npy', encoding = 'bytes')

train_dataset = train_data[:-400]
test_dataset = train_data[-400:]


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

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

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

model.save(MODEL_NAME)

Alexnet 模型:

#alexnet.py

import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from tflearn.layers.normalization import local_response_normalization

def alexnet(width, height, lr):
    network = input_data(shape=[None, width, height, 1], name='input')
    network = conv_2d(network, 96, 11, strides=4, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 256, 5, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = conv_2d(network, 384, 3, activation='relu')
    network = conv_2d(network, 384, 3, activation='relu')
    network = conv_2d(network, 256, 3, activation='relu')
    network = max_pool_2d(network, 3, strides=2)
    network = local_response_normalization(network)
    network = fully_connected(network, 4096, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, 4096, activation='tanh')
    network = dropout(network, 0.5)
    network = fully_connected(network, 3, activation='softmax')
    network = regression(network, optimizer='momentum',
                         loss='categorical_crossentropy',
                         learning_rate=lr, name='targets')

    model = tflearn.DNN(network, checkpoint_path='model_alexnet',
                        max_checkpoints=1, tensorboard_verbose=2, tensorboard_dir='log')

    return model

在您的 Feed 字典中使用的模型中:{'target' :Y}。不幸的是,您将目标输出命名为 "targets"。将 model.fit 行更改为此应该有效:

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