目录

ts.torch_handler 包

子包

子模块

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自定义模型规格的处理方法。

describe_handle()[source]

自定义描述处理器

Returns:

一个字典响应。

Return type:

字典

explain_handle(data_preprocess, raw_data)[source]

Captum 解释器

Parameters:
  • 数据预处理 (Torch Tensor) – 预处理数据用于captum

  • 原始数据 (列表) – 从请求中获取目标的未处理数据

Returns:

一个包含解释的字典响应。

Return type:

字典

get_device()[source]

获取设备

Returns:

我的设备

Return type:

字符串

handle(data, context)[source]
Entry point for default handler. It takes the data from the input request and returns

输入的预测结果。

Parameters:
  • 数据 (列表) – 需要进行预测请求的输入数据。

  • 上下文 (Context) – 它是一个包含模型 artifacts 参数信息的 JSON 对象。

Returns:

返回一个包含预测响应的字典列表。

Return type:

列表

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.base_handler.setup_ort_session(model_pt_path, map_location)[source]

ts.torch_handler.contractions 模块

文本分类模型的缩写映射

ts.torch_handler.densenet_handler 模块

图像分类默认处理器模块

class ts.torch_handler.densenet_handler.DenseNetHandler[source]

基础: object

DenseNetHandler 处理器类。这个处理器接受一张图片并返回该图片中物体的名称。

handle(data, context)[source]

默认处理程序的入口点

inference(data, *args, **kwargs)[source]

覆盖以自定义推理 :param data: PyTorch张量,匹配模型输入形状 :return: 预测输出作为PyTorch张量

initialize(context)[source]

首先尝试加载torchscript,否则加载基于state_dict的 eager mode 模型。

ts.torch_handler.densenet_handler.list_classes_from_module(module, parent_class=None)[source]

解析用户定义的模块,获取其中的所有模型服务类。

Parameters:
  • 模块

  • parent_class

Returns:

模型服务类定义列表

ts.torch_handler.image_classifier 模块

图像分类默认处理器模块

class ts.torch_handler.image_classifier.ImageClassifier[source]

Bases: VisionHandler

图像分类器处理器类。此处理器接收一张图片并返回该图片中物体的名称。

get_max_result_classes()[source]
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)
set_max_result_classes(topk)[source]
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]) )
postprocess(data)[source]

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 文件缺失时,引发运行时错误

postprocess(data)[source]
threshold = 0.5

ts.torch_handler.text_classifier 模块

文本分类默认处理器模块 不支持批量!

class ts.torch_handler.text_classifier.TextClassifier[source]

Bases: TextHandler

文本分类器处理器类。此处理器接受一个文本(字符串)作为输入,并根据模型词汇表返回相应的分类文本。

get_insights(text_preprocess, _, target=0)[source]

计算捕获见解

Parameters:
  • 文本预处理 (张量) – 输入文本的张量

  • _ (str) – 输入请求中指定的原始文本数据

  • 目标 (int) – 默认为0,用户需要为captum解释指定目标。

Returns:

返回一个包含词令牌重要性的字典

Return type:

(字典)

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
postprocess(data)[source]
The post process function converts the prediction response into a

支持TorchServe的格式

Parameters:
  • 数据 (Torch Tensor) – 数据参数来自预测输出

  • 输出解释 (None) – 默认为 None。

Returns:

Returns the response containing the predictions and explanations

如果遇到错误。它以列表的形式出现,每个元素是一个字典。

Return type:

(列表)

preprocess(data)[source]
Normalizes the input text for PyTorch model using following basic cleanup operations :
  • 移除 HTML 标签

  • 将所有文本转换为小写

  • 扩展缩写 [像 I’d -> 我会,don’t -> 不]

  • 删除带点字符

  • 删除标点符号

将归一化的文本转换为张量,使用源词典。

Parameters:

数据 (字符串) – 输入数据是以字符串形式存在的

Returns:

经过预处理操作后返回张量(str):该函数还会返回原始输入。

Return type:

(Tensor)

ts.torch_handler.text_handler 模块

基于所有文本默认处理模块的基础类。 包含各种基于文本的实用方法。

class ts.torch_handler.text_handler.TextHandler[source]

Bases: BaseHandler, ABC

所有基于文本的默认处理类的基础类。 包含各种基于文本的实用方法。

get_source_vocab_path(ctx)[source]
get_word_token(input_tokens)[source]

从文本中构建词袋

initialize(context)[source]

加载模型并初始化必要的 artifacts

summarize_attributions(attributions)[source]

总结了多轮运行的归因

ts.torch_handler.vision_handler 模块

所有视觉处理模块的基础模块

class ts.torch_handler.vision_handler.VisionHandler[source]

Bases: BaseHandler, ABC

所有视觉处理器的基础类

get_insights(tensor_data, _, target=0)[source]
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)

模块内容

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源