mischeiwiller commited on
Commit
0b5b6f2
1 Parent(s): e6bfcaf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -36
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(f_name):
13
- image_to_tensor = transforms.ToTensor()
14
- img = image_to_tensor(cv2.imread(f_name, cv2.IMREAD_COLOR))
15
- resized_image = resize(img,(50, 50))
16
- return resized_image
 
 
17
 
18
  def predict(images, eps):
19
- eps = float(eps)
20
- f_names = [img.name for img in images]
21
- images = [read_image(f) for f in f_names]
22
- images = torch.stack(images, dim = 0).to(device)
23
- zca = K.enhance.ZCAWhitening(eps=eps, compute_inv=True)
24
- zca.fit(images)
25
- zca_images = zca(images)
26
- grid_zca = make_grid(zca_images, nrow=3, normalize=True).cpu().numpy()
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 sjould be atleast 2 images!*
32
  Learn more about [ZCA Whitening and Kornia](https://kornia.readthedocs.io/en/latest/_modules/kornia/enhance/zca.html)'''
33
 
34
- iface = gr.Interface(fn=predict,
35
- inputs=['files', gr.Slider(0.01, 1)],
36
- outputs=gr.Image(),
37
- allow_flagging="never",
38
- title=title,
39
- description=description,
40
- examples=[[
41
- [
42
- 'irises.jpg',
43
- 'roses.jpg',
44
- 'sunflower.jpg',
45
- 'violets.jpg',
46
- 'chamomile.jpg',
47
- 'tulips.jpg',
48
- 'Alstroemeria.jpg',
49
- 'Carnation.jpg',
50
- 'Orchid.jpg',
51
- 'Peony.jpg'
52
- ], 0.01]]
53
- )
 
 
 
54
 
55
  if __name__ == "__main__":
56
- iface.launch(show_error=True)
 
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)