使用PyTorch實作ResNet並提取指定層輸出之特徵

(1)提取經過多層的特徵(使用list保存,可進行indexing)
def feature_list(self, x):
(2)提取經過N層後的特徵
def intermediate_forward(self, x, layer_index):
(3)提取倒數第二層特徵(常拿來來視覺化t-SNE)
def penultimate_forward(self, x):
# function to extract the multiple features
def feature_list(self, x):
out_list = []
out = F.relu(self.bn1(self.conv1(x)))
out_list.append(out)
out = self.layer1(out)
out_list.append(out)
out = self.layer2(out)
out_list.append(out)
out = self.layer3(out)
out_list.append(out)
out = self.layer4(out)
out_list.append(out)
out = F.avg_pool2d(out, 4)
out = out.view(out.size(0), -1)
y = self.linear(out)
return y, out_list
# function to extract a specific feature
def intermediate_forward(self, x, layer_index):
out = F.relu(self.bn1(self.conv1(x)))
if layer_index == 1:
out = self.layer1(out)
elif layer_index == 2:
out = self.layer1(out)
out = self.layer2(out)
elif layer_index == 3:
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
elif layer_index == 4:
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
return out
# function to extract the penultimate features
def penultimate_forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
penultimate = self.layer4(out)
out = F.avg_pool2d(penultimate, 4)
out = out.view(out.size(0), -1)
y = self.linear(out)
return y, penultimate

--

--

--

Machine Learning | Deep Learning | https://linktr.ee/yanwei

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yanwei Liu

Yanwei Liu

Machine Learning | Deep Learning | https://linktr.ee/yanwei

More from Medium

Introduction to Neural Networks

Deep Convolutional Neural Networks (DCNNs) explained in layman's terms

Traditional machine learning algorithms for machine vision — Kapernikov

Quantization In Neural Networks