大幅度提高了精准模式的运行速度,舍弃了部分精度

pull/1/head
lidapeng 5 years ago
parent efdd926d7d
commit 139acaf74b

@ -59,8 +59,8 @@ public class Convolution {
List<FrameBody> frameBodies = new ArrayList<>();
double xNub = frame.getLengthHeight();
double yNub = frame.getLengthWidth();
for (int i = 0; i < x - xFrame; i += xNub) {
for (int j = 0; j < y - yFrame; j += yNub) {
for (int i = 0; i <= x - xFrame; i += xNub) {
for (int j = 0; j <= y - yFrame; j += yNub) {
FrameBody frameBody = new FrameBody();
Matrix myMatrix = matrix.getSonOfMatrix(i, j, xFrame, yFrame);
frameBody.setMatrix(myMatrix);

@ -0,0 +1,52 @@
package org.wlld.imageRecognition;
import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation;
import org.wlld.tools.ArithUtil;
/**
* @author lidapeng
* @description K
* @date 9:05 2020/2/1
*/
public class KMatrix {
private Matrix sigmaMatrix;//矩阵和
private int nub;//加和次数
private boolean isFinish = false;//是否结算
KMatrix(int x, int y) {
sigmaMatrix = new Matrix(x, y);
}
public double getEDist(Matrix matrix) throws Exception {//返回欧式距离
if (isFinish && matrix.getX() == sigmaMatrix.getX()
&& matrix.getY() == sigmaMatrix.getY()) {
double sigma = 0;
for (int i = 0; i < matrix.getX(); i++) {
for (int j = 0; j < matrix.getY(); j++) {
double sub = ArithUtil.sub(matrix.getNumber(i, j), sigmaMatrix.getNumber(i, j));
sigma = ArithUtil.add(Math.pow(sub, 2), sigma);
}
}
sigma = Math.sqrt(sigma);
return sigma;
} else {
throw new Exception("K is not finish or matrix not equal");
}
}
public void addMatrix(Matrix matrix) throws Exception {
sigmaMatrix = MatrixOperation.add(sigmaMatrix, matrix);
nub++;
}
public void getK() throws Exception {//结算K均值
if (nub != 0) {
double aNub = ArithUtil.div(1, nub);
MatrixOperation.mathMul(sigmaMatrix, aNub);
isFinish = true;
} else {
throw new Exception("not value");
}
}
}

@ -65,17 +65,10 @@ public class Operation {//进行计算
}
//卷积核学习
public void learning(Matrix matrix, Map<Integer, Double> tagging, boolean isNerveStudy) throws Exception {
public void learning(Matrix matrix, int tagging, boolean isNerveStudy) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) {
Border border = null;
int id = 0;
for (Map.Entry<Integer, Double> entry : tagging.entrySet()) {
if (entry.getValue() > 0.5) {
id = entry.getKey();
break;
}
}
if (templeConfig.isHavePosition() && isNerveStudy && id > 0) {
if (templeConfig.isHavePosition() && isNerveStudy && tagging > 0) {
outBack = imageBack;
border = convolution.borderOnce(matrix, templeConfig);
}
@ -88,12 +81,19 @@ public class Operation {//进行计算
isKernelStudy, tagging, matrixBack);
if (isNerveStudy) {
Matrix myMatrix = matrixBack.getMatrix();
if (templeConfig.isHavePosition() && id > 0) {
border.end(myMatrix, id);
if (templeConfig.isHavePosition() && tagging > 0) {
border.end(myMatrix, tagging);
}
long eventId = matrixBack.getEventId();
List<Double> featureList = getFeatureList(myMatrix);
intoNerve(eventId, featureList, templeConfig.getSensoryNerves(), true, tagging, outBack);
//System.out.println(myMatrix.getString());
//进行聚类
Map<Integer, KMatrix> kMatrixMap = templeConfig.getkMatrixMap();
if (kMatrixMap.containsKey(tagging)) {
KMatrix kMatrix = kMatrixMap.get(tagging);
kMatrix.addMatrix(myMatrix);
} else {
throw new Exception("not find tag");
}
//intoNerve(1, featureList, templeConfig.getSensoryNerves(), true, tagging, outBack);
}
} else {
throw new Exception("pattern is wrong");
@ -106,7 +106,7 @@ public class Operation {//进行计算
List<Double> list = convolution(matrix, null);
intoNerve(eventId, list, templeConfig.getSensoryNerves(), false, null, outBack);
} else if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) {
see(matrix, eventId);
throw new Exception("studyPattern not right");
}
}
@ -138,7 +138,7 @@ public class Operation {//进行计算
} else if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) {
for (FrameBody frameBody : frameBodies) {
intoNerve2(eventId, frameBody.getMatrix(), templeConfig.getConvolutionNerveManager().getSensoryNerves(),
false, null, matrixBack);
false, -1, matrixBack);
Matrix myMatrix = matrixBack.getMatrix();
//卷积层输出即边框回归的输入的特征向量
frameBody.setEndMatrix(myMatrix);
@ -279,13 +279,24 @@ public class Operation {//进行计算
}
//图像视觉 Accuracy 模式
private void see(Matrix matrix, long eventId) throws Exception {
public int toSee(Matrix matrix) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Accuracy_Pattern) {
intoNerve2(eventId, matrix, templeConfig.getConvolutionNerveManager().getSensoryNerves(),
false, null, matrixBack);
intoNerve2(2, matrix, templeConfig.getConvolutionNerveManager().getSensoryNerves(),
false, -1, matrixBack);
Matrix myMatrix = matrixBack.getMatrix();
List<Double> featureList = getFeatureList(myMatrix);
intoNerve(eventId, featureList, templeConfig.getSensoryNerves(), false, null, outBack);
int id = 0;
double distEnd = 0;
Map<Integer, KMatrix> kMatrixMap = templeConfig.getkMatrixMap();
for (Map.Entry<Integer, KMatrix> entry : kMatrixMap.entrySet()) {
int key = entry.getKey();
KMatrix kMatrix = entry.getValue();
double dist = kMatrix.getEDist(myMatrix);
if (distEnd == 0 || dist < distEnd) {
id = key;
distEnd = dist;
}
}
return id;
} else {
throw new Exception("pattern is wrong");
}
@ -311,23 +322,23 @@ public class Operation {//进行计算
List<Double> list = new ArrayList<>();
for (int i = 0; i < matrix.getX(); i++) {
for (int j = 0; j < matrix.getY(); j++) {
double nub = ArithUtil.div(matrix.getNumber(i, j), 10);
double nub = matrix.getNumber(i, j);
list.add(nub);
}
}
return list;
}
private void intoNerve(long eventId, List<Double> featurList, List<SensoryNerve> sensoryNerveList
private void intoNerve(long eventId, List<Double> featureList, List<SensoryNerve> sensoryNerveList
, boolean isStudy, Map<Integer, Double> map, OutBack imageBack) throws Exception {
for (int i = 0; i < sensoryNerveList.size(); i++) {
sensoryNerveList.get(i).postMessage(eventId, featurList.get(i), isStudy, map
sensoryNerveList.get(i).postMessage(eventId, featureList.get(i), isStudy, map
, imageBack);
}
}
private void intoNerve2(long eventId, Matrix featur, List<SensoryNerve> sensoryNerveList
, boolean isKernelStudy, Map<Integer, Double> E, OutBack outBack) throws Exception {
, boolean isKernelStudy, int E, OutBack outBack) throws Exception {
for (int i = 0; i < sensoryNerveList.size(); i++) {
sensoryNerveList.get(i).postMatrixMessage(eventId, featur, isKernelStudy, E, outBack);
}

@ -28,6 +28,7 @@ public class TempleConfig {
private int studyPattern;//学习模式
private boolean isHavePosition = false;//是否需要锁定物体位置
private Map<Integer, BorderBody> borderBodyMap = new HashMap<>();//border特征集合
private Map<Integer, KMatrix> kMatrixMap = new HashMap<>();//K均值矩阵集合
private Frame frame;//先验边框
private double th = 0.6;//标准阈值
private boolean boxReady = false;//边框已经学习完毕
@ -65,6 +66,16 @@ public class TempleConfig {
return borderBodyMap;
}
public Map<Integer, KMatrix> getkMatrixMap() {
return kMatrixMap;
}
public void clustering() throws Exception {//进行聚类结算
for (Map.Entry<Integer, KMatrix> kMatrixEntry : kMatrixMap.entrySet()) {
kMatrixEntry.getValue().getK();
}
}
private void border(BorderBody borderBody) throws Exception {
Matrix parameter = borderBody.getX();//参数矩阵
Matrix tx = borderBody.getTx();
@ -160,11 +171,13 @@ public class TempleConfig {
height = height / 3;
deep++;
}
//加载各识别分类的期望矩阵
double nub = ArithUtil.div(10, classificationNub);//每个分类期望参数的跨度
matrixMap.put(0, new Matrix(height, width));
double nub = 10;//每个分类期望参数的跨度
kMatrixMap.put(0, new KMatrix(height, width));
for (int k = 0; k < classificationNub; k++) {
Matrix matrix = new Matrix(height, width);//初始化期望矩阵
kMatrixMap.put(k + 1, new KMatrix(height, width));
double t = (k + 1) * nub;//期望矩阵的分类参数数值
for (int i = 0; i < height; i++) {//给期望矩阵注入期望参数
for (int j = 0; j < width - 1; j++) {
@ -175,17 +188,23 @@ public class TempleConfig {
}
convolutionNerveManager = new NerveManager(1, 1,
1, deep - 1, new ReLu(), true);
initNerveManager(initPower, width * height, 2);
//initNerveManager(initPower, width * height, 2);
convolutionNerveManager.setMatrixMap(matrixMap);//给卷积网络管理器注入期望矩阵
convolutionNerveManager.init(initPower, true);
}
public ModelParameter getModel() throws Exception {//获取模型参数
ModelParameter modelParameter = nerveManager.getModelParameter();
ModelParameter modelParameter = new ModelParameter();
if (studyPattern == StudyPattern.Accuracy_Pattern) {
ModelParameter modelParameter1 = convolutionNerveManager.getModelParameter();
modelParameter.setDymNerveStudies(modelParameter1.getDymNerveStudies());
modelParameter.setDymOutNerveStudy(modelParameter1.getDymOutNerveStudy());
//modelParameter.setkMatrixMap(kMatrixMap);
} else if (studyPattern == StudyPattern.Speed_Pattern) {
ModelParameter modelParameter1 = nerveManager.getModelParameter();
modelParameter.setDepthNerves(modelParameter1.getDepthNerves());
modelParameter.setOutNevers(modelParameter1.getOutNevers());
}
if (isHavePosition) {//存在边框学习模型参数
modelParameter.setBorderBodyMap(borderBodyMap);
@ -226,9 +245,14 @@ public class TempleConfig {
//注入模型参数
public void insertModel(ModelParameter modelParameter) throws Exception {
nerveManager.insertModelParameter(modelParameter);
if (studyPattern == StudyPattern.Accuracy_Pattern) {
convolutionNerveManager.insertModelParameter(modelParameter);
Map<Integer, KMatrix> kMatrixs = modelParameter.getkMatrixMap();
if (kMatrixs != null && kMatrixs.size() == kMatrixMap.size()) {
kMatrixMap = modelParameter.getkMatrixMap();
}
} else if (studyPattern == StudyPattern.Speed_Pattern) {
nerveManager.insertModelParameter(modelParameter);
}
Frame frame = modelParameter.getFrame();
Map<Integer, BorderBody> borderBodyMap = modelParameter.getBorderBodyMap();

@ -41,7 +41,7 @@ public class HiddenNerve extends Nerve {
@Override
protected void inputMatrix(long eventId, Matrix matrix, boolean isStudy
, Map<Integer, Double> E, OutBack outBack) throws Exception {
, int E, OutBack outBack) throws Exception {
Matrix myMatrix = dynamicNerve(matrix, eventId, isStudy);//处理过的矩阵
sendMatrix(eventId, myMatrix, isStudy, E, outBack);
}

@ -1,6 +1,7 @@
package org.wlld.nerveEntity;
import org.wlld.MatrixTools.Matrix;
import org.wlld.imageRecognition.KMatrix;
import org.wlld.imageRecognition.border.BorderBody;
import org.wlld.imageRecognition.border.Frame;
@ -21,12 +22,21 @@ public class ModelParameter {
private List<DymNerveStudy> dymNerveStudies = new ArrayList<>();//动态神经元隐层
private DymNerveStudy dymOutNerveStudy = new DymNerveStudy();//动态神经元输出层
private Map<Integer, BorderBody> borderBodyMap = new HashMap<>();//border特征集合
private Map<Integer, KMatrix> kMatrixMap = new HashMap<>();//K均值矩阵集合
private Frame frame;//先验边框
public Map<Integer, BorderBody> getBorderBodyMap() {
return borderBodyMap;
}
public Map<Integer, KMatrix> getkMatrixMap() {
return kMatrixMap;
}
public void setkMatrixMap(Map<Integer, KMatrix> kMatrixMap) {
this.kMatrixMap = kMatrixMap;
}
public void setBorderBodyMap(Map<Integer, BorderBody> borderBodyMap) {
this.borderBodyMap = borderBodyMap;
}

@ -77,11 +77,11 @@ public abstract class Nerve {
this.studyPoint = studyPoint;
}
public void sendMessage(long enevtId, double parameter, boolean isStudy, Map<Integer, Double> E
public void sendMessage(long eventId, double parameter, boolean isStudy, Map<Integer, Double> E
, OutBack outBack) throws Exception {
if (son.size() > 0) {
for (Nerve nerve : son) {
nerve.input(enevtId, parameter, isStudy, E, outBack);
nerve.input(eventId, parameter, isStudy, E, outBack);
}
} else {
throw new Exception("this layer is lastIndex");
@ -123,7 +123,7 @@ public abstract class Nerve {
}
public void sendMatrix(long eventId, Matrix parameter, boolean isStudy,
Map<Integer, Double> E, OutBack outBack) throws Exception {
int E, OutBack outBack) throws Exception {
if (son.size() > 0) {
for (Nerve nerve : son) {
nerve.inputMatrix(eventId, parameter, isStudy, E, outBack);
@ -154,7 +154,7 @@ public abstract class Nerve {
}
protected void inputMatrix(long eventId, Matrix matrix, boolean isKernelStudy, Map<Integer, Double> E, OutBack outBack) throws Exception {//输入动态矩阵
protected void inputMatrix(long eventId, Matrix matrix, boolean isKernelStudy, int E, OutBack outBack) throws Exception {//输入动态矩阵
}
private void backGetMessage(double parameter, long eventId) throws Exception {//反向传播

@ -19,7 +19,6 @@ import java.util.Map;
*/
public class OutNerve extends Nerve {
private Map<Integer, Matrix> matrixMapE;//主键与期望矩阵的映射
private Matrix matrixF;
public OutNerve(int id, int upNub, int downNub, double studyPoint, boolean init,
ActiveFunction activeFunction, boolean isDynamic) throws Exception {
@ -58,22 +57,12 @@ public class OutNerve extends Nerve {
@Override
protected void inputMatrix(long eventId, Matrix matrix, boolean isKernelStudy
, Map<Integer, Double> E, OutBack outBack) throws Exception {
, int E, OutBack outBack) throws Exception {
Matrix myMatrix = dynamicNerve(matrix, eventId, isKernelStudy);
if (matrixF == null) {
matrixF = new Matrix(myMatrix.getX(), myMatrix.getY());
}
if (isKernelStudy) {//回传
for (Map.Entry<Integer, Double> entry : E.entrySet()) {
double g;
if (entry.getValue() > 0.5) {//正模板
Matrix matrix1 = matrixMapE.get(entry.getKey());
g = getGradient(myMatrix, matrix1);
} else {
g = getGradient(myMatrix, matrixF);
}
backMatrix(g, eventId);
}//所有训练集的卷积核训练结束,才需要再次训练全连接层
Matrix matrix1 = matrixMapE.get(E);
double g = getGradient(myMatrix, matrix1);
backMatrix(g, eventId);
} else {//卷积层输出
if (outBack != null) {
outBack.getBackMatrix(myMatrix, eventId);

@ -25,7 +25,7 @@ public class SensoryNerve extends Nerve {
}
public void postMatrixMessage(long eventId, Matrix parameter, boolean isKernelStudy
, Map<Integer, Double> E, OutBack outBack) throws Exception {
, int E, OutBack outBack) throws Exception {
sendMatrix(eventId, parameter, isKernelStudy, E, outBack);
}

@ -8,11 +8,9 @@ import org.wlld.imageRecognition.Operation;
import org.wlld.imageRecognition.Picture;
import org.wlld.imageRecognition.TempleConfig;
import org.wlld.imageRecognition.border.Frame;
import org.wlld.imageRecognition.border.FrameBody;
import org.wlld.nerveEntity.ModelParameter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -22,16 +20,49 @@ import java.util.Map;
*/
public class HelloWorld {
public static void main(String[] args) throws Exception {
testPic();
test();
//testPic();
//testModel();
}
public static void test() throws Exception {
Picture picture = new Picture();
TempleConfig templeConfig = new TempleConfig();
ModelParameter modelParameter = JSONObject.parseObject(ModelData.DATA, ModelParameter.class);
templeConfig.init(StudyPattern.Accuracy_Pattern, true, 3204, 4032, 1);
templeConfig.insertModel(modelParameter);
Operation operation = new Operation(templeConfig);
for (int i = 1; i < 120; i++) {//神经网络学习
System.out.println("study==" + i);
//读取本地URL地址图片,并转化成矩阵
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + i + ".png");
Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b" + i + ".png");
//将图像矩阵和标注加入进行学习Accuracy_Pattern 模式 进行第二次学习
//第二次学习的时候,第三个参数必须是 true
operation.learning(right, 1, true);
operation.learning(wrong, 0, true);
}
templeConfig.clustering();//进行聚类
for (int j = 121; j < 140; j++) {
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + j + ".png");
Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b" + j + ".png");
int rightId = operation.toSee(right);
int wrongId = operation.toSee(wrong);
System.out.println("right==" + rightId);
System.out.println("wrong==" + wrongId);
}
ModelParameter modelParameter1 = templeConfig.getModel();
String a = JSON.toJSONString(modelParameter1);
System.out.println(a);
}
public static void testPic() throws Exception {
//测试SPEED模式学习过程
//初始化图像转矩阵类:作用就是说将一个图片文件转化为矩阵类
Picture picture = new Picture();
//初始化配置模板类,设置模式为SPEED_PATTERN模式 即速度模式
TempleConfig templeConfig = getTemple(true, StudyPattern.Speed_Pattern);
TempleConfig templeConfig = getTemple(true, StudyPattern.Speed_Pattern, false);
//初始化计算类,并将配置模版和输出回调类载入计算类
//运算类有两个构造一个是配置回调类,一个是不配置回调类
//若使用定位功能,则无需配置回调类,若不启用,则要配置回调类
@ -46,7 +77,7 @@ public class HelloWorld {
rightTagging.put(1, 1.0);
wrongTagging.put(1, 0.0);
// 例如上面的标注了 只有一种分类第一个MAP是true标注第二个map是false标注
for (int i = 1; i < 999; i++) {
for (int i = 1; i < 1000; i++) {
System.out.println("开始学习1==" + i);
//读取本地URL地址图片(适用于电脑本地图片),并转化成矩阵
//注意学习图片至少要一千张+同物体的不同图片,学习的越多就越准,拿同样的图片反复循环学习是没用的
@ -59,6 +90,15 @@ public class HelloWorld {
//wrong这个矩阵是错误的图片所以要配置上面错误的标注0.0 学习 告诉计算机这个图片是错误的
operation.study(wrong, wrongTagging);
}
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/test/a101.png");
Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b1000.png");
ma.setNub(1);
operation.look(wrong, 3);
ma.setNub(0);
operation.look(right, 2);
//如果启用物体坐标定位则在学习结束的时候一定要执行boxStudy方法
//若不启用,则请不要使用,否则会报错
//templeConfig.boxStudy();
@ -68,32 +108,43 @@ public class HelloWorld {
ModelParameter modelParameter = templeConfig.getModel();
//将模型MODEL转化成JSON 字符串 保存到数据库 留待下次服务启动的时候,识别提取用
String model = JSON.toJSONString(modelParameter);
System.out.println(model);
//以上就是SPEED模式下的学习全过程识别的过程就是再次初始化将学习结果注入之后使用
//识别过程
//将从数据库取出的JSON字符串转化为模型MODEL
ModelParameter modelParameter1 = JSONObject.parseObject(model, ModelParameter.class);
//初始化模型配置
TempleConfig templeConfig1 = getTemple(false, StudyPattern.Speed_Pattern);
//注入之前学习结果的模型MODEL到配置模版里面将学习结果注入就可以使用识别了
templeConfig1.insertModel(modelParameter1);
//将配置模板配置到运算类
Operation operation1 = new Operation(templeConfig1);
//获取本地图片字节码转化成降纬后的灰度矩阵
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/test/a101.png");
Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b1000.png");
//进行图像识别 参数说明 eventId,事件id,因为输出结果是在回调类回调的,所以必须有个主键去判断事件
//说明你回调是响应的哪一次调用的ID,所以每一次识别调用请用不同的id
operation1.look(wrong, 3);
operation1.look(right, 2);
// ModelParameter modelParameter1 = JSONObject.parseObject(model, ModelParameter.class);
// //初始化模型配置
// TempleConfig templeConfig1 = getTemple(false, StudyPattern.Speed_Pattern, false);
// //注入之前学习结果的模型MODEL到配置模版里面将学习结果注入就可以使用识别了
// templeConfig1.insertModel(modelParameter1);
// //将配置模板配置到运算类
// Operation operation1 = new Operation(templeConfig1);
// //获取本地图片字节码转化成降纬后的灰度矩阵
// Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/test/a101.png");
// Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b1000.png");
// //进行图像识别 参数说明 eventId,事件id,因为输出结果是在回调类回调的,所以必须有个主键去判断事件
// //说明你回调是响应的哪一次调用的ID,所以每一次识别调用请用不同的id
// operation1.look(wrong, 3);
// operation1.look(right, 2);
//若启用定位功能检测请使用lookWithPosition,若没有启用,使用检测会报错
//返回map,主键是分类id,值是该图片中此分类有多少个物体,每个物体的具体位置坐标的大小
//Map<Integer, List<FrameBody>> map = operation1.lookWithPosition(right, 4);
}
public static TempleConfig getTemple(boolean isFirst, int pattern) throws Exception {
public static TempleConfig getTemple(boolean isFirst, int pattern
, boolean isPosition) throws Exception {
//创建一个配置模板类,作用:主要是保存及载入一些配置参数用
TempleConfig templeConfig = new TempleConfig();
if (isPosition) {
templeConfig.setHavePosition(true);
Frame frame = new Frame();
frame.setWidth(3024);
frame.setHeight(4032);
frame.setLengthHeight(100);
frame.setLengthWidth(100);
templeConfig.setFrame(frame);
}
//全连接层深度,选填可不填 不填默认值为2
//这就像人类大脑的意识深度原理一样,深度学习越深,训练结果越准,但是训练量成几何倍数增加
//比如默认深度是2 需要 正负模板各一千+张照片进行训练。识别率70%(数值只是举个例子,不是具体数值)
@ -101,7 +152,7 @@ public class HelloWorld {
//以此类推,,内存允许的情况下,深度无限 识别率无限接近与百分之百
//但是有极限,即超过某个深度,即使再增加深度,识别率反而会下降。需要具体不断尝试找到 合适的深度
//注意:若深度提升,训练量没有成倍增长,则准确度反而更低!
templeConfig.setDeep(2);
//templeConfig.setDeep(2);
//启用定位学习 注意启用在图片中对某个物体进行定位,要注意
//学习的图片必须除了学习的物体以外,其他位置都是白色或者空白(即用PS扣空)。
//即该图片除了这个物体,没有其他任何干扰杂色(一个像素的杂色都不可以有)
@ -134,11 +185,11 @@ public class HelloWorld {
public static void testModel() throws Exception {
// 模型参数获取及注入 实例
TempleConfig templeConfig = getTemple(true, StudyPattern.Accuracy_Pattern);
TempleConfig templeConfig = getTemple(true, StudyPattern.Accuracy_Pattern, false);
ModelParameter modelParameter1 = templeConfig.getModel();
String model = JSON.toJSONString(modelParameter1);
System.out.println(model);
TempleConfig templeConfig2 = getTemple(false, StudyPattern.Accuracy_Pattern);
TempleConfig templeConfig2 = getTemple(false, StudyPattern.Accuracy_Pattern, false);
ModelParameter modelParameter3 = JSONObject.parseObject(model, ModelParameter.class);
templeConfig2.insertModel(modelParameter3);
ModelParameter modelParameter2 = templeConfig2.getModel();
@ -150,14 +201,15 @@ public class HelloWorld {
public static void testPic2() throws Exception {
//测试Accuracy_Pattern 模式学习过程跟SPEED模式相同的部分就不再说明了
Picture picture = new Picture();
TempleConfig templeConfig = getTemple(true, StudyPattern.Accuracy_Pattern);
Operation operation = new Operation(templeConfig);
TempleConfig templeConfig = getTemple(true, StudyPattern.Accuracy_Pattern, false);
Ma ma = new Ma();
Operation operation = new Operation(templeConfig, ma);
//标注主键为 第几种分类,值为标注 1 是TRUE 0是FALSE
Map<Integer, Double> rightTagging = new HashMap<>();//分类标注
Map<Integer, Double> wrongTagging = new HashMap<>();//分类标注
rightTagging.put(1, 1.0);
wrongTagging.put(1, 0.0);
for (int i = 1; i < 2; i++) {
for (int i = 1; i < 100; i++) {
System.out.println("开始学习1==" + i);
//读取本地URL地址图片,并转化成矩阵
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + i + ".png");
@ -166,24 +218,35 @@ public class HelloWorld {
//这里使用learning方法前两个参数与SPEED模式相同多了一个第三个参数
//第一次学习的时候 这个参数必须是 false
//最后一个参数id
operation.learning(right, rightTagging, false);
operation.learning(wrong, wrongTagging, false);
operation.learning(right, 1, false);
operation.learning(wrong, 0, false);
}
for (int i = 1; i < 2; i++) {//神经网络学习
for (int i = 1; i < 300; i++) {//神经网络学习
System.out.println("开始学习2==" + i);
//读取本地URL地址图片,并转化成矩阵
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + i + ".png");
Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b" + i + ".png");
//将图像矩阵和标注加入进行学习Accuracy_Pattern 模式 进行第二次学习
//第二次学习的时候,第三个参数必须是 true
operation.learning(right, rightTagging, true);
operation.learning(wrong, wrongTagging, true);
operation.learning(right, 1, true);
operation.learning(wrong, 0, true);
}
ModelParameter modelParameter = templeConfig.getModel();
String st = JSON.toJSONString(modelParameter);
System.out.println(st);
for (int i = 300; i < 320; i++) {//神经网络学习
//读取本地URL地址图片,并转化成矩阵
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + i + ".png");
Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b" + i + ".png");
//将图像矩阵和标注加入进行学习Accuracy_Pattern 模式 进行第二次学习
//第二次学习的时候,第三个参数必须是 true
ma.setNub(1);
operation.look(right, 100 + i);
ma.setNub(0);
operation.look(wrong, 200 + i);
}
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/test/a101.png");
Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b1000.png");
//进行图像识别Accuracy_Pattern 模式学习结果获取和注入跟SPEED模式一致
//若有疑问可以参考一下 testModel()方法
operation.look(right, 2);
operation.look(wrong, 3);
}
}

@ -17,7 +17,8 @@ public class Ma implements OutBack {
@Override
public void getBack(double out, int id, long eventId) {
System.out.println("id==" + id + ",out==" + out + ",nub==" + nub);
System.out.println("id==" + id + ",out==" + out + ",nub==" + nub +
",eid==" + eventId);
}
@Override

Loading…
Cancel
Save