| 2898 | } |
| 2899 | |
| 2900 | svm_model *svm_load_binary_model(const char *model_file_name) |
| 2901 | { |
| 2902 | FILE *fp = fopen(model_file_name,"rb"); |
| 2903 | if(fp==NULL) return NULL; |
| 2904 | |
| 2905 | struct svm_model_bin model_bin; |
| 2906 | //printf("binsize: %i\n",sizeof(struct svm_model_bin)); |
| 2907 | // read parameters |
| 2908 | fread(&model_bin, sizeof(struct svm_model_bin), 1, fp); |
| 2909 | |
| 2910 | // compute total size, alloc memory, and load the rest: |
| 2911 | long size = binload_intConv(model_bin.SV) + binload_intConv(model_bin.SVsize) * sizeof(struct svm_node_bin) ; //- sizeof(struct svm_model_bin); |
| 2912 | void * pool = malloc(size); |
| 2913 | if (pool == NULL) { printf("ERROR: out-of-memory (malloc for size %li returned NULL)\n",size); fflush(stdout); } |
| 2914 | long rd = (long)fread(pool, 1, size, fp); |
| 2915 | if (rd < size) { |
| 2916 | printf("SVM: error, model file '%s' is corrupt, read only %li bytes instead of %li excpected!\n",model_file_name,rd,size); |
| 2917 | } |
| 2918 | |
| 2919 | // then adjust the pointers and copy to newly allocated svm_model struct ... |
| 2920 | svm_model *model = Malloc(svm_model,1); |
| 2921 | svm_parameter& param = model->param; |
| 2922 | |
| 2923 | //copy parameters |
| 2924 | |
| 2925 | // convert model to model_bin, adjust pointers in model_bin to relative pointers |
| 2926 | param.svm_type = binload_intConv(model_bin.param.svm_type); |
| 2927 | param.kernel_type = binload_intConv(model_bin.param.kernel_type); |
| 2928 | param.gamma = binload_doubleConv(model_bin.param.gamma); |
| 2929 | param.degree = binload_intConv(model_bin.param.degree); |
| 2930 | param.coef0 = binload_doubleConv(model_bin.param.coef0); |
| 2931 | model->nr_class = binload_intConv(model_bin.nr_class); |
| 2932 | model->l = binload_intConv(model_bin.l); |
| 2933 | |
| 2934 | // load data from pool |
| 2935 | int n = model->nr_class*(model->nr_class-1)/2; |
| 2936 | |
| 2937 | //printf("offset rho: %i \n",model_bin.rho); |
| 2938 | model->rho = floatArrToDouble(pool, model_bin.rho, n); |
| 2939 | |
| 2940 | if (model_bin.probA != -1) |
| 2941 | model->probA = floatArrToDouble(pool, model_bin.probA, n); //(double *)((char*)pool + model_bin.probA); |
| 2942 | else |
| 2943 | model->probA = NULL; |
| 2944 | |
| 2945 | if (model_bin.probB != -1) |
| 2946 | model->probB = floatArrToDouble(pool, model_bin.probB, n); //(double *)((char*)pool + model_bin.probB); |
| 2947 | else |
| 2948 | model->probB = NULL; |
| 2949 | |
| 2950 | // todo: convert int... |
| 2951 | if (model_bin.label >= 0) model->label = Malloc(int, model->nr_class); |
| 2952 | else model->label = NULL; |
| 2953 | if (model_bin.nSV >= 0) model->nSV = Malloc(int, model->nr_class); |
| 2954 | else model->nSV = NULL; |
| 2955 | |
| 2956 | int32_t *label = (int32_t*)((char*)pool + model_bin.label); |
| 2957 | int32_t *nSV = (int32_t*)((char*)pool + model_bin.nSV); |
no test coverage detected