-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathzsl.cpp
More file actions
56 lines (46 loc) · 1.61 KB
/
zsl.cpp
File metadata and controls
56 lines (46 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// zero-shot image labeling example
#include "clip.h"
#include "common-clip.h"
int main(int argc, char ** argv) {
app_params params;
if (!app_params_parse(argc, argv, params, 2, 1)) {
print_help(argc, argv, params, 2, 1);
return 1;
}
const size_t n_labels = params.texts.size();
if (n_labels < 2) {
printf("%s: You must specify at least 2 texts for zero-shot labeling\n", __func__);
}
const char ** labels = new const char *[n_labels];
for (size_t i = 0; i < n_labels; ++i) {
labels[i] = params.texts[i].c_str();
}
auto ctx = clip_model_load(params.model.c_str(), params.verbose);
if (!ctx) {
printf("%s: Unable to load model from %s", __func__, params.model.c_str());
return 1;
}
// load the image
const auto & img_path = params.image_paths[0].c_str();
clip_image_u8 input_img;
if (!clip_image_load_from_file(img_path, &input_img)) {
fprintf(stderr, "%s: failed to load image from '%s'\n", __func__, img_path);
return 1;
}
float *sorted_scores = new float[n_labels];
int *sorted_indices = new int[n_labels];
if (!clip_zero_shot_label_image(ctx, params.n_threads, &input_img, labels, n_labels, sorted_scores, sorted_indices)) {
fprintf(stderr, "Unable to apply ZSL\n");
return 1;
}
for (int i = 0; i < n_labels; i++) {
auto label = labels[sorted_indices[i]];
float score = sorted_scores[i];
printf("%s = %1.4f\n", label, score);
}
clip_free(ctx);
delete[] labels;
delete[] sorted_scores;
delete[] sorted_indices;
return 0;
}