快捷方式

R3MTransform

class torchrl.envs.transforms.R3MTransform(*args, **kwargs)[來源]

R3M 轉換類別。

R3M 提供預訓練的 ResNet 權重,旨在促進機器人任務的視覺嵌入。這些模型是使用 Ego4d 訓練的。

請參閱論文
R3M:用於機器人操作的通用視覺表示(Suraj Nair,

Aravind Rajeswaran, Vikash Kumar, Chelsea Finn, Abhinav Gupta) https://arxiv.org/abs/2203.12601

R3MTransform 是以延遲方式建立的:只有在查詢屬性(規格或 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 or corresponding string) – 如果為 True,則將使用 torch.hub download 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) – (選擇性) 可以將原始影像(從環境收集)保留在輸出 tensordict 中。如果未提供任何值,則不會收集此值。

to(dest: Union[device, str, int, 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 張量移動到 CUDA 裝置。

請參閱以下範例。

注意

此方法會就地修改模組。

參數:
  • device (torch.device) – 此模組中參數和緩衝區的目標裝置

  • dtype (torch.dtype) – 此模組中參數和緩衝區的目標浮點數或複數資料類型 (dtype)

  • tensor (torch.Tensor) – 張量,其資料類型 (dtype) 和裝置是此模組中所有參數和緩衝區的目標資料類型 (dtype) 和裝置

  • memory_format (torch.memory_format) – 此模組中 4D 參數和緩衝區的目標記憶體格式(僅限關鍵字參數)

傳回:

self

傳回型別:

Module

範例

>>> # 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 的完整開發人員文件

檢視文件

教學

取得初學者和進階開發人員的深入教學課程

檢視教學

資源

尋找開發資源並獲得問題解答

檢視資源