U-Net

Architecture

The architecture in UNET consists of two paths. Contracting path (encoder network) followed by an expansive path (decoder network). U-Net gets its name from the architecture, like the letter U, as depicted in the image below.

Original paper by Olaf Ronneberger, Philipp Fischer, Thomas Brox

The encoder (left side) typically consists of a pre-trained classification network, such as ResNet, using convolution blocks followed by maxpool downsampling to encode the input image into feature representations at various levels. In this architecture, there is repeated application of two 3x3 convolutions. Each convolution is followed by a ReLU and batch normalization. Then a 2x2 max pooling operation is applied to reduce dimensions by half. Again, at each downsampling step, we double the number of feature channels, while we cut in half the spatial dimensions.

The decoder path (right side) consists of upsampling of the feature map followed by a 2x2 transpose convolution, which cuts the number of feature channels in half. The concatenation with the matching feature map from the contracting path, as well as a 3x3 convolutional filter (each followed by a ReLU) is also implemented. A 1x1 convolution is employed in the final layer to map the channels to the required number of classes.

U-Net differs from autoencoder architecture by overcoming the problem of compressed low dimensional representation of the input.

Code Implementation example

U-Net with batch normalization for biomedical image segmentation with pretrained weights for abnormality
segmentation in brain MRI


  import torch
  #Loading a U-Net model pre-trained for abnormality segmentation
  model = torch.hub.load('mateuszbuda/brain-segmentation-pytorch', 'unet',
      in_channels=3, out_channels=1, init_features=32, pretrained=True)

  # Download an example input image
  import urllib
  url, filename = ("https://github.com/mateuszbuda/brain-segmentation- 
  pytorch/raw/master/assets/TCGA_CS_4944.png", "TCGA_CS_4944.png")
  try: urllib.URLopener().retrieve(url, filename)
  except: urllib.request.urlretrieve(url, filename)

  import numpy as np
  from PIL import Image
  from torchvision import transforms

  input_image = Image.open(filename)
  m, s = np.mean(input_image, axis=(0, 1)), np.std(input_image, axis=(0, 1))
  preprocess = transforms.Compose([
      transforms.ToTensor(),
      transforms.Normalize(mean=m, std=s),
  ])
  input_tensor = preprocess(input_image)
  input_batch = input_tensor.unsqueeze(0)

  if torch.cuda.is_available():
      input_batch = input_batch.to('cuda')
      model = model.to('cuda')

  with torch.no_grad():
      output = model(input_batch)

  print(torch.round(output[0]))

U-Net Segmentation results (IOU) on PhC-U373 and DIC-HeLa datasets.

Parameters in model playground

Encoder network

In Model Playground, we can select feature extraction (encoding) network to use as either ResNet or EffiecientNet. The depth of the ResNet model/ EfficientNet sub type has to be specified as well.

Weights

After selecting the desired encoder network, the weights to be used for model initialization can be chosen.

Further Resources

U-Net: Convolutional Networks for Biomedical Image Segmentation

Stripping down U-Net for Segmentation of Biomedical Images on Platforms with Low Computational Budgets

Wiki entry for DeepLabv3+

Wiki entry for U-Net++

Last updated on Jun 01, 2022

Removing the risk from vision AI.

Only 13% of vision AI projects make it to production, with Hasty we boost that number to 100%.