Skip to content

Commit

Permalink
save model path to image index and load from image index
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Jun 24, 2023
1 parent 19e51ae commit 2385eaa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions examples/image-search/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ int main(int argc, char** argv) {
// TODO: figrue out why usearch leaks

std::ofstream image_file_index_file("images.paths", std::ios::binary | std::ios::trunc);
// first line is model
image_file_index_file << params.model << "\n";
for (const auto& i_path : image_file_index) {
image_file_index_file << i_path << "\n";
}
Expand Down
18 changes: 13 additions & 5 deletions examples/image-search/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

struct my_app_params {
int32_t n_threads {4};
std::string model {"../models/ggml-model-f16.bin"};
std::string model;
int32_t verbose {1};
// TODO: index dir

Expand All @@ -19,7 +19,7 @@ void my_print_help(int argc, char **argv, my_app_params &params) {
printf("Usage: %s [options] <search string>\n", argv[0]);
printf("\nOptions:");
printf(" -h, --help: Show this message and exit\n");
printf(" -m <path>, --model <path>: path to model. Default: %s\n", params.model.c_str());
printf(" -m <path>, --model <path>: overwrite path to model. Read from images.paths by default.\n");
printf(" -t N, --threads N: Number of threads to use for inference. Default: %d\n", params.n_threads);
printf(" -v <level>, --verbose <level>: Control the level of verbosity. 0 = minimum, 2 = maximum. Default: %d\n", params.verbose);

Expand Down Expand Up @@ -84,6 +84,16 @@ int main(int argc, char** argv) {
return 1;
}

// load model path
std::ifstream image_file_index_file("images.paths", std::ios::binary);
std::string line;
std::getline(image_file_index_file, line);
if (params.model.empty()) {
params.model = line;
} else {
printf("%s: using alternative model from %s. Make sure you use the same model you used for indexing, or the embeddings wont work.\n", __func__, params.model.c_str());
}

auto clip_ctx = clip_model_load(params.model.c_str(), params.verbose);
if (!clip_ctx) {
printf("%s: Unable to load model from %s\n", __func__, params.model.c_str());
Expand All @@ -95,9 +105,7 @@ int main(int argc, char** argv) {

embd_index.view("images.usearch");

// load paths
std::ifstream image_file_index_file("images.paths", std::ios::binary);
std::string line;
// load image paths
do {
std::getline(image_file_index_file, line);
if (line.empty()) {
Expand Down

0 comments on commit 2385eaa

Please sign in to comment.