An API used to upscale the resolution of a given input image. Built with Pytorch, FastAPI and Docker.
The aim of this project was to upscale the resolution of images using deep learning techniques and algorithms such as GANs and also deploy the model as an API using FastAPI framework. The model was developed using Pytorch and ESRGAN.
Note: The model has been tested with both GPU and CPU with the GPU version giving faster response times. To run on GPU make sure you have cuda installed and the pytorch-cuda11 installed for it to work properly.
To run the model with GPU, change this line in the app.py file:
device =torch.device('cpu')
to
device =torch.device('cuda')
The Super-Resolution Generative Adversarial Network (SRGAN) is a seminal work that is capable of generating realistic textures during single image super-resolution. However, the hallucinated details are often accompanied with unpleasant artifacts. To further enhance the visual quality, we thoroughly study three key components of SRGAN - network architecture, adversarial loss and perceptual loss, and improve each of them to derive an Enhanced SRGAN (ESRGAN). In particular, we introduce the Residual-in-Residual Dense Block (RRDB) without batch normalization as the basic network building unit. Moreover, we borrow the idea from relativistic GAN to let the discriminator predict relative realness instead of the absolute value. Finally, we improve the perceptual loss by using the features before activation, which could provide stronger supervision for brightness consistency and texture recovery. Benefiting from these improvements, the proposed ESRGAN achieves consistently better visual quality with more realistic and natural textures than SRGAN and won the first place in the PIRM2018-SR Challenge.
Credit: ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks
Xintao Wang, Ke Yu, Shixiang Wu, Jinjin Gu, Yihao Liu, Chao Dong, Chen Change Loy, Yu Qiao, Xiaoou Tang
To run a demo or carry out testing with the API it's best to do that with Google Colab. To run/test the API on Google Colab do the following:
- Clone the repository.
- Open a Google Colab instance and upload the .ipynb file to that instance.
- Run each cell until the last cell and you should be able to view the API with a link that has the name ngrok in it.
- Clone the repository to your local machine
- Install the requirements from the requirements.txt file:
pip install -r requirements.txt
- Then from your command line run:
python -m uvicorn --port 5000 --host 127.0.0.1 app:app --reload
Then you can view the site on your local server: http://127.0.0.1:5000/
To build the docker container image;
- Install docker
- Clone the repository
- Then from your command line run:
docker build . -t superimageres
- Run the docker container:
docker run -d --name mycontainer -p 5000:5000 superimageres
Then you can view the site on your local server: http://127.0.0.1:5000/
The api can be deployed using the dockerfile or the procfile on heroku.
Assuming you have git and heroku cli installed just carry out the following steps:
- Clone the repository
git clone https://github.com/Nneji123/Super-Resolution-Model.git
- Change the working directory
cd Super-Resolution-Model
- Create the heroku app
heroku create your-app-name
Replace your-app-name with the name of your choosing.
- Set the heroku cli git remote to that app
heroku git:remote your-app-name
- Set the heroku stack setting to container
heroku stack:set container
- Push to heroku
git push heroku main
- Clone the repository and copy the contents to your own repository on github
- Create a new app name on Heroku.
- Choose deploy with Github in deployment options on Heroku and after it's built and deployed you should be able to view your app.
The api and its documentation can be viewed here: https://super-resolution-api.herokuapp.com/docs or https://super-resolution-api.herokuapp.com/redoc