Skip to content

Commit d493ddd

Browse files
authored
refactor: reversed unet update
1 parent 12ac6f5 commit d493ddd

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

  • src/aind_exaspim_image_compression/machine_learning

src/aind_exaspim_image_compression/machine_learning/unet3d.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
"""
23
Created on Fri Aug 14 15:00:00 2025
34
@@ -51,7 +52,7 @@ def __init__(self, width_multiplier=1, trilinear=True):
5152
super(UNet, self).__init__()
5253

5354
# Initializations
54-
_channels = (32, 64, 128, 256)
55+
_channels = (32, 64, 128, 256, 512)
5556
factor = 2 if trilinear else 1
5657

5758
# Instance attributes
@@ -62,12 +63,14 @@ def __init__(self, width_multiplier=1, trilinear=True):
6263
self.inc = DoubleConv(1, self.channels[0])
6364
self.down1 = Down(self.channels[0], self.channels[1])
6465
self.down2 = Down(self.channels[1], self.channels[2])
65-
self.down3 = Down(self.channels[2], self.channels[3] // factor)
66+
self.down3 = Down(self.channels[2], self.channels[3])
67+
self.down4 = Down(self.channels[3], self.channels[4] // factor)
6668

6769
# Expanding layers
68-
self.up1 = Up(self.channels[3], self.channels[2] // factor, trilinear)
69-
self.up2 = Up(self.channels[2], self.channels[1] // factor, trilinear)
70-
self.up3 = Up(self.channels[1], self.channels[0], trilinear)
70+
self.up1 = Up(self.channels[4], self.channels[3] // factor, trilinear)
71+
self.up2 = Up(self.channels[3], self.channels[2] // factor, trilinear)
72+
self.up3 = Up(self.channels[2], self.channels[1] // factor, trilinear)
73+
self.up4 = Up(self.channels[1], self.channels[0], trilinear)
7174
self.outc = OutConv(self.channels[0], 1)
7275

7376
def forward(self, x):
@@ -90,11 +93,13 @@ def forward(self, x):
9093
x2 = self.down1(x1)
9194
x3 = self.down2(x2)
9295
x4 = self.down3(x3)
96+
x5 = self.down4(x4)
9397

9498
# Expanding layers
95-
x = self.up1(x4, x3)
96-
x = self.up2(x, x2)
97-
x = self.up3(x, x1)
99+
x = self.up1(x5, x4)
100+
x = self.up2(x, x3)
101+
x = self.up3(x, x2)
102+
x = self.up4(x, x1)
98103
logits = self.outc(x)
99104
return logits
100105

@@ -188,14 +193,7 @@ def __init__(self, in_channels, out_channels):
188193

189194
# Instance attributes
190195
self.maxpool_conv = nn.Sequential(
191-
nn.Conv3d(
192-
in_channels,
193-
out_channels,
194-
kernel_size=3,
195-
stride=2,
196-
padding=1
197-
),
198-
DoubleConv(out_channels, out_channels)
196+
nn.MaxPool3d(2), DoubleConv(in_channels, out_channels)
199197
)
200198

201199
def forward(self, x):
@@ -275,7 +273,7 @@ def forward(self, x1, x2):
275273
276274
Returns
277275
-------
278-
x : torch.Tensor
276+
torch.Tensor
279277
Output tensor after upsampling, concatenation with the skip
280278
connection, and double convolution. The output shape is
281279
(B, out_channels, D, H2, W2).
@@ -289,8 +287,7 @@ def forward(self, x1, x2):
289287
[diffX // 2, diffX - diffX // 2, diffY // 2, diffY - diffY // 2],
290288
)
291289
x = torch.cat([x2, x1], dim=1)
292-
x = self.conv(x)
293-
return x
290+
return self.conv(x)
294291

295292

296293
class OutConv(nn.Module):

0 commit comments

Comments
 (0)