AI2THOR 2.1.0使用教程

作者 : admin 本文共5078个字,预计阅读时间需要13分钟 发布时间: 2024-06-10 共2人阅读

一、安装和入门

1.1 AI2-THOR使用要求

  • 操作系统: Mac OS X 10.9+, Ubuntu 14.04+
  • 显卡:DX9(着色器型号 3.0)或 DX11,功能级别为 9.3。
  • CPU:支持 SSE2 指令集。
  • Python 2.7 或 Python 3.5+
  • Linux 用户:启用了 GLX 模块的 X 服务器

1.2 使用 pip 安装

  您可以使用 pip 安装 AI2-THOR。创建 Python 2.7/3.5/3.6 虚拟环境,

conda create -n ai2thor_env python=3.6

  然后进入虚拟环境

conda activate ai2thor_env

  然后安装ai2thor

pip install ai2thor

  在运行以下代码之前,请确保正在运行带有 OpenGL 的 X 服务器,并且已为您的显卡安装了 OpenGL 扩展。

import ai2thor.controller
controller = ai2thor.controller.Controller()
controller.start()

  第一次使用控制器时,包含 3D 场景的游戏环境将被下载到 $HOME/.ai2thor。二进制文件的大小约为 500MB。

二、概念

  • Agent: 胶囊形状的实体,可在场景中导航并与物体互动。

  • Scene: AI2-THOR 中的场景代表一个虚拟房间,代理可以在其中导航并与之互动。有 4 个场景类别,每个类别有 30 个独特的场景: 厨房、起居室、卧室、浴室。

  • Action: 让代理在场景中执行的离散命令(例如,向前移动、向右旋转、拾取对象)

  • Sim Object: 可以与代理互动的对象。根据 “对象类型”(Object Type)所定义的 “承受能力”(affordanced),”对象 “具有一系列交互功能。

  • Object Visibility: 当一个对象满足三个条件时,它就被认为是可见的: 它必须位于摄像机的视口内,必须与 Agent 的中心距离在一个阈值范围内(默认值:1.5 米),并且从摄像机发射的光线必须在不首先击中其他障碍物的情况下击中该对象。请注意,图像中渲染的物体并不总是对 Agent 可见。例如,1.5 米阈值之外的物体可以在图像中看到,但对 Agent 来说将被报告为不可见。

  • Object Interactability: 如果一个物体被标记为可见,并且没有任何其他物体遮挡,那么这个物体就是可交互的。大多数对象只要也是可见的,就是可交互的,但有些对象是透明的,这可能会导致对象被报告为透过它们是可见的。例如,玻璃淋浴门后面有一个海绵物体。玻璃门将被标记为 “可见 “和 “可交互”,但海绵仅为 “可见”。如果试图与海绵进行交互,就会出现错误,因为无法通过玻璃门接触到海绵,只能看到海绵。

  • Receptacle: 一种可以容纳另一个物体的物体。例如 桌面、杯子、沙发、床、桌子、碗等。有些容器无法在场景中移动,它们大多是无法移动的大型物体(台面、水槽等)。有些收纳盒还可以打开和关闭(微波炉、橱柜、抽屉等),而有些收纳盒还可以被代理拿起来移动(盘子、碗、盒子等)。

三、示例

  我们提供了一些示例来展示如何使用 AI2-THOR。

3.1 简单示例

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

import ai2thor.controller
controller = ai2thor.controller.Controller()
controller.start()

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

controller.reset('FloorPlan28')
controller.step(dict(action='Initialize', gridSize=0.25))

event = controller.step(dict(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

3.2 调用复杂操作的示例

  拿起杯子, 打开微波炉, 把杯子放在微波炉里

  要拾取对象,代理必须首先导航到有可拾取/可见对象的区域。通常,它应该通过一系列 MoveAhead、RotateLeft、RotateRight 命令来完成。在这里,我们直接传送到一个已知的位置,那里有一个杯子。

import ai2thor.controller
controller = ai2thor.controller.Controller()
controller.start()
controller.reset('FloorPlan28')
controller.step(dict(action='Initialize', gridSize=0.25))
controller.step(dict(action='Teleport', x=-2.5, y=0.900998235, z=-3.0))
controller.step(dict(action='LookDown'))
event = controller.step(dict(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':
event = controller.step(dict(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
event = controller.step(dict(action='LookUp'))
event = controller.step(dict(action='RotateLeft'))
event = controller.step(dict(action='MoveLeft'))
event = controller.step(dict(action='MoveLeft'))
event = controller.step(dict(action='MoveLeft'))
event = controller.step(dict(action='MoveLeft'))
event = controller.step(dict(action='MoveAhead'))
event = controller.step(dict(action='MoveAhead'))
event = controller.step(dict(action='MoveAhead'))
event = controller.step(dict(action='MoveAhead'))
event = controller.step(dict(action='MoveAhead'))
event = controller.step(dict(action='MoveAhead'))
for o in event.metadata['objects']:
if o['visible'] and o['openable'] and o['objectType'] == 'Microwave':
event = controller.step(dict(action='OpenObject', objectId=o['objectId']), raise_for_failure=True)
receptacle_object_id = o['objectId']
break
event = controller.step(dict(
action='PutObject',
receptacleObjectId=receptacle_object_id,
objectId=mug_object_id), raise_for_failure=True)
# close the microwave
event = controller.step(dict(
action='CloseObject',
objectId=receptacle_object_id), raise_for_failure=True)

3.3 多智能体示例

  此示例说明如何在多代理设置中运行 AI2-THOR。

import ai2thor.controller
controller = ai2thor.controller.Controller()
controller.start()
# agentCount specifies the number of agents in a scene
multi_agent_event = controller.step(dict(action='Initialize', gridSize=0.25, agentCount=2))
# print out agentIds
for e in mult_agent_event.events:
print(e.metadata['agentId'])
# move the second agent ahead, agents are 0-indexed
multi_agent_event = controller.step(dict(action='MoveAhead', agentId=1)) 

3.4 多线程示例

  此示例演示如何以多线程方式运行代理的多个实例。

mport threading
import time
import ai2thor.controller
thread_count = 8
def run():
controller = ai2thor.controller.Controller()
controller.start()
# 100 is an arbritary number
for _ in range(100):
t_start = time.time()
controller.reset('FloorPlan1')
controller.step({'action' : 'Initialize', 'gridSize' : 0.25})
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')

3.5 一个示例结果

AI2THOR 2.1.0使用教程插图

参考文献

[1] http://allenai.github.io/ai2thor-v2.1.0-documentation/installation#

[2] http://ai2thor.allenai.org/

本站无任何商业行为
个人在线分享 » AI2THOR 2.1.0使用教程
E-->