当前位置:网站首页>Understanding of intermediatelayergetter
Understanding of intermediatelayergetter
2022-06-13 03:02:00 【Python's path to becoming a God】
First look at this IntermediateLayerGetter name ,( Adjust the middle layer to obtain , Translated by myself ), Not very understandable , Only by looking at its explanation and combining with the examples given can we understand .
understand : This class is to get a Model In, you specify which layers of output you want to get , Then the output of these layers will be in an ordered Dictionary , In the dictionary key This is the class just passed in ,value Namely feature After specifying the output of the required layer .
This is the sample in the source code :
import torchvision
import torch
m = torchvision.models.resnet18(pretrained=True)
new_m = torchvision.models._utils.IntermediateLayerGetter(m, {'layer1': 'feat1', 'layer3': 'feat2'})
out = new_m(torch.rand(1, 3, 224, 224))
print([(k, v.shape) for k, v in out.items()])
Output :
[('feat1', torch.Size([1, 64, 56, 56])), ('feat2', torch.Size([1, 256, 14, 14]))]The fourth line of the code passes in a Model, the second OrderedDict Parameters ,key Is to specify to get Model Which layers in the output ,value Yes, the output of these layers will be placed in a OrderedDict in , In this ordered Dictionary key It's the one in front value, It is equivalent to setting the output according to your preferences key.
Let's see how the code is implemented :
class IntermediateLayerGetter(nn.ModuleDict):
"""
Module wrapper that returns intermediate layers from a model
It has a strong assumption that the modules have been registered
into the model in the same order as they are used.
This means that one should **not** reuse the same nn.Module
twice in the forward if you want this to work.
Additionally, it is only able to query submodules that are directly
assigned to the model. So if `model` is passed, `model.feature1` can
be returned, but not `model.feature1.layer2`.
Args:
model (nn.Module): model on which we will extract the features
return_layers (Dict[name, new_name]): a dict containing the names
of the modules for which the activations will be returned as
the key of the dict, and the value of the dict is the name
of the returned activation (which the user can specify).
Examples::
>>> m = torchvision.models.resnet18(pretrained=True)
>>> # extract layer1 and layer3, giving as names `feat1` and feat2`
>>> new_m = torchvision.models._utils.IntermediateLayerGetter(m,
>>> {'layer1': 'feat1', 'layer3': 'feat2'})
>>> out = new_m(torch.rand(1, 3, 224, 224))
>>> print([(k, v.shape) for k, v in out.items()])
>>> [('feat1', torch.Size([1, 64, 56, 56])),
>>> ('feat2', torch.Size([1, 256, 14, 14]))]
"""
_version = 2
__annotations__ = {
"return_layers": Dict[str, str],
}
def __init__(self, model: nn.Module, return_layers: Dict[str, str]) -> None:
if not set(return_layers).issubset([name for name, _ in model.named_children()]):
raise ValueError("return_layers are not present in model")
orig_return_layers = return_layers
return_layers = {str(k): str(v) for k, v in return_layers.items()}
layers = OrderedDict()
for name, module in model.named_children():
layers[name] = module
if name in return_layers:
del return_layers[name]
if not return_layers:
break
super(IntermediateLayerGetter, self).__init__(layers)
self.return_layers = orig_return_layers
def forward(self, x):
out = OrderedDict()
for name, module in self.items():
x = module(x)
if name in self.return_layers:
out_name = self.return_layers[name]
out[out_name] = x
return outLet's analyze def __init__(self, model: nn.Module, return_layers: Dict[str, str]) -> None:
This method is actually traversing Model, I've been looking for our incoming return_layers layer , Until I find return_layers The last one inside , Then the traversal ends . We need to pay attention to it ,return_layers Inside key In the same order Model As defined in . Find it this way return_layers All in key stay Model The corresponding layer in ( This layer may be convoluted , Or maybe ReLu, Or maybe Sequential, Just look Model How to define ). The layers found are stored in
layers = OrderedDict() in , Then use this layers Initialize this class IntermediateLayerGetter( This layers In fact, there are Model From the first floor to the return_layers The last layer in , It's like Model Yes 1-10 layer ,return_layers yes {'1':'one', '5':'five'}, such layers The content of is {'1':'one', '2':'two','3':'three','4':'four','5':'five'}, At this time, some students asked , I just 1 and 5 ah , Don't worry. , The key is the following forward 了 )reanalysis forward(self, x), In this method, a out = OrderedDict() Orderly dictionary
Although it says layers Deposit 1-5 layer , But in forward in , Pass in a feature Time is also from layers Take one layer in turn for convolution , Only the corresponding layer is in return_layers It's stored in out in , such out Only the output of the layer we need .
At first, I thought it was to get the layer I needed , For example, the third layer convolution , The fourth level ReLu, But by init not have understood until then , It contains from the first floor to the fourth floor , Only in forward propagation can the results of the required layer be output . The name makes me think Model Layers required in , But it's over forward And examples to understand , Is to get the output result of the required layer , Namely IntermediateLayerGetter It's not for a separate third layer convolution , The fourth level ReLu, It's for you feature The output results after these two layers .
Of course , We can also get IntermediateLayerGetter Returned object , Then obtain the corresponding layer through logic .
边栏推荐
- Vs Code modify default terminal_ Modify the default terminal opened by vs Code
- Simple use of leaflet - offline map scheme
- Data processing in detailed machine learning (II) -- Feature Normalization
- Ijkplayer source code -- Library loading and initialization
- Summary of the latest IOS interview questions in June 2020 (answers)
- Collection of IOS development interview and underlying learning videos
- Six special GPU products for domestic aircraft passed the appraisal and review
- JS merge multiple string arrays to maintain the original order and remove duplicates
- Ijkplayer source code --- decode
- Beginner development process_ Project development FAQs
猜你喜欢

Mvcc and bufferpool (VI)

Pycharm installation pyqt5 and its tools (QT designer, pyuic, pyrcc) detailed tutorial

CDN single page reference of indexbar index column in vant framework cannot be displayed normally

The extra money we made in those years

JVM virtual machine stack (III)

Node uses post to request req Pit with empty body

Linked list: palindrome linked list

OneNote User Guide (1)

MySQL index optimization (4)
![[data and Analysis Visualization] D3 introductory tutorial 1-d3 basic knowledge](/img/a8/468a0c4d4a009e155679898fac4b81.jpg)
[data and Analysis Visualization] D3 introductory tutorial 1-d3 basic knowledge
随机推荐
【 enregistrement pytorch】 paramètre et tampon des variables pytorch. Self. Register Buffer (), self. Register Paramètre ()
Six special GPU products for domestic aircraft passed the appraisal and review
Vant框架中关于IndexBar索引栏的CDN单页面引用,无法正常展示
Introduction to Kestrel_ Introduction to kestrel web server
Modify the color of El input, textarea and El checkbox when they are disabled
MySQL index optimization (4)
Pytorch record: pytorch variables parameter and buffer. self. register_ buffer()、self. register_ parameter()
Kotlin memorandum
Ijkplayer source code - audio playback
Linked list: orderly circular linked list
Entity framework extends the actual combat, small project reconfiguration, no trouble
Ijkplayer source code --- decode
Vant realizes the adaptation of mobile terminal
Five old code farmers, program life review: peace of mind is not the place to go
Image classification system based on support vector machine (Matlab GUI interface version)
The extra money we made in those years
JMeter quick start
Prometheus node_exporter安装并注册为服务
JVM heap (IV)
Ijkplayer source code - rendering