-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add two-stage extractor model * docs: fix links * chore: update RUL datasets dependency
- Loading branch information
Showing
6 changed files
with
114 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import torch | ||
from torch import nn | ||
|
||
|
||
class TwoStageExtractor(nn.Module): | ||
"""This module combines two feature extractors into a single network. | ||
The input data is expected to be of shape `[batch_size, upper_seq_len, | ||
input_channels, lower_seq_len]`. An example would be vibration data recorded in | ||
spaced intervals, where lower_seq_len is the length of an interval and | ||
upper_seq_len is the window size of a sliding window over the intervals. | ||
The lower_stage is applied to each interval individually to extract features. | ||
The upper_stage is then applied to the extracted features of the window. | ||
The resulting feature vector should represent the window without the need to | ||
manually extract features from the raw data of the intervals. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
lower_stage: nn.Module, | ||
upper_stage: nn.Module, | ||
): | ||
""" | ||
Create a new two-stage extractor. | ||
The lower stage needs to take a tensor of shape `[batch_size, input_channels, | ||
seq_len]` and return a tensor of shape `[batch_size, lower_output_units]`. The | ||
upper stage needs to take a tensor of shape `[batch_size, upper_seq_len, | ||
lower_output_units]` and return a tensor of shape `[batch_size, | ||
upper_output_units]`. Args: lower_stage: upper_stage: | ||
""" | ||
super().__init__() | ||
|
||
self.lower_stage = lower_stage | ||
self.upper_stage = upper_stage | ||
|
||
def forward(self, inputs: torch.Tensor) -> torch.Tensor: | ||
""" | ||
Apply the two-stage extractor to the input tensor. | ||
The input tensor is expected to be of shape `[batch_size, upper_seq_len, | ||
input_channels, lower_seq_len]`. The output tensor will be of shape | ||
`[batch_size, upper_output_units]`. | ||
Args: | ||
inputs: the input tensor | ||
Returns: | ||
an output tensor of shape `[batch_size, upper_output_units]` | ||
""" | ||
batch_size, upper_seq_len, input_channels, lower_seq_len = inputs.shape | ||
inputs = inputs.reshape(-1, input_channels, lower_seq_len) | ||
inputs = self.lower_stage(inputs) | ||
inputs = inputs.reshape(batch_size, upper_seq_len, -1) | ||
inputs = torch.transpose(inputs, 1, 2) | ||
inputs = self.upper_stage(inputs) | ||
|
||
return inputs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import pytest | ||
import torch | ||
|
||
from rul_adapt.model import TwoStageExtractor | ||
|
||
|
||
@pytest.fixture() | ||
def extractor(): | ||
lower_stage = torch.nn.Sequential( | ||
torch.nn.Conv1d(3, 8, 3), | ||
torch.nn.ReLU(), | ||
torch.nn.Flatten(), | ||
torch.nn.Linear(8 * 62, 8), | ||
) | ||
upper_stage = torch.nn.Sequential( | ||
torch.nn.Conv1d(8, 8, 2), | ||
torch.nn.ReLU(), | ||
torch.nn.Flatten(), | ||
torch.nn.Linear(8 * 3, 8), | ||
) | ||
extractor = TwoStageExtractor(lower_stage, upper_stage) | ||
|
||
return extractor | ||
|
||
|
||
@pytest.fixture() | ||
def inputs(): | ||
return torch.rand(16, 4, 3, 64) | ||
|
||
|
||
def test_forward_shape(inputs, extractor): | ||
outputs = extractor(inputs) | ||
|
||
assert outputs.shape == (16, 8) | ||
|
||
|
||
def test_forward_upper_lower_interaction(inputs, extractor): | ||
one_sample = inputs[3] | ||
|
||
lower_outputs = extractor.lower_stage(one_sample) | ||
upper_outputs = extractor.upper_stage( | ||
torch.transpose(lower_outputs.unsqueeze(0), 1, 2) | ||
) | ||
outputs = extractor(inputs) | ||
|
||
assert torch.allclose(upper_outputs, outputs[3]) |