LeNet model modified to accept two inputs.
| 33 | |
| 34 | |
| 35 | class LeNetMultiInput(nn.Layer): |
| 36 | """LeNet model modified to accept two inputs.""" |
| 37 | |
| 38 | def __init__(self, num_classes: int = 10) -> None: |
| 39 | super().__init__() |
| 40 | self.num_classes = num_classes |
| 41 | |
| 42 | # Convolution layers for the first input |
| 43 | self.features1 = nn.Sequential( |
| 44 | nn.Conv2D(1, 6, 3, stride=1, padding=1), |
| 45 | nn.ReLU(), |
| 46 | nn.MaxPool2D(2, 2), |
| 47 | nn.Conv2D(6, 16, 5, stride=1, padding=0), |
| 48 | nn.ReLU(), |
| 49 | nn.MaxPool2D(2, 2), |
| 50 | ) |
| 51 | |
| 52 | # Convolution layers for the second input |
| 53 | self.features2 = nn.Sequential( |
| 54 | nn.Conv2D(1, 6, 3, stride=1, padding=1), |
| 55 | nn.ReLU(), |
| 56 | nn.MaxPool2D(2, 2), |
| 57 | nn.Conv2D(6, 16, 5, stride=1, padding=0), |
| 58 | nn.ReLU(), |
| 59 | nn.MaxPool2D(2, 2), |
| 60 | ) |
| 61 | |
| 62 | # Fully connected layers |
| 63 | if num_classes > 0: |
| 64 | self.fc = nn.Sequential( |
| 65 | nn.Linear(400 * 2, 120), # Adjusted for two inputs |
| 66 | nn.Linear(120, 84), |
| 67 | nn.Linear(84, num_classes), |
| 68 | ) |
| 69 | |
| 70 | def forward(self, input1: Tensor, input2: Tensor) -> Tensor: |
| 71 | # Apply feature extraction on both inputs |
| 72 | x1 = self.features1(input1) |
| 73 | x2 = self.features2(input2) |
| 74 | |
| 75 | # Flatten both feature maps |
| 76 | x1 = paddle.flatten(x1, 1) |
| 77 | x2 = paddle.flatten(x2, 1) |
| 78 | |
| 79 | # Concatenate the features from both inputs |
| 80 | x = paddle.concat([x1, x2], axis=1) |
| 81 | |
| 82 | if self.num_classes > 0: |
| 83 | x = self.fc(x) |
| 84 | |
| 85 | return x |
| 86 | |
| 87 | |
| 88 | class CumsumModel(nn.Layer): |