这是我的班级svm_predict
package pack.test;
import java.io.*;
import java.util.*;
public class svm_predict {
File inputFile;
File outputFile;
File modelFile;
public svm_predict(File inputFile, File modelFile,File outputFile) {
super();
this.inputFile = inputFile;
this.outputFile = outputFile;
this.modelFile = modelFile;
}
private static svm_print_interface svm_print_null = new svm_print_interface()
{
public void print(String s) {}
};
private static svm_print_interface svm_print_stdout = new svm_print_interface()
{
public void print(String s)
{
System.out.print(s);
}
};
private static svm_print_interface svm_print_string = svm_print_stdout;
static void info(String s)
{
svm_print_string.print(s);
}
private static double atof(String s)
{
return Double.valueOf(s).doubleValue();
}
private static int atoi(String s)
{
return Integer.parseInt(s);
}
private static void predict(BufferedReader input, svm_model model, DataOutputStream output, int predict_probability) throws IOException
{
int correct = 0;
int total = 0;
double error = 0;
double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
int svm_type=svm.svm_get_svm_type(model);
int nr_class=svm.svm_get_nr_class(model);
double[] prob_estimates=null;
if(predict_probability == 1)
{
if(svm_type == svm_parameter.EPSILON_SVR ||
svm_type == svm_parameter.NU_SVR)
{
svm_predict.info("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma="+svm.svm_get_svr_probability(model)+"\n");
}
else
{
int[] labels=new int[nr_class];
svm.svm_get_labels(model,labels);
prob_estimates = new double[nr_class];
output.writeBytes("labels");
for(int j=0;j<nr_class;j++)
output.writeBytes(" "+labels[j]);
output.writeBytes("\n");
}
}
while(true)
{
String line = input.readLine();
if(line == null) break;
StringTokenizer st = new StringTokenizer(line," \t\n\r\f:");
double target = atof(st.nextToken());
int m = st.countTokens()/2;
svm_node[] x = new svm_node[m];
for(int j=0;j<m;j++)
{
x[j] = new svm_node();
x[j].index = atoi(st.nextToken());
x[j].value = atof(st.nextToken());
}
double v;
if (predict_probability==1 && (svm_type==svm_parameter.C_SVC || svm_type==svm_parameter.NU_SVC))
{
v = svm.svm_predict_probability(model,x,prob_estimates);
output.writeBytes(v+" ");
for(int j=0;j<nr_class;j++)
output.writeBytes(prob_estimates[j]+" ");
output.writeBytes("\n");
}
else
{
v = svm.svm_predict(model,x);
output.writeBytes(v+"\n");
}
if(v == target)
++correct;
error += (v-target)*(v-target);
sumv += v;
sumy += target;
sumvv += v*v;
sumyy += target*target;
sumvy += v*target;
++total;
}
if(svm_type == svm_parameter.EPSILON_SVR ||
svm_type == svm_parameter.NU_SVR)
{
svm_predict.info("Mean squared error = "+error/total+" (regression)\n");
svm_predict.info("Squared correlation coefficient = "+
((total*sumvy-sumv*sumy)*(total*sumvy-sumv*sumy))/
((total*sumvv-sumv*sumv)*(total*sumyy-sumy*sumy))+
" (regression)\n");
}
else
svm_predict.info("Accuracy = "+(double)correct/total*100+
"% ("+correct+"/"+total+") (classification)\n");
}
private static void exit_with_help()
{
System.err.print("usage: svm_predict [options] test_file model_file output_file\n"
+"options:\n"
+"-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet\n"
+"-q : quiet mode (no outputs)\n");
System.exit(1);
}
public void run ()
{
int i, predict_probability=0;
try
{
BufferedReader input = new BufferedReader(new FileReader(inputFile));
DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
System.out.println(modelFile.canRead());
System.out.println(modelFile.getName());
svm_model model = new svm().svm_load_model(modelFile.getName());
if(predict_probability == 1)
{
if(svm.svm_check_probability_model(model)==0)
{
System.err.print("Model does not support probabiliy estimates\n");
System.exit(1);
}
}
else
{
if(svm.svm_check_probability_model(model)!=0)
{
svm_predict.info("Model supports probability estimates, but disabled in prediction.\n");
}
}
predict(input,model,output,predict_probability);
System.out.println(modelFile.canRead());
System.out.println(modelFile.getName());
}
catch(FileNotFoundException e)
{
e.printStackTrace();
exit_with_help();
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
exit_with_help();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
当我尝试使用单个模型文件时,它可以正常工作
new svm_predict(new File("VisualCaractristic.libsvm"),modelFile,new File(Directory.listFiles()[i].getName()+".predit")).run();
但是当我尝试循环目录File
时我收到例外FileNotfoundException
File Directory = new File ("visualModels");
if (Directory.isDirectory()) System.out.println("true");
System.out.println(Directory.canRead());
for (int i = 0; i < Directory.listFiles().length; i++) {
new svm_predict(new File("VisualCaractristic.libsvm"),Directory.listFiles()[i],new File(Directory.listFiles()[i].getName()+".predit")).run();
}
班级svm.java
public svm_model svm_load_model(String model_file_name) throws IOException
{
return svm_load_model(new BufferedReader(new FileReader(model_file_name)));
}
public svm_model svm_load_model(BufferedReader fp) throws IOException
{
// read parameters
svm_model model = new svm_model();
svm_parameter param = new svm_parameter();
model.param = param;
model.rho = null;
model.probA = null;
model.probB = null;
model.label = null;
model.nSV = null;
while(true)
{
String cmd = fp.readLine();
String arg = cmd.substring(cmd.indexOf(' ')+1);
if(cmd.startsWith("svm_type"))
{
int i;
for(i=0;i<svm_type_table.length;i++)
{
if(arg.indexOf(svm_type_table[i])!=-1)
{
param.svm_type=i;
break;
}
}
if(i == svm_type_table.length)
{
System.err.print("unknown svm type.\n");
return null;
}
}
else if(cmd.startsWith("kernel_type"))
{
int i;
for(i=0;i<kernel_type_table.length;i++)
{
if(arg.indexOf(kernel_type_table[i])!=-1)
{
param.kernel_type=i;
break;
}
}
if(i == kernel_type_table.length)
{
System.err.print("unknown kernel function.\n");
return null;
}
}
else if(cmd.startsWith("degree"))
param.degree = atoi(arg);
else if(cmd.startsWith("gamma"))
param.gamma = atof(arg);
else if(cmd.startsWith("coef0"))
param.coef0 = atof(arg);
else if(cmd.startsWith("nr_class"))
model.nr_class = atoi(arg);
else if(cmd.startsWith("total_sv"))
model.l = atoi(arg);
else if(cmd.startsWith("rho"))
{
int n = model.nr_class * (model.nr_class-1)/2;
model.rho = new double[n];
StringTokenizer st = new StringTokenizer(arg);
for(int i=0;i<n;i++)
model.rho[i] = atof(st.nextToken());
}
else if(cmd.startsWith("label"))
{
int n = model.nr_class;
model.label = new int[n];
StringTokenizer st = new StringTokenizer(arg);
for(int i=0;i<n;i++)
model.label[i] = atoi(st.nextToken());
}
else if(cmd.startsWith("probA"))
{
int n = model.nr_class*(model.nr_class-1)/2;
model.probA = new double[n];
StringTokenizer st = new StringTokenizer(arg);
for(int i=0;i<n;i++)
model.probA[i] = atof(st.nextToken());
}
else if(cmd.startsWith("probB"))
{
int n = model.nr_class*(model.nr_class-1)/2;
model.probB = new double[n];
StringTokenizer st = new StringTokenizer(arg);
for(int i=0;i<n;i++)
model.probB[i] = atof(st.nextToken());
}
else if(cmd.startsWith("nr_sv"))
{
int n = model.nr_class;
model.nSV = new int[n];
StringTokenizer st = new StringTokenizer(arg);
for(int i=0;i<n;i++)
model.nSV[i] = atoi(st.nextToken());
}
else if(cmd.startsWith("SV"))
{
break;
}
else
{
System.err.print("unknown text in model file: ["+cmd+"]\n");
return null;
}
}
// read sv_coef and SV
int m = model.nr_class - 1;
int l = model.l;
model.sv_coef = new double[m][l];
model.SV = new svm_node[l][];
for(int i=0;i<l;i++)
{
String line = fp.readLine();
StringTokenizer st = new StringTokenizer(line," \t\n\r\f:");
for(int k=0;k<m;k++)
model.sv_coef[k][i] = atof(st.nextToken());
int n = st.countTokens()/2;
model.SV[i] = new svm_node[n];
for(int j=0;j<n;j++)
{
model.SV[i][j] = new svm_node();
model.SV[i][j].index = atoi(st.nextToken());
model.SV[i][j].value = atof(st.nextToken());
}
}
//fp.close();
return model;
}
答案 0 :(得分:3)
好的,这就是问题所在:
svm_model model = new svm().svm_load_model(modelFile.getName());
这只是使用文件名的最后一位 - 它不知道要在哪个目录中找到它。您需要传递modelFile.getPath()
,或者理想情况下更改您的svm_load_model
方法以接受File
代替String
。
此外,您应该将代码更改为:
Directory.listFiles
一次并记住结果(目前效率非常低)Directory.isFile
确保您只尝试加载文件。您可能还想将其更改为仅加载具有给定扩展名的文件。答案 1 :(得分:0)
您可以尝试使用: 文件目录=新文件(“visualModels”); (变量的名称以小写字母开头,这是一种惯例)
并在你的循环中使用: directory.isFile()来检查它是否是文件。
grtz