mhamilton723 commited on
Commit
eaaab0d
1 Parent(s): 5aa316f
Files changed (2) hide show
  1. README.md +2 -2
  2. app.py +70 -14
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: FeatUp
3
  emoji: 👣⬆️
4
- colorFrom: blue
5
- colorTo: purple
6
  sdk: docker
7
  pinned: false
8
  ---
 
1
  ---
2
  title: FeatUp
3
  emoji: 👣⬆️
4
+ colorFrom: pink
5
+ colorTo: yellow
6
  sdk: docker
7
  pinned: false
8
  ---
app.py CHANGED
@@ -11,30 +11,80 @@ import os
11
  def plot_feats(image, lr, hr):
12
  assert len(image.shape) == len(lr.shape) == len(hr.shape) == 3
13
  seed_everything(0)
14
- [lr_feats_pca, hr_feats_pca], _ = pca([lr.unsqueeze(0), hr.unsqueeze(0)])
15
- fig, ax = plt.subplots(1, 3, figsize=(15, 5))
16
- ax[0].imshow(image.permute(1, 2, 0).detach().cpu())
17
- ax[0].set_title("Image")
18
- ax[1].imshow(lr_feats_pca[0].permute(1, 2, 0).detach().cpu())
19
- ax[1].set_title("Original Features")
20
- ax[2].imshow(hr_feats_pca[0].permute(1, 2, 0).detach().cpu())
21
- ax[2].set_title("Upsampled Features")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  remove_axes(ax)
23
  plt.tight_layout()
24
  plt.close(fig) # Close plt to avoid additional empty plots
25
  return fig
26
 
27
 
 
 
 
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- if __name__ == "__main__":
31
  os.environ['TORCH_HOME'] = '/tmp/.cache'
32
 
33
- options = ['dino16','vit', 'dinov2', 'clip', 'resnet50']
34
- image_input = gr.Image(label="Choose an image to featurize", type="pil", image_mode='RGB')
 
 
 
 
 
 
35
  model_option = gr.Radio(options, value="dino16", label='Choose a backbone to upsample')
36
 
37
- models = {o:torch.hub.load("mhamilton723/FeatUp", o) for o in options}
 
38
 
39
  def upsample_features(image, model_option):
40
  # Image preprocessing
@@ -60,7 +110,13 @@ if __name__ == "__main__":
60
  inputs=[image_input, model_option],
61
  outputs="plot",
62
  title="Feature Upsampling Demo",
63
- description="This demo allows you to upsample features of an image using selected models.")
 
 
 
 
 
64
 
65
- demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)
66
 
 
 
11
  def plot_feats(image, lr, hr):
12
  assert len(image.shape) == len(lr.shape) == len(hr.shape) == 3
13
  seed_everything(0)
14
+ [lr_feats_pca, hr_feats_pca], _ = pca([lr.unsqueeze(0), hr.unsqueeze(0)], dim=9)
15
+ fig, ax = plt.subplots(3, 3, figsize=(15, 15))
16
+ ax[0, 0].imshow(image.permute(1, 2, 0).detach().cpu())
17
+ ax[1, 0].imshow(image.permute(1, 2, 0).detach().cpu())
18
+ ax[2, 0].imshow(image.permute(1, 2, 0).detach().cpu())
19
+
20
+ ax[0, 0].set_title("Image", fontsize=22)
21
+ ax[0, 1].set_title("Original", fontsize=22)
22
+ ax[0, 2].set_title("Upsampled Features", fontsize=22)
23
+
24
+ ax[0, 1].imshow(lr_feats_pca[0, :3].permute(1, 2, 0).detach().cpu())
25
+ ax[0, 0].set_ylabel("PCA Components 1-3", fontsize=22)
26
+ ax[0, 2].imshow(hr_feats_pca[0, :3].permute(1, 2, 0).detach().cpu())
27
+
28
+ ax[1, 1].imshow(lr_feats_pca[0, 3:6].permute(1, 2, 0).detach().cpu())
29
+ ax[1, 0].set_ylabel("PCA Components 4-6", fontsize=22)
30
+ ax[1, 2].imshow(hr_feats_pca[0, 3:6].permute(1, 2, 0).detach().cpu())
31
+
32
+ ax[2, 1].imshow(lr_feats_pca[0, 6:9].permute(1, 2, 0).detach().cpu())
33
+ ax[2, 0].set_ylabel("PCA Components 7-9", fontsize=22)
34
+ ax[2, 2].imshow(hr_feats_pca[0, 6:9].permute(1, 2, 0).detach().cpu())
35
+
36
  remove_axes(ax)
37
  plt.tight_layout()
38
  plt.close(fig) # Close plt to avoid additional empty plots
39
  return fig
40
 
41
 
42
+ if __name__ == "__main__":
43
+ import requests
44
+ import os
45
+
46
 
47
+ def download_image(url, save_path):
48
+ response = requests.get(url)
49
+ with open(save_path, 'wb') as file:
50
+ file.write(response.content)
51
+
52
+ base_url = "https://marhamilresearch4.blob.core.windows.net/feature-upsampling-public/sample_images/"
53
+ sample_images_urls = {
54
+ "skate.jpg": base_url + "skate.jpg",
55
+ "car.jpg": base_url + "car.jpg",
56
+ "plant.png": base_url + "plant.png",
57
+ }
58
+
59
+ sample_images_dir = "sample_images"
60
+
61
+ # Ensure the directory for sample images exists
62
+ os.makedirs(sample_images_dir, exist_ok=True)
63
+
64
+ # Download each sample image
65
+ for filename, url in sample_images_urls.items():
66
+ save_path = os.path.join(sample_images_dir, filename)
67
+ # Download the image if it doesn't already exist
68
+ if not os.path.exists(save_path):
69
+ print(f"Downloading {filename}...")
70
+ download_image(url, save_path)
71
+ else:
72
+ print(f"{filename} already exists. Skipping download.")
73
 
 
74
  os.environ['TORCH_HOME'] = '/tmp/.cache'
75
 
76
+ options = ['dino16', 'vit', 'dinov2', 'clip', 'resnet50']
77
+
78
+ image_input = gr.Image(label="Choose an image to featurize",
79
+ height=480,
80
+ type="pil",
81
+ image_mode='RGB',
82
+ sources=['upload', 'webcam', 'clipboard']
83
+ )
84
  model_option = gr.Radio(options, value="dino16", label='Choose a backbone to upsample')
85
 
86
+ models = {o: torch.hub.load("mhamilton723/FeatUp", o) for o in options}
87
+
88
 
89
  def upsample_features(image, model_option):
90
  # Image preprocessing
 
110
  inputs=[image_input, model_option],
111
  outputs="plot",
112
  title="Feature Upsampling Demo",
113
+ description="This demo allows you to upsample features of an image using selected models.",
114
+ examples=[
115
+ ["sample_images/skate.jpg", "dino16"],
116
+ ["sample_images/car.jpg", "dinov2"],
117
+ ["sample_images/plant.png", "dino16"],
118
+ ]
119
 
120
+ )
121
 
122
+ demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)