You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+44-30Lines changed: 44 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,16 @@
1
1
# clip.cpp
2
+
2
3
CLIP inference in plain C/C++ with no extra dependencies
3
4
4
5
## Description
6
+
5
7
This is a dependency free implementation of well known [CLIP](https://github.com/openai/clip) by OpenAI,
6
8
thanks to the great work in [GGML](https://github.com/ggerganov/ggml).
7
9
You can use it to work with CLIP models from both OpenAI and LAION
8
10
in Transformers format.
9
11
10
12
## Motivation
13
+
11
14
CLIP is deployed for several task from semantic image search to zero-shot image labeling.
12
15
It's also a part of Stable Diffusion and and the recently emerging field of large multimodal models (LMM).
13
16
This repo is aimed at powering useful applications based on such models on computation- or memory-constraint devices.
@@ -16,21 +19,24 @@ This repo is aimed at powering useful applications based on such models on compu
16
19
clip.cpp also has a short startup time compared to large ML frameworks, which makes it suitable for serverless deployments where the cold start is an issue.
17
20
18
21
## Hot topics
19
-
- 09/14/2023: All functions are C-compatible now. `zsl` example is updated to match Huggingface's zero-shot behavior in the zero-shot pipeline.
20
-
- 09/11/2023: Introduce Python bindings.
21
-
- 07/12/2023: Batch inference support for image encoding.
22
-
- 07/11/2023: Semantic image search [example](examples/image-search/README.md) directly in C++.
22
+
23
+
- 09/14/2023: All functions are C-compatible now. `zsl` example is updated to match Huggingface's zero-shot behavior in the zero-shot pipeline.
24
+
- 09/11/2023: Introduce Python bindings.
25
+
- 07/12/2023: Batch inference support for image encoding.
26
+
- 07/11/2023: Semantic image search [example](examples/image-search/README.md) directly in C++.
23
27
24
28
## Note about image preprocessing
25
-
PIL uses a two-pass convolutions-based bicubic interpolation in resizing with antialiasing applied. In Pytorch, antialiasing is optional. It needs some extra attention to implement this preprocessing logic that matches their results numerically. However, I found that linear interpolation is also good enough for both comparison of different embeddings from this implementation and also comparison of an embedding from this implementation and another one from Transformers. So let's use it until we craft a proper bicubic interpolation.
26
29
30
+
PIL uses a two-pass convolutions-based bicubic interpolation in resizing with antialiasing applied. In Pytorch, antialiasing is optional. It needs some extra attention to implement this preprocessing logic that matches their results numerically. However, I found that linear interpolation is also good enough for both comparison of different embeddings from this implementation and also comparison of an embedding from this implementation and another one from Transformers. So let's use it until we craft a proper bicubic interpolation.
27
31
28
32
## Preconverted Models
33
+
29
34
Preconverted Models can be found in [HuggingFace Repositories tagged with `clip.cpp`](https://huggingface.co/models?other=clip.cpp).
30
35
If you want to do conversion yourself for some reason, see below for how.
31
36
Otherwise, download a model of your choice from the link above and then feel free to jump to the building section.
32
37
33
38
## Model conversion
39
+
34
40
You can convert CLIP models from OpenAI and LAION in Transformers format. Apparently, LAION's models outperform OpenAI models in several benchmarks, so they are recommended.
@@ -84,13 +91,14 @@ And the binaries are in the `./bin` directory.
84
91
I couldn't reproduce it on my Macbook M2 pro so cannot help further. If you know a solution that I can include in `CMakeLists.txt` please ping me [here](https://github.com/monatis/clip.cpp/issues/24).
85
92
86
93
## Quantization
94
+
87
95
`clip.cpp` currently supports q4_0 and q4_1 quantization types.
88
96
You can quantize a model in F16 to one of these types by using the `./bin/quantize` binary.
89
97
90
98
```
91
-
usage: ./bin/quantize /path/to/ggml-model-f16.bin /path/to/ggml-model-quantized.bin type
92
-
type = 2 - q4_0
93
-
type = 3 - q4_1
99
+
usage: ./bin/quantize /path/to/ggml-model-f16.bin /path/to/ggml-model-quantized.bin type
100
+
type = 2 - q4_0
101
+
type = 3 - q4_1
94
102
```
95
103
96
104
For example, you can run the following to convert the model to q4_0:
@@ -102,27 +110,28 @@ For example, you can run the following to convert the model to q4_0:
102
110
Now you can use `ggml-model-q4_0.bin` just like the model in F16.
103
111
104
112
## Usage
113
+
105
114
Currently we have three examples: `main`, `zsl` and `image-search`.
106
115
107
116
1.`main` is just for demonstrating the usage of API and optionally print out some verbose timings. It simply calculates the similarity between one image and one text passed as CLI args.
108
117
109
118
```
110
-
Usage: ./bin/main [options]
111
-
112
-
Options: -h, --help: Show this message and exit
113
-
-m <path>, --model <path>: path to model. Default: models/ggml-model-f16.bin
114
-
-t N, --threads N: Number of threads to use for inference. Default: 4
115
-
--text <text>: Text to encode. At least one text should be specified
116
-
--image <path>: Path to an image file. At least one image path should be specified
117
-
-v <level>, --verbose <level>: Control the level of verbosity. 0 = minimum, 2 = maximum. Default: 1
119
+
Usage: ./bin/main [options]
120
+
121
+
Options: -h, --help: Show this message and exit
122
+
-m <path>, --model <path>: path to model. Default: models/ggml-model-f16.bin
123
+
-t N, --threads N: Number of threads to use for inference. Default: 4
124
+
--text <text>: Text to encode. At least one text should be specified
125
+
--image <path>: Path to an image file. At least one image path should be specified
126
+
-v <level>, --verbose <level>: Control the level of verbosity. 0 = minimum, 2 = maximum. Default: 1
118
127
```
119
128
120
129
2.`zsl` is a zero-shot image labeling example. It labels an image with one of the labels.
121
-
The CLI args are the same as in `main`,
122
-
but you must specify multiple `--text` arguments to specify the labels.
130
+
The CLI args are the same as in `main`,
131
+
but you must specify multiple `--text` arguments to specify the labels.
123
132
124
133
3.`image-search` is an example for semantic image search with [USearch](https://github.com/unum-cloud/usearch/).
125
-
You must enable `CLIP_BUILD_IMAGE_SEARCH` option to compile it, and the dependency will be automatically fetched by cmake:
134
+
You must enable `CLIP_BUILD_IMAGE_SEARCH` option to compile it, and the dependency will be automatically fetched by cmake:
126
135
127
136
```sh
128
137
mkdir build
@@ -137,6 +146,7 @@ make
137
146
See [examples/image-search/README.md](examples/image-search/README.md) for more info and usage.
138
147
139
148
## Python bindings
149
+
140
150
You can use clip.cpp in Python with no third-party libraries (no dependencies other than standard Python libraries).
141
151
It uses `ctypes` to load a dynamically linked library (DLL) to interface the implementation in C/C++.
142
152
@@ -146,6 +156,10 @@ If you are on an X64 Linux distribution, you can simply Pip-install it with AVX2
146
156
pip install clip_cpp
147
157
```
148
158
159
+
> Colab Notebook available for quick experiment :
160
+
>
161
+
> <ahref="https://colab.research.google.com/github/Yossef-Dawoad/clip.cpp/blob/add_colab_notebook_example/examples/python_bindings/notebooks/clipcpp_demo.ipynb"target="_blank"><imgsrc="https://colab.research.google.com/assets/colab-badge.svg"alt="Open In Colab"/></a>
162
+
149
163
If you are on another operating system or architecture,
150
164
or if you want to make use of support for instruction sets other than AVX2 (e.g., AVX512),
151
165
you can build it from source.
@@ -165,23 +179,23 @@ make
165
179
And find the `libclip.so` binary in the `build` directory.
166
180
See [examples/python_bindings/README.md](examples/python_bindings/README.md) for more info and usage.
167
181
168
-
169
182
## Benchmarking
183
+
170
184
You can use the benchmarking utility to compare the performances of different checkpoints and quantization types.
images_dir: path to a directory of images where images are organized into subdirectories named classes
191
+
num_images_per_dir: maximum number of images to read from each one of subdirectories. if 0, read all files
192
+
output_file: optional. if specified, dump the output to this file instead of stdout
193
+
```
181
194
182
195
TODO: share benchmarking results for a common dataset later on.
183
196
184
197
## Future Work
185
-
-[ ] Support `text-only`, `image-only` and `both` (current) options when exporting, and modify model loading logic accordingly. It might be relevant to use a single modality in certain cases, as in large multimodal models, or building and/or searching for semantic image search.
186
-
-[ ] Seperate memory buffers for text and image models, as their memory requirements are different.
187
-
-[ ] Implement proper bicubic interpolation (PIL uses a convolutions-based algorithm, and it's more stable than affine transformations).
198
+
199
+
-[ ] Support `text-only`, `image-only` and `both` (current) options when exporting, and modify model loading logic accordingly. It might be relevant to use a single modality in certain cases, as in large multimodal models, or building and/or searching for semantic image search.
200
+
-[ ] Seperate memory buffers for text and image models, as their memory requirements are different.
201
+
-[ ] Implement proper bicubic interpolation (PIL uses a convolutions-based algorithm, and it's more stable than affine transformations).
0 commit comments