Bases: IntermediateComponent
A single convolutional layer.
It inputs a set of feature maps; the conv layer is applied to each feature map
independently, and list of outputs is returned.
Source code in rslearn/models/conv.py
| class Conv(IntermediateComponent):
"""A single convolutional layer.
It inputs a set of feature maps; the conv layer is applied to each feature map
independently, and list of outputs is returned.
"""
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
padding: str | int = "same",
stride: int = 1,
activation: torch.nn.Module = torch.nn.ReLU(inplace=True),
batch_norm: bool = False,
):
"""Initialize a Conv.
Args:
in_channels: number of input channels.
out_channels: number of output channels.
kernel_size: kernel size, see torch.nn.Conv2D.
padding: padding to apply, see torch.nn.Conv2D.
stride: stride to apply, see torch.nn.Conv2D.
activation: activation to apply after convolution
batch_norm: whether to apply batch normalization before activation
"""
super().__init__()
self.layer = torch.nn.Conv2d(
in_channels, out_channels, kernel_size, padding=padding, stride=stride
)
self.batch_norm = torch.nn.BatchNorm2d(out_channels) if batch_norm else None
self.activation = activation
def forward(self, intermediates: Any, context: ModelContext) -> FeatureMaps:
"""Apply conv layer on each feature map.
Args:
intermediates: the previous output, which must be a FeatureMaps.
context: the model context.
Returns:
the resulting feature maps after applying the same Conv2d on each one.
"""
if not isinstance(intermediates, FeatureMaps):
raise ValueError("input to Conv must be FeatureMaps")
new_features = []
for feat_map in intermediates.feature_maps:
feat_map = self.layer(feat_map)
if self.batch_norm is not None:
feat_map = self.batch_norm(feat_map)
feat_map = self.activation(feat_map)
new_features.append(feat_map)
return FeatureMaps(new_features)
|
forward
Apply conv layer on each feature map.
Parameters:
| Name |
Type |
Description |
Default |
intermediates
|
Any
|
the previous output, which must be a FeatureMaps.
|
required
|
context
|
ModelContext
|
|
required
|
Returns:
| Type |
Description |
FeatureMaps
|
the resulting feature maps after applying the same Conv2d on each one.
|
Source code in rslearn/models/conv.py
| def forward(self, intermediates: Any, context: ModelContext) -> FeatureMaps:
"""Apply conv layer on each feature map.
Args:
intermediates: the previous output, which must be a FeatureMaps.
context: the model context.
Returns:
the resulting feature maps after applying the same Conv2d on each one.
"""
if not isinstance(intermediates, FeatureMaps):
raise ValueError("input to Conv must be FeatureMaps")
new_features = []
for feat_map in intermediates.feature_maps:
feat_map = self.layer(feat_map)
if self.batch_norm is not None:
feat_map = self.batch_norm(feat_map)
feat_map = self.activation(feat_map)
new_features.append(feat_map)
return FeatureMaps(new_features)
|