| 2767 | } |
| 2768 | |
| 2769 | int svm_save_binary_model(const char *model_file_name, const svm_model *model) |
| 2770 | { |
| 2771 | FILE *fp = fopen(model_file_name,"wb"); |
| 2772 | if(fp==NULL) return -1; |
| 2773 | |
| 2774 | int nr_class = model->nr_class; |
| 2775 | int n = nr_class*(nr_class-1)/2; |
| 2776 | int l = model->l; |
| 2777 | |
| 2778 | struct svm_model_bin model_bin; |
| 2779 | |
| 2780 | // compute size of SV data to save |
| 2781 | long size = 0; |
| 2782 | |
| 2783 | if (model->param.kernel_type == PRECOMPUTED) |
| 2784 | size = l*sizeof(struct svm_node); |
| 2785 | else { |
| 2786 | for(int i=0;i<l;i++) |
| 2787 | { |
| 2788 | const svm_node *p = model->SV[i]; |
| 2789 | while(p->index != -1) |
| 2790 | { |
| 2791 | p++; size++; |
| 2792 | } |
| 2793 | size++; // count the '-1' element!? |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | //printf("binsize: %i\n",sizeof(struct svm_model_bin)); |
| 2798 | // convert model to model_bin, adjust pointers in model_bin to relative pointers |
| 2799 | model_bin.param.svm_type = binsave_intConv(model->param.svm_type); |
| 2800 | model_bin.param.kernel_type = binsave_intConv(model->param.kernel_type); |
| 2801 | model_bin.param.degree = binsave_intConv(model->param.degree); |
| 2802 | model_bin.param.gamma = binsave_doubleConv(model->param.gamma); |
| 2803 | model_bin.param.coef0 = binsave_doubleConv(model->param.coef0); |
| 2804 | model_bin.nr_class = binsave_intConv(model->nr_class); |
| 2805 | model_bin.l = binsave_intConv(model->l); |
| 2806 | |
| 2807 | int offset = 0; // sizeof(struct svm_model_bin); |
| 2808 | |
| 2809 | model_bin.rho = binsave_ptrConv(offset); offset += n * sizeof(float); |
| 2810 | if (model->probA) { |
| 2811 | model_bin.probA = binsave_ptrConv(offset); offset += n * sizeof(float); |
| 2812 | } else { |
| 2813 | model_bin.probA = binsave_ptrConv(-1); |
| 2814 | } |
| 2815 | if (model->probB) { |
| 2816 | model_bin.probB = binsave_ptrConv(offset); offset += n * sizeof(float); |
| 2817 | } else { |
| 2818 | model_bin.probB = binsave_ptrConv(-1); |
| 2819 | } |
| 2820 | |
| 2821 | |
| 2822 | if (model->label) { |
| 2823 | model_bin.label = binsave_ptrConv(offset); offset += nr_class * sizeof(int32_t); |
| 2824 | } else { |
| 2825 | model_bin.label = binsave_ptrConv(-1); |
| 2826 | } |
no test coverage detected