-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing truss files for real esrgan #68
Open
htrivedi99
wants to merge
3
commits into
basetenlabs:main
Choose a base branch
from
htrivedi99:real-esrgan
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Real-ESRGAN Truss | ||
|
||
This is a [Truss](https://truss.baseten.co/) for Real-ESRGAN which is an AI image upscaling model. | ||
Open-source image generation models like Stable Diffusion 1.5 can sometime produce blurry or low resolution images. Using Real-ESRGAN, those low quality images can be upscaled making them look sharper and more detailed. | ||
|
||
## Deployment | ||
|
||
First, clone this repository: | ||
|
||
``` | ||
git clone https://github.com/basetenlabs/truss-examples/ | ||
cd real-esrgan-truss | ||
``` | ||
|
||
Before deployment: | ||
|
||
1. Make sure you have a [Baseten account](https://app.baseten.co/signup) and [API key](https://app.baseten.co/settings/account/api_keys). | ||
2. Install the latest version of Truss: `pip install --upgrade truss` | ||
|
||
With `real-esrgan-truss` as your working directory, you can deploy the model with: | ||
|
||
``` | ||
truss push | ||
``` | ||
|
||
Paste your Baseten API key if prompted. | ||
|
||
For more information, see [Truss documentation](https://truss.baseten.co). | ||
|
||
## API route: `predict` | ||
The predict route is the primary method for upscaling an image. In order to send the image to our model, the image must first be converted into a base64 string. | ||
|
||
- __image__: The image converted to a base64 string | ||
|
||
|
||
## Invoking the model | ||
|
||
```sh | ||
truss predict -d '{"image": "<BASE64-STRING-HERE>"}' | ||
``` | ||
|
||
You can also use python to call the model: | ||
|
||
```python | ||
BASE64_PREAMBLE = "data:image/png;base64," | ||
|
||
def pil_to_b64(pil_img): | ||
buffered = BytesIO() | ||
pil_img.save(buffered, format="PNG") | ||
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8") | ||
return img_str | ||
|
||
def b64_to_pil(b64_str): | ||
return Image.open(BytesIO(base64.b64decode(b64_str.replace(BASE64_PREAMBLE, "")))) | ||
|
||
img = Image.open("/path/to/image/ship.jpeg") | ||
b64_img = pil_to_b64(img) | ||
|
||
headers = {"Authorization": f"Api-Key <BASETEN-API-KEY>"} | ||
data = {"image": b64_img} | ||
res = requests.post("https://model-{MODEL_ID}.api.baseten.co/development/predict", headers=headers, json=data) | ||
output = res.json() | ||
|
||
result_b64 = output.get("model_output").get("upscaled_image") | ||
pil_img = b64_to_pil(result_b64) | ||
pil_img.save("upscaled_output_img.png") | ||
``` | ||
|
||
The model returns a JSON object containing the key `upscaled_image`, which is the upscaled image as a base64 string. | ||
|
||
## Results | ||
|
||
<div style="display: flex; justify-content: space-between;"> | ||
<div style="flex: 1; margin-right: 10px;"> | ||
<img src="ship.jpeg" alt="original image" style="width: 100%;"> | ||
<p>Original Image Stable Diffusion 1.5</p> | ||
</div> | ||
<div style="flex: 1;"> | ||
<img src="result_image.jpeg" alt="upscaled image" style="width: 100%;"> | ||
<p>Upscaled Image</p> | ||
</div> | ||
</div> | ||
|
||
<div style="display: flex; justify-content: space-between;"> | ||
<div style="flex: 1; margin-right: 10px;"> | ||
<img src="racecar.jpeg" alt="original image" style="width: 100%;"> | ||
<p>Original Image SDXL</p> | ||
</div> | ||
<div style="flex: 1;"> | ||
<img src="racecar_upscaled.jpeg" alt="upscaled image" style="width: 100%;"> | ||
<p>Upscaled Image</p> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
environment_variables: {} | ||
external_package_dirs: [] | ||
model_metadata: | ||
example_model_input: {"image": "BASE64-STRING-HERE"} | ||
pretty_name: Real ESRGAN | ||
model_name: Real ESRGAN | ||
python_version: py310 | ||
requirements: | ||
- numpy==1.23.5 | ||
- torch==2.0.1 | ||
- torchvision==0.15.2 | ||
- facexlib==0.3.0 | ||
- gfpgan==1.3.8 | ||
- basicsr==1.4.2 | ||
- opencv-python==4.8.0.76 | ||
- opencv-python-headless==4.8.1.78 | ||
- Pillow==9.4.0 | ||
- tqdm==4.66.1 | ||
resources: | ||
cpu: "3" | ||
memory: 14Gi | ||
use_gpu: true | ||
accelerator: T4 | ||
secrets: {} | ||
system_packages: | ||
- libgl1-mesa-glx | ||
- libglib2.0-0 | ||
external_data: | ||
- url: https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth | ||
local_data_path: weights/RealESRGAN_x4plus.pth |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import base64 | ||
import io | ||
import os | ||
import subprocess | ||
import sys | ||
from io import BytesIO | ||
from typing import Dict | ||
|
||
import numpy as np | ||
from PIL import Image | ||
|
||
git_repo_url = "https://github.com/xinntao/Real-ESRGAN.git" | ||
git_clone_command = ["git", "clone", git_repo_url] | ||
commit_hash = "5ca1078535923d485892caee7d7804380bfc87fd" | ||
original_working_directory = os.getcwd() | ||
|
||
try: | ||
subprocess.run(git_clone_command, check=True) | ||
print("Git repository cloned successfully!") | ||
|
||
os.chdir(os.path.join(original_working_directory, "Real-ESRGAN")) | ||
checkout_command = ["git", "checkout", commit_hash] | ||
subprocess.run(checkout_command, check=True) | ||
subprocess.run([sys.executable, "setup.py", "develop"], check=True) | ||
|
||
except Exception as e: | ||
print(e) | ||
raise Exception("Error cloning Real-ESRGAN repo :(") | ||
|
||
sys.path.append(os.path.join(os.getcwd())) | ||
|
||
from basicsr.archs.rrdbnet_arch import RRDBNet | ||
from realesrgan import RealESRGANer | ||
|
||
|
||
class Model: | ||
def __init__(self, **kwargs): | ||
self._data_dir = kwargs["data_dir"] | ||
self.model_checkpoint_path = os.path.join( | ||
original_working_directory, | ||
self._data_dir, | ||
"weights", | ||
"RealESRGAN_x4plus.pth", | ||
) | ||
self.model = None | ||
|
||
def pil_to_b64(self, pil_img): | ||
buffered = BytesIO() | ||
pil_img.save(buffered, format="PNG") | ||
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8") | ||
return img_str | ||
|
||
def load(self): | ||
rrdb_net_model = RRDBNet( | ||
num_in_ch=3, | ||
num_out_ch=3, | ||
num_feat=64, | ||
num_block=23, | ||
num_grow_ch=32, | ||
scale=4, | ||
) | ||
netscale = 4 | ||
|
||
self.model = RealESRGANer( | ||
scale=netscale, | ||
model_path=self.model_checkpoint_path, | ||
model=rrdb_net_model, | ||
tile=0, | ||
tile_pad=10, | ||
pre_pad=0, | ||
half=True, | ||
) | ||
|
||
def predict(self, request: Dict) -> Dict: | ||
image = request.get("image") | ||
scale = 4 | ||
|
||
pil_img = Image.open(io.BytesIO(base64.decodebytes(bytes(image, "utf-8")))) | ||
pil_image_array = np.asarray(pil_img) | ||
|
||
output, _ = self.model.enhance(pil_image_array, outscale=scale) | ||
output = Image.fromarray(output) | ||
output_b64 = self.pil_to_b64(output) | ||
return {"upscaled_image": output_b64} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to drop all this and just add to the requirements