深度学习在许多任务中主要充当着特征学习的作用,而学习完的特征才是后续应用的一个关键。本文将主要介绍,如何提取任意目标层的特征图。
本文以输入数据为图片为例。
博主使用了ResNet50训练了一个人脸识别的网络
训练完成的深度学习模型,我们会保存一个参数文件,文件中保存的是前期训练过的网络模型参数,如下图:
提取特征的时候又要分为两种情况,一种是只提取一层特征,另一种是提取多层特征。
一层特征:
在提取一层特征的时候,我们不需要完整的网络结构,只需要在forward的时候截止到我们想要的那一层为止。从下述代码可以看到,我注释了fclass2
这一层,因为这一层是在训练的时候用来计算损失,并不是我想要的。
所以我提取的是fclass1
这一层的特征
class CNN(nn.Module):
def __init__(self, block, layers, num_classes=10000):
self.inplanes = 64
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1,1))
self.fclass1 = nn.Linear(2048, 199)
# self.fclass2 = nn.Linear(199, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fclass1(x)
# x = self.fclass2(x)
return x
多层特征:
多层特征其实只要在forward
的时候,return
出不同层的值就可以了,具体就不多说了
其实这一步和我之前博客写的差不多,就是将.pkl文件中的网络参数,载入到本实验的程序中,不明白的地方,可以看之前的博客。
cnn = CNN(Bottleneck, [3, 4, 6, 3])
pretrained_dict = torch.load('/model/recognition/res50_net_params_253.pkl')
model1_dict = cnn.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model1_dict}
model1_dict.update(pretrained_dict)
cnn.load_state_dict(model1_dict)
cnn = cnn.cuda()
准备工作做完之后,我们就要设计一个特征提取器了,我是写成了子程序的形式,你也可以直接写在主程序中。
## 这段代码,一次只处理一张图片。img_path为图片的路径,save_path是特征保存的npy文件的路径,net就是我们的网络了
def extractors(img_path, sava_path, net):
#在上一篇博客有提到,pytorch处理的数据一定要是tensor的形式,所以transform就是将图片转为tensor,必须加,加多少看需要
transform = transforms.Compose([
# transforms.Resize(256),
# transforms.CenterCrop(224),
transforms.ToTensor()]
)
img = Image.open(img_path)
img = transform(img)
#在训练的时候,数据的第一维代表的是batch_size,虽然我们这里只处理一张图,但是维度必须和原来保持一致,即下面这一行
x = Variable(torch.unsqueeze(img, dim=0).float(), requires_grad=False)
x = x.cuda()
y = net(x)
y = torch.squeeze(y)
#由于numpy的操作是在cpu上进行的,本博客的网络都是用GPU处理的,所以需要将tensor从GPU转至CPU再变成numpy的形式进行保存
y = y.cpu().detach().numpy()
np.save(save_path, y)
##如果是保存图片,
# unloader = transforms.ToPILImage()
# image = y.cpu().clone()
# image = image.squeeze(0)
# image = unloader(image)
# image.save(save_path)
这一步就没那么多讲究了,就按照你图片的存放方式去读取就好了。主要就是别忘了保存路径。
data_dir = '/faceSD/S/'
fea_dir = '/faceSDfea/S/'
data_list = os.listdir(data_dir)
for i in range(len(data_list)):
group_dir = data_dir + data_list[i] + '/'
group_list = os.listdir(group_dir)
save_dir = fea_dir + data_list[i] + '/'
if os.path.exists(save_dir) == False:
os.makedirs(save_dir)
for j in range(2):
img_path = group_dir + group_list[j]
save_path = save_dir + group_list[j][0:-3] + 'npy'
extractors(img_path,save_path,cnn)
import torch
import torch.nn as nn
from torch.autograd import Variable
from torchvision import models, transforms
from PIL import Image
import numpy as np
import os, glob
import math
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * 4)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class CNN(nn.Module):
def __init__(self, block, layers, num_classes=10000):
self.inplanes = 64
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1,1))
self.fclass1 = nn.Linear(2048, 199)
# self.fclass2 = nn.Linear(199, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fclass1(x)
# x = self.fclass2(x)
return x
#载入模型参数
cnn = CNN(Bottleneck, [3, 4, 6, 3])
pretrained_dict = torch.load('/model/recognition/res50_net_params_253.pkl')
model1_dict = cnn.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model1_dict}
model1_dict.update(pretrained_dict)
cnn.load_state_dict(model1_dict)
cnn = cnn.cuda()
##################################################################################################################
def extractors(img_path, sava_path, net):
transform = transforms.Compose([
# transforms.Resize(256),
# transforms.CenterCrop(224),
transforms.ToTensor()]
)
img = Image.open(img_path)
img = transform(img)
# print(img.shape)
x = Variable(torch.unsqueeze(img, dim=0).float(), requires_grad=False)
# print(x.shape)
x = x.cuda()
y = net(x)
y = torch.squeeze(y)
y = y.cpu().detach().numpy()
np.save(save_path, y)
##################################################################################
data_dir = '/faceSD/S/'
fea_dir = '/faceSDfea/S/'
data_list = os.listdir(data_dir)
for i in range(len(data_list)):
group_dir = data_dir + data_list[i] + '/'
group_list = os.listdir(group_dir)
save_dir = fea_dir + data_list[i] + '/'
if os.path.exists(save_dir) == False:
os.makedirs(save_dir)
for j in range(2):
img_path = group_dir + group_list[j]
save_path = save_dir + group_list[j][0:-3] + 'npy'
extractors(img_path,save_path,cnn)