Spaces:
Running
Running
mischeiwiller
commited on
Commit
•
0b5b6f2
1
Parent(s):
e6bfcaf
Update app.py
Browse files
app.py
CHANGED
@@ -6,51 +6,56 @@ import cv2
|
|
6 |
import numpy as np
|
7 |
from torchvision import transforms
|
8 |
from torchvision.utils import make_grid
|
|
|
9 |
|
10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
|
12 |
-
def read_image(
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
17 |
|
18 |
def predict(images, eps):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
return np.transpose(grid_zca,[1,2,0])
|
28 |
|
29 |
title = 'ZCA Whitening with Kornia!'
|
30 |
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:
|
31 |
-
*Note that you can upload only image files, e.g. jpg, png etc and there
|
32 |
Learn more about [ZCA Whitening and Kornia](https://kornia.readthedocs.io/en/latest/_modules/kornia/enhance/zca.html)'''
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
-
|
|
|
6 |
import numpy as np
|
7 |
from torchvision import transforms
|
8 |
from torchvision.utils import make_grid
|
9 |
+
from PIL import Image
|
10 |
|
11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
|
13 |
+
def read_image(img):
|
14 |
+
image_to_tensor = transforms.ToTensor()
|
15 |
+
if isinstance(img, np.ndarray):
|
16 |
+
img = Image.fromarray(img)
|
17 |
+
img_tensor = image_to_tensor(img)
|
18 |
+
resized_image = resize(img_tensor.unsqueeze(0), (50, 50)).squeeze(0)
|
19 |
+
return resized_image
|
20 |
|
21 |
def predict(images, eps):
|
22 |
+
eps = float(eps)
|
23 |
+
images = [read_image(img) for img in images]
|
24 |
+
images = torch.stack(images, dim=0).to(device)
|
25 |
+
zca = K.enhance.ZCAWhitening(eps=eps, compute_inv=True)
|
26 |
+
zca.fit(images)
|
27 |
+
zca_images = zca(images)
|
28 |
+
grid_zca = make_grid(zca_images, nrow=3, normalize=True).cpu().numpy()
|
29 |
+
return np.transpose(grid_zca, [1, 2, 0])
|
|
|
30 |
|
31 |
title = 'ZCA Whitening with Kornia!'
|
32 |
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:
|
33 |
+
*Note that you can upload only image files, e.g. jpg, png etc and there should be at least 2 images!*
|
34 |
Learn more about [ZCA Whitening and Kornia](https://kornia.readthedocs.io/en/latest/_modules/kornia/enhance/zca.html)'''
|
35 |
|
36 |
+
with gr.Blocks(title=title) as demo:
|
37 |
+
gr.Markdown(f"# {title}")
|
38 |
+
gr.Markdown(description)
|
39 |
+
|
40 |
+
with gr.Row():
|
41 |
+
input_images = gr.File(file_count="multiple", label="Input Images")
|
42 |
+
eps_slider = gr.Slider(minimum=0.01, maximum=1, value=0.01, label="Epsilon")
|
43 |
+
|
44 |
+
output_image = gr.Image(label="ZCA Whitened Images")
|
45 |
+
|
46 |
+
submit_button = gr.Button("Apply ZCA Whitening")
|
47 |
+
submit_button.click(fn=predict, inputs=[input_images, eps_slider], outputs=output_image)
|
48 |
+
|
49 |
+
gr.Examples(
|
50 |
+
examples=[
|
51 |
+
[
|
52 |
+
['irises.jpg', 'roses.jpg', 'sunflower.jpg', 'violets.jpg', 'chamomile.jpg',
|
53 |
+
'tulips.jpg', 'Alstroemeria.jpg', 'Carnation.jpg', 'Orchid.jpg', 'Peony.jpg'],
|
54 |
+
0.01
|
55 |
+
]
|
56 |
+
],
|
57 |
+
inputs=[input_images, eps_slider],
|
58 |
+
)
|
59 |
|
60 |
if __name__ == "__main__":
|
61 |
+
demo.launch(show_error=True)
|