将训练集构建成ImageNet模型
以下程序实现将训练集构建为ImageNet模型,训练集图片为56个民族
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; public class createTxt { public static void createClassInd(){//56个民族编号及名称 FileOutputStream fout; try { fout = new FileOutputStream("E:\\resourse\\InnovationFund\\file\\classInd.txt");//保存在该文件中 PrintWriter pwr = new PrintWriter(fout); for (int i = 1; i < 57; i++) { pwr.print(i); pwr.print(" "); switch(i){ case 1: pwr.print("han"); break; case 2: pwr.print("hui"); break; case 3: pwr.print("zhuang"); break; case 4: pwr.print("zang"); break; case 5: pwr.print("yao"); break; case 6: pwr.print("yugu"); break; case 7: pwr.print("yi"); break; case 8: pwr.print("xibo"); break; case 9: pwr.print("weiwuer"); break; case 10: pwr.print("li"); break; case 11: pwr.print("wa"); break; case 12: pwr.print("tujia"); break; case 13: pwr.print("tu"); break; case 14: pwr.print("tataer"); break; case 15: pwr.print("tajike"); break; case 16: pwr.print("shui"); break; case 17: pwr.print("sala"); break; case 18: pwr.print("qiang"); break; case 19: pwr.print("pumi"); break; case 20: pwr.print("nu"); break; case 21: pwr.print("naxi"); break; case 22: pwr.print("miao"); break; case 23: pwr.print("menggu"); break; case 24: pwr.print("menba"); break; case 25: pwr.print("maonan"); break; case 26: pwr.print("man"); break; case 27: pwr.print("luoba"); break; case 28: pwr.print("keerkezi"); break; case 29: pwr.print("wuzibieke"); break; case 30: pwr.print("she"); break; case 31: pwr.print("mulao"); break; case 32: pwr.print("susu"); break; case 33: pwr.print("lagu"); break; case 34: pwr.print("jinuo"); break; case 35: pwr.print("jing"); break; case 36: pwr.print("hezhe"); break; case 37: pwr.print("hasake"); break; case 38: pwr.print("hani"); break; case 39: pwr.print("qilao"); break; case 40: pwr.print("gaoshan"); break; case 41: pwr.print("ewenke"); break; case 42: pwr.print("eluosi"); break; case 43: pwr.print("elunchun"); break; case 44: pwr.print("dulong"); break; case 45: pwr.print("dongxiang"); break; case 46: pwr.print("tong"); break; case 47: pwr.print("deang"); break; case 48: pwr.print("dai"); break; case 49: pwr.print("dawoer"); break; case 50: pwr.print("chaoxian"); break; case 51: pwr.print("buyi"); break; case 52: pwr.print("bulang"); break; case 53: pwr.print("baoan"); break; case 54: pwr.print("bai"); break; case 55: pwr.print("achang"); break; case 56: pwr.print("jingpo"); break; } pwr.print("\r\n");//换行 } pwr.close(); }catch(Exception e){System.out.println("ClassInd。txt失败");} } public static boolean noDb(String str){//不包含返回true,判断文件是否为.db文件 boolean in = true; if(str.indexOf(".db")!=-1)//包含 in=false; return in; } private static void getFile(String path, PrintWriter pwr,PrintWriter tpwr,int n) throws FileNotFoundException{ //测试集:训练集:验证集=6:2:2,访问目录下文件的文件名 File file = new File(path); //图片集文件夹 // 获得该文件夹内的所有文件 File[] array = file.listFiles(); //目录下所有文件名 for(int i=0;i<array.length;i++) { if(array[i].isFile())//如果是文件 { int fn=array.length*3/5;//6成 int tfn = array.length*4/5;//2成 // 只输出文件名字 if(i < fn && noDb(array[i].getName())){//测试集 //System.out.println( array[i].getName()+" "+fn); pwr.print(array[i].getName()); pwr.print(" "); pwr.print(n); //编号 pwr.print("\r\n"); //System.out.println( array[i].getPath()); //移动图片到指定目录 File afile = new File(array[i].getPath()); afile.renameTo(new File("E:\\resourse\\InnovationFund\\file\\test\\" + afile.getName())); }else if(i < tfn && noDb(array[i].getName())){//训练集 tpwr.print(array[i].getName()); tpwr.print(" "); tpwr.print(n); tpwr.print("\r\n"); File afile = new File(array[i].getPath()); afile.renameTo(new File("E:\\resourse\\InnovationFund\\file\\trian\\" + afile.getName())); } } else if(array[i].isDirectory())//如果是文件夹 { System.out.println( array[i].getName()); n++; getFile(array[i].getPath(), pwr, tpwr, n); //递归 } } } public static void main(String args[]) throws FileNotFoundException { createClassInd(); String path = "E:\\一些资料\\创新基金\\民族服饰 cai"; FileOutputStream fout; fout = new FileOutputStream("E:\\resourse\\InnovationFund\\file\\test_label.txt"); PrintWriter pwr = new PrintWriter(fout); FileOutputStream tfout; tfout = new FileOutputStream("E:\\resourse\\InnovationFund\\file\\train_label.txt"); PrintWriter tpwr = new PrintWriter(tfout); int n=0; getFile(path, pwr,tpwr, n); } }