它可以让您方便地使用预训练的模型进行各种任务¹。当您用pipeline函数创建一个图像分割的pipeline时,它会自动加载和初始化一个SegformerForSemanticSegmentation的实例,并且封装了一些预处理和后处理的逻辑,例如将图像转换为张量,将输出转换为分割图等²。您可以直接用pipeline函数对图像或图像列表进行分割,而不需要关心模型的细节。
SegformerForSemanticSegmentation是一个具体的模型类,它由一个分层的Transformer编码器和一个轻量级的全MLP解码器组成,可以实现高效的图像分割³。当您用SegformerForSemanticSegmentation.from_pretrained方法加载一个预训练的模型时,它会返回一个SegformerForSemanticSegmentation的实例,但是您需要自己处理输入和输出的数据格式,例如使用AutoFeatureExtractor来提取图像特征,使用torch.argmax来获取分割结果等。
AutoModel.from_pretrained是一个通用的方法,它可以根据给定的模型名称或路径,自动识别模型的类型,并返回一个相应的模型类的实例³。例如,如果给定的模型名称是"bert-base-chinese",那么这个方法会返回一个BertModel的实例,它是一个用于文本表示的模型。这个方法可以处理多种不同类型的模型,但是它不能处理特定任务的模型,例如图像分割或序列标注。
本人实验代码:
- import cv2
- import PIL.Image as Image
- import numpy as np
- from transformers import pipeline
-
- model_dir = '/speed/speed/code/DECA/face_parsing_model/face-parsing'
- image_path = "/speed/speed/code/DECA/TestSamples/examples/6.png"
-
- pipe = pipeline("image-segmentation", model="jonathandinu/face-parsing")
- img = Image.open(image_path)
- # 加载图片
- # img = cv2.imread(image_path)
- # 预处理图片
- # img = cv2.resize(img, (256, 256))
- # img = img.astype(np.float32) / 255.0
-
- # 使用模型分割图片
- result = pipe(img)
-
- # Load model directly
- from transformers import AutoFeatureExtractor, SegformerForSemanticSegmentation
- from transformers import AutoFeatureExtractor, AutoModel
- model_dir = '/speed/speed/code/DECA/face_parsing_model/face-parsing'
- image_path = "/speed/speed/code/DECA/TestSamples/examples/6.png"
-
- # extractor = AutoFeatureExtractor.from_pretrained("jonathandinu/face-parsing")
- # model = SegformerForSemanticSegmentation.from_pretrained("jonathandinu/face-parsing")
- extractor = AutoFeatureExtractor.from_pretrained(model_dir)
- model = SegformerForSemanticSegmentation.from_pretrained(model_dir)
-
- img = Image.open(image_path)
- # 加载图片
- img = cv2.imread(image_path)
- img = cv2.resize(img, (1024, 1024))
-
- inputs = extractor(img, return_tensors="pt")
- outputs = model(**inputs).logits
- print('')