mercredi 30 août 2017

If statements don't work on Tensorflow variables

I tried to program a self driving car in Python and Tensorflow with this code:

import tensorflow as tf
from PIL import ImageGrab
import numpy as np
import pyautogui as pag
import time

x_p = tf.placeholder(tf.uint8)
y_p = tf.placeholder(tf.float32)

weights = [tf.Variable(tf.random_normal([5,5,3,32],0.1)),
           tf.Variable(tf.random_normal([5,5,32,64],0.1)),
           tf.Variable(tf.random_normal([5,5,64,128],0.1)),
           tf.Variable(tf.random_normal([25*25*128,1064],0.1)),
           tf.Variable(tf.random_normal([1064,1],0.1))]

def CNN(x, weights):
    output = tf.nn.conv2d(x, weights[0], [1,1,1,1], 'SAME')
    output = tf.nn.relu(output)
    output = tf.nn.conv2d(output, weights[1], [1,2,2,1], 'SAME')
    output = tf.nn.relu(output)
    output = tf.nn.conv2d(output, weights[2], [1,2,2,1], 'SAME')
    output = tf.nn.relu(output)
    output = tf.reshape(output, [-1,25*25*128])
    output = tf.matmul(output, weights[3])
    output = tf.nn.relu(output)
    output = tf.matmul(output, weights[4])
    output = tf.reduce_sum(output)
    return output

prediction = CNN(tf.cast(x_p, tf.float32), weights)
saver = tf.train.Saver()
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    saver.restore(sess, 'saved/model.ckpt')
    for t in range(5):
        print(t+1)
        time.sleep(1)

    while True:
        x = ImageGrab.grab()
        x = x.resize((100,100))
        x = np.asarray(x)
        output = sess.run(prediction, feed_dict={x_p:[x]})
        print(output)
        if output < 0.5 and output > 1.5:
            pag.keyDown('W')
            pag.keyUp('S')
            pag.keyUp('D')
            pag.keyUp('A')
        elif output < 1.5 and output > 2.5:
            pag.keyUp('W')
            pag.keyDown('S')
            pag.keyUp('D')
            pag.keyUp('A')
        elif output < 2.5 and output > 3.5:
            pag.keyDown('W')
            pag.keyUp('S')
            pag.keyDown('D')
            pag.keyUp('A')
        elif output < 3.5 and output > 4.5:
            pag.keyDown('W')
            pag.keyUp('S')
            pag.keyUp('D')
            pag.keyDown('A')
        elif output < 4.5 and output > 5.5:
            pag.keyDown('W')
            pag.keyUp('S')
            pag.keyDown('D')
            pag.keyUp('A')
        elif output < 5.5 and output > 6.5:
            pag.keyDown('W')
            pag.keyUp('S')
            pag.keyUp('D')
            pag.keyDown('A')
        elif output < 6.5 and output > 7.5:
            pag.keyUp('W')
            pag.keyDown('S')
            pag.keyDown('D')
            pag.keyUp('A')
        elif output < 7.5 and output > 8.5:
            pag.keyUp('W')
            pag.keyDown('S')
            pag.keyUp('D')
            pag.keyDown('A')
        else:
            pag.keyUp('W')
            pag.keyUp('S')
            pag.keyUp('D')
            pag.keyUp('A')

But the problem is that only the else statement fires, even though output has (for example) the value 1.3. I was able to find out, that the problem is caused by the variable output in the if statements, but I didn't manage to fix the problem. Can some of you please help me?

Aucun commentaire:

Enregistrer un commentaire