00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "RandomForestModel.h"
00034 #if CMK_USE_ZLIB
00035 #include "zlib.h"
00036 #endif
00037 #include <stdio.h>
00038 #ifndef PATH_MAX
00039 #define PATH_MAX 65535
00040 #endif
00041 #define LINE_SIZE 1024
00042
00043 using namespace rfmodel;
00044
00045 void ForestModel::readModel(const char* dir) {
00046 #if CMK_USE_ZLIB
00047 treeModels.resize(NUM_TREES);
00048
00049 gzFile pFile;
00050 double f;
00051
00052 for (int i = 0; i < LB_CLASSES; i++) classes[i] = i + 1;
00053
00054 char buffer[PATH_MAX];
00055 char linebuffer[LINE_SIZE];
00056 sprintf(buffer, "%s/big_leafdist.txt.gz", dir);
00057 pFile = gzopen(buffer, "r");
00058
00059 if (pFile == NULL) CkAbort("\nUnable to open model files.\n");
00060 for (int k = 0; k < NUM_TREES; k++) {
00061 int i, X, D;
00062 gzgets(pFile, linebuffer, LINE_SIZE);
00063 sscanf(linebuffer, "%d %d %d", &i, &X, &D);
00064
00065 treeModels[i - 1].classes = classes;
00066 treeModels[i - 1].l_X = X;
00067 treeModels[i - 1].l_D = D;
00068 treeModels[i - 1].leafdist.resize(X * D);
00069
00070 for (int j = 0; j < X * D; j++) {
00071 gzgets(pFile, linebuffer, LINE_SIZE);
00072 sscanf(linebuffer, "%lf", &f);
00073 treeModels[i - 1].leafdist[j] = f;
00074 }
00075 }
00076 gzclose(pFile);
00077
00078 sprintf(buffer, "%s/big_weakmodel.txt.gz", dir);
00079 pFile = gzopen(buffer, "r");
00080
00081 if (pFile == NULL) CkAbort("\nUnable to open model files.\n");
00082 for (int k = 0; k < NUM_TREES; k++) {
00083 int classifierID, r1, r2;
00084 double val;
00085 std::vector<double> w(3);
00086
00087 int w_i;
00088 gzgets(pFile, linebuffer, LINE_SIZE);
00089 sscanf(linebuffer, "%d", &w_i);
00090
00091 int X, D;
00092
00093 X = treeModels[w_i - 1].l_X;
00094 D = treeModels[w_i - 1].l_D;
00095
00096 treeModels[w_i - 1].weakModels.resize(X * D);
00097
00098 for (int j = 0; j < X - 1; j++) {
00099 gzgets(pFile, linebuffer, LINE_SIZE);
00100 sscanf(linebuffer, "%lf", &val);
00101 classifierID = (int)val;
00102 if (classifierID != 0) {
00103 double val1, val2;
00104 sscanf(linebuffer, "%lf %lf %lf %lf %lf %lf", &val, &val1, &val2, &w[0], &w[1],
00105 &w[2]);
00106 r1 = (int)val1;
00107 r2 = (int)val2;
00108 }
00109
00110 #ifdef DEBUG
00111 if (classifierID != 0)
00112 printf("\nclassifier = %d %d %d %lf %lf %lf \n", classifierID, r1, r2, w[0], w[1],
00113 w[2]);
00114 #endif
00115
00116 treeModels[w_i - 1].weakModels[j].classifierID = classifierID;
00117 treeModels[w_i - 1].weakModels[j].r1 = r1;
00118 treeModels[w_i - 1].weakModels[j].r2 = r2;
00119 treeModels[w_i - 1].weakModels[j].w = w;
00120 }
00121 }
00122
00123 gzclose(pFile);
00124 #endif
00125
00126 return;
00127 }