返回模型
说明文档
仅含嵌入的 ResNet-50
这是标准 ResNet-50 架构的修改版本,其中用于分类的最后一层全连接层已被移除。
这实际上为您提供的是嵌入向量。
注意:您可能需要展平嵌入向量,否则其形状将为 (1, 20248, 1, 1)。
示例
import onnxruntime
from PIL import Image
from torchvision import transforms
def load_and_preprocess_image(image_path):
# Define the same preprocessing as used in training
preprocess = transforms.Compose(
[
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
]
)
# Open the image file
img = Image.open(image_path)
# Preprocess the image
img_preprocessed = preprocess(img)
# Add batch dimension
return img_preprocessed.unsqueeze(0).numpy()
onnx_model_path = \"resnet50_embeddings.onnx\"
session = onnxruntime.InferenceSession(onnx_model_path)
input_name = session.get_inputs()[0].name
# Load and preprocess an image (replace with your image path)
image_path = \"disco-ball.jpg\"
input_data = load_and_preprocess_image(image_path)
# Run inference
outputs = session.run(None, {input_name: input_data})
# The output should be a single tensor (the embeddings)
embeddings = outputs[0]
# Flatten the embeddings
embeddings = embeddings.reshape(embeddings.shape[0], -1)
jxtc/resnet-50-embeddings
作者 jxtc
image-feature-extraction
↓ 0
♥ 1
创建时间: 2024-09-29 20:29:21+00:00
更新时间: 2024-09-29 21:18:09+00:00
在 Hugging Face 上查看文件 (3)
.gitattributes
README.md
resnet50_embeddings.onnx
ONNX