You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ViT模型HuggingFace推理API报错:未知类型image-feature-extraction

问题描述

我在使用Vision Transformer(ViT)模型rshrott/vit-base-renovation2的HuggingFace推理API时遇到问题。调用API时收到如下错误:

{
  "error": "HfApiJson(Deserialize(Error(\"unknown variant image-feature-extraction, expected one of audio-classification, audio-to-audio, audio-source-separation, automatic-speech-recognition, feature-extraction, text-classification, token-classification, question-answering, translation, summarization, text-generation, text2text-generation, fill-mask, zero-shot-classification, zero-shot-image-classification, conversational, table-question-answering, image-classification, image-segmentation, image-to-text, text-to-speech, … visual-question-answering, video-classification, document-question-answering, image-to-image, depth-estimation, line: 1, column: 318)))"
}

但直接在Python中使用transformers pipeline时模型运行正常,代码如下:

from transformers import pipeline
from PIL import Image
import requests

pipe = pipeline(model="rshrott/vit-base-renovation2")
url = 'https://example.com/image.jpeg'
image = Image.open(requests.get(url, stream=True).raw)
preds = pipe(image)

我怀疑问题和预期任务类型的配置有关,但不确定解决方法。已检查模型卡片和配置,未找到“image-feature-extraction”的来源及被期望使用的原因。请问错误原因是什么,如何修复?

错误原因及修复方案

错误原因

HuggingFace推理API的任务类型列表是固定的,并不包含image-feature-extraction,但你的模型在API调用时被自动识别为该任务类型,导致序列化失败,返回错误。

本地用transformers pipeline调用时,pipeline能自动兼容模型的任务逻辑(该模型实际支持图像分类或特征提取),但推理API对任务类型的校验更严格,不支持未在预设列表中的类型,因此触发报错。

修复方法

1. 调用API时显式指定支持的任务类型

不要依赖API自动检测任务类型,手动指定为API支持的image-classification或feature-extraction:

  • 若需求是图像分类,用以下请求示例:
curl -X POST https://api-inference.huggingface.co/models/rshrott/vit-base-renovation2 \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"inputs": "https://example.com/image.jpeg", "task": "image-classification"}'

Python版本:

import requests

API_URL = "https://api-inference.huggingface.co/models/rshrott/vit-base-renovation2"
headers = {"Authorization": "Bearer YOUR_API_TOKEN"}

def query(image_url):
    payload = {"inputs": image_url, "task": "image-classification"}
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()

output = query("https://example.com/image.jpeg")
  • 若需求是提取图像特征,指定feature-extraction任务:
curl -X POST https://api-inference.huggingface.co/models/rshrott/vit-base-renovation2 \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"inputs": "https://example.com/image.jpeg", "task": "feature-extraction"}'

2. 修改模型配置文件(永久解决)

若希望模型被API自动识别为正确任务类型,可修改模型的config.json,添加task_specific_params字段指定默认任务:

"task_specific_params": {
  "image-classification": {}
}

修改后重新上传模型到HuggingFace Hub,后续API调用会自动匹配正确任务类型,无需手动指定。

内容的提问来源于stack exchange,提问作者John Doe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 14:05:37