How to do upsampling and down sampling using keras?

This recipe helps you do upsampling and down sampling using keras

Recipe Objective

Explaining UpSampling and DownSampling using keras. In UpSampling we insert the null-values between original values to increase the sampling rate. This process is also called Zero - stuffing. The Upsampling creates a layer with no weights it doubles the dimensions of input and so that it can be used in the generation of the model to be followed by a traditional convolutional layer. The DownSampling is reducing the features of an array or an image. Suppose you have an input layer of (32 X 32), and you have applied 2:1 downsampling, you will have (16 x 16) layer. We Can do similarly with the images.

Step 1- Import Libraries

# example of using the upsampling layer import numpy as np from keras.models import Sequential from keras.layers import UpSampling2D

Step 2 - Define the input array and reshape it.

We will define an input array and reshape it, to feed it to the model.

# define input data X = np.array([10, 6, 3, 20]) # show input data for context print(X) # reshape input data into one sample a sample with a channel X = X.reshape((1, 2, 2, 1)) # define model model = Sequential() model.add(UpSampling2D(input_shape=(2, 2, 1))) # summarize the model model.summary() # make a prediction with the model y_pred = model.predict(X) # reshape output to remove channel to make printing easier y_pred = y_pred.reshape((4, 4)) # summarize output print(y_pred)

[10  6  3 20]
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
up_sampling2d_1 (UpSampling2 (None, 4, 4, 1)           0         
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
[[10. 10.  6.  6.]
 [10. 10.  6.  6.]
 [ 3.  3. 20. 20.]
 [ 3.  3. 20. 20.]]

Step 3 - Define Sequential model

Define the model as Sequential and add UpSampling to it.

# define model model = Sequential() model.add(UpSampling2D(input_shape=(2, 2, 1))) # summarize the model model.summary()

Step 4 - Predict the model

y_pred = model.predict(X) # reshape output to remove channel to make printing easier y_pred = y_pred.reshape((4, 4)) # summarize output print(y_pred)

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.