快捷方式

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_V1download="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)

文件

訪問全面的 PyTorch 開發者文件

檢視文件

教程

為初學者和高階開發者提供深入的教程

檢視教程

資源

查詢開發資源並讓您的問題得到解答

檢視資源