第一种方式:cv2:
import cv2
class my_net(nn.Module):
def __init__(self):
super(my_net, self).__init__()
#加载ResNet预训练模型
self.model = resnet50(pretrained=False)
self.model.load_state_dict(torch.load('./models/resnet50-19c8e357.pth'))
self.model = nn.Sequential(*list(self.model.children())[:-1])
self.device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
# self.model = self.model.to(self.device).eval() # 发现设置为评估模式更低了
self.model = self.model.to(self.device)
self.model.eval()
# ransforms.ToTensor()作用 可参考下边这个连接
# https://blog.csdn.net/weixin_43593330/article/details/107543737
self.train_transformer = transforms.Compose([
transforms.Resize([256, 256]), # frame的形状(720,1280,3) ,要先变成3,224,224
transforms.CenterCrop(224),
transforms.ToTensor(),
])
def forward(self, x):
# x = self.train_transformer(x) 此方式要把这两个给注释了
# x = x.reshape(1, 3, 224, 224)
x = x.to(self.device)
# with torch.no_grad(): # 没有求导所以无须此步操作
x = self.model(x)
x = x.reshape(1, 2048)
x = x.cpu()
return x
image = cv2.imread("你的图片路径") # 返回的图片的颜色模型是BGR顺序,通道为hwc通道
# cv2默认为 BGR顺序,而其他软件一般使用RGB cv2读取image后返回的类型为numpy类型
image = image[:, :, ::-1].copy() # 必须要用copy 否则会报错 [:, :, ::-1]将BGR转换成RGB
image = image.transpose((2, 0, 1)) # 转变图片的格式 将hwc 转换为chw
input_x = torch.from_numpy(image).unsqueeze(0) # 最外层添加一个维度
result = my_net()(input_x.to(torch.float32)) # 就可以直接使用网络进行特征提取了
# result<class 'torch.Tensor'> torch.Size([1, 2048])
第一种方法image = image[:, :, ::-1].copy()的补充:
# 其作用是将BGR转换成RGB现在发现三种转换方式:上边这个为第一种
rgb_image = image[:, :, ::-1].copy()
# 第二种:
b, g, r = cv2.split(image)
rgb_img = cv2.merge([r, g, b])
# 第三种:
cvtColor_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 验证如下:
print(rgb_img == cvtColor_image)
print("-"*100)
print(rgb_img == rgb_image)
结果如下:
第二种方式:
from PIL import Image
class my_net(nn.Module):
def __init__(self):
super(my_net, self).__init__()
#加载ResNet预训练模型
self.model = resnet50(pretrained=False)
self.model.load_state_dict(torch.load('./models/resnet50-19c8e357.pth'))
self.model = nn.Sequential(*list(self.model.children())[:-1])
self.device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
# self.model = self.model.to(self.device).eval() # 发现设置为评估模式更低了
self.model = self.model.to(self.device)
self.model.eval()
# ransforms.ToTensor()作用 可参考下边这个连接
# https://blog.csdn.net/weixin_43593330/article/details/107543737
self.train_transformer = transforms.Compose([
transforms.Resize([256, 256]), # frame的形状(720,1280,3) ,要先变成3,224,224
transforms.CenterCrop(224),
transforms.ToTensor(),
])
def forward(self, x):
x = self.train_transformer(x)
x = x.reshape(1, 3, 224, 224)
x = x.to(self.device)
# with torch.no_grad(): # 没有求导所以无须此步操作
x = self.model(x)
x = x.reshape(1, 2048)
x = x.cpu()
return x
other_image = Image.open("你的图片路径") # other_image的类型为:<class 'PIL.JpegImagePlugin.JpegImageFile'>
convert_image = other_image.convert("RGB") # convert_image的类型为:<class 'PIL.Image.Image'>
result = my_net()(convert_image) # result<class 'torch.Tensor'> torch.Size([1, 2048])