How to modify the final FC layer based on DenseNet? #1506
shenao1994
started this conversation in
General
Replies: 2 comments 3 replies
-
Is this what you had in mind? import torch.nn as nn
from monai.networks.blocks import SimpleASPP
from monai.networks.nets import DenseNet
import types
im_size = (1,) + tuple(train_ds[0]["image"].shape)
# Create model
model = DenseNet(spatial_dims=2, in_channels=1, out_channels=6,
init_features=2, growth_rate=2, block_config=(2,)).to(device)
# Print summary: before
summary(model, im_size[1:])
# Figure out number of channels leading up to ASPP
rand_im = torch.randn(im_size,device=device)
out_features = model.features(rand_im)
# Create ASPP
model.aspp = SimpleASPP(
spatial_dims=2, in_channels=out_features.shape[1], conv_out_channels=6).to(device)
# Get number of channels after ASPP
out_aspp_channels = model.aspp(out_features).shape[1]
# Modify linear layer accordingly
out_lin_channels = model.class_layers[-1].out_features
model.class_layers[-1] = nn.Linear(out_aspp_channels, out_lin_channels).to(device)
# Modify forward method
def forward_w_aspp(self, x):
x = self.features(x)
x = self.aspp(x)
x = self.class_layers(x)
return x
model.forward = types.MethodType(forward_w_aspp, model)
# Print summary: after
summary(model, im_size[1:]) |
Beta Was this translation helpful? Give feedback.
3 replies
-
I have a question related to the final layer in densenet model implementation in monai.
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to add an SPP(Spatial Pyramid Pooling) in front of densenet FC, but I can't start. I would like to ask how to modify it. Thank you!
Beta Was this translation helpful? Give feedback.
All reactions