Instructions to use isp-uv-es/superIX with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- TF-Keras
How to use isp-uv-es/superIX with TF-Keras:
# Note: 'keras<3.x' or 'tf_keras' must be installed (legacy), and from_pretrained_keras was removed in huggingface_hub 1.0. # See https://github.com/keras-team/tf-keras for more details. # !pip install "huggingface_hub<1.0" tf_keras from huggingface_hub import from_pretrained_keras model = from_pretrained_keras("isp-uv-es/superIX") - Notebooks
- Google Colab
- Kaggle
Download ldm_baseline/utils.py from isp-uv-es/superIX: direct link, hf CLI and curl.
- Browser
- Download file 2.19 kB
-
https://huggingface.co/isp-uv-es/superIX/resolve/refs%2Fpr%2F1/ldm_baseline/utils.py
- Command line
-
hf download hf://isp-uv-es/superIX@refs/pr/1/ldm_baseline/utils.py
-
curl -L -o utils.py https://huggingface.co/isp-uv-es/superIX/resolve/refs%2Fpr%2F1/ldm_baseline/utils.py
2.19 kB
| import pickle | |
| from typing import Union | |
| import numpy as np | |
| import opensr_test | |
| import torch | |
| from diffusers import LDMSuperResolutionPipeline | |
| def create_stable_diffusion_model( | |
| device: Union[str, torch.device] = "cuda" | |
| ) -> LDMSuperResolutionPipeline: | |
| """Create the stable diffusion model | |
| Returns: | |
| LDMSuperResolutionPipeline: The model to use for | |
| super resolution. | |
| """ | |
| model_id = "CompVis/ldm-super-resolution-4x-openimages" | |
| pipeline = LDMSuperResolutionPipeline.from_pretrained(model_id) | |
| pipeline = pipeline.to(device) | |
| return pipeline | |
| def run_diffuser( | |
| model: LDMSuperResolutionPipeline, | |
| lr: torch.Tensor, | |
| hr: torch.Tensor, | |
| device: Union[str, torch.device] = "cuda", | |
| ) -> dict: | |
| """Run the model on the low resolution image | |
| Args: | |
| model (LDMSuperResolutionPipeline): The model to use | |
| lr (torch.Tensor): The low resolution image | |
| hr (torch.Tensor): The high resolution image | |
| device (Union[str, torch.device], optional): The device | |
| to use. Defaults to "cuda". | |
| Returns: | |
| dict: The results of the model | |
| """ | |
| # move the images to the device | |
| lr = (torch.from_numpy(lr[[3, 2, 1]]) / 2000).to(device).clamp(0, 1) | |
| if lr.shape[1] == 121: | |
| # add padding | |
| lr = torch.nn.functional.pad( | |
| lr[None], pad=(3, 4, 3, 4), mode="reflect" | |
| ).squeeze() | |
| # run the model | |
| with torch.no_grad(): | |
| sr = model(lr[None], num_inference_steps=100, eta=1) | |
| sr = torch.from_numpy(np.array(sr.images[0]) / 255).permute(2, 0, 1).float() | |
| # remove padding | |
| sr = sr[:, 3 * 4 : -4 * 4, 3 * 4 : -4 * 4] | |
| lr = lr[:, 3:-4, 3:-4] | |
| else: | |
| # run the model | |
| with torch.no_grad(): | |
| sr = model(lr[None], num_inference_steps=100, eta=1) | |
| sr = torch.from_numpy(np.array(sr.images[0]) / 255).permute(2, 0, 1).float() | |
| lr = (lr.cpu().numpy() * 2000).astype(np.uint16) | |
| hr = ((hr[0:3] / 2000).clip(0, 1) * 2000).astype(np.uint16) | |
| sr = (sr.cpu().numpy() * 2000).astype(np.uint16) | |
| results = {"lr": lr, "hr": hr, "sr": sr} | |
| return results | |