VisualNavigation--3D 环境 ai2thor 例子

本博客旨在提供开始使用AI2-THOR的基本入门示例。

1. MoveAhead

一个简单的示例,将智能体向前移动一步并返回相应的图像和元数据。

from ai2thor.controller import Controller

# Kitchens: FloorPlan1 - FloorPlan30
# Living rooms: FloorPlan201 - FloorPlan230
# Bedrooms: FloorPlan301 - FloorPlan330
# Bathrooms: FloorPLan401 - FloorPlan430

controller = Controller(scene='FloorPlan28', gridSize=0.25)

event = controller.step(action='MoveAhead')

# Numpy Array - shape (width, height, channels), channels are in RGB order
event.frame

# Numpy Array in BGR order suitable for use with OpenCV
event.cv2image

# current metadata dictionary that includes the state of the scene
event.metadata

# shuts down the controller
controller.stop()

2. Context Managers

初始化controller的另一种方法是使用上下文管理器。这里,前面的MoveAhead示例可以写成:

from ai2thor.controller import Controller

with Controller(scene='FloorPlan28', gridSize=0.25) as c:
    event =  c.step(action='MoveAhead')
    event.frame
    event.cv2image
    event.metadata

如果with命令内部存在错误或with命令内部的所有内容都已执行,则controller将立即停止。

3. Object interaction

我们的目标是拿起杯子,然后打开微波炉,然后将杯子放入微波炉中。 要拾取物体,智能体必须首先导航到存在可拾取/可见物体的区域。 通常,应通过一系列MoveAhead,RotateLeft,RotateRight命令完成此操作。 在这里,我们将直接移动到存在杯子的已知位置。

from ai2thor.controller import Controller

controller = Controller(scene='FloorPlan28', gridSize=0.25)

# change starting locations
controller.step(action='Teleport', x=-2.5, y=0.900998235, z=-3.0)
controller.step(action='LookDown')
event = controller.step(action='Rotate', rotation=180)

# in FloorPlan28, the agent should now be looking at a mug
for o in event.metadata['objects']:
    if o['visible'] and o['pickupable'] and o['objectType'] == 'Mug':
        # pick up the mug
        event = controller.step(action='PickupObject',
                                objectId=o['objectId'],
                                raise_for_failure=True)
        mug_object_id = o['objectId']
        break

# the agent now has the Mug in its inventory
# to put it into the Microwave, we need to open the microwave first

# move to the microwave
event = controller.step(action='LookUp')
event = controller.step(action='RotateLeft')
event = controller.step(action='MoveLeft', moveMagnitude=0.25 * 4)
event = controller.step(action='MoveAhead', moveMagnitude=0.25 * 6)

# the agent should now be looking at the microwave
for o in event.metadata['objects']:
    if o['visible'] and o['openable'] and o['objectType'] == 'Microwave':
        # open the microwave
        event = controller.step(action='OpenObject',
                                objectId=o['objectId'],
                                raise_for_failure=True)
        receptacle_object_id = o['objectId']
        break

# put the object in the microwave
event = controller.step(
    action='PutObject',
    receptacleObjectId=receptacle_object_id,
    objectId=mug_object_id,
    raise_for_failure=True)

# close the microwave
event = controller.step(
    action='CloseObject',
    objectId=receptacle_object_id,
    raise_for_failure=True)

4. Multi-agent

本示例说明如何在多智能体设置中运行AI2-THOR。

from ai2thor.controller import Controller

controller = Controller()

# agentCount specifies the number of agents in a scene
event = controller.step(action='Initialize', gridSize=0.25, agentCount=2)

# print out agentIds
for e in event.events:
    print(e.metadata['agentId'])

# move the second agent ahead, agents are 0-indexed
multi_agent_event = controller.step(action='MoveAhead', agentId=1)

5. Multi-threaded

本示例说明如何以多线程方式运行智能体的多个实例。

import threading
import time
from ai2thor.controller import Controller

thread_count = 8
episodes = 100


def run():
    controller = Controller(scene='FloorPlan1', gridSize=0.25)
    for _ in range(episodes):
        t_start = time.time()
        controller.reset('FloorPlan1')
        print('init time', time.time() - t_start)
        t_start_total = time.time()
        for _ in range(10):
            controller.step(action='MoveAhead')
            controller.step(action='RotateRight')
        total_time = time.time() - t_start_total
        print('total time', total_time, 20 / total_time, 'fps')


threads = [threading.Thread(target=run) for _ in range(thread_count)]
for t in threads:
    t.daemon = True
    t.start()
    time.sleep(1)

for t in threads:
    # calling join() in a loop/timeout to allow for Python 2.7
    # to be interrupted with SIGINT
    while t.isAlive():
        t.join(1)

print('done')
发布了66 篇原创文章 · 获赞 101 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/u010705932/article/details/104499554