使用Python线程在TensorFlow NN中进行预测

2024-07-07 06:02:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要根据套接字从客户端发送到服务器的命令进行预测。服务器进行预测(TensorFlow神经网络),并将其发送给另一个运行模拟机器人的软件。因此,在服务器接收数据时进行预测。我创建了一个线程来计算预测,另一个线程发送给机器人软件。我一直在尝试下面的代码,但它不起作用

def pioneer():
sensor_val = np.array([])
for x in range(1, 16 + 1):
    errorCode, detectionState, detectedPoint, detectedObjectHandle, detectedSurfaceNormalVector = sim.simxReadProximitySensor(
        clientID, sensor_h[x - 1], sim.simx_opmode_buffer)
    sensor_val = np.append(sensor_val, detectionState)  # get list of values
sensor[:, :] = sensor_val.reshape(1, 16)
errorCode = sim.simxSetJointTargetVelocity(clientID, left_motor_handle, pred[:, 0],
                                            sim.simx_opmode_streaming)
errorCode = sim.simxSetJointTargetVelocity(clientID, right_motor_handle, pred[:, 1],
                                            sim.simx_opmode_streaming)

def prediction():
with tf.compat.v1.Session() as sess:
    sess.run(init)
    loader = tf.compat.v1.train.Saver()
    loader.restore(sess, "output_pioneer")
    pred = sess.run(Y_hat, feed_dict={X: direction_data})
    print(pred)

try:
while True:
    print("Aguardando conexao")
    con, client = tcp.accept()
    print("Conectado por", client)
    sim.simxStartSimulation(clientID, sim.simx_opmode_blocking)
    t1 = threading.Thread(target=pioneer())
    t2 = threading.Thread(target=prediction())
    t1.start()
    t2.start()
    while True:
        msg = con.recv(4096)
        if not msg: break
        direction_data = np.array(pickle.loads(msg)).reshape(1,4)
        print(direction_data)
    print("Finalizando conexão do cliente", client)
    con.close()
    sim.simxStopSimulation(clientID, sim.simx_opmode_blocking)
except KeyboardInterrupt:
pass

如果你能帮助我,我将非常感激。谢谢大家!


Tags: datanpvalsimsensorconsessprint