Unity-Technologies / ml-agents

The Unity Machine Learning Agents Toolkit (ML-Agents) is an open-source project that enables games and simulations to serve as environments for training intelligent agents using deep reinforcement learning and imitation learning.
https://unity.com/products/machine-learning-agents
Other
16.93k stars 4.14k forks source link

IndexError on Barracuda Convert #2616

Closed gomes-eric closed 4 years ago

gomes-eric commented 4 years ago

Hi everyone,

I come here to ask for help regarding an IndexError exception when I try to convert an OpenAI Baselines PPO2 Model to Barracuda Model. I don`t know if the ML-Agents support this, but I hope so.

Train script:

from gym_unity.envs import UnityEnv
from baselines.common.vec_env.subproc_vec_env import SubprocVecEnv
from baselines.bench import Monitor
from baselines import logger
from tensorflow.python.tools import freeze_graph
from mlagents.trainers import tensorflow_to_barracuda as tf2bc
import baselines.ppo2.ppo2 as ppo2
import tensorflow as tf

import os

try:
    from mpi4py import MPI
except ImportError:
    MPI = None

ENV_PATH = 'D:\\Unknown\\Projects\\Unity Projects\\Deep Learning Boss\\Build\\Deep Learning Boss'
SAVE_PATH = 'D:\\Unknown\\Projects\\Python\\Deep-Learning-Boss\\'

def make_unity_envs(env_directory, num_env, visual, graphics, start_index=0):
    def make_env(rank):
        def _thunk():
            env = UnityEnv(env_directory, rank, use_visual=visual, uint8_visual=visual, no_graphics=not graphics)
            env = Monitor(env, logger.get_dir() and os.path.join(logger.get_dir(), str(rank)))

            return env

        return _thunk

    return SubprocVecEnv([make_env(i + start_index) for i in range(num_env)])

def main():
    env = make_unity_envs(ENV_PATH, 1, True, False)
    model = ppo2.learn(network="mlp", env=env, total_timesteps=1000, lr=1e-3)
    saver = tf.train.Saver()

    saver.save(model.sess, SAVE_PATH + 'model.cptk')
    tf.train.write_graph(model.sess.graph, SAVE_PATH, 'raw_graph.pb', as_text=False)

    target_nodes = ",".join([node.name for node in model.sess.graph.as_graph_def().node])
    freeze_graph.freeze_graph(
        input_graph=SAVE_PATH + "raw_graph.pb",
        input_binary=True,
        input_checkpoint=SAVE_PATH + 'model.cptk',
        output_node_names=target_nodes,
        output_graph=SAVE_PATH + 'frozen_graph.pb',
        clear_devices=True,
        initializer_nodes="",
        input_saver="",
        restore_op_name="save/restore_all",
        filename_tensor_name="save/Const:0",
    )
    tf2bc.convert(SAVE_PATH + 'frozen_graph.pb', SAVE_PATH + 'model.nn')

if __name__ == '__main__':
    main()

Error:

Traceback (most recent call last):
  File "D:/Unknown/Projects/Python/Deep-Learning-Boss/train.py", line 59, in <module>
    main()
  File "D:/Unknown/Projects/Python/Deep-Learning-Boss/train.py", line 55, in main
    tf2bc.convert(SAVE_PATH + 'frozen_graph.pb', SAVE_PATH + 'model.nn')
  File "C:\Users\Unknown\Anaconda3\envs\ml-agents\lib\site-packages\mlagents\trainers\tensorflow_to_barracuda.py", line 1552, in convert
    i_model, args
  File "C:\Users\Unknown\Anaconda3\envs\ml-agents\lib\site-packages\mlagents\trainers\tensorflow_to_barracuda.py", line 1380, in process_model
    nodes, var_tensors, const_tensors, o_context
  File "C:\Users\Unknown\Anaconda3\envs\ml-agents\lib\site-packages\mlagents\trainers\tensorflow_to_barracuda.py", line 480, in <lambda>
    inputs[0],
IndexError: list index out of range

Thanks in advance!

chriselion commented 4 years ago

Hi @Unknown-Technologies, We can only support graphs that were generated by ML-Agents. @mantasp from the Barracuda team might be able to help you with this though.

gomes-eric commented 4 years ago

Hi @chriselion,

Thanks for your answer. Is there any way to do the training with Baselines and use the Tensor Model in Unity?

For curiosity, what the best algorithm Baselines PPO2 or Unity PPO? I'm undecided about which one to use.

Thanks in advance!

gomes-eric commented 4 years ago

@chriselion @mantasp, please, help me!

mantasp commented 4 years ago

@Unknown-Technologies any chance you could share your frozen_graph.pb file?

gomes-eric commented 4 years ago

@mantasp sure!

Link: https://drive.google.com/open?id=1kMw4_NH0xOMK7brBFf2L93el1QtNXLQA

mantasp commented 4 years ago

@Unknown-Technologies unfortunately this graph include too many nodes that we do not support. It seems that during the freezing you requested to save all the nodes:

target_nodes = ",".join([node.name for node in model.sess.graph.as_graph_def().node])

could you try freezing graph only with output nodes that are actually needed as output of your model?

gomes-eric commented 4 years ago

@mantasp, thanks for your help, but I see that the ML-Agents won't supply my needs. So I decided to create my own Unity library for Deep Learning training. Soon I will post the code to the community.

github-actions[bot] commented 3 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.