Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3194 update vitautoenc for 2d #3420

Merged
merged 3 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions monai/networks/nets/vitautoenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from monai.networks.blocks.patchembedding import PatchEmbeddingBlock
from monai.networks.blocks.transformerblock import TransformerBlock
from monai.networks.layers import Conv

__all__ = ["ViTAutoEnc"]

Expand All @@ -35,6 +36,8 @@ def __init__(
in_channels: int,
img_size: Union[Sequence[int], int],
patch_size: Union[Sequence[int], int],
out_channels: int = 1,
deconv_chns: int = 16,
hidden_size: int = 768,
mlp_dim: int = 3072,
num_layers: int = 12,
Expand All @@ -49,6 +52,8 @@ def __init__(
img_size: dimension of input image.
patch_size: dimension of patch size.
hidden_size: dimension of hidden layer.
out_channels: number of output channels.
deconv_chns: number of channels for the deconvolution layers.
mlp_dim: dimension of feedforward layer.
num_layers: number of transformer blocks.
num_heads: number of attention heads.
Expand All @@ -69,14 +74,7 @@ def __init__(

super().__init__()

if not (0 <= dropout_rate <= 1):
raise ValueError("dropout_rate should be between 0 and 1.")

if hidden_size % num_heads != 0:
raise ValueError("hidden_size should be divisible by num_heads.")

if spatial_dims == 2:
raise ValueError("Not implemented for 2 dimensions, please try 3")
self.spatial_dims = spatial_dims

self.patch_embedding = PatchEmbeddingBlock(
in_channels=in_channels,
Expand All @@ -86,17 +84,18 @@ def __init__(
num_heads=num_heads,
pos_embed=pos_embed,
dropout_rate=dropout_rate,
spatial_dims=spatial_dims,
spatial_dims=self.spatial_dims,
)
self.blocks = nn.ModuleList(
[TransformerBlock(hidden_size, mlp_dim, num_heads, dropout_rate) for i in range(num_layers)]
)
self.norm = nn.LayerNorm(hidden_size)

new_patch_size = (4, 4, 4)
self.conv3d_transpose = nn.ConvTranspose3d(hidden_size, 16, kernel_size=new_patch_size, stride=new_patch_size)
self.conv3d_transpose_1 = nn.ConvTranspose3d(
in_channels=16, out_channels=1, kernel_size=new_patch_size, stride=new_patch_size
new_patch_size = [4] * self.spatial_dims
conv_trans = Conv[Conv.CONVTRANS, self.spatial_dims]
self.conv_transpose = conv_trans(hidden_size, deconv_chns, kernel_size=new_patch_size, stride=new_patch_size)
self.conv_transpose_1 = conv_trans(
in_channels=deconv_chns, out_channels=out_channels, kernel_size=new_patch_size, stride=new_patch_size
)

def forward(self, x):
Expand All @@ -107,9 +106,8 @@ def forward(self, x):
hidden_states_out.append(x)
x = self.norm(x)
x = x.transpose(1, 2)
cuberoot = round(math.pow(x.size()[2], 1 / 3))
x_shape = x.size()
x = torch.reshape(x, [x_shape[0], x_shape[1], cuberoot, cuberoot, cuberoot])
x = self.conv3d_transpose(x)
x = self.conv3d_transpose_1(x)
d = [round(math.pow(x.shape[2], 1 / self.spatial_dims))] * self.spatial_dims
x = torch.reshape(x, [x.shape[0], x.shape[1], *d])
x = self.conv_transpose(x)
x = self.conv_transpose_1(x)
return x, hidden_states_out
2 changes: 1 addition & 1 deletion tests/test_vitautoenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
for img_size in [64, 96, 128]:
for patch_size in [16]:
for pos_embed in ["conv", "perceptron"]:
for nd in [3]:
for nd in [2, 3]:
test_case = [
{
"in_channels": in_channels,
Expand Down