SAMImageConverter

[原始碼]

SAMImageConverter 類別

keras_hub.layers.SAMImageConverter(
    image_size=None,
    scale=None,
    offset=None,
    crop_to_aspect_ratio=True,
    pad_to_aspect_ratio=False,
    interpolation="bilinear",
    bounding_box_format="yxyx",
    data_format=None,
    **kwargs
)

將原始影像預處理為模型可用的輸入。

此類別將原始影像轉換為模型可用的輸入。此轉換按以下步驟進行

  1. 使用 image_size 調整影像大小。如果 image_sizeNone,則會跳過此步驟。
  2. 通過乘以 scale 來重新縮放影像,可以是全域或逐通道。如果 scaleNone,則會跳過此步驟。
  3. 通過添加 offset 來偏移影像,可以是全域或逐通道。如果 offsetNone,則會跳過此步驟。

此層將以通道最後或通道優先格式接收原始影像張量作為輸入,並輸出用於建模的預處理影像輸入。此張量可以是批次的(rank 4)或非批次的(rank 3)。

此層可以與 from_preset() 建構子一起使用,以載入將為特定預訓練模型重新縮放和調整影像大小的層。以這種方式使用此層允許編寫預處理程式碼,而無需在模型檢查點之間切換時進行更新。

引數

  • image_size(int, int) 元組或 None。影像的輸出大小,不包括通道軸。如果 None,則不會調整輸入大小。
  • scale:float、float 元組或 None。要應用於輸入的縮放比例。如果 scale 是單個 float,則整個輸入將乘以 scale。如果 scale 是一個元組,則假定它包含針對輸入影像的每個通道相乘的逐通道縮放值。如果 scaleNone,則不應用縮放。
  • offset:float、float 元組或 None。要應用於輸入的偏移量。如果 offset 是單個 float,則整個輸入將與 offset 相加。如果 offset 是一個元組,則假定它包含與輸入影像的每個通道相加的逐通道偏移值。如果 offsetNone,則不應用偏移。
  • crop_to_aspect_ratio:如果為 True,則調整影像大小時不扭曲長寬比。當原始長寬比與目標長寬比不同時,將裁剪輸出影像,以便傳回影像中與目標長寬比匹配的最大可能視窗(大小為 (height, width))。預設情況下(crop_to_aspect_ratio=False),長寬比可能無法保留。
  • interpolation:字串,插值方法。支援 "bilinear""nearest""bicubic""lanczos3""lanczos5"。預設為 "bilinear"
  • bounding_box_format:一個字串,指定邊界框的格式,可以是 "xyxy""rel_xyxy""xywh""center_xywh""yxyx""rel_yxyx" 之一。指定邊界框的格式,這些邊界框將與影像一起調整大小為 image_size。要將邊界框傳遞到此層,請在呼叫該層時傳遞一個帶有鍵 "images""bounding_boxes" 的字典。
  • data_format:字串,可以是 "channels_last""channels_first"。輸入中維度的順序。"channels_last" 對應於形狀為 (batch, height, width, channels) 的輸入,而 "channels_first" 對應於形狀為 (batch, channels, height, width) 的輸入。它預設為在您的 Keras 設定檔 ~/.keras/keras.json 中找到的 image_data_format 值。如果您從未設定它,則它將為 "channels_last"

範例

# Resize raw images and scale them to [0, 1].
converter = keras_hub.layers.ImageConverter(
    image_size=(128, 128),
    scale=1. / 255,
)
converter(np.random.randint(0, 256, size=(2, 512, 512, 3)))

# Resize images to the specific size needed for a PaliGemma preset.
converter = keras_hub.layers.ImageConverter.from_preset(
    "pali_gemma_3b_224"
)
converter(np.random.randint(0, 256, size=(2, 512, 512, 3)))

[原始碼]

from_preset 方法

SAMImageConverter.from_preset(preset, **kwargs)

從模型預設實例化 keras_hub.layers.ImageConverter

預設是一個組態、權重和其他檔案資產的目錄,用於儲存和載入預訓練模型。preset 可以作為以下之一傳遞

  1. 一個內建的預設識別符,例如 'pali_gemma_3b_224'
  2. 一個 Kaggle 模型句柄,例如 'kaggle://user/paligemma/keras/pali_gemma_3b_224'
  3. 一個 Hugging Face 句柄,例如 'hf://user/pali_gemma_3b_224'
  4. 一個本地預設目錄的路徑,例如 './pali_gemma_3b_224'

您可以執行 cls.presets.keys() 以列出類別上所有可用的內建預設。

引數

  • preset:字串。一個內建的預設識別符、Kaggle 模型句柄、Hugging Face 句柄或本地目錄的路徑。
  • load_weights:bool。如果為 True,則權重將載入到模型架構中。如果為 False,則權重將隨機初始化。

範例

batch = np.random.randint(0, 256, size=(2, 512, 512, 3))

# Resize images for `"pali_gemma_3b_224"`.
converter = keras_hub.layers.ImageConverter.from_preset(
    "pali_gemma_3b_224"
)
converter(batch) # # Output shape (2, 224, 224, 3)

# Resize images for `"pali_gemma_3b_448"` without cropping.
converter = keras_hub.layers.ImageConverter.from_preset(
    "pali_gemma_3b_448",
    crop_to_aspect_ratio=False,
)
converter(batch) # # Output shape (2, 448, 448, 3)
預設 參數 描述
sam_base_sa1b 93.74M 在 SA1B 資料集上訓練的基本 SAM 模型。
sam_huge_sa1b 312.34M 在 SA1B 資料集上訓練的巨型 SAM 模型。
sam_large_sa1b 641.09M 在 SA1B 資料集上訓練的大型 SAM 模型。