From d7c10df5d0894ecd99fb3327fe786b49f790a381 Mon Sep 17 00:00:00 2001 From: kaijieshi7 <1638961687@qq.com> Date: Wed, 19 Jan 2022 05:55:39 +0000 Subject: [PATCH 1/3] delete flowvision.models._util --- flowvision/models/_utils.py | 57 ------------------- flowvision/models/detection/backbone_utils.py | 4 +- 2 files changed, 2 insertions(+), 59 deletions(-) delete mode 100644 flowvision/models/_utils.py diff --git a/flowvision/models/_utils.py b/flowvision/models/_utils.py deleted file mode 100644 index 0dd62028..00000000 --- a/flowvision/models/_utils.py +++ /dev/null @@ -1,57 +0,0 @@ -from collections import OrderedDict - -from oneflow import nn -from typing import Dict - - -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) - """ - - __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 out diff --git a/flowvision/models/detection/backbone_utils.py b/flowvision/models/detection/backbone_utils.py index 691f0924..fceab145 100644 --- a/flowvision/models/detection/backbone_utils.py +++ b/flowvision/models/detection/backbone_utils.py @@ -9,7 +9,7 @@ ) from flowvision.layers.blocks import misc as misc_nn_ops -from .._utils import IntermediateLayerGetter +from ..segmentation.seg_utils import IntermediateLayerGetter from .. import resnet from .. import mobilenet @@ -17,7 +17,7 @@ class BackboneWithFPN(nn.Module): """ Adds a FPN on top of a model. - Internally, it uses flowvision.models._utils.IntermediateLayerGetter to + Internally, it uses flowvision.models.segmentation.seg_utils.IntermediateLayerGetter to extract a submodel that returns the feature maps specified in return_layers. The same limitations of IntermediateLayerGetter apply here. Args: From e2f1e729e66353b0448a91f449e8127ae0fd0bf9 Mon Sep 17 00:00:00 2001 From: kaijieshi7 <1638961687@qq.com> Date: Wed, 26 Jan 2022 02:21:55 +0000 Subject: [PATCH 2/3] . --- flowvision/models/detection/backbone_utils.py | 4 +- flowvision/models/layer_getter.py | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 flowvision/models/layer_getter.py diff --git a/flowvision/models/detection/backbone_utils.py b/flowvision/models/detection/backbone_utils.py index fceab145..d3524ab5 100644 --- a/flowvision/models/detection/backbone_utils.py +++ b/flowvision/models/detection/backbone_utils.py @@ -9,7 +9,7 @@ ) from flowvision.layers.blocks import misc as misc_nn_ops -from ..segmentation.seg_utils import IntermediateLayerGetter +from ..layer_getter import IntermediateLayerGetter from .. import resnet from .. import mobilenet @@ -17,7 +17,7 @@ class BackboneWithFPN(nn.Module): """ Adds a FPN on top of a model. - Internally, it uses flowvision.models.segmentation.seg_utils.IntermediateLayerGetter to + Internally, it uses flowvision.models.layer_getter.IntermediateLayerGetter to extract a submodel that returns the feature maps specified in return_layers. The same limitations of IntermediateLayerGetter apply here. Args: diff --git a/flowvision/models/layer_getter.py b/flowvision/models/layer_getter.py new file mode 100644 index 00000000..e2ac5f29 --- /dev/null +++ b/flowvision/models/layer_getter.py @@ -0,0 +1,54 @@ +from collections import OrderedDict + +from oneflow import nn +from typing import Dict + + +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) + """ + + __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 out \ No newline at end of file From 43206dfcfacfa5bc1adfac8b2bbf1ed07ea537f3 Mon Sep 17 00:00:00 2001 From: oneflow-ci-bot Date: Wed, 26 Jan 2022 03:08:02 +0000 Subject: [PATCH 3/3] auto format by CI --- flowvision/models/layer_getter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flowvision/models/layer_getter.py b/flowvision/models/layer_getter.py index e2ac5f29..a6e610f3 100644 --- a/flowvision/models/layer_getter.py +++ b/flowvision/models/layer_getter.py @@ -51,4 +51,4 @@ def forward(self, x): if name in self.return_layers: out_name = self.return_layers[name] out[out_name] = x - return out \ No newline at end of file + return out