Spaces:
Build error
Build error
temp
Browse files- how/layers/functional.py +11 -11
- how/networks/how_net.py +0 -54
how/layers/functional.py
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
import torch
|
4 |
import torch.nn.functional as F
|
5 |
|
6 |
-
import cirtorch.layers.functional as CF
|
7 |
|
8 |
|
9 |
def smoothing_avg_pooling(feats, kernel_size):
|
@@ -18,17 +18,17 @@ def smoothing_avg_pooling(feats, kernel_size):
|
|
18 |
count_include_pad=False)
|
19 |
|
20 |
|
21 |
-
def weighted_spoc(ms_feats, ms_weights):
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
|
33 |
|
34 |
def how_select_local(ms_feats, ms_masks, *, scales, features_num):
|
|
|
3 |
import torch
|
4 |
import torch.nn.functional as F
|
5 |
|
6 |
+
# import cirtorch.layers.functional as CF
|
7 |
|
8 |
|
9 |
def smoothing_avg_pooling(feats, kernel_size):
|
|
|
18 |
count_include_pad=False)
|
19 |
|
20 |
|
21 |
+
# def weighted_spoc(ms_feats, ms_weights):
|
22 |
+
# """Weighted SPoC pooling, summed over scales.
|
23 |
|
24 |
+
# :param list ms_feats: A list of feature maps, each at a different scale
|
25 |
+
# :param list ms_weights: A list of weights, each at a different scale
|
26 |
+
# :return torch.Tensor: L2-normalized global descriptor
|
27 |
+
# """
|
28 |
+
# desc = torch.zeros((1, ms_feats[0].shape[1]), dtype=torch.float32, device=ms_feats[0].device)
|
29 |
+
# for feats, weights in zip(ms_feats, ms_weights):
|
30 |
+
# desc += (feats * weights).sum((-2, -1)).squeeze()
|
31 |
+
# return CF.l2n(desc)
|
32 |
|
33 |
|
34 |
def how_select_local(ms_feats, ms_masks, *, scales, features_num):
|
how/networks/how_net.py
CHANGED
@@ -5,12 +5,6 @@ import torch
|
|
5 |
import torch.nn as nn
|
6 |
import torchvision
|
7 |
|
8 |
-
from .. import layers
|
9 |
-
from ..layers import functional as HF
|
10 |
-
from ..utils import io_helpers
|
11 |
-
|
12 |
-
NUM_WORKERS = 6
|
13 |
-
|
14 |
|
15 |
class HOWNet(nn.Module):
|
16 |
"""Network for the HOW method
|
@@ -70,7 +64,6 @@ class HOWNet(nn.Module):
|
|
70 |
|
71 |
|
72 |
# Forward
|
73 |
-
|
74 |
def features_attentions(self, x, *, scales):
|
75 |
"""Return a tuple (features, attentions) where each is a list containing requested scales"""
|
76 |
feats = []
|
@@ -99,50 +92,3 @@ class HOWNet(nn.Module):
|
|
99 |
def meta_repr(self):
|
100 |
"""Return meta representation"""
|
101 |
return str(self)
|
102 |
-
|
103 |
-
|
104 |
-
def init_network(architecture, pretrained, skip_layer, dim_reduction, smoothing, runtime):
|
105 |
-
"""Initialize HOW network
|
106 |
-
|
107 |
-
:param str architecture: Network backbone architecture (e.g. resnet18)
|
108 |
-
:param bool pretrained: Whether to start with a network pretrained on ImageNet
|
109 |
-
:param int skip_layer: How many layers of blocks should be skipped (from the end)
|
110 |
-
:param dict dim_reduction: Options for the dimensionality reduction layer
|
111 |
-
:param dict smoothing: Options for the smoothing layer
|
112 |
-
:param dict runtime: Runtime options to be stored in the network
|
113 |
-
:return HOWNet: Initialized network
|
114 |
-
"""
|
115 |
-
# Take convolutional layers as features, always ends with ReLU to make last activations non-negative
|
116 |
-
net_in = getattr(torchvision.models, architecture)(pretrained=pretrained)
|
117 |
-
if architecture.startswith('alexnet') or architecture.startswith('vgg'):
|
118 |
-
features = list(net_in.features.children())[:-1]
|
119 |
-
elif architecture.startswith('resnet'):
|
120 |
-
features = list(net_in.children())[:-2]
|
121 |
-
elif architecture.startswith('densenet'):
|
122 |
-
features = list(net_in.features.children()) + [nn.ReLU(inplace=True)]
|
123 |
-
elif architecture.startswith('squeezenet'):
|
124 |
-
features = list(net_in.features.children())
|
125 |
-
else:
|
126 |
-
raise ValueError('Unsupported or unknown architecture: {}!'.format(architecture))
|
127 |
-
|
128 |
-
if skip_layer > 0:
|
129 |
-
features = features[:-skip_layer]
|
130 |
-
backbone_dim = 2048 // (2 ** skip_layer)
|
131 |
-
|
132 |
-
att_layer = layers.attention.L2Attention()
|
133 |
-
smooth_layer = None
|
134 |
-
if smoothing:
|
135 |
-
smooth_layer = layers.pooling.SmoothingAvgPooling(**smoothing)
|
136 |
-
reduction_layer = None
|
137 |
-
if dim_reduction:
|
138 |
-
reduction_layer = layers.dim_reduction.ConvDimReduction(**dim_reduction, input_dim=backbone_dim)
|
139 |
-
|
140 |
-
meta = {
|
141 |
-
"architecture": architecture,
|
142 |
-
"backbone_dim": backbone_dim,
|
143 |
-
"outputdim": reduction_layer.out_channels if dim_reduction else backbone_dim,
|
144 |
-
"corercf_size": 32 // (2 ** skip_layer),
|
145 |
-
}
|
146 |
-
return HOWNet(nn.Sequential(*features), att_layer, smooth_layer, reduction_layer, meta, runtime)
|
147 |
-
|
148 |
-
|
|
|
5 |
import torch.nn as nn
|
6 |
import torchvision
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
class HOWNet(nn.Module):
|
10 |
"""Network for the HOW method
|
|
|
64 |
|
65 |
|
66 |
# Forward
|
|
|
67 |
def features_attentions(self, x, *, scales):
|
68 |
"""Return a tuple (features, attentions) where each is a list containing requested scales"""
|
69 |
feats = []
|
|
|
92 |
def meta_repr(self):
|
93 |
"""Return meta representation"""
|
94 |
return str(self)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|