mhamilton723 commited on
Commit
5aa316f
1 Parent(s): 804efd3
Files changed (5) hide show
  1. Dockerfile +24 -0
  2. README.md +3 -6
  3. app.py +50 -30
  4. pre-requirements.txt +0 -15
  5. requirements.txt +0 -1
Dockerfile ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an NVIDIA CUDA base image with CUDA 11.8 and Ubuntu 20.04
2
+ FROM mhamilton723/featup:latest
3
+
4
+ # Set a working directory
5
+ WORKDIR /app
6
+
7
+ RUN pip3 install gradio
8
+
9
+ # Copy your application files into the container
10
+ COPY . /app
11
+
12
+ # Expose the port Streamlit will run on
13
+ EXPOSE 7860
14
+
15
+ RUN mkdir -m 700 flagged
16
+
17
+ ENV PYTHONUNBUFFERED=1 \
18
+ GRADIO_ALLOW_FLAGGING=never \
19
+ GRADIO_NUM_PORTS=1 \
20
+ GRADIO_SERVER_NAME=0.0.0.0 \
21
+ SYSTEM=spaces
22
+
23
+ # Set the command to run your Streamlit app
24
+ CMD ["python3", "app.py"]
README.md CHANGED
@@ -1,13 +1,10 @@
1
  ---
2
  title: FeatUp
3
  emoji: 👣⬆️
4
- colorFrom: green
5
- colorTo: indigo
6
- sdk: streamlit
7
- sdk_version: 1.32.2
8
- app_file: app.py
9
  pinned: false
10
- license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: FeatUp
3
  emoji: 👣⬆️
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: docker
 
 
7
  pinned: false
 
8
  ---
9
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,46 +1,66 @@
1
- import streamlit as st
2
  import torch
3
  import torchvision.transforms as T
4
  from PIL import Image
 
 
 
 
5
 
6
- # Assuming the necessary packages (featup, clip, etc.) are installed and accessible
7
- from featup.util import norm, unnorm
8
- from featup.plotting import plot_feats
9
 
10
- # Setup - ensure the repository content is accessible in the environment
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Streamlit UI
13
- st.title("Feature Upsampling Demo")
14
 
15
- # File uploader
16
- uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
17
- if uploaded_file is not None:
18
- image = Image.open(uploaded_file).convert("RGB")
19
 
20
- # Image preprocessing
21
- input_size = 224
22
- transform = T.Compose([
23
- T.Resize(input_size),
24
- T.CenterCrop((input_size, input_size)),
25
- T.ToTensor(),
26
- norm
27
- ])
28
 
29
- image_tensor = transform(image).unsqueeze(0) # Assuming CUDA is available, .cuda()
 
30
 
31
- # Model selection
32
- model_option = st.selectbox(
33
- 'Choose a model for feature upsampling',
34
- ('dino16', 'dinov2', 'clip', 'resnet50')
35
- )
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- if st.button('Upsample Features'):
38
  # Load the selected model
39
- upsampler = torch.hub.load("mhamilton723/FeatUp", model_option).cuda()
40
  hr_feats = upsampler(image_tensor)
41
  lr_feats = upsampler.model(image_tensor)
 
 
 
 
 
 
 
 
 
 
42
 
43
- # Plotting - adjust the plot_feats function or find an alternative to display images in Streamlit
44
- # This step will likely need customization to display within Streamlit's interface
45
- plot_feats(unnorm(image_tensor)[0], lr_feats[0], hr_feats[0])
46
 
 
1
+ import matplotlib.pyplot as plt
2
  import torch
3
  import torchvision.transforms as T
4
  from PIL import Image
5
+ import gradio as gr
6
+ from featup.util import norm, unnorm, pca, remove_axes
7
+ from pytorch_lightning import seed_everything
8
+ import os
9
 
 
 
 
10
 
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
41
+ input_size = 224
42
+ transform = T.Compose([
43
+ T.Resize(input_size),
44
+ T.CenterCrop((input_size, input_size)),
45
+ T.ToTensor(),
46
+ norm
47
+ ])
48
+ image_tensor = transform(image).unsqueeze(0).cuda()
49
 
 
50
  # Load the selected model
51
+ upsampler = models[model_option].cuda()
52
  hr_feats = upsampler(image_tensor)
53
  lr_feats = upsampler.model(image_tensor)
54
+ upsampler.cpu()
55
+
56
+ return plot_feats(unnorm(image_tensor)[0], lr_feats[0], hr_feats[0])
57
+
58
+
59
+ demo = gr.Interface(fn=upsample_features,
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
 
pre-requirements.txt DELETED
@@ -1,15 +0,0 @@
1
- -i https://download.pytorch.org/whl/cu118
2
- --extra-index-url https://pypi.org/simple/
3
- torch
4
- torchvision
5
- torchaudio
6
- kornia
7
- omegaconf
8
- pytorch-lightning
9
- torchvision
10
- tqdm
11
- torchmetrics
12
- scikit-learn
13
- numpy
14
- matplotlib
15
- git+https://github.com/mhamilton723/CLIP.git
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt DELETED
@@ -1 +0,0 @@
1
- git+https://github.com/mhamilton723/FeatUp