load the model's weights from a file
| 506 | |
| 507 | // load the model's weights from a file |
| 508 | bool sam_model_load(const sam_params & params, sam_model & model) { |
| 509 | fprintf(stderr, "%s: loading model from '%s' - please wait ...\n", __func__, params.model.c_str()); |
| 510 | |
| 511 | auto fin = std::ifstream(params.model, std::ios::binary); |
| 512 | if (!fin) { |
| 513 | fprintf(stderr, "%s: failed to open '%s'\n", __func__, params.model.c_str()); |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | // verify magic |
| 518 | { |
| 519 | uint32_t magic; |
| 520 | fin.read((char *) &magic, sizeof(magic)); |
| 521 | if (magic != 0x67676d6c) { |
| 522 | fprintf(stderr, "%s: invalid model file '%s' (bad magic)\n", __func__, params.model.c_str()); |
| 523 | return false; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // load hparams |
| 528 | { |
| 529 | // Override defaults with user choices |
| 530 | model.hparams.mask_threshold = params.mask_threshold; |
| 531 | model.hparams.iou_threshold = params.iou_threshold; |
| 532 | model.hparams.stability_score_threshold = params.stability_score_threshold; |
| 533 | model.hparams.stability_score_offset = params.stability_score_offset; |
| 534 | model.hparams.eps = params.eps; |
| 535 | model.hparams.eps_decoder_transformer = params.eps_decoder_transformer; |
| 536 | |
| 537 | auto & hparams = model.hparams; |
| 538 | |
| 539 | fin.read((char *) &hparams.n_enc_state, sizeof(hparams.n_enc_state)); |
| 540 | fin.read((char *) &hparams.n_enc_layer, sizeof(hparams.n_enc_layer)); |
| 541 | fin.read((char *) &hparams.n_enc_head, sizeof(hparams.n_enc_head)); |
| 542 | fin.read((char *) &hparams.n_enc_out_chans, sizeof(hparams.n_enc_out_chans)); |
| 543 | fin.read((char *) &hparams.n_pt_embd, sizeof(hparams.n_pt_embd)); |
| 544 | fin.read((char *) &hparams.ftype, sizeof(hparams.ftype)); |
| 545 | |
| 546 | const int32_t qntvr = hparams.ftype / GGML_QNT_VERSION_FACTOR; |
| 547 | |
| 548 | printf("%s: n_enc_state = %d\n", __func__, hparams.n_enc_state); |
| 549 | printf("%s: n_enc_layer = %d\n", __func__, hparams.n_enc_layer); |
| 550 | printf("%s: n_enc_head = %d\n", __func__, hparams.n_enc_head); |
| 551 | printf("%s: n_enc_out_chans = %d\n", __func__, hparams.n_enc_out_chans); |
| 552 | printf("%s: n_pt_embd = %d\n", __func__, hparams.n_pt_embd); |
| 553 | printf("%s: ftype = %d\n", __func__, hparams.ftype); |
| 554 | printf("%s: qntvr = %d\n", __func__, qntvr); |
| 555 | |
| 556 | hparams.ftype %= GGML_QNT_VERSION_FACTOR; |
| 557 | |
| 558 | } |
| 559 | |
| 560 | // for the big tensors, we have the option to store the data in 16-bit floats or quantized |
| 561 | // in order to save memory and also to speed up the computation |
| 562 | ggml_type wtype = ggml_ftype_to_ggml_type((ggml_ftype) (model.hparams.ftype)); |
| 563 | if (wtype == GGML_TYPE_COUNT) { |
| 564 | fprintf(stderr, "%s: invalid model file '%s' (bad ftype value %d)\n", |
| 565 | __func__, params.model.c_str(), model.hparams.ftype); |
no test coverage detected