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

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .