Upscaling Images in Python with ESRGANs
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: LLM Engineering
View skill →Related Reads
📰
📰
📰
📰
Agentic AI In Software Development: What Experienced Engineers Do Differently And What They Avoid
Forbes Innovation
My Checklist for Reviewing AI-Generated Code
Dev.to · Iurii Rogulia
I'm building a Chrome extension that turns YouTube tutorials into interactive learning experiences. Looking for honest feedback.
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.
Dev.to · Tori TIC
🎓
Tutor Explanation
DeepCamp AI