Search code examples
pythontensorflowtflearn

Code not working : Trying a train an alexnet model


I'm trying to train a neural network on game inputs more precisely the bike in GTA 5 is driven by the model. It takes screen frames as input and recorded the key I input during training.

Constantly getting this error message and i can't figure out how to fix it.

Here's the error message:

>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

Code for training the model:

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 model:

#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

Solution

  • In the model you use this in your feed dictionary: {'target' :Y}. Unfortunately you named your target output "targets". Changing the model.fit line to this should work:

    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)