From 1891f141eaecee1103eeff1297f30ff9002a3dd8 Mon Sep 17 00:00:00 2001 From: "xiangyu.xie" Date: Fri, 12 Jun 2026 11:09:54 +0200 Subject: [PATCH] Add new 3ph model without dropout --- src/models.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/models.py b/src/models.py index 5f9891a..2274a40 100644 --- a/src/models.py +++ b/src/models.py @@ -316,5 +316,64 @@ class triplePhotonNet_260529(nn.Module): ## adapted from doublePhotonNet_260507, flat_feat = c4.view(c4.size(0), -1) # [B, 6272] + coords = self.fc(flat_feat) # [B, 6] + return coords + + +class triplePhotonNet_260611(nn.Module): ## adapted from triplePhotonNet_260529 + def __init__(self): + super().__init__() + # Backbone: deeper for 9x9 input containing 3 photons + self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1) ### 9x9 + self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1) ## 9x9 + self.conv3 = nn.Conv2d(64, 128, kernel_size=3) ## 7x7 + self.conv4 = nn.Conv2d(128, 128, kernel_size=3) ## 5x5 + + # Spatial Attention Module + self.spatial_attn = nn.Sequential( + nn.Conv2d(128, 1, kernel_size=1), + nn.Sigmoid() + ) + + self.fc = nn.Sequential( + nn.Linear(128 * 5 * 5, 512), + nn.ReLU(), + nn.Linear(512, 128), + nn.ReLU(), + nn.Linear(128, 6) + ) + + self._init_weights() + self._init_coords() + + def _init_coords(self): + # Create a coordinate grid; moved from dataset generation to model initialization for lower traffic and more flexibility + nSize = 9 # should match the input size of the model + x = np.linspace(-nSize/2. + 0.5, nSize/2. - 0.5, nSize) + y = np.linspace(-nSize/2. + 0.5, nSize/2. - 0.5, nSize) + x_grid, y_grid = np.meshgrid(x, y, indexing='ij') # (nSize,nSize), (nSize,nSize) + self.x_grid = torch.tensor(np.expand_dims(x_grid, axis=0)).float().contiguous().to('cuda') # (1, nSize, nSize) + self.y_grid = torch.tensor(np.expand_dims(y_grid, axis=0)).float().contiguous().to('cuda') # (1, nSize, nSize) + + def _init_weights(self): + for m in self.modules(): + if isinstance(m, nn.Conv2d): + nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') + elif isinstance(m, nn.Linear): + nn.init.xavier_uniform_(m.weight) + nn.init.zeros_(m.bias) + + def forward(self, x): + x = torch.cat((x, self.x_grid.expand(x.size(0), -1, -1, -1), self.y_grid.expand(x.size(0), -1, -1, -1)), dim=1) # [B, 3, 9, 9] + c1 = F.relu(self.conv1(x)) # [B, 32, 9, 9] + c2 = F.relu(self.conv2(c1)) # [B, 64, 9, 9] + c3 = F.relu(self.conv3(c2)) # [B, 128, 7, 7] + c4 = F.relu(self.conv4(c3)) # [B, 128, 5, 5] + + attn = self.spatial_attn(c4) # [B, 1, 5, 5] + c4 = c4 * attn # [B, 128, 5, 5] + + flat_feat = c4.view(c4.size(0), -1) # [B, 3200] + coords = self.fc(flat_feat) # [B, 6] return coords \ No newline at end of file