ts.torch_handler 套件¶
子套件¶
- ts.torch_handler.request_envelope 套件
- ts.torch_handler.unit_tests 套件
- 子套件
- 子模組
- ts.torch_handler.unit_tests.test_base_handler 模組
- ts.torch_handler.unit_tests.test_envelopes 模組
- ts.torch_handler.unit_tests.test_image_classifier 模組
- ts.torch_handler.unit_tests.test_image_segmenter 模組
- ts.torch_handler.unit_tests.test_mnist_kf 模組
- ts.torch_handler.unit_tests.test_object_detector 模組
- 模組內容
子模組¶
ts.torch_handler.base_handler 模組¶
用於載入 torchscript 或 eager 模式 [state_dict] 模型的預設基本處理常式。此外,根據 torch serve 自訂模型規格提供處理方法
- class ts.torch_handler.base_handler.BaseHandler[原始碼]¶
基於:
ABC
用於載入 torchscript 或 eager 模式 [state_dict] 模型的預設基本處理常式。此外,根據 torch serve 自訂模型規格提供處理方法
- explain_handle(data_preprocess, raw_data)[原始碼]¶
Captum 解釋處理常式
- 參數:
data_preprocess (Torch Tensor) – 用於 captum 的預處理資料
raw_data (list) – 從請求中獲取目標的未處理資料
- 傳回:
包含解釋回應的字典回應。
- 傳回類型:
- inference(*args, **kwargs)¶
- initialize(context)[原始碼]¶
- Initialize 函數載入 model.pt 檔案並初始化模型物件。
首先嘗試載入 torchscript,否則載入基於 eager mode state_dict 的模型。
- 參數:
context (context) – 這是一個 JSON 物件,包含
parameters. (關於模型工件的) –
- 引發:
RuntimeError – 當 model.py 遺失時,引發 Runtime error
- postprocess(*args, **kwargs)¶
- preprocess(*args, **kwargs)¶
ts.torch_handler.contractions 模組¶
文字分類模型的簡寫對應表。
ts.torch_handler.densenet_handler 模組¶
用於圖像分類預設處理器的模組
ts.torch_handler.image_classifier 模組¶
用於圖像分類預設處理器的模組
- class ts.torch_handler.image_classifier.ImageClassifier[原始碼]¶
繼承:
VisionHandler
ImageClassifier 處理器類別。 此處理器接受圖像並傳回該圖像中物件的名稱。
- image_processing = Compose( Resize(size=256, interpolation=bilinear, max_size=None, antialias=True) CenterCrop(size=(224, 224)) ToTensor() Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) )¶
- postprocess(*args, **kwargs)¶
- topk = 5¶
ts.torch_handler.image_segmenter 模組¶
用於影像分割預設處理器的模組
- class ts.torch_handler.image_segmenter.ImageSegmenter[source]¶
繼承:
VisionHandler
ImageSegmenter 處理器類別。此處理器接收一批影像,並傳回輸出形狀為 [N K H W],其中 N - 批次大小,K - 類別數量,H - 高度,W - 寬度。
- image_processing = Compose( Resize(size=256, interpolation=bilinear, max_size=None, antialias=True) CenterCrop(size=(224, 224)) ToTensor() Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) )¶
ts.torch_handler.object_detector 模組¶
用於物件偵測預設處理器的模組
- class ts.torch_handler.object_detector.ObjectDetector[source]¶
繼承:
VisionHandler
ObjectDetector 處理器類別。此處理器接收一張影像,並分別傳回偵測到的類別和邊界框的列表。
- image_processing = Compose( ToTensor() )¶
- initialize(context)[source]¶
- Initialize 函數載入 model.pt 檔案並初始化模型物件。
首先嘗試載入 torchscript,否則載入基於 eager mode state_dict 的模型。
- 參數:
context (context) – 這是一個 JSON 物件,包含
parameters. (關於模型工件的) –
- 引發:
RuntimeError – 當 model.py 遺失時,引發 Runtime error
- threshold = 0.5¶
ts.torch_handler.text_classifier 模組¶
用於文字分類預設處理器的模組,**不支援批次處理!**
- class ts.torch_handler.text_classifier.TextClassifier[source]¶
繼承自:
TextHandler
TextClassifier 處理器類別。 此處理器接收一段文字(字串)作為輸入,並根據模型詞彙表傳回分類文字。
- inference(data, *args, **kwargs)[source]¶
推論請求是透過此函數發出的,使用者需要覆寫推論函數以進行自訂。
- 參數:
data (torch 張量) –
資料的形式為 Torch 張量,其形狀應與
模型輸入形狀相符。
- 傳回:
- 模型預測的回應將會在此函數中
傳回。
- 傳回類型:
(Torch 張量)
- ngrams = 2¶
ts.torch_handler.text_handler 模組¶
所有基於文字的預設處理器的基礎模組。包含各種基於文字的實用方法
ts.torch_handler.vision_handler 模組¶
所有視覺處理器的基礎模組
- class ts.torch_handler.vision_handler.VisionHandler[原始碼]¶
基於:
BaseHandler
,ABC
所有視覺處理器的基礎類別
- initialize(context)[原始碼]¶
- Initialize 函數載入 model.pt 檔案並初始化模型物件。
首先嘗試載入 torchscript,否則載入基於 eager mode state_dict 的模型。
- 參數:
context (context) – 這是一個 JSON 物件,包含
parameters. (關於模型工件的) –
- 引發:
RuntimeError – 當 model.py 遺失時,引發 Runtime error
- preprocess(*args, **kwargs)¶