-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: integrates spandrel for upscaling
- Loading branch information
1 parent
1bf53e4
commit 65deb1a
Showing
6 changed files
with
95 additions
and
33 deletions.
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
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
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
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,46 @@ | ||
import torch | ||
import torchvision.transforms.functional as F | ||
from spandrel import ImageModelDescriptor, ModelLoader | ||
from torchvision import transforms | ||
|
||
from imaginairy.schema import LazyLoadingImage | ||
from imaginairy.utils import get_device | ||
from imaginairy.utils.downloads import get_cached_url_path | ||
|
||
upscale_models = { | ||
"ultrasharp": "https://huggingface.co/lokCX/4x-Ultrasharp/resolve/1856559b50de25116a7c07261177dd128f1f5664/4x-UltraSharp.pth", | ||
"realesrgan": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth", | ||
"HAT": "https://huggingface.co/Acly/hat/resolve/main/HAT_SRx4_ImageNet-pretrain.pth?download=true", | ||
} | ||
|
||
|
||
def upscale_image(img: LazyLoadingImage, upscaler_model: str = "realesrgan"): | ||
model_path = get_cached_url_path(upscale_models[upscaler_model]) | ||
model = ModelLoader().load_from_file(model_path) | ||
|
||
assert isinstance(model, ImageModelDescriptor) | ||
|
||
device = get_device() | ||
model.to(device).eval() | ||
|
||
image_tensor = load_image(img, device) | ||
|
||
def process(image: torch.Tensor) -> torch.Tensor: | ||
with torch.no_grad(): | ||
return model(image) | ||
|
||
upscaled_img = process(image_tensor) | ||
|
||
upscaled_img = upscaled_img.squeeze(0) | ||
image = F.to_pil_image(upscaled_img) | ||
|
||
return image | ||
|
||
|
||
def load_image(img: LazyLoadingImage, device: str): | ||
|
||
transform = transforms.ToTensor() | ||
image_tensor = transform(img.as_pillow()) | ||
|
||
image_tensor = image_tensor.unsqueeze(0) | ||
return image_tensor.to(device) |
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
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