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 mode [state_dict] 模型。此外,还提供了针对torch serve自定义模型规格的处理方法。
- class ts.torch_handler.base_handler.BaseHandler[source]¶
Bases:
ABC默认处理程序用于加载torchscript或 eager mode [state_dict] 模型。此外,还提供了针对torch serve自定义模型规格的处理方法。
- handle(data, context)[source]¶
- Entry point for default handler. It takes the data from the input request and returns
输入的预测结果。
- inference(*args, **kwargs)¶
- initialize(context)[source]¶
- Initialize function loads the model.pt file and initialized the model object.
首先尝试加载torchscript,否则加载基于state_dict的 eager mode 模型。
- Parameters:
上下文 (上下文) – 它是一个包含信息的JSON对象
参数。 (与模型 artifacts 相关) –
- Raises:
运行时错误 – 当 model.py 文件缺失时,引发运行时错误
- 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[source]¶
Bases:
VisionHandler图像分类器处理器类。此处理器接收一张图片并返回该图片中物体的名称。
- 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]¶
Bases:
VisionHandler图像分割器处理类。这个处理类接收一批图片,并返回输出形状为[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]¶
Bases:
VisionHandler目标检测处理器类。这个处理器接受一张图片并返回一个包含检测到的类别和边界框的列表。
- image_processing = Compose( ToTensor() )¶
- initialize(context)[source]¶
- Initialize function loads the model.pt file and initialized the model object.
首先尝试加载torchscript,否则加载基于state_dict的 eager mode 模型。
- Parameters:
上下文 (上下文) – 它是一个包含信息的JSON对象
参数。 (与模型 artifacts 相关) –
- Raises:
运行时错误 – 当 model.py 文件缺失时,引发运行时错误
- threshold = 0.5¶
ts.torch_handler.text_classifier 模块¶
文本分类默认处理器模块 不支持批量!
- class ts.torch_handler.text_classifier.TextClassifier[source]¶
Bases:
TextHandler文本分类器处理器类。此处理器接受一个文本(字符串)作为输入,并根据模型词汇表返回相应的分类文本。
- inference(data, *args, **kwargs)[source]¶
推理请求通过这个功能发起,用户需要覆盖推理函数以进行自定义。
- Parameters:
数据 (torch tensor) –
数据以Torch Tensor的形式存在,其形状应与
Model Input shape.
- Returns:
- The predicted response from the model is returned
在这个函数中。
- Return type:
(Torch张量)
- ngrams = 2¶
ts.torch_handler.text_handler 模块¶
基于所有文本默认处理模块的基础类。 包含各种基于文本的实用方法。
- class ts.torch_handler.text_handler.TextHandler[source]¶
Bases:
BaseHandler,ABC所有基于文本的默认处理类的基础类。 包含各种基于文本的实用方法。
ts.torch_handler.vision_handler 模块¶
所有视觉处理模块的基础模块
- class ts.torch_handler.vision_handler.VisionHandler[source]¶
Bases:
BaseHandler,ABC所有视觉处理器的基础类
- initialize(context)[source]¶
- Initialize function loads the model.pt file and initialized the model object.
首先尝试加载torchscript,否则加载基于state_dict的 eager mode 模型。
- Parameters:
上下文 (上下文) – 它是一个包含信息的JSON对象
参数。 (与模型 artifacts 相关) –
- Raises:
运行时错误 – 当 model.py 文件缺失时,引发运行时错误
- preprocess(*args, **kwargs)¶