Transformers 文件
ZoeDepth
並獲得增強的文件體驗
開始使用
ZoeDepth
ZoeDepth 是一個深度估算模型,它結合了相對深度估算(物體彼此之間的距離)和度量深度估算(在度量尺度上精確測量深度)的泛化效能,兩者都來自單張影像。它在 12 個數據集上使用相對深度進行預訓練,並在 2 個數據集(NYU Depth v2 和 KITTI)上進行度量準確性訓練。為每個域使用一個帶有度量 bin 模組的輕量級頭部,在推理過程中,它透過一個潛在分類器自動為每個輸入影像選擇合適的頭部。

你可以在 Intel 組織下找到所有原始的 ZoeDepth 檢查點。
下面的示例演示瞭如何使用 Pipeline 或 AutoModel 類來估算深度。
import requests
import torch
from transformers import pipeline
from PIL import Image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
image = Image.open(requests.get(url, stream=True).raw)
pipeline = pipeline(
task="depth-estimation",
model="Intel/zoedepth-nyu-kitti",
torch_dtype=torch.float16,
device=0
)
results = pipeline(image)
results["depth"]
注意
- 在原始實現中,ZoeDepth 對原始影像和翻轉影像都執行推理,並對結果取平均值。`post_process_depth_estimation` 函式透過將翻轉後的輸出傳遞給可選的 `outputs_flipped` 引數來處理此問題,如下所示。
with torch.no_grad(): outputs = model(pixel_values) outputs_flipped = model(pixel_values=torch.flip(inputs.pixel_values, dims=[3])) post_processed_output = image_processor.post_process_depth_estimation( outputs, source_sizes=[(image.height, image.width)], outputs_flipped=outputs_flipped, )
資源
- 有關推理示例,請參閱此 notebook。
ZoeDepthConfig
class transformers.ZoeDepthConfig
< 原始碼 >( backbone_config = None backbone = None use_pretrained_backbone = False backbone_kwargs = None hidden_act = 'gelu' initializer_range = 0.02 batch_norm_eps = 1e-05 readout_type = 'project' reassemble_factors = [4, 2, 1, 0.5] neck_hidden_sizes = [96, 192, 384, 768] fusion_hidden_size = 256 head_in_index = -1 use_batch_norm_in_fusion_residual = False use_bias_in_fusion_residual = None num_relative_features = 32 add_projection = False bottleneck_features = 256 num_attractors = [16, 8, 4, 1] bin_embedding_dim = 128 attractor_alpha = 1000 attractor_gamma = 2 attractor_kind = 'mean' min_temp = 0.0212 max_temp = 50.0 bin_centers_type = 'softplus' bin_configurations = [{'n_bins': 64, 'min_depth': 0.001, 'max_depth': 10.0}] num_patch_transformer_layers = None patch_transformer_hidden_size = None patch_transformer_intermediate_size = None patch_transformer_num_attention_heads = None **kwargs )
引數
- backbone_config (
Union[dict[str, Any], PretrainedConfig]
, 可選, 預設為BeitConfig()
) — 主幹模型的配置。 - backbone (
str
, 可選) — 當backbone_config
為None
時使用的主幹名稱。如果 `use_pretrained_backbone` 為True
,將從 timm 或 transformers 庫載入相應的預訓練權重。如果use_pretrained_backbone
為False
,將載入主幹的配置並用其初始化主幹的隨機權重。 - use_pretrained_backbone (
bool
, 可選, 預設為False
) — 是否為主幹使用預訓練權重。 - backbone_kwargs (
dict
, 可選) — 在從檢查點載入時傳遞給 AutoBackbone 的關鍵字引數,例如 `{'out_indices': (0, 1, 2, 3)}`。如果設定了 `backbone_config`,則不能指定此項。 - hidden_act (
str
orfunction
, 可選, 預設為"gelu"
) — 編碼器和池化層中的非線性啟用函式(函式或字串)。如果是字串,支援"gelu"
、"relu"
、"selu"
和"gelu_new"
。 - initializer_range (
float
, 可選, 預設為 0.02) — 用於初始化所有權重矩陣的 truncated_normal_initializer 的標準差。 - batch_norm_eps (
float
, 可選, 預設為 1e-05) — 批次歸一化層使用的 epsilon。 - readout_type (
str
, 可選, 預設為"project"
) — 在處理 ViT 主幹中間隱藏狀態的讀出令牌(CLS 令牌)時使用的讀出型別。可以是 ["ignore"
、"add"
、"project"
] 中的一個。- “ignore” 簡單地忽略 CLS 令牌。
- “add” 透過新增表示將 CLS 令牌的資訊傳遞給所有其他令牌。
- “project” 透過將讀出連線到所有其他令牌,然後使用線性層和 GELU 非線性將表示投影到原始特徵維度 D,從而將資訊傳遞給其他令牌。
- reassemble_factors (
list[int]
, 可選, 預設為[4, 2, 1, 0.5]
) — 重組層的上/下采樣因子。 - neck_hidden_sizes (
list[str]
, 可選, 預設為[96, 192, 384, 768]
) — 用於主幹特徵圖投影的隱藏大小。 - fusion_hidden_size (
int
, 可選, 預設為 256) — 融合前的通道數。 - head_in_index (
int
, 可選, 預設為 -1) — 在頭部中使用的特徵索引。 - use_batch_norm_in_fusion_residual (
bool
, 可選, 預設為False
) — 是否在融合塊的預啟用殘差單元中使用批次歸一化。 - use_bias_in_fusion_residual (
bool
, 可選, 預設為True
) — 是否在融合塊的預啟用殘差單元中使用偏置。 - num_relative_features (
int
, 可選, 預設為 32) — 相對深度估計頭中使用的特徵數量。 - add_projection (
bool
, 可選, 預設為False
) — 是否在深度估計頭之前新增投影層。 - bottleneck_features (
int
, 可選, 預設為 256) — 瓶頸層中的特徵數量。 - num_attractors (
list[int], *可選*, 預設為
[16, 8, 4, 1]`) — 每個階段中使用的吸引子數量。 - bin_embedding_dim (
int
, 可選, 預設為 128) — bin 嵌入的維度。 - attractor_alpha (
int
, 可選, 預設為 1000) — 在吸引子中使用的 alpha 值。 - attractor_gamma (
int
, 可選, 預設為 2) — 在吸引子中使用的 gamma 值。 - attractor_kind (
str
, 可選, 預設為"mean"
) — 使用的吸引子型別。可以是 ["mean"
,"sum"
] 中的一個。 - min_temp (
float
, 可選, 預設為 0.0212) — 考慮的最小溫度值。 - max_temp (
float
, 可選, 預設為 50.0) — 考慮的最大溫度值。 - bin_centers_type (
str
, 可選, 預設為"softplus"
) — 用於 bin 中心的啟用型別。可以是“normed”或“softplus”。對於“normed” bin 中心,應用線性歸一化技巧。這導致有界的 bin 中心。對於“softplus”,使用 softplus 啟用,因此是無界的。 - bin_configurations (
list[dict]
, 可選, 預設為[{'n_bins' -- 64, 'min_depth': 0.001, 'max_depth': 10.0}]
):每個 bin 頭部的配置。每個配置應包含以下鍵:- name (
str
):bin 頭部的名稱——僅在有多個 bin 配置時需要。 - `n_bins` (
int
):要使用的 bin 數量。 - `min_depth` (
float
):要考慮的最小深度值。 - `max_depth` (
float
):要考慮的最大深度值。如果只傳遞一個配置,模型將使用具有指定配置的單個頭部。如果傳遞多個配置,模型將使用具有指定配置的多個頭部。
- name (
- num_patch_transformer_layers (
int
, 可選) — 補丁轉換器中使用的 transformer 層數。僅在有多個 bin 配置時使用。 - patch_transformer_hidden_size (
int
, 可選) — 補丁轉換器中使用的隱藏大小。僅在有多個 bin 配置時使用。 - patch_transformer_intermediate_size (
int
, 可選) — 補丁轉換器中使用的中間大小。僅在有多個 bin 配置時使用。 - patch_transformer_num_attention_heads (
int
, 可選) — 補丁轉換器中使用的注意力頭數量。僅在有多個 bin 配置時使用。
這是一個配置類,用於儲存 ZoeDepthForDepthEstimation 的配置。它用於根據指定的引數例項化一個 ZoeDepth 模型,定義模型架構。使用預設值例項化配置將產生與 ZoeDepth Intel/zoedepth-nyu 架構類似的配置。
配置物件繼承自 PretrainedConfig,可用於控制模型輸出。有關更多資訊,請閱讀 PretrainedConfig 的文件。
示例
>>> from transformers import ZoeDepthConfig, ZoeDepthForDepthEstimation
>>> # Initializing a ZoeDepth zoedepth-large style configuration
>>> configuration = ZoeDepthConfig()
>>> # Initializing a model from the zoedepth-large style configuration
>>> model = ZoeDepthForDepthEstimation(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
ZoeDepthImageProcessor
class transformers.ZoeDepthImageProcessor
< 原始碼 >( do_pad: bool = True do_rescale: bool = True rescale_factor: typing.Union[int, float] = 0.00392156862745098 do_normalize: bool = True image_mean: typing.Union[float, list[float], NoneType] = None image_std: typing.Union[float, list[float], NoneType] = None do_resize: bool = True size: typing.Optional[dict[str, int]] = None resample: Resampling = <Resampling.BILINEAR: 2> keep_aspect_ratio: bool = True ensure_multiple_of: int = 32 **kwargs )
引數
- do_pad (
bool
, 可選, 預設為True
) — 是否對輸入進行填充。 - do_rescale (
bool
, 可選, 預設為True
) — 是否透過指定的縮放比例rescale_factor
重新縮放影像。可在preprocess
中透過do_rescale
引數覆蓋。 - rescale_factor (
int
或float
, 可選, 預設為1/255
) — 如果重新縮放影像,則使用此縮放因子。可在preprocess
中透過rescale_factor
引數覆蓋。 - do_normalize (
bool
, 可選, 預設為True
) — 是否對影像進行歸一化。可在preprocess
方法中透過do_normalize
引數覆蓋。 - image_mean (
float
或list[float]
, 可選, 預設為IMAGENET_STANDARD_MEAN
) — 如果對影像進行歸一化,則使用此均值。這是一個浮點數或浮點數列表,其長度等於影像的通道數。可在preprocess
方法中透過image_mean
引數覆蓋。 - image_std (
float
或list[float]
, 可選, 預設為IMAGENET_STANDARD_STD
) — 如果對影像進行歸一化,則使用此標準差。這是一個浮點數或浮點數列表,其長度等於影像的通道數。可在preprocess
方法中透過image_std
引數覆蓋。 - do_resize (
bool
, 可選, 預設為True
) — 是否調整影像的 (height, width) 尺寸。可在preprocess
中透過do_resize
引數覆蓋。 - size (
dict[str, int]
可選, 預設為{"height" -- 384, "width": 512}
): 調整大小後圖像的尺寸。如果keep_aspect_ratio
為True
,則透過選擇高度和寬度縮放因子中較小的一個,並將其用於兩個維度來調整影像大小。如果還設定了ensure_multiple_of
,則影像將進一步調整為該值的倍數。可在preprocess
中透過size
引數覆蓋。 - resample (
PILImageResampling
, 可選, 預設為Resampling.BILINEAR
) — 定義調整影像大小時使用的重取樣濾波器。可在preprocess
中透過resample
引數覆蓋。 - keep_aspect_ratio (
bool
, 可選, 預設為True
) — 如果為True
,則透過選擇高度和寬度縮放因子中較小的一個,並將其用於兩個維度來調整影像大小。這確保了影像在儘可能少地縮小的同時仍能適應期望的輸出尺寸。如果還設定了ensure_multiple_of
,則透過將高度和寬度向下取整到該值的最近倍數,進一步將影像調整為該值的倍數。可在preprocess
中透過keep_aspect_ratio
引數覆蓋。 - ensure_multiple_of (
int
, 可選, 預設為 32) — 如果do_resize
為True
,則將影像大小調整為此值的倍數。透過將高度和寬度向下取整到該值的最近倍數來實現。無論
keep_aspect_ratio
是否設定為True
,此引數都有效。可在preprocess
中透過ensure_multiple_of
引數覆蓋。
構建一個 ZoeDepth 影像處理器。
preprocess
< 來源 >( images: typing.Union[ForwardRef('PIL.Image.Image'), numpy.ndarray, ForwardRef('torch.Tensor'), list['PIL.Image.Image'], list[numpy.ndarray], list['torch.Tensor']] do_pad: typing.Optional[bool] = None do_rescale: typing.Optional[bool] = None rescale_factor: typing.Optional[float] = None do_normalize: typing.Optional[bool] = None image_mean: typing.Union[float, list[float], NoneType] = None image_std: typing.Union[float, list[float], NoneType] = None do_resize: typing.Optional[bool] = None size: typing.Optional[int] = None keep_aspect_ratio: typing.Optional[bool] = None ensure_multiple_of: typing.Optional[int] = None resample: Resampling = None return_tensors: typing.Union[transformers.utils.generic.TensorType, str, NoneType] = None data_format: ChannelDimension = <ChannelDimension.FIRST: 'channels_first'> input_data_format: typing.Union[str, transformers.image_utils.ChannelDimension, NoneType] = None )
引數
- images (
ImageInput
) — 待預處理的影像。需要單個或一批畫素值在 0 到 255 範圍內的影像。如果傳入的影像畫素值在 0 到 1 之間,請設定do_rescale=False
。 - do_pad (
bool
, 可選, 預設為self.do_pad
) — 是否對輸入影像進行填充。 - do_rescale (
bool
, 可選, 預設為self.do_rescale
) — 是否將影像值重新縮放到 [0 - 1] 之間。 - rescale_factor (
float
, 可選, 預設為self.rescale_factor
) — 如果do_rescale
設定為True
,則用於重新縮放影像的因子。 - do_normalize (
bool
, 可選, 預設為self.do_normalize
) — 是否對影像進行歸一化。 - image_mean (
float
或list[float]
, 可選, 預設為self.image_mean
) — 影像均值。 - image_std (
float
或list[float]
, 可選, 預設為self.image_std
) — 影像標準差。 - do_resize (
bool
, 可選, 預設為self.do_resize
) — 是否調整影像大小。 - size (
dict[str, int]
, 可選, 預設為self.size
) — 調整大小後圖像的尺寸。如果keep_aspect_ratio
為True
,則透過選擇高度和寬度縮放因子中較小的一個,並將其用於兩個維度來調整影像大小。如果還設定了ensure_multiple_of
,則影像將進一步調整為該值的倍數。 - keep_aspect_ratio (
bool
, 可選, 預設為self.keep_aspect_ratio
) — 如果為True
且do_resize=True
,則透過選擇高度和寬度縮放因子中較小的一個,並將其用於兩個維度來調整影像大小。這確保了影像在儘可能少地縮小的同時仍能適應期望的輸出尺寸。如果還設定了ensure_multiple_of
,則透過將高度和寬度向下取整到該值的最近倍數,進一步將影像調整為該值的倍數。 - ensure_multiple_of (
int
, 可選, 預設為self.ensure_multiple_of
) — 如果do_resize
為True
,則將影像大小調整為此值的倍數。透過將高度和寬度向下取整到該值的最近倍數來實現。無論
keep_aspect_ratio
是否設定為True
,此引數都有效。可在preprocess
中透過ensure_multiple_of
引數覆蓋。 - resample (
int
, 可選, 預設為self.resample
) — 調整影像大小時使用的重取樣濾波器。可以是PILImageResampling
列舉之一,僅在do_resize
設定為True
時生效。 - return_tensors (
str
或TensorType
, 可選) — 要返回的張量型別。可以是以下之一:- 未設定:返回一個
np.ndarray
列表。 TensorType.TENSORFLOW
或'tf'
:返回一個tf.Tensor
型別的批次。TensorType.PYTORCH
或'pt'
:返回一個torch.Tensor
型別的批次。TensorType.NUMPY
或'np'
:返回一個np.ndarray
型別的批次。TensorType.JAX
或'jax'
:返回一個jax.numpy.ndarray
型別的批次。
- 未設定:返回一個
- data_format (
ChannelDimension
或str
, 可選, 預設為ChannelDimension.FIRST
) — 輸出影像的通道維度格式。可以是以下之一:ChannelDimension.FIRST
:影像格式為 (num_channels, height, width)。ChannelDimension.LAST
:影像格式為 (height, width, num_channels)。
- input_data_format (
ChannelDimension
或str
, 可選) — 輸入影像的通道維度格式。如果未設定,將從輸入影像中推斷通道維度格式。可以是以下之一:"channels_first"
或ChannelDimension.FIRST
:影像格式為 (num_channels, height, width)。"channels_last"
或ChannelDimension.LAST
:影像格式為 (height, width, num_channels)。"none"
或ChannelDimension.NONE
:影像格式為 (height, width)。
預處理一張或一批影像。
ZoeDepthImageProcessorFast
class transformers.ZoeDepthImageProcessorFast
< 來源 >( **kwargs: typing_extensions.Unpack[transformers.models.zoedepth.image_processing_zoedepth_fast.ZoeDepthFastImageProcessorKwargs] )
構建一個快速的 Zoedepth 影像處理器。
preprocess
< 來源 >( images: typing.Union[ForwardRef('PIL.Image.Image'), numpy.ndarray, ForwardRef('torch.Tensor'), list['PIL.Image.Image'], list[numpy.ndarray], list['torch.Tensor']] **kwargs: typing_extensions.Unpack[transformers.models.zoedepth.image_processing_zoedepth_fast.ZoeDepthFastImageProcessorKwargs] ) → <class 'transformers.image_processing_base.BatchFeature'>
引數
- images (
Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list['PIL.Image.Image'], list[numpy.ndarray], list['torch.Tensor']]
) — 待預處理的影像。需要單個或一批畫素值在 0 到 255 範圍內的影像。如果傳入的影像畫素值在 0 到 1 之間,請設定do_rescale=False
。 - do_resize (
bool
, 可選) — 是否調整影像大小。 - size (
dict[str, int]
, 可選) — 描述模型的最大輸入尺寸。 - default_to_square (
bool
, 可選) — 在調整大小時,如果尺寸是整數,是否預設為方形影像。 - resample (
Union[PILImageResampling, F.InterpolationMode, NoneType]
) — 調整影像大小時使用的重取樣濾波器。可以是PILImageResampling
列舉之一。僅在do_resize
設定為True
時生效。 - do_center_crop (
bool
, 可選) — 是否對影像進行中心裁剪。 - crop_size (
dict[str, int]
, 可選) — 應用center_crop
後輸出影像的尺寸。 - do_rescale (
bool
, 可選) — 是否重新縮放影像。 - rescale_factor (
Union[int, float, NoneType]
) — 如果do_rescale
設定為True
,則用於重新縮放影像的因子。 - do_normalize (
bool
, 可選) — 是否對影像進行歸一化。 - image_mean (
Union[float, list[float], NoneType]
) — 用於歸一化的影像均值。僅在do_normalize
設定為True
時生效。 - image_std (
Union[float, list[float], NoneType]
) — 用於歸一化的影像標準差。僅在do_normalize
設定為True
時生效。 - do_convert_rgb (
bool
, 可選) — 是否將影像轉換為 RGB。 - return_tensors (
Union[str, ~utils.generic.TensorType, NoneType]
) — 如果設定為 `pt`,則返回堆疊的張量,否則返回張量列表。 - data_format (
~image_utils.ChannelDimension
, 可選) — 僅支援ChannelDimension.FIRST
。為與慢速處理器相容而新增。 - input_data_format (
Union[str, ~image_utils.ChannelDimension, NoneType]
) — 輸入影像的通道維度格式。如果未設定,將從輸入影像中推斷通道維度格式。可以是以下之一:"channels_first"
或ChannelDimension.FIRST
:影像格式為 (num_channels, height, width)。"channels_last"
或ChannelDimension.LAST
:影像格式為 (height, width, num_channels)。"none"
或ChannelDimension.NONE
:影像格式為 (height, width)。
- device (
torch.device
, 可選) — 處理影像的裝置。如果未設定,將從輸入影像中推斷裝置。 - disable_grouping (
bool
, 可選) — 是否停用按尺寸分組影像,以便單獨處理而不是分批處理。如果為 None,則如果影像在 CPU 上,則設定為 True,否則設定為 False。此選擇基於經驗觀察,詳見此處:https://github.com/huggingface/transformers/pull/38157 - do_pad (
bool
, 可選, 預設為True
) — 是否對輸入應用填充。 - keep_aspect_ratio (
bool
, 可選, 預設為True
) — 如果為True
,則透過選擇高度和寬度縮放因子中較小的一個,並將其用於兩個維度來調整影像大小。這確保了影像在縮小的同時儘可能少地失真,並且仍然能適應期望的輸出尺寸。如果同時設定了ensure_multiple_of
,影像將進一步調整大小,使其尺寸是該值的倍數,方法是將高度和寬度向下取整到最接近該值的倍數。該引數可在preprocess
中被keep_aspect_ratio
覆蓋。 - ensure_multiple_of (
int
, 可選, 預設為 32) — 如果do_resize
為True
,則影像將被調整為該值的倍數。其工作原理是將高度和寬度向下取整到最接近該值的倍數。無論keep_aspect_ratio
是否設定為True
,此引數都有效。該引數可在preprocess
中被ensure_multiple_of
覆蓋。
返回
<class 'transformers.image_processing_base.BatchFeature'>
- data (
dict
) — 由 call 方法返回的列表/陣列/張量字典(“pixel_values”等)。 - tensor_type (
Union[None, str, TensorType]
, 可選) — 您可以在此處提供一個`tensor_type`,以便在初始化時將整數列表轉換為PyTorch/TensorFlow/Numpy張量。
ZoeDepthForDepthEstimation
class transformers.ZoeDepthForDepthEstimation
< source >( config )
引數
- config (ZoeDepthForDepthEstimation) — 包含模型所有引數的模型配置類。使用配置檔案進行初始化不會載入與模型相關的權重,只會載入配置。請檢視 from_pretrained() 方法來載入模型權重。
ZoeDepth 模型,其頂部帶有一個或多個度量深度估計頭。
該模型繼承自 PreTrainedModel。請查閱超類文件,瞭解該庫為所有模型實現的通用方法(例如下載或儲存、調整輸入嵌入大小、修剪頭部等)。
該模型也是 PyTorch 的 torch.nn.Module 子類。可以像常規的 PyTorch 模組一樣使用它,並參考 PyTorch 文件瞭解所有與通用用法和行為相關的事項。
forward
< source >( pixel_values: FloatTensor labels: typing.Optional[torch.LongTensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) → transformers.modeling_outputs.DepthEstimatorOutput 或 tuple(torch.FloatTensor)
引數
- pixel_values (
torch.FloatTensor
,形狀為(batch_size, num_channels, image_size, image_size)
) — 與輸入影像對應的張量。畫素值可以使用{image_processor_class}
獲得。有關詳細資訊,請參閱{image_processor_class}.__call__
({processor_class}
使用{image_processor_class}
來處理影像)。 - labels (
torch.LongTensor
,形狀為(batch_size, height, width)
, 可選) — 用於計算損失的真實深度估計圖。 - output_attentions (
bool
, 可選) — 是否返回所有注意力層的注意力張量。有關更多詳細資訊,請參閱返回張量下的attentions
。 - output_hidden_states (
bool
, 可選) — 是否返回所有層的隱藏狀態。有關更多詳細資訊,請參閱返回張量下的hidden_states
。 - return_dict (
bool
, 可選) — 是否返回一個 ModelOutput 而不是一個普通的元組。
返回
transformers.modeling_outputs.DepthEstimatorOutput 或 tuple(torch.FloatTensor)
一個 transformers.modeling_outputs.DepthEstimatorOutput 或一個 torch.FloatTensor
的元組(如果傳遞了 return_dict=False
或 config.return_dict=False
),包含根據配置(ZoeDepthConfig)和輸入的不同元素。
-
loss (形狀為
(1,)
的torch.FloatTensor
,可選,當提供labels
時返回) — 分類損失(如果 config.num_labels==1,則為迴歸損失)。 -
predicted_depth (
torch.FloatTensor
,形狀為(batch_size, height, width)
) — 每個畫素的預測深度。 -
hidden_states (
tuple(torch.FloatTensor)
, 可選, 當傳遞output_hidden_states=True
或config.output_hidden_states=True
時返回) —torch.FloatTensor
的元組(如果模型有嵌入層,則一個用於嵌入層的輸出,另外每個層一個輸出),形狀為(batch_size, num_channels, height, width)
。模型在每個層輸出的隱藏狀態以及可選的初始嵌入輸出。
-
attentions (
tuple(torch.FloatTensor)
, 可選, 當傳遞output_attentions=True
或config.output_attentions=True
時返回) —torch.FloatTensor
的元組(每層一個),形狀為(batch_size, num_heads, patch_size, sequence_length)
。注意力 softmax 後的注意力權重,用於計算自注意力頭中的加權平均值。
ZoeDepthForDepthEstimation 的 forward 方法會覆蓋 __call__
特殊方法。
儘管前向傳播的流程需要在此函式中定義,但之後應該呼叫 Module
例項而不是此函式,因為前者會處理預處理和後處理步驟,而後者會默默地忽略它們。
示例
>>> from transformers import AutoImageProcessor, ZoeDepthForDepthEstimation
>>> import torch
>>> import numpy as np
>>> from PIL import Image
>>> import requests
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> image_processor = AutoImageProcessor.from_pretrained("Intel/zoedepth-nyu-kitti")
>>> model = ZoeDepthForDepthEstimation.from_pretrained("Intel/zoedepth-nyu-kitti")
>>> # prepare image for the model
>>> inputs = image_processor(images=image, return_tensors="pt")
>>> with torch.no_grad():
... outputs = model(**inputs)
>>> # interpolate to original size
>>> post_processed_output = image_processor.post_process_depth_estimation(
... outputs,
... source_sizes=[(image.height, image.width)],
... )
>>> # visualize the prediction
>>> predicted_depth = post_processed_output[0]["predicted_depth"]
>>> depth = predicted_depth * 255 / predicted_depth.max()
>>> depth = depth.detach().cpu().numpy()
>>> depth = Image.fromarray(depth.astype("uint8"))