ts.torch_handler package¶
子包¶
- ts.torch_handler.request_envelope package
- ts.torch_handler.unit_tests package
- 子包
- 子模組
- ts.torch_handler.unit_tests.test_base_handler module
- ts.torch_handler.unit_tests.test_envelopes module
- ts.torch_handler.unit_tests.test_image_classifier module
- ts.torch_handler.unit_tests.test_image_segmenter module
- ts.torch_handler.unit_tests.test_mnist_kf module
- ts.torch_handler.unit_tests.test_object_detector module
- 模組內容
子模組¶
ts.torch_handler.base_handler module¶
用於載入 torchscript 或 eager mode [state_dict] 模型的基本預設處理器。此外,還根據 torch serve 自定義模型規範提供 handle 方法
- class ts.torch_handler.base_handler.BaseHandler[source]¶
基類:
ABC用於載入 torchscript 或 eager mode [state_dict] 模型的基本預設處理器。此外,還根據 torch serve 自定義模型規範提供 handle 方法
- inference(*args, **kwargs)¶
- initialize(context)[source]¶
- Initialize 函式載入 model.pt 檔案並初始化模型物件。
首先嚐試載入 torchscript,否則載入基於 eager mode state_dict 的模型。
- 引數:
context (context) – 這是一個包含資訊的 JSON 物件
引數. (關於模型 artifacts 的) –
- 丟擲異常:
RuntimeError – 當 model.py 缺失時丟擲 Runtime 錯誤
- postprocess(*args, **kwargs)¶
- preprocess(*args, **kwargs)¶
ts.torch_handler.contractions module¶
用於文字分類模型的縮寫對映。
ts.torch_handler.densenet_handler module¶
影像分類預設處理器模組
ts.torch_handler.image_classifier module¶
影像分類預設處理器模組
- class ts.torch_handler.image_classifier.ImageClassifier[source]¶
基類:
VisionHandlerImageClassifier 處理器類。此處理器接收一張影像並返回影像中物件的名稱。
- 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 module¶
影像分割預設處理器模組
- class ts.torch_handler.image_segmenter.ImageSegmenter[source]¶
基類:
VisionHandlerImageSegmenter 處理器類。此處理器接收一批影像,並返回形狀為 [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 module¶
物件檢測預設處理器模組
- class ts.torch_handler.object_detector.ObjectDetector[source]¶
基類:
VisionHandlerObjectDetector 處理器類。此處理器接收一張影像,並分別返回檢測到的類別列表和邊界框。
- image_processing = Compose( ToTensor() )¶
- initialize(context)[source]¶
- Initialize 函式載入 model.pt 檔案並初始化模型物件。
首先嚐試載入 torchscript,否則載入基於 eager mode state_dict 的模型。
- 引數:
context (context) – 這是一個包含資訊的 JSON 物件
引數. (關於模型 artifacts 的) –
- 丟擲異常:
RuntimeError – 當 model.py 缺失時丟擲 Runtime 錯誤
- threshold = 0.5¶
ts.torch_handler.text_classifier module¶
文字分類預設處理器模組 不支援批次處理!
- class ts.torch_handler.text_classifier.TextClassifier[source]¶
基類:
TextHandler,ABCTextClassifier 處理器類。此處理器接收文字 (string) 作為輸入,並根據模型詞彙表返回分類文字。
- inference(data, *args, **kwargs)[source]¶
推理請求透過此函式完成,使用者需要重寫 inference 函式來對其進行自定義。
- 引數:
data (torch tensor) –
資料為 Torch Tensor 形式,其形狀應與
模型輸入形狀匹配。
- 返回:
- 返回模型預測的響應
在此函式中。
- 返回型別:
(Torch Tensor)
- ngrams = 2¶
ts.torch_handler.text_handler module¶
所有基於文字的預設處理器的基本模組。包含各種基於文字的實用方法
- class ts.torch_handler.text_handler.TextHandler[source]¶
基類:
BaseHandler,ABC所有基於文字的預設處理器的基類。包含各種基於文字的實用方法
ts.torch_handler.vision_handler module¶
所有視覺處理器的基本模組
- class ts.torch_handler.vision_handler.VisionHandler[source]¶
基類:
BaseHandler,ABC所有視覺處理程式的基類
- initialize(context)[source]¶
- Initialize 函式載入 model.pt 檔案並初始化模型物件。
首先嚐試載入 torchscript,否則載入基於 eager mode state_dict 的模型。
- 引數:
context (context) – 這是一個包含資訊的 JSON 物件
引數. (關於模型 artifacts 的) –
- 丟擲異常:
RuntimeError – 當 model.py 缺失時丟擲 Runtime 錯誤
- preprocess(*args, **kwargs)¶