Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • DSA
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps
    • Software and Tools
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Go Premium
  • Deep Learning Tutorial
  • Data Analysis Tutorial
  • Python – Data visualization tutorial
  • NumPy
  • Pandas
  • OpenCV
  • R
  • Machine Learning Tutorial
  • Machine Learning Projects
  • Machine Learning Interview Questions
  • Machine Learning Mathematics
  • Deep Learning Project
  • Deep Learning Interview Questions
  • Computer Vision Tutorial
  • Computer Vision Projects
  • NLP
  • NLP Project
  • NLP Interview Questions
  • Statistics with Python
  • 100 Days of Machine Learning
Open In App

U-Net Architecture Explained

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

U-Net is a kind of neural network mainly used for image segmentation which means dividing an image into different parts to identify specific objects for example separating a tumor from healthy tissue in a medical scan. The name “U-Net” comes from the shape of its architecture which looks like the letter “U” when drawn. It is widely used in medical imaging because it performs well even with a small amount of labeled data.

U-Net Architecture

The architecture is symmetric and has three key parts:

Contracting Path (Encoder):

  • Uses small filters (3×3 pixels) to scan the image and find features.
  • Apply an activation function called ReLU to add non-linearity help the model to learn better.
  • Uses max pooling (2×2 filters) to shrink the image size while keeping important information. This helps the network focus on bigger features.

Bottleneck:

The middle of the “U” where the most compressed and abstract information is stored. It links the encoder and decoder.

Expansive Path (Decoder):

  • Uses upsampling i.e increasing image size to get back the original image size.
  • Combines information from the encoder using “skip connections.” These connections help the decoder get spatial details that might have been lost when shrinking the image.
  • Uses convolution layers again to clean up and refine the output.
U-Net Architecture -geeksforgeeks
U-Net Architecture

The above image shows U-Net turning a 572×572 image into a smaller 388×388 segmented map. It shrinks the image to capture features then upsamples to restore size using skip connections to keep details. The output labels each pixel as object or background.

How U-Net Works

After understanding the architecture, it’s important to see how U-Net actually processes data to perform segmentation:

  1. Input Image: The process starts by feeding a medical or other input image typically grayscale into the network.
  2. Feature Extraction (Encoder): The encoder extracts increasingly abstract features by applying convolutions and downsampling. At each level the spatial size decreases while the number of feature channels increases and allow the model to capture higher-level patterns.
  3. Bottleneck Processing: This is the middle part of the network where the image is reduced the most. It holds a small but very meaningful version of the image that captures the main features.
  4. Reconstruction and Localization (Decoder): The decoder begins to reconstruct the original image size through upsampling. At each level it combines decoder features with corresponding encoder features using skip connections to retain fine-grained spatial details.
  5. Skip Connections for Precision: Skip connections help preserve spatial accuracy by bringing forward detailed features from earlier layers. These are especially useful when the model needs to distinguish boundaries in segmentation tasks.
  6. Final Prediction: A 1×1 convolution at the end converts the refined feature maps into the final segmentation map where each pixel is classified into a specific class like foreground or background. This output has the same spatial resolution as the input image.

Implementation of U-Net

Now we will implement the U-Net architecture using Python 3 and the TensorFlow library. The implementation consists of three main parts:

  1. Encoder Block: The contraction path block containing two 3x3 convolutional layers with ReLU activations, followed by a 2x2 max pooling layer.
  2. Decoder Block: The expansive path block which upsamples the input, concatenates it with the corresponding encoder features and applies two 3x3 convolutional layers with ReLU activations.
  3. U-Net Model: Combining the encoder and decoder blocks to define the complete U-Net architecture.

1. Encoder

The encoder is responsible for extracting features from the input image. It applies two convolutional layers followed by a ReLU Activation to learn patterns and then uses max pooling to reduce the image size help the model focus on important features.

Python
import tensorflow as tf  def encoder_block(inputs, num_filters):      x = tf.keras.layers.Conv2D(num_filters, 3, padding='valid')(inputs)     x = tf.keras.layers.Activation('relu')(x)          x = tf.keras.layers.Conv2D(num_filters, 3, padding='valid')(x)     x = tf.keras.layers.Activation('relu')(x)      x = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=2)(x)          return x 

2. Decoder

The decoder helps restore the original image size while combining the low-level and high-level features. It starts by upsampling the feature map, resizes the corresponding encoder output (skip connection), merges them and then applies two convolution layers with ReLU.

Python
def decoder_block(inputs, skip_features, num_filters):      x = tf.keras.layers.Conv2DTranspose(num_filters, (2, 2), strides=2, padding='valid')(inputs)      skip_features = tf.keras.layers.Resizing(x.shape[1], x.shape[2])(skip_features)      x = tf.keras.layers.Concatenate()([x, skip_features])      x = tf.keras.layers.Conv2D(num_filters, 3, padding='valid')(x)     x = tf.keras.layers.Activation('relu')(x)     x = tf.keras.layers.Conv2D(num_filters, 3, padding='valid')(x)     x = tf.keras.layers.Activation('relu')(x)      return x 

3. Defining the U-Net Model

This function builds the complete U-Net architecture. It connects multiple encoder and decoder blocks and includes a bottleneck in the middle. The final output layer uses a sigmoid activation for segmentation.

Python
def unet_model(input_shape=(256, 256, 3), num_classes=1):     inputs = tf.keras.layers.Input(shape=input_shape)          # Contracting Path (Encoder)     s1 = encoder_block(inputs, 64)     s2 = encoder_block(s1, 128)     s3 = encoder_block(s2, 256)     s4 = encoder_block(s3, 512)          # Bottleneck     b1 = tf.keras.layers.Conv2D(1024, 3, padding='valid')(s4)     b1 = tf.keras.layers.Activation('relu')(b1)     b1 = tf.keras.layers.Conv2D(1024, 3, padding='valid')(b1)     b1 = tf.keras.layers.Activation('relu')(b1)          # Expansive Path (Decoder)     d1 = decoder_block(b1, s4, 512)     d2 = decoder_block(d1, s3, 256)     d3 = decoder_block(d2, s2, 128)     d4 = decoder_block(d3, s1, 64)          outputs = tf.keras.layers.Conv2D(num_classes, 1, padding='valid', activation='sigmoid')(d4)          model = tf.keras.models.Model(inputs=inputs, outputs=outputs, name='U-Net')     return model  if __name__ == '__main__':     model = unet_model(input_shape=(572, 572, 3), num_classes=2)     model.summary() 

Output:

Screenshot-2025-05-29-105001
U Net Model

4. Applying the Model to an Image

Below is an example to load an image, preprocess it, run it through the U-Net model and save the predicted segmentation mask. You can download the input image from here

Python
import numpy as np from PIL import Image from tensorflow.keras.preprocessing import image  img = Image.open('cat.png').convert('RGB') img = img.resize((572, 572)) img_array = image.img_to_array(img) / 255.0 img_array = np.expand_dims(img_array, axis=0)    model = unet_model(input_shape=(572, 572, 3), num_classes=2)  predictions = model.predict(img_array)  pred_mask = np.squeeze(predictions, axis=0) pred_mask = np.argmax(pred_mask, axis=-1).astype(np.uint8) * 255 pred_mask_img = Image.fromarray(pred_mask) pred_mask_img = pred_mask_img.resize(img.size)  pred_mask_img.save('predicted_image.jpg') pred_mask_img.show() 

Output:

1/1 [==============================] - 2s 2s/step

Predicted Image-Geeksforgeeks
Predicted Image

We can see that our model is able to segement and create boundaries around the cat which means our model is working fine. U-Net is flexible and used in many areas like image cleaning, translation, enhancement, object detection and language tasks. You can also explore some of these applications in the following articles:

  • Image Segmentation Using TensorFlow
  • Image-to-Image Translation using Pix2Pix

A

aditya_taparia
Improve
Article Tags :
  • Machine Learning
  • AI-ML-DS
  • Tensorflow
  • python
  • Deep-Learning
  • Python-Tensorflow
Practice Tags :
  • Machine Learning
  • python

Similar Reads

    Machine Learning Tutorial
    Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.Do you
    5 min read

    Introduction to Machine Learning

    Introduction to Machine Learning
    Machine learning (ML) allows computers to learn and make decisions without being explicitly programmed. It involves feeding data into algorithms to identify patterns and make predictions on new data. It is used in various applications like image recognition, speech processing, language translation,
    8 min read
    Types of Machine Learning
    Machine learning is the branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data and improve from previous experience without being explicitly programmed for every task.In simple words, ML teaches the systems to think and understand like h
    13 min read
    What is Machine Learning Pipeline?
    In artificial intelligence, developing a successful machine learning model involves more than selecting the best algorithm; it requires effective data management, training, and deployment in an organized manner. A machine learning pipeline becomes crucial in this situation. A machine learning pipeli
    7 min read
    Applications of Machine Learning
    Machine Learning (ML) is one of the most significant advancements in the field of technology. It gives machines the ability to learn from data and improve over time without being explicitly programmed. ML models identify patterns from data and use them to make predictions or decisions.Organizations
    3 min read

    Python for Machine Learning

    Machine Learning with Python Tutorial
    Python language is widely used in Machine Learning because it provides libraries like NumPy, Pandas, Scikit-learn, TensorFlow, and Keras. These libraries offer tools and functions essential for data manipulation, analysis, and building machine learning models. It is well-known for its readability an
    5 min read
    Pandas Tutorial
    Pandas (stands for Python Data Analysis) is an open-source software library designed for data manipulation and analysis. Revolves around two primary Data structures: Series (1D) and DataFrame (2D)Built on top of NumPy, efficiently manages large datasets, offering tools for data cleaning, transformat
    6 min read
    NumPy Tutorial - Python Library
    NumPy is a core Python library for numerical computing, built for handling large arrays and matrices efficiently.ndarray object – Stores homogeneous data in n-dimensional arrays for fast processing.Vectorized operations – Perform element-wise calculations without explicit loops.Broadcasting – Apply
    3 min read
    Scikit Learn Tutorial
    Scikit-learn (also known as sklearn) is a widely-used open-source Python library for machine learning. It builds on other scientific libraries like NumPy, SciPy and Matplotlib to provide efficient tools for predictive data analysis and data mining.It offers a consistent and simple interface for a ra
    3 min read
    ML | Data Preprocessing in Python
    Data preprocessing is a important step in the data science transforming raw data into a clean structured format for analysis. It involves tasks like handling missing values, normalizing data and encoding variables. Mastering preprocessing in Python ensures reliable insights for accurate predictions
    6 min read
    EDA - Exploratory Data Analysis in Python
    Exploratory Data Analysis (EDA) is a important step in data analysis which focuses on understanding patterns, trends and relationships through statistical tools and visualizations. Python offers various libraries like pandas, numPy, matplotlib, seaborn and plotly which enables effective exploration
    6 min read

    Feature Engineering

    What is Feature Engineering?
    Feature engineering is the process of turning raw data into useful features that help improve the performance of machine learning models. It includes choosing, creating and adjusting data attributes to make the model’s predictions more accurate. The goal is to make the model better by providing rele
    5 min read
    Introduction to Dimensionality Reduction
    When working with machine learning models, datasets with too many features can cause issues like slow computation and overfitting. Dimensionality reduction helps to reduce the number of features while retaining key information. Techniques like principal component analysis (PCA), singular value decom
    4 min read
    Feature Selection Techniques in Machine Learning
    In data science many times we encounter vast of features present in a dataset. But it is not necessary all features contribute equally in prediction that's where feature selection comes. It involves selecting a subset of relevant features from the original feature set to reduce the feature space whi
    5 min read
    Feature Engineering: Scaling, Normalization, and Standardization
    Feature Scaling is a technique to standardize the independent features present in the data. It is performed during the data pre-processing to handle highly varying values. If feature scaling is not done then machine learning algorithm tends to use greater values as higher and consider smaller values
    6 min read

    Supervised Learning

    Supervised Machine Learning
    Supervised machine learning is a fundamental approach for machine learning and artificial intelligence. It involves training a model using labeled data, where each input comes with a corresponding correct output. The process is like a teacher guiding a student—hence the term "supervised" learning. I
    12 min read
    Linear Regression in Machine learning
    Linear regression is a type of supervised machine-learning algorithm that learns from the labelled datasets and maps the data points with most optimized linear functions which can be used for prediction on new datasets. It assumes that there is a linear relationship between the input and output, mea
    15+ min read
    Logistic Regression in Machine Learning
    Logistic Regression is a supervised machine learning algorithm used for classification problems. Unlike linear regression which predicts continuous values it predicts the probability that an input belongs to a specific class. It is used for binary classification where the output can be one of two po
    11 min read
    Decision Tree in Machine Learning
    A decision tree is a supervised learning algorithm used for both classification and regression tasks. It has a hierarchical tree structure which consists of a root node, branches, internal nodes and leaf nodes. It It works like a flowchart help to make decisions step by step where: Internal nodes re
    9 min read
    Random Forest Algorithm in Machine Learning
    Random Forest is a machine learning algorithm that uses many decision trees to make better predictions. Each tree looks at different random parts of the data and their results are combined by voting for classification or averaging for regression. This helps in improving accuracy and reducing errors.
    5 min read
    K-Nearest Neighbor(KNN) Algorithm
    K-Nearest Neighbors (KNN) is a supervised machine learning algorithm generally used for classification but can also be used for regression tasks. It works by finding the "k" closest data points (neighbors) to a given input and makesa predictions based on the majority class (for classification) or th
    8 min read
    Support Vector Machine (SVM) Algorithm
    Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or
    9 min read
    Naive Bayes Classifiers
    Naive Bayes is a classification algorithm that uses probability to predict which category a data point belongs to, assuming that all features are unrelated. This article will give you an overview as well as more advanced use and implementation of Naive Bayes in machine learning. Illustration behind
    7 min read

    Unsupervised Learning

    What is Unsupervised Learning?
    Unsupervised learning is a branch of machine learning that deals with unlabeled data. Unlike supervised learning, where the data is labeled with a specific category or outcome, unsupervised learning algorithms are tasked with finding patterns and relationships within the data without any prior knowl
    8 min read
    K means Clustering – Introduction
    K-Means Clustering is an unsupervised machine learning algorithm that helps group data points into clusters based on their inherent similarity. Unlike supervised learning, where we train models using labeled data, K-Means is used when we have data that is not labeled and the goal is to uncover hidde
    6 min read
    Hierarchical Clustering in Machine Learning
    Hierarchical clustering is used to group similar data points together based on their similarity creating a hierarchy or tree-like structure. The key idea is to begin with each data point as its own separate cluster and then progressively merge or split them based on their similarity. Lets understand
    7 min read
    DBSCAN Clustering in ML - Density based clustering
    DBSCAN is a density-based clustering algorithm that groups data points that are closely packed together and marks outliers as noise based on their density in the feature space. It identifies clusters as dense regions in the data space separated by areas of lower density. Unlike K-Means or hierarchic
    6 min read
    Apriori Algorithm
    Apriori Algorithm is a basic method used in data analysis to find groups of items that often appear together in large sets of data. It helps to discover useful patterns or rules about how items are related which is particularly valuable in market basket analysis. Like in a grocery store if many cust
    6 min read
    Frequent Pattern Growth Algorithm
    The FP-Growth (Frequent Pattern Growth) algorithm efficiently mines frequent itemsets from large transactional datasets. Unlike the Apriori algorithm which suffers from high computational cost due to candidate generation and multiple database scans. FP-Growth avoids these inefficiencies by compressi
    5 min read
    ECLAT Algorithm - ML
    ECLAT stands for Equivalence Class Clustering and bottom-up Lattice Traversal. It is a data mining algorithm used to find frequent itemsets in a dataset. These frequent itemsets are then used to create association rules which helps to identify patterns in data. It is an improved alternative to the A
    3 min read
    Principal Component Analysis(PCA)
    PCA (Principal Component Analysis) is a dimensionality reduction technique used in data analysis and machine learning. It helps you to reduce the number of features in a dataset while keeping the most important information. It changes your original features into new features these new features don’t
    7 min read

    Model Evaluation and Tuning

    Evaluation Metrics in Machine Learning
    When building machine learning models, it’s important to understand how well they perform. Evaluation metrics help us to measure the effectiveness of our models. Whether we are solving a classification problem, predicting continuous values or clustering data, selecting the right evaluation metric al
    9 min read
    Regularization in Machine Learning
    Regularization is an important technique in machine learning that helps to improve model accuracy by preventing overfitting which happens when a model learns the training data too well including noise and outliers and perform poor on new data. By adding a penalty for complexity it helps simpler mode
    7 min read
    Cross Validation in Machine Learning
    Cross-validation is a technique used to check how well a machine learning model performs on unseen data. It splits the data into several parts, trains the model on some parts and tests it on the remaining part repeating this process multiple times. Finally the results from each validation step are a
    7 min read
    Hyperparameter Tuning
    Hyperparameter tuning is the process of selecting the optimal values for a machine learning model's hyperparameters. These are typically set before the actual training process begins and control aspects of the learning process itself. They influence the model's performance its complexity and how fas
    7 min read
    ML | Underfitting and Overfitting
    Machine learning models aim to perform well on both training data and new, unseen data and is considered "good" if:It learns patterns effectively from the training data.It generalizes well to new, unseen data.It avoids memorizing the training data (overfitting) or failing to capture relevant pattern
    5 min read
    Bias and Variance in Machine Learning
    There are various ways to evaluate a machine-learning model. We can use MSE (Mean Squared Error) for Regression; Precision, Recall, and ROC (Receiver operating characteristics) for a Classification Problem along with Absolute Error. In a similar way, Bias and Variance help us in parameter tuning and
    10 min read

    Advance Machine Learning Technique

    Reinforcement Learning
    Reinforcement Learning (RL) is a branch of machine learning that focuses on how agents can learn to make decisions through trial and error to maximize cumulative rewards. RL allows machines to learn by interacting with an environment and receiving feedback based on their actions. This feedback comes
    6 min read
    Semi-Supervised Learning in ML
    Today's Machine Learning algorithms can be broadly classified into three categories, Supervised Learning, Unsupervised Learning, and Reinforcement Learning. Casting Reinforced Learning aside, the primary two categories of Machine Learning problems are Supervised and Unsupervised Learning. The basic
    4 min read
    Self-Supervised Learning (SSL)
    In this article, we will learn a major type of machine learning model which is Self-Supervised Learning Algorithms. Usage of these algorithms has increased widely in the past times as the sizes of the model have increased up to billions of parameters and hence require a huge corpus of data to train
    8 min read
    Ensemble Learning
    Ensemble learning is a method where we use many small models instead of just one. Each of these models may not be very strong on its own, but when we put their results together, we get a better and more accurate answer. It's like asking a group of people for advice instead of just one person—each on
    8 min read

    Machine Learning Practice

    Top 50+ Machine Learning Interview Questions and Answers
    Machine Learning involves the development of algorithms and statistical models that enable computers to improve their performance in tasks through experience. Machine Learning is one of the booming careers in the present-day scenario.If you are preparing for machine learning interview, this intervie
    15+ min read
    100+ Machine Learning Projects with Source Code [2025]
    This article provides over 100 Machine Learning projects and ideas to provide hands-on experience for both beginners and professionals. Whether you're a student enhancing your resume or a professional advancing your career these projects offer practical insights into the world of Machine Learning an
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Campus Training Program
  • Explore
  • POTD
  • Job-A-Thon
  • Community
  • Videos
  • Blogs
  • Nation Skill Up
  • Tutorials
  • Programming Languages
  • DSA
  • Web Technology
  • AI, ML & Data Science
  • DevOps
  • CS Core Subjects
  • Interview Preparation
  • GATE
  • Software and Tools
  • Courses
  • IBM Certification
  • DSA and Placements
  • Web Development
  • Programming Languages
  • DevOps & Cloud
  • GATE
  • Trending Technologies
  • Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
  • Preparation Corner
  • Aptitude
  • Puzzles
  • GfG 160
  • DSA 360
  • System Design
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences