Upscaling Images in Python with ESRGANs

NeuralNine · Beginner ·💻 AI-Assisted Coding ·1y ago

Key Takeaways

This video demonstrates how to upscale images in Python using ESRGANs, a type of generative adversarial network, with tools such as PyTorch and Pillow. The video covers the installation of necessary packages, loading model parameters, creating a model instance, and applying the model to upscale an image by a factor of four.

Full Transcript

what is going on guys welcome back in this video today we're going to learn how to upscale images using python so how to take an image with low resolution and increase the resolution by a factor of four so let us get right into [Music] ited all right so we're going to learn how to upscale images in Python today for this I've already prepared an image I want to work with an image of an old guy that I found on pixabay so it's copyright free and when we zoom in you can see that we already see some pixels um when we don't have the smoothing effect here you can see that the resolution is not very high I can rightclick the image go to properties image and you can see it has 640 * 427 pixels so we're going to take this image and get four times the resolution we're going to upscale it by a factor of four and we're going to do that using this model here so here we have the P torch model weights uh from the real ESR Gan model this is a generative adversarial Network again but it's an enhanced super resolution G so ESR G um and it's made for upscaling so you can get these model weights from GitHub you will find a link to this page in the description down below just go to the releases page of real ESR Gan and then you can just look for um X4 and then plus. pth and then you're going to find this link here get upom and then all of this real ESR again X4 plus pth so this is times 4 a factor of for upscaling we're going to use this model here these model weights so I'm going to close this now I'm going to move to my coding directory and here I already have these two files so the model weights in the image and now we're going to start by installing the packages for this video today and then we're going to code the simple procedure now I am going to make a couple of comments U regarding the architecture and the functionality but this video is not going to be to learn about the architecture this is going to be just application so we're going to load the model we're going to apply it we're going to set some parameters but we're not going to discuss the methodology in detail here so we're just going to use it this is going to be focused on the application not on understanding the architecture so what we're going to do first is we're going to install a couple of packages we're going to install basic Sr for basic super resolution we're going to install real ESR game and we're going to install pillow we're going to install numpy and torch so these are the packages that we're going to need for this video today I already have them on my system so I'm not going to install them and once you have them just open up a new python file and we can start with the Imports we're going to import torch we're going to import numpy SNP we're going to import from pillow image and we're going to import from from real or actually let's start with the basic Sr first from basic Sr architectur so Arts we're going to import uh from RR dbet which stands for residual in residual dense block network uh basically the idea is you have dense blocks with a lot of residual connections as far as I understand it but I'm not an expert on this architecture um import RR DB net and then also from real ESR Gan import real ESR ganner so this is basically here we have the model itself we could say and here we have a framework that uses it that uses the model so what we're going to do now is we're going to load the model we're going to load the parameters from the model from the pytorch file that we have and then we're going to just create a model inste iate it with parameters and then we're going to use um this framework here to create an upsampler and then we're going to just apply it that's what we're going to do so the model path is going to be our um our real ESR pth file and then we're going to say that the state dictionary so what we want to load into pytorch is going to be torch load model path and map load location is going to be torch device um I think we can use the CPU here because it shouldn't be too much you can also use your GPU if you want to uh just I think Cuda should work here not sure about this but I think just torch device Cuda should work uh we're going to get a couple of warnings here because of some package version conflict so what I'm going to do here is I'm going to say import warnings this is of course not a professional fix this is just trying to make the video not so distracting so I'm going to say warnings do filter warnings and then ignore so we're just going to ignore all the warning messages so that we have a clean output or no output at all so we're going to load uh this and in particular we want to load the parameters and we want to smoothen them with an exponential moving average or we want to take the parameters that have a smooth exponential moving average applied to them so we get params EMA and then we take the state dictionary and we say model is equal to R rdbn net uh we're going to instantiate this model with the following parameters we're going to say we want to have three input channels because we have RGB red green blue we want to have three output channels because the output is also RGB so we have an image as an input and we have an image as an output so three and three we want to say that the feature maps of the first um of the first layer are going to be 64 we want to have 64 feature Maps we want to have 23 of these oh sorry Number Block 23 of these uh residual in residual out uh dense block things so we want to have 30 uh 23 such blocks um and then we also want to specify the number of growth channels this basically means how many more feature Maps we're going to add um in every layer so we want to add 32 per layer and we want to define the scale this is the most important one to be four we want to have an upscaling factor of four and then we say model. loadstate dictionary uh State dict and then strict is equal to true so this loads the model parameters from the pth file into our model here and with that we can go ahead and create an upsampler using the framework so upsampler is going to be equal to real ESR G ER and we're going to say of course the scale is four come on what kind of autoc completion is this uh model path is equal to model path why do I get scale all the time I need to fix this my NE ofm config is not ready to use yet so model is going to be equal to model no I don't want to use scale thank you um tiling is going to be disabled the idea of tiling is you can have very large images that you split into tiles and then you process them individually we're going to not do that because our image is not very um very large then we're going to say pre padding is also zero we don't want to do any padding before we do the processing and we're going to use half Precision floating Point numbers just to save some uh memory and also increase uh the the performance or to speed up the computation so that is our up sampler and now all we have to do is load the image by saying image is equal to image open image JPEG in our case we convert this to RGB red green blue and then we turn this into an numpy array by saying that it's going to be NP array of image and then we say the output is going to be we're going to have an empty placeholder here for the return value the output is going to be upsampler do enhance the image and we're going to say outscale one more time is equal to four and then the output image is going to be equal to image so pillow image from array because the result is also going to be an ire array from array output and then output image is going to be saved onto the disk as output. jpack or actually let's go with PNG all right so let's save this and let's run this now if I didn't make any mistakes this should work take some time and hopefully we then see the result let me open up Nautilus here uh actually now this opens up Nautilus yeah okay let's just switch to this um so when I open the image the original one you can see it has some pixels here when I open the output image you can see that the resolution is much better I have to zoom much more into the image to get the same uh pixel effect so I have roughly the same Zoom right now maybe like this this has much better resolution now you can see it looks a little bit cartoonish so it's not going to look perfect but this is way better than the upscaling that we had like uh before models like this now just model is from 2021 so it's not super new but 10 years ago upscaling was very very bad and this is pretty pretty decent so the guy doesn't look very different I would say I mean a little bit maybe a little bit more cartoonish but it is the same guy but with better resolution so yeah this is how you can do simple image upscaling in Python so that's it for today's video I hope you enjoyed it and I hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye

Original Description

In this video we learn how to upscale images in Python by a factor of four using ESRGANs. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 💼 Services 💼 💻 Freelancing & Tutoring: https://www.neuralnine.com/services 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to upscale images in Python using ESRGANs, covering the installation of necessary packages, loading model parameters, and applying the model to upscale an image. The video provides a practical example of how to use PyTorch and Pillow for image upscaling.

Key Takeaways
  1. Install necessary packages such as PyTorch and Pillow
  2. Load model parameters from a PyTorch file
  3. Create a model instance with the loaded parameters
  4. Apply the model to upscale an image by a factor of four
  5. Save the output image as a PNG file
  6. Run the code to test the image upscaling model
💡 ESRGANs can be used to effectively upscale images in Python using PyTorch and Pillow, with the ability to fine-tune the model for specific image upscaling tasks.

Related Reads

📰
​Agentic AI In Software Development: What Experienced Engineers Do Differently And What They Avoid
Learn how experienced engineers leverage agentic AI in software development and what pitfalls to avoid
Forbes Innovation
📰
My Checklist for Reviewing AI-Generated Code
Learn to review AI-generated code effectively with a senior's checklist, focusing on high-cost errors that can slip through normal reviews
Dev.to · Iurii Rogulia
📰
I'm building a Chrome extension that turns YouTube tutorials into interactive learning experiences. Looking for honest feedback.
Turn YouTube tutorials into interactive learning with a Chrome extension and AI-powered tools, and discover how to enhance learning experiences
Reddit r/learnprogramming
📰
Your AI-built app works in the builder but breaks on deploy with a Supabase permission denied error. Here is why, and how to fix it.
Fix Supabase permission denied errors when deploying AI-built apps by adjusting roles and permissions
Dev.to · Tori TIC
Up next
How to Create ONE PAGE Website using Claude AI (FREE & FAST)
Quick Tips - Web Desiign & Ai Tools
Watch →