How to create my own layers on MONAI U-Net?

I'm using MONAI on Spyder Anaconda to build a U-Net network. I want to add/modify layers starting from this baseline.

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = nets.UNet( spatial_dims = 2, in_channels = 3, out_channels = 1, channels = (4, 8, 16, 32, 64), strides = (2, 2, 2, 2), num_res_units = 3, norm = layers.Norm.BATCH, kernel_size=3,).to(device)
loss_function = losses.DiceLoss()
torch.backends.cudnn.benchmark = True
optimizer = torch.optim.Adam(model.parameters(), lr = 1e-4, weight_decay = 0)
post_pred = Compose([EnsureType(), Activations(sigmoid = True), AsDiscrete(threshold=0.5)])
post_label = Compose([EnsureType()])
inferer = SimpleInferer()
utils.set_determinism(seed=46)

My final aim is to create a MultiResUNet that has different layers such as:

class Conv2d_batchnorm(torch.nn.Module): ''' 2D Convolutional layers Arguments: num_in_filters {int} -- number of input filters num_out_filters {int} -- number of output filters kernel_size {tuple} -- size of the convolving kernel stride {tuple} -- stride of the convolution (default: {(1, 1)}) activation {str} -- activation function (default: {'relu'}) ''' def __init__(self, num_in_filters, num_out_filters, kernel_size, stride = (1,1), activation = 'relu'): super().__init__() self.activation = activation self.conv1 = torch.nn.Conv2d(in_channels=num_in_filters, out_channels=num_out_filters, kernel_size=kernel_size, stride=stride, padding = 'same') self.batchnorm = torch.nn.BatchNorm2d(num_out_filters) def forward(self,x): x = self.conv1(x) x = self.batchnorm(x) if self.activation == 'relu': return torch.nn.functional.relu(x) else: return x

This is just an example of a different Conv2d layer that I would use instead of the native one of the baseline.

Hope some of you can figure out how to proceed.

Thanks, Fede

Related questions 2 Kubernetes - PersistentVolumeClaim failed 21 GKE clusterrolebinding for cluster-admin fails with permission error 2 Using sysctls in Google Kubernetes Engine (GKE) Related questions 2 Kubernetes - PersistentVolumeClaim failed 21 GKE clusterrolebinding for cluster-admin fails with permission error 2 Using sysctls in Google Kubernetes Engine (GKE) 0 kubectl exec: Permission denied 5 kubelet Error while processing event /sys/fs/cgroup/memory/libcontainer_10010_systemd_test_default.slice 2 Error: Flag --allowed-unsafe-sysctls has been deprecated 3 Google Kubernetes Engine (GKE) cluster `error while creating mount source path` due to `read-only file system` 0 Can't deploy to GKE by a specific linux user 3 Is it possible to install the Sysbox container runtime on GKE? 0 kubectl - Error from server (Forbidden): users "[email protected]" is forbidden: User "system:serviceaccount:gke-connect:connect-agent-sa" Load 7 more related questions Show fewer related questions Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like