Skip to content

napari_cellseg3d.code_models.models.wnet.model

Implementation of a 3D W-Net model, based on the 2D version from https://arxiv.org/abs/1711.08506. The model performs unsupervised segmentation of 3D images.

Classes:

Name Description
Block

Basic block of the U-Net architecture.

InBlock

Input block of the U-Net architecture.

OutBlock

Output block of the U-Net architecture.

UNet

Half of the W-Net model, based on the U-Net architecture.

WNet

Implementation of a 3D W-Net model, based on the 2D version from https://arxiv.org/abs/1711.08506.

WNet_encoder

WNet3D with encoder only.

Block

Bases: Module

Basic block of the U-Net architecture.

Methods:

Name Description
__init__

Initialize the basic block.

forward

Forward pass of the basic block.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
class Block(nn.Module):
    """Basic block of the U-Net architecture."""

    def __init__(self, in_channels, out_channels, dropout=0.65):
        """Initialize the basic block.

        Args:
            in_channels (int): Number of input channels.
            out_channels (int): Number of output channels.
            dropout (float, optional): Dropout probability. Defaults to 0.65.
        """
        super(Block, self).__init__()
        # self.device = device
        self.module = nn.Sequential(
            nn.Conv3d(in_channels, in_channels, 3, padding=1),
            nn.Conv3d(in_channels, out_channels, 1),
            nn.ReLU(),
            nn.Dropout(p=dropout),
            # nn.BatchNorm3d(out_channels),
            nn.GroupNorm(num_groups=NUM_GROUPS, num_channels=out_channels),
            nn.Conv3d(out_channels, out_channels, 3, padding=1),
            nn.Conv3d(out_channels, out_channels, 1),
            nn.ReLU(),
            nn.Dropout(p=dropout),
            # nn.BatchNorm3d(out_channels),
            nn.GroupNorm(num_groups=NUM_GROUPS, num_channels=out_channels),
        )

    def forward(self, x):
        """Forward pass of the basic block."""
        return self.module(x)

__init__

__init__(in_channels, out_channels, dropout=0.65)

Initialize the basic block.

Parameters:

Name Type Description Default
in_channels int

Number of input channels.

required
out_channels int

Number of output channels.

required
dropout float

Dropout probability. Defaults to 0.65.

0.65
Source code in napari_cellseg3d/code_models/models/wnet/model.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
def __init__(self, in_channels, out_channels, dropout=0.65):
    """Initialize the basic block.

    Args:
        in_channels (int): Number of input channels.
        out_channels (int): Number of output channels.
        dropout (float, optional): Dropout probability. Defaults to 0.65.
    """
    super(Block, self).__init__()
    # self.device = device
    self.module = nn.Sequential(
        nn.Conv3d(in_channels, in_channels, 3, padding=1),
        nn.Conv3d(in_channels, out_channels, 1),
        nn.ReLU(),
        nn.Dropout(p=dropout),
        # nn.BatchNorm3d(out_channels),
        nn.GroupNorm(num_groups=NUM_GROUPS, num_channels=out_channels),
        nn.Conv3d(out_channels, out_channels, 3, padding=1),
        nn.Conv3d(out_channels, out_channels, 1),
        nn.ReLU(),
        nn.Dropout(p=dropout),
        # nn.BatchNorm3d(out_channels),
        nn.GroupNorm(num_groups=NUM_GROUPS, num_channels=out_channels),
    )

forward

forward(x)

Forward pass of the basic block.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
236
237
238
def forward(self, x):
    """Forward pass of the basic block."""
    return self.module(x)

InBlock

Bases: Module

Input block of the U-Net architecture.

Methods:

Name Description
__init__

Create the input block.

forward

Forward pass of the input block.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
class InBlock(nn.Module):
    """Input block of the U-Net architecture."""

    def __init__(self, in_channels, out_channels, dropout=0.65):
        """Create the input block.

        Args:
            in_channels (int): Number of input channels.
            out_channels (int): Number of output channels.
            dropout (float, optional): Dropout probability. Defaults to 0.65.
        """
        super(InBlock, self).__init__()
        # self.device = device
        self.module = nn.Sequential(
            nn.Conv3d(in_channels, out_channels, 3, padding=1),
            nn.ReLU(),
            nn.Dropout(p=dropout),
            # nn.BatchNorm3d(out_channels),
            nn.GroupNorm(num_groups=NUM_GROUPS, num_channels=out_channels),
            nn.Conv3d(out_channels, out_channels, 3, padding=1),
            nn.ReLU(),
            nn.Dropout(p=dropout),
            # nn.BatchNorm3d(out_channels),
            nn.GroupNorm(num_groups=NUM_GROUPS, num_channels=out_channels),
        )

    def forward(self, x):
        """Forward pass of the input block."""
        return self.module(x)

__init__

__init__(in_channels, out_channels, dropout=0.65)

Create the input block.

Parameters:

Name Type Description Default
in_channels int

Number of input channels.

required
out_channels int

Number of output channels.

required
dropout float

Dropout probability. Defaults to 0.65.

0.65
Source code in napari_cellseg3d/code_models/models/wnet/model.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def __init__(self, in_channels, out_channels, dropout=0.65):
    """Create the input block.

    Args:
        in_channels (int): Number of input channels.
        out_channels (int): Number of output channels.
        dropout (float, optional): Dropout probability. Defaults to 0.65.
    """
    super(InBlock, self).__init__()
    # self.device = device
    self.module = nn.Sequential(
        nn.Conv3d(in_channels, out_channels, 3, padding=1),
        nn.ReLU(),
        nn.Dropout(p=dropout),
        # nn.BatchNorm3d(out_channels),
        nn.GroupNorm(num_groups=NUM_GROUPS, num_channels=out_channels),
        nn.Conv3d(out_channels, out_channels, 3, padding=1),
        nn.ReLU(),
        nn.Dropout(p=dropout),
        # nn.BatchNorm3d(out_channels),
        nn.GroupNorm(num_groups=NUM_GROUPS, num_channels=out_channels),
    )

forward

forward(x)

Forward pass of the input block.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
203
204
205
def forward(self, x):
    """Forward pass of the input block."""
    return self.module(x)

OutBlock

Bases: Module

Output block of the U-Net architecture.

Methods:

Name Description
__init__

Initialize the output block.

forward

Forward pass of the output block.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
class OutBlock(nn.Module):
    """Output block of the U-Net architecture."""

    def __init__(self, in_channels, out_channels, dropout=0.65):
        """Initialize the output block.

        Args:
            in_channels (int): Number of input channels.
            out_channels (int): Number of output channels.
            dropout (float, optional): Dropout probability. Defaults to 0.65.
        """
        super(OutBlock, self).__init__()
        # self.device = device
        self.module = nn.Sequential(
            nn.Conv3d(in_channels, 64, 3, padding=1),
            nn.ReLU(),
            nn.Dropout(p=dropout),
            # nn.BatchNorm3d(64),
            nn.GroupNorm(num_groups=NUM_GROUPS, num_channels=64),
            nn.Conv3d(64, 64, 3, padding=1),
            nn.ReLU(),
            nn.Dropout(p=dropout),
            # nn.BatchNorm3d(64),
            nn.GroupNorm(num_groups=NUM_GROUPS, num_channels=64),
            nn.Conv3d(64, out_channels, 1),
        )

    def forward(self, x):
        """Forward pass of the output block."""
        return self.module(x)

__init__

__init__(in_channels, out_channels, dropout=0.65)

Initialize the output block.

Parameters:

Name Type Description Default
in_channels int

Number of input channels.

required
out_channels int

Number of output channels.

required
dropout float

Dropout probability. Defaults to 0.65.

0.65
Source code in napari_cellseg3d/code_models/models/wnet/model.py
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
def __init__(self, in_channels, out_channels, dropout=0.65):
    """Initialize the output block.

    Args:
        in_channels (int): Number of input channels.
        out_channels (int): Number of output channels.
        dropout (float, optional): Dropout probability. Defaults to 0.65.
    """
    super(OutBlock, self).__init__()
    # self.device = device
    self.module = nn.Sequential(
        nn.Conv3d(in_channels, 64, 3, padding=1),
        nn.ReLU(),
        nn.Dropout(p=dropout),
        # nn.BatchNorm3d(64),
        nn.GroupNorm(num_groups=NUM_GROUPS, num_channels=64),
        nn.Conv3d(64, 64, 3, padding=1),
        nn.ReLU(),
        nn.Dropout(p=dropout),
        # nn.BatchNorm3d(64),
        nn.GroupNorm(num_groups=NUM_GROUPS, num_channels=64),
        nn.Conv3d(64, out_channels, 1),
    )

forward

forward(x)

Forward pass of the output block.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
268
269
270
def forward(self, x):
    """Forward pass of the output block."""
    return self.module(x)

UNet

Bases: Module

Half of the W-Net model, based on the U-Net architecture.

Methods:

Name Description
__init__

Creates a U-Net model, which is half of the W-Net model.

forward

Forward pass of the U-Net model.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
class UNet(nn.Module):
    """Half of the W-Net model, based on the U-Net architecture."""

    def __init__(
        self,
        # device,
        in_channels: int,
        out_channels: int,
        channels: List[int] = None,
        softmax: bool = True,
        dropout: float = 0.65,
    ):
        """Creates a U-Net model, which is half of the W-Net model."""
        if channels is None:
            channels = [64, 128, 256, 512, 1024]
        if len(channels) != 5:
            raise ValueError(
                "Channels must be a list of channels in the form: [64, 128, 256, 512, 1024]"
            )
        super(UNet, self).__init__()
        # self.device = device
        self.channels = channels
        self.max_pool = nn.MaxPool3d(2)
        self.in_b = InBlock(in_channels, self.channels[0], dropout=dropout)
        self.conv1 = Block(channels[0], self.channels[1], dropout=dropout)
        self.conv2 = Block(channels[1], self.channels[2], dropout=dropout)
        # self.conv3 = Block(channels[2], self.channels[3], dropout=dropout)
        # self.bot = Block(channels[3], self.channels[4], dropout=dropout)
        self.bot = Block(channels[2], self.channels[3], dropout=dropout)
        # self.bot = Block(channels[1], self.channels[2], dropout=dropout)
        # self.bot = Block(channels[0], self.channels[1], dropout=dropout)
        # self.deconv1 = Block(channels[4], self.channels[3], dropout=dropout)
        self.deconv2 = Block(channels[3], self.channels[2], dropout=dropout)
        self.deconv3 = Block(channels[2], self.channels[1], dropout=dropout)
        self.out_b = OutBlock(channels[1], out_channels, dropout=dropout)
        # self.conv_trans1 = nn.ConvTranspose3d(
        #     self.channels[4], self.channels[3], 2, stride=2
        # )
        self.conv_trans2 = nn.ConvTranspose3d(
            self.channels[3], self.channels[2], 2, stride=2
        )
        self.conv_trans3 = nn.ConvTranspose3d(
            self.channels[2], self.channels[1], 2, stride=2
        )
        self.conv_trans_out = nn.ConvTranspose3d(
            self.channels[1], self.channels[0], 2, stride=2
        )

        self.sm = nn.Softmax(dim=1)
        self.softmax = softmax

    def forward(self, x):
        """Forward pass of the U-Net model."""
        in_b = self.in_b(x)
        c1 = self.conv1(self.max_pool(in_b))
        c2 = self.conv2(self.max_pool(c1))
        # c3 = self.conv3(self.max_pool(c2))
        # x = self.bot(self.max_pool(c3))
        x = self.bot(self.max_pool(c2))
        # x = self.bot(self.max_pool(c1))
        # x = self.bot(self.max_pool(in_b))
        # x = self.deconv1(
        #     torch.cat(
        #         [
        #             c3,
        #             self.conv_trans1(x),
        #         ],
        #         dim=1,
        #     )
        # )
        x = self.deconv2(
            torch.cat(
                [
                    c2,
                    self.conv_trans2(x),
                ],
                dim=1,
            )
        )
        x = self.deconv3(
            torch.cat(
                [
                    c1,
                    self.conv_trans3(x),
                ],
                dim=1,
            )
        )
        x = self.out_b(
            torch.cat(
                [
                    in_b,
                    self.conv_trans_out(x),
                ],
                dim=1,
            )
        )
        if self.softmax:
            x = self.sm(x)
        return x

__init__

__init__(in_channels: int, out_channels: int, channels: List[int] = None, softmax: bool = True, dropout: float = 0.65)

Creates a U-Net model, which is half of the W-Net model.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def __init__(
    self,
    # device,
    in_channels: int,
    out_channels: int,
    channels: List[int] = None,
    softmax: bool = True,
    dropout: float = 0.65,
):
    """Creates a U-Net model, which is half of the W-Net model."""
    if channels is None:
        channels = [64, 128, 256, 512, 1024]
    if len(channels) != 5:
        raise ValueError(
            "Channels must be a list of channels in the form: [64, 128, 256, 512, 1024]"
        )
    super(UNet, self).__init__()
    # self.device = device
    self.channels = channels
    self.max_pool = nn.MaxPool3d(2)
    self.in_b = InBlock(in_channels, self.channels[0], dropout=dropout)
    self.conv1 = Block(channels[0], self.channels[1], dropout=dropout)
    self.conv2 = Block(channels[1], self.channels[2], dropout=dropout)
    # self.conv3 = Block(channels[2], self.channels[3], dropout=dropout)
    # self.bot = Block(channels[3], self.channels[4], dropout=dropout)
    self.bot = Block(channels[2], self.channels[3], dropout=dropout)
    # self.bot = Block(channels[1], self.channels[2], dropout=dropout)
    # self.bot = Block(channels[0], self.channels[1], dropout=dropout)
    # self.deconv1 = Block(channels[4], self.channels[3], dropout=dropout)
    self.deconv2 = Block(channels[3], self.channels[2], dropout=dropout)
    self.deconv3 = Block(channels[2], self.channels[1], dropout=dropout)
    self.out_b = OutBlock(channels[1], out_channels, dropout=dropout)
    # self.conv_trans1 = nn.ConvTranspose3d(
    #     self.channels[4], self.channels[3], 2, stride=2
    # )
    self.conv_trans2 = nn.ConvTranspose3d(
        self.channels[3], self.channels[2], 2, stride=2
    )
    self.conv_trans3 = nn.ConvTranspose3d(
        self.channels[2], self.channels[1], 2, stride=2
    )
    self.conv_trans_out = nn.ConvTranspose3d(
        self.channels[1], self.channels[0], 2, stride=2
    )

    self.sm = nn.Softmax(dim=1)
    self.softmax = softmax

forward

forward(x)

Forward pass of the U-Net model.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def forward(self, x):
    """Forward pass of the U-Net model."""
    in_b = self.in_b(x)
    c1 = self.conv1(self.max_pool(in_b))
    c2 = self.conv2(self.max_pool(c1))
    # c3 = self.conv3(self.max_pool(c2))
    # x = self.bot(self.max_pool(c3))
    x = self.bot(self.max_pool(c2))
    # x = self.bot(self.max_pool(c1))
    # x = self.bot(self.max_pool(in_b))
    # x = self.deconv1(
    #     torch.cat(
    #         [
    #             c3,
    #             self.conv_trans1(x),
    #         ],
    #         dim=1,
    #     )
    # )
    x = self.deconv2(
        torch.cat(
            [
                c2,
                self.conv_trans2(x),
            ],
            dim=1,
        )
    )
    x = self.deconv3(
        torch.cat(
            [
                c1,
                self.conv_trans3(x),
            ],
            dim=1,
        )
    )
    x = self.out_b(
        torch.cat(
            [
                in_b,
                self.conv_trans_out(x),
            ],
            dim=1,
        )
    )
    if self.softmax:
        x = self.sm(x)
    return x

WNet

Bases: Module

Implementation of a 3D W-Net model, based on the 2D version from https://arxiv.org/abs/1711.08506.

The model performs unsupervised segmentation of 3D images. It first encodes the input image into a latent space using the U-Net UEncoder, then decodes it back to the original image using the U-Net UDecoder.

Methods:

Name Description
__init__

Initialize the W-Net model.

forward

Forward pass of the W-Net model. Returns the segmentation and the reconstructed image.

forward_decoder

Forward pass of the decoder part of the W-Net model.

forward_encoder

Forward pass of the encoder part of the W-Net model.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class WNet(nn.Module):
    """Implementation of a 3D W-Net model, based on the 2D version from https://arxiv.org/abs/1711.08506.

    The model performs unsupervised segmentation of 3D images.
    It first encodes the input image into a latent space using the U-Net UEncoder, then decodes it back to the original image using the U-Net UDecoder.
    """

    def __init__(
        self,
        in_channels=1,
        out_channels=2,
        num_classes=2,
        dropout=0.65,
    ):
        """Initialize the W-Net model."""
        super(WNet, self).__init__()
        self.encoder = UNet(in_channels, num_classes, softmax=True, dropout=dropout)
        self.decoder = UNet(num_classes, out_channels, softmax=False, dropout=dropout)

    def forward(self, x):
        """Forward pass of the W-Net model. Returns the segmentation and the reconstructed image."""
        enc = self.forward_encoder(x)
        return enc, self.forward_decoder(enc)

    def forward_encoder(self, x):
        """Forward pass of the encoder part of the W-Net model."""
        return self.encoder(x)

    def forward_decoder(self, enc):
        """Forward pass of the decoder part of the W-Net model."""
        return self.decoder(enc)

__init__

__init__(in_channels=1, out_channels=2, num_classes=2, dropout=0.65)

Initialize the W-Net model.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
49
50
51
52
53
54
55
56
57
58
59
def __init__(
    self,
    in_channels=1,
    out_channels=2,
    num_classes=2,
    dropout=0.65,
):
    """Initialize the W-Net model."""
    super(WNet, self).__init__()
    self.encoder = UNet(in_channels, num_classes, softmax=True, dropout=dropout)
    self.decoder = UNet(num_classes, out_channels, softmax=False, dropout=dropout)

forward

forward(x)

Forward pass of the W-Net model. Returns the segmentation and the reconstructed image.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
61
62
63
64
def forward(self, x):
    """Forward pass of the W-Net model. Returns the segmentation and the reconstructed image."""
    enc = self.forward_encoder(x)
    return enc, self.forward_decoder(enc)

forward_decoder

forward_decoder(enc)

Forward pass of the decoder part of the W-Net model.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
70
71
72
def forward_decoder(self, enc):
    """Forward pass of the decoder part of the W-Net model."""
    return self.decoder(enc)

forward_encoder

forward_encoder(x)

Forward pass of the encoder part of the W-Net model.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
66
67
68
def forward_encoder(self, x):
    """Forward pass of the encoder part of the W-Net model."""
    return self.encoder(x)

WNet_encoder

Bases: Module

WNet3D with encoder only.

Methods:

Name Description
__init__

Initialize the W-Net encoder.

forward

Forward pass of the W-Net model.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class WNet_encoder(nn.Module):
    """WNet3D with encoder only."""

    def __init__(
        self,
        in_channels=1,
        out_channels=2,
        # num_classes=2,
        softmax=True,
    ):
        """Initialize the W-Net encoder."""
        super().__init__()
        self.encoder = UNet(
            in_channels=in_channels,
            out_channels=out_channels,
            softmax=softmax,
        )

    def forward(self, x):
        """Forward pass of the W-Net model."""
        return self.encoder(x)

__init__

__init__(in_channels=1, out_channels=2, softmax=True)

Initialize the W-Net encoder.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def __init__(
    self,
    in_channels=1,
    out_channels=2,
    # num_classes=2,
    softmax=True,
):
    """Initialize the W-Net encoder."""
    super().__init__()
    self.encoder = UNet(
        in_channels=in_channels,
        out_channels=out_channels,
        softmax=softmax,
    )

forward

forward(x)

Forward pass of the W-Net model.

Source code in napari_cellseg3d/code_models/models/wnet/model.py
37
38
39
def forward(self, x):
    """Forward pass of the W-Net model."""
    return self.encoder(x)