lorebianchi98 commited on
Commit
232b4b7
·
1 Parent(s): 4a132ff

Removed MMCV dependency

Browse files
Files changed (5) hide show
  1. .gitignore +1 -0
  2. README.md +27 -55
  3. hf_demo.ipynb +0 -0
  4. hf_model/__init__.py +0 -0
  5. hf_model/talk2dino.py +0 -1
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__/
README.md CHANGED
@@ -44,86 +44,58 @@ Open-Vocabulary Segmentation (OVS) aims at segmenting images from free-form text
44
  ### Mapping CLIP Text Embeddings to DINOv2 space with Talk2DINO
45
  We can use Talk2DINO to map CLIP text embeddings into the DINOv2 patch embedding space.
46
  ```python
47
- import clip
48
- from src.model import ProjectionLayer
49
- import torch
50
- import os
51
 
52
  # Device setup
53
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
54
 
55
- # Configuration and weights
56
- proj_name = 'vitb_mlp_infonce'
57
- config_path = os.path.join("configs", f"{proj_name}.yaml")
58
- weights_path = os.path.join("weights", f"{proj_name}.pth")
59
-
60
- # Load Talk2DINO projection layer
61
- talk2dino = ProjectionLayer.from_config(config_path)
62
- talk2dino.load_state_dict(torch.load(weights_path, map_location=device))
63
- talk2dino.to(device)
64
-
65
- # Load CLIP model
66
- clip_model, clip_preprocess = clip.load("ViT-B/16", device=device, jit=False)
67
- tokenizer = clip.tokenize
68
-
69
- # Example: Tokenize and project text features
70
- texts = ["a cat"]
71
- text_tokens = tokenizer(texts).to(device)
72
- text_features = clip_model.encode_text(text_tokens)
73
- projected_text_features = talk2dino.project_clip_txt(text_features)
74
- ```
75
 
76
- ### Demo
77
- In `demo.py` we provide a simple example on how to use Talk2DINO for inference on a given image with custom textual categories. Run
 
 
78
 
79
- ```bash
80
- python demo.py --input custom_input_image --output custom_output_seg [--with_background] --textual_categories category_1,category_2,..
81
- ```
82
 
83
- Example:
84
- ```bash
85
- python demo.py --input assets/pikachu.png --output pikachu_seg.png --textual_categories pikachu,traffic_sign,forest,route
86
  ```
87
 
 
 
88
  Result:
89
  <div align="center">
90
  <table><tr><td><figure>
91
  <img alt="" src="./assets/pikachu.png" width=300>
92
  </figure></td><td><figure>
93
- <img alt="" src="./pikachu_seg.png" width=300>
94
  </figure></td></tr></table>
95
  </div>
96
 
97
  ## Installation
98
- ```bash
99
- # Create a new environment with Python 3.10
100
- conda create --name talk2dino python=3.10 -c conda-forge
101
- conda activate talk2dino
102
 
103
- # Install compilers for C++/CUDA extensions
104
- conda install -c conda-forge "gxx_linux-64=11.*" "gcc_linux-64=11.*"
105
 
106
- # Install CUDA toolkit and cuDNN
107
- conda install -c nvidia/label/cuda-11.7.0 cuda
108
- conda install -c nvidia/label/cuda-11.7.0 cuda-nvcc
109
- conda install -c conda-forge cudnn cudatoolkit=11.7.0
110
-
111
- # Install PyTorch 2.1 with CUDA 11.8 support
112
- # Note: This is crucial, as it matches the requirements of mmcv-full 1.7.2
113
- pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 --index-url https://download.pytorch.org/whl/cu118
114
 
115
- # Install other dependencies
116
  pip install -r requirements.txt
117
- pip install -U openmim
118
- mim install mmengine
119
-
120
- # Install a compatible version of mmcv-full (1.7.2) for PyTorch 2.1
121
- pip install mmcv-full==1.7.2 -f https://download.openmmlab.com/mmcv/dist/cu118/torch2.1.0/index.html
122
 
123
- # Install mmsegmentation
124
- pip install mmsegmentation==0.30.0
125
  ```
126
 
 
 
 
 
127
  <details>
128
  <summary>Qualitative Results</summary>
129
 
 
44
  ### Mapping CLIP Text Embeddings to DINOv2 space with Talk2DINO
45
  We can use Talk2DINO to map CLIP text embeddings into the DINOv2 patch embedding space.
46
  ```python
47
+ from hf_model.talk2dino import Talk2DINO
48
+ from torchvision.io import read_image
 
 
49
 
50
  # Device setup
51
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
52
 
53
+ # Model Loading
54
+ model = Talk2DINO.from_pretrained("lorebianchi98/Talk2DINO-ViTB").to(device).eval()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ # Embedding generation
57
+ with torch.no_grad():
58
+ text_embed = model.encode_text("a pikachu")
59
+ image_embed = model.encode_image(image)
60
 
61
+ # normalize the features to perform cosine similarity
62
+ text_embed = text_embed / text_embed.norm(dim=-1, keepdim=True)
63
+ image_embed = image_embed / image_embed.norm(dim=-1, keepdim=True)
64
 
65
+ similarity = (image_embed @ text_embed.T).squeeze(0, -1).cpu().numpy()
 
 
66
  ```
67
 
68
+ ### Demo
69
+ In `demo.ipynb` we provide a simple example on how to use Talk2DINO for inference on a given image with custom textual categories.
70
  Result:
71
  <div align="center">
72
  <table><tr><td><figure>
73
  <img alt="" src="./assets/pikachu.png" width=300>
74
  </figure></td><td><figure>
75
+ <img alt="" src="./assets/pikachu_seg.png" width=300>
76
  </figure></td></tr></table>
77
  </div>
78
 
79
  ## Installation
 
 
 
 
80
 
81
+ To use the **Hugging Face interface** for inference:
 
82
 
83
+ ```bash
84
+ # Clone the repository
85
+ git clone https://huggingface.co/lorebianchi98/Talk2DINO-ViTB
86
+ cd Talk2DINO-ViTB
 
 
 
 
87
 
88
+ # Install dependencies
89
  pip install -r requirements.txt
 
 
 
 
 
90
 
91
+ # Install PyTorch and torchvision with the appropriate CUDA version
92
+ pip install torch torchvision --index-url https://download.pytorch.org/whl/cu126
93
  ```
94
 
95
+ > For the **full MMCV interface** to perform evaluation on segmentation benchmarks, please refer to the [original Talk2DINO repository](https://github.com/lorebianchi98/Talk2DINO).
96
+
97
+
98
+
99
  <details>
100
  <summary>Qualitative Results</summary>
101
 
hf_demo.ipynb CHANGED
The diff for this file is too large to render. See raw diff
 
hf_model/__init__.py ADDED
File without changes
hf_model/talk2dino.py CHANGED
@@ -406,7 +406,6 @@ class DINOText(nn.Module):
406
 
407
  from huggingface_hub import PyTorchModelHubMixin
408
 
409
-
410
  class Talk2DINO(DINOText, PyTorchModelHubMixin):
411
  def encode_text(self, texts):
412
  """ texts: string or list of strings
 
406
 
407
  from huggingface_hub import PyTorchModelHubMixin
408
 
 
409
  class Talk2DINO(DINOText, PyTorchModelHubMixin):
410
  def encode_text(self, texts):
411
  """ texts: string or list of strings