python多图拼接并利用resnet提取特征
生活随笔
收集整理的這篇文章主要介紹了
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提取特征的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bert-as-Service库Embe
- 下一篇: linux上传github项目