import gradio as gr import torch import kornia as K from kornia.core import Tensor from kornia.geometry.transform import resize from torchvision.utils import make_grid eps: float = 0.01 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") def read_image(f_name: str) -> Tensor: # load the image using the rust backend img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32) img = img[None] # 1xCxHxW / fp32 / [0, 1] return resize(img,(50, 50)) def predict(images): images = [read_image(f.name) for f in f_names] images = torch.stack(images, dim = 0).to(device) zca = K.enhance.ZCAWhitening(eps=eps, compute_inv=True) zca.fit(images) zca_images = zca(images) grid_zca = make_grid(zca_images, nrow=3, normalize=True) return K.tensor_to_image(grid_zca) title = 'ZCA Whitening with Kornia!' description = '''[ZCA Whitening](https://paperswithcode.com/method/zca-whitening) is an image preprocessing method that leads to a transformation of data such that the covariance matrix is the identity matrix, leading to decorrelated features: *Note that you can upload only image files, e.g. jpg, png etc and there sjould be atleast 2 images!* Learn more about [ZCA Whitening and Kornia](https://kornia.readthedocs.io/en/latest/_modules/kornia/enhance/zca.html)''' iface = gr.Interface(fn=predict, inputs=['files', gr.Slider(0.01, 1)], outputs=gr.Image(), allow_flagging="never", title=title, description=description, examples=[[ [ 'irises.jpg', 'roses.jpg', 'sunflower.jpg', 'violets.jpg', 'chamomile.jpg', 'tulips.jpg', 'Alstroemeria.jpg', 'Carnation.jpg', 'Orchid.jpg', 'Peony.jpg' ]]] ) if __name__ == "__main__": iface.launch(show_error=True)