Example-Research-COAT/Code/Python/backbone/test_resnet50.py

35 lines
1.0 KiB
Python

from resnet import build_resnet
import cv2
import torchvision.transforms as transforms
backbone, _ = build_resnet(name="resnet50", pretrained=True)
# 读取测试图像
image = cv2.imread('E:/DeepLearning/PersonSearch/COAT/COAT/main/backbone/test.jpg') # 替换为您的测试图像文件路径
image = cv2.resize(image, (224, 224))
# 使用 OpenCV 显示原始图像
cv2.imshow("Original Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 创建转换以将图像缩放到 224x224 大小并转换为张量
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((224, 224)),
transforms.ToTensor(),
])
# 应用转换并将图像转换为张量
image_tensor = transform(image)
image_tensor = image_tensor.unsqueeze(0) # 添加批次维度,将形状变为 [1, 3, 224, 224]
# 使用模型进行推理
output = backbone(image_tensor)
# 输入的shape是[1,1024,7,7]
#print(output)
print(output['feat_res4'].shape)
# 在这里,您可以处理模型的输出,进行后续的操作或分析