Python读取图片的几种方法供net使用

69 篇文章 8 订阅

第一种方式: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])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值