R3MTransform¶
- class torchrl.envs.transforms.R3MTransform(*args, **kwargs)[源代码]¶
R3M Transform 类。
R3M 提供了预训练的 ResNet 权重,旨在促进机器人任务的视觉嵌入。这些模型使用 Ego4d 进行训练。
- 参阅论文
- R3M: A Universal Visual Representation for Robot Manipulation (Suraj Nair,
Aravind Rajeswaran, Vikash Kumar, Chelsea Finn, Abhinav Gupta) https://arxiv.org/abs/2203.12601
R3MTransform 以惰性方式创建:对象仅在查询属性(spec 或 forward 方法)时进行初始化。原因是
_init()
方法需要访问父环境的某些属性(如果有)。通过使类惰性,我们可以确保以下代码片段按预期工作。示例
>>> transform = R3MTransform("resnet50", in_keys=["pixels"]) >>> env.append_transform(transform) >>> # the forward method will first call _init which will look at env.observation_spec >>> env.reset()
- 参数:
model_name (str) – resnet50、resnet34 或 resnet18 中的一个
in_keys (list of str) – 输入键列表。如果留空,则假定为“pixels”键。
out_keys (list of str, optional) – 输出键列表。如果留空,则假定为“r3m_vec”。
size (int, optional) – 输入到 resnet 的图像大小。默认为 244。
stack_images (bool, optional) – 如果为 False,则
in_keys
参数中提供的图像将被单独处理,并且每个图像将在输出 tensordict 中拥有一个单独的条目。默认为True
。download (bool, torchvision Weights config 或 相应的字符串) – 如果为
True
,将使用 torch.hub 下载 API 下载权重(即,权重将被缓存以供将来使用)。这些权重是 R3M 出版物中的原始权重。如果需要 torchvision 权重,可以通过以下两种方式获取:download=ResNet50_Weights.IMAGENET1K_V1
或download="IMAGENET1K_V1"
,其中ResNet50_Weights
可以通过from torchvision.models import resnet50, ResNet50_Weights
导入。默认为 False。download_path (str, optional) – 要下载模型的路径。默认为 None(由 torch.hub utils 确定的缓存路径)。
tensor_pixels_keys (list of str, optional) – 可选地,可以保留原始图像(从 env 收集)在输出 tensordict 中。如果未提供值,则不会收集。
- to(dest: DEVICE_TYPING | torch.dtype)[源代码]¶
移动和/或转换参数和缓冲区。
这可以这样调用
- to(device=None, dtype=None, non_blocking=False)[源代码]
- to(dtype, non_blocking=False)[源代码]
- to(tensor, non_blocking=False)[源代码]
- to(memory_format=torch.channels_last)[源代码]
其签名类似于
torch.Tensor.to()
,但只接受浮点或复数dtype
。此外,此方法只会将浮点或复数参数和缓冲区转换为dtype
(如果已给出)。整数参数和缓冲区将移动到device
(如果已给出),但dtype
保持不变。当设置non_blocking
时,它会尝试与主机异步进行转换/移动(如果可能),例如将具有固定内存的 CPU Tensor 移动到 CUDA 设备。有关示例,请参阅下文。
注意
此方法就地修改模块。
- 参数:
device (
torch.device
) – the desired device of the parameters and buffers in this module – 此模块中参数和缓冲区的目标设备。dtype (
torch.dtype
) – the desired floating point or complex dtype of the parameters and buffers in this module – 此模块中参数和缓冲区的目标浮点数或复数 dtype。tensor (torch.Tensor) – Tensor whose dtype and device are the desired dtype and device for all parameters and buffers in this module – 其 dtype 和 device 是此模块中所有参数和缓冲区的目标 dtype 和 device 的 Tensor。
memory_format (
torch.memory_format
) – the desired memory format for 4D parameters and buffers in this module (keyword only argument) – 此模块中 4D 参数和缓冲区的目标内存格式(仅关键字参数)。
- 返回:
self
- 返回类型:
模块
示例
>>> # xdoctest: +IGNORE_WANT("non-deterministic") >>> linear = nn.Linear(2, 2) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]]) >>> linear.to(torch.double) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]], dtype=torch.float64) >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1) >>> gpu1 = torch.device("cuda:1") >>> linear.to(gpu1, dtype=torch.half, non_blocking=True) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1') >>> cpu = torch.device("cpu") >>> linear.to(cpu) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16) >>> linear = nn.Linear(2, 2, bias=None).to(torch.cdouble) >>> linear.weight Parameter containing: tensor([[ 0.3741+0.j, 0.2382+0.j], [ 0.5593+0.j, -0.4443+0.j]], dtype=torch.complex128) >>> linear(torch.ones(3, 2, dtype=torch.cdouble)) tensor([[0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j]], dtype=torch.complex128)