#include "clip.h"
#include <stdbool.h>
#include <stdio.h>
int main() {
char * model_path = "../../models/openai_clip-vit-base-patch32.q4_1.gguf";
char * img_path = "../../tests/red_apple.jpg";
char * text = "an apple";
int n_threads = 4;
int verbosity = 1;
struct clip_ctx * ctx = clip_model_load(model_path, verbosity);
if (!ctx) {
printf("%s: Unable to load model from %s", __func__, model_path);
return 1;
}
int vec_dim = clip_get_vision_hparams(ctx)->projection_dim;
struct clip_image_u8 * img0 = clip_image_u8_make();
if (!clip_image_load_from_file(img_path, img0)) {
fprintf(stderr, "%s: failed to load image from '%s'\n", __func__, img_path);
return 1;
}
struct clip_image_f32 * img_res = clip_image_f32_make();
if (!clip_image_preprocess(ctx, img0, img_res)) {
fprintf(stderr, "%s: failed to preprocess image\n", __func__);
return 1;
}
float img_vec[vec_dim];
if (!clip_image_encode(ctx, n_threads, img_res, img_vec, true)) {
fprintf(stderr, "%s: failed to encode image\n", __func__);
return 1;
}
struct clip_tokens * tokens = NULL;
clip_tokenize(ctx, text, tokens);
float txt_vec[vec_dim];
if (!clip_text_encode(ctx, n_threads, tokens, txt_vec, true)) {
fprintf(stderr, "%s: failed to encode text\n", __func__);
return 1;
}
float score = clip_similarity_score(img_vec, txt_vec, vec_dim);
printf("Similarity score = %2.3f\n", score);
clip_free(ctx);
return 0;
}