欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python多图拼接并利用resnet提取特征

发布时间:2025/4/16 python 51 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python多图拼接并利用resnet提取特征 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

代码功能:

1、将多张图拼接成一张大图;

2、基于resnet提取大图的特征。

import torch import torch.nn as nn from torchvision import models, transforms from torch.autograd import Variable import numpy as np from PIL import Image from os import listdir #resnet50提取图像特征 transform1 = transforms.Compose([transforms.Scale(256),transforms.CenterCrop(224),transforms.ToTensor()]) resnet50_feature_extractor = models.resnet50(pretrained = True) resnet50_feature_extractor.fc = nn.Linear(2048, 2048) torch.nn.init.eye(resnet50_feature_extractor.fc.weight) for param in resnet50_feature_extractor.parameters():param.requires_grad = Falseimages = [Image.open(fn) for fn in listdir() if fn.endswith('.png')] #多图拼接成一张长图 if len(images)>0:width,height=images[0].sizefor image in images:w,h = image.sizeif w>width:width=wif h>height: height=hlongImg = Image.new(images[0].mode,(width,height*len(images)))for i,im in enumerate(images):longImg.paste(im,box=(0,i*height))#拼接#长图向量化 imgarr = np.array(longImg)if imgarr.shape[2] == 4: #四通道转为三通道img1 = img.convert("RGB")#img = Image.fromarray(img.astype('uint8')).convert('RGB')img2 = transform1(img1)x = Variable(torch.unsqueeze(img2, dim=0).float(), requires_grad=False)y = resnet50_feature_extractor(x)y = y.data.numpy()print (y.shape)

 

总结

以上是生活随笔为你收集整理的python多图拼接并利用resnet提取特征的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。