3网络的训练

package train;


import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import tool.gongju;
import net.net;
import net.newnet;
public class train {


/**
* 测试
* @throws IOException
*/

 

public static void main(String[] str) throws IOException {
newnet net;
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = new Mat();
String[] use= {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","J","K","M","N","Q","R","S","U","V","X","Y","Z"};
int[]shuliang= {93,8,16,19,14,8,33,8,100,25,17,15,3,1,3,5,6,3,7,12,4,7,3,7,4,1,4,2,7};
if(gongju.duquwangluo()==null)
net=new newnet();
else
net=(newnet) gongju.duquwangluo();
Date date=new Date();
Random random=new Random(date.getTime());
for(int i=0;i<4;i++) {
for(int j=0;j<100;j++) {
int zifu=j%29;
int which=(j%(random.nextInt(shuliang[zifu])+1))+1;
mat=Imgcodecs.imread("D:\\xunlian\\"+use[zifu]+"\\ ("+which+").jpg");
// System.out.println("D:\\xunlian\\"+use[zifu]+"\\ ("+which+").jpg");
Size size=new Size(28,28);
Imgproc.resize(mat, mat, size);
Imgproc.cvtColor(mat,mat,Imgproc.COLOR_BGR2GRAY);
double[][][] y=new double[29][1][1];
double[][] x=new double[1][28*28];
y[zifu][0][0]=1;
x[0]=mattodouble(mat);
net.setin(x);
net.setjieguo(y);
if(j==99) {
System.out.print("第"+(i+1)+"次误差值:");
net.bp(1);
}
else
net.bp();
}
}
gongju.baocunwangluo(net);
int errorcount=0;
for(int j=0;j<1000;j++) {
int zifu=random.nextInt(29);
int which=random.nextInt(shuliang[zifu])+1;
mat=Imgcodecs.imread("D:\\xunlian\\"+use[zifu]+"\\ ("+which+").jpg");
Size size=new Size(28,28);
Imgproc.resize(mat, mat, size);
Imgproc.cvtColor(mat,mat,Imgproc.COLOR_BGR2GRAY);
double[][] x=new double[1][28*28];
x[0]=mattodouble(mat);
net.setin(x);
int pos=net.output();
if(pos!=zifu)
errorcount++;
}
double zz=((double)(1000-errorcount)/1000)*100;
System.out.println("准确率为"+zz+"%");
for(int i=0;i<29;i++) {
mat=Imgcodecs.imread("D:\\xunlian\\"+use[i]+"\\ ("+1+").jpg");
System.out.println("D:\\xunlian\\"+i+"\\ ("+1+").jpg");
Size size=new Size(28,28);
Imgproc.resize(mat, mat, size);
Imgproc.cvtColor(mat,mat,Imgproc.COLOR_BGR2GRAY);
double[][] x=new double[1][28*28];
x[0]=mattodouble(mat);
net.setin(x);
int pos=net.output();
System.out.println(i);
HighGui.imshow(""+pos+" "+i, mat);}
HighGui.waitKey(0);
HighGui.destroyAllWindows();
}
private static double[] mattodouble(Mat mat) {
double[] x=new double[mat.cols()*mat.rows()];
for(int i=0;i<mat.rows();i++) {
for(int j=0;j<mat.cols();j++) {
double[] a = mat.get(i,j); // 获取坐标为 (i,j) 的像素值
a[0] = a[0]/255;
x[i*mat.cols()+j]= a[0]; // 设置坐标为(i,j)的像素值
}
}
return x;
}
}

posted @ 2022-06-14 17:49  记得赞  阅读(22)  评论(0编辑  收藏  举报