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

如何调整图像尺寸与形状以适配ArcFace模型的输入要求

报错原因

ArcFace 模型的预测接口默认接收批量输入,要求输入为4维张量,格式为(样本数量, 高度, 宽度, 通道数),即便只输入单张图像,也需要补全首个batch维度。你当前的输入是3维的(112, 112, 3),会被模型将图像高度维度误识别为样本数量,最终读到的输入形状不符合要求,触发报错。

修复方案

只需在调用predict前给图像增加batch维度即可,同时可适配高版本Pillow的语法、增加像素归一化步骤避免特征偏差,完整可运行代码如下:

from deepface.basemodels.ArcFace import loadModel as loadArcFace
from PIL import Image
import numpy as np

model = loadArcFace()
img = Image.open('face.jpg')
# Pillow 9.1.0及以上版本已废弃ANTIALIAS,统一用LANCZOS实现高质量resize
img = img.resize((112, 112), Image.Resampling.LANCZOS)
img = np.array(img)

# 新增batch维度,两种写法二选一即可
img_input = np.expand_dims(img, axis=0)
# 第二种写法:img_input = img.reshape(1, 112, 112, 3)

# ArcFace要求输入像素值归一化到[0,1]区间,避免特征计算偏差
img_input = img_input / 255.0

# 执行预测,verbose=0可关闭不必要的进度条输出
feature = model.predict(img_input, verbose=0)

内容的提问来源于stack exchange,提问作者Tlaloc-ES

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 20:06:09