apida大改

pull/2/head
lidapeng 5 years ago
parent 5a26118ccf
commit b809a4a60f

@ -2,6 +2,9 @@ package org.wlld.MatrixTools;
import org.wlld.tools.ArithUtil; import org.wlld.tools.ArithUtil;
import java.util.ArrayList;
import java.util.List;
public class MatrixOperation { public class MatrixOperation {
private MatrixOperation() { private MatrixOperation() {
@ -370,4 +373,22 @@ public class MatrixOperation {
} }
} }
} }
//行向量转LIST
public static List<Double> rowVectorToList(Matrix matrix) throws Exception {
List<Double> list = new ArrayList<>();
for (int j = 0; j < matrix.getY(); j++) {
list.add(matrix.getNumber(0, j));
}
return list;
}
//list转行向量
public static Matrix listToRowVector(List<Double> list) throws Exception {
Matrix matrix = new Matrix(1, list.size());
for (int i = 0; i < list.size(); i++) {
matrix.setNub(0, i, list.get(i));
}
return matrix;
}
} }

@ -8,4 +8,6 @@ package org.wlld.config;
public class StudyPattern { public class StudyPattern {
public static final int Speed_Pattern = 1;//速度学习模式 public static final int Speed_Pattern = 1;//速度学习模式
public static final int Accuracy_Pattern = 2;//精准学习模式 public static final int Accuracy_Pattern = 2;//精准学习模式
public static final int Cover_Pattern = 3;//覆盖率模式
} }

@ -96,19 +96,34 @@ public class Convolution {
if (isOnce) { if (isOnce) {
return null; return null;
} else { } else {
return late(myMatrix); return late(myMatrix, 2);
} }
} }
private Matrix late(Matrix matrix) throws Exception {//迟化处理 public Matrix getBorder(Matrix matrix, Matrix kernel) throws Exception {
int x = matrix.getX() - 2;//求导后矩阵的行数
int y = matrix.getY() - 2;//求导后矩阵的列数
Matrix myMatrix = new Matrix(x, y);//最终合成矩阵
for (int i = 0; i < x; i++) {//遍历行
for (int j = 0; j < y; j++) {//遍历每行的列
double dm = MatrixOperation.convolution(matrix, kernel, i, j);
if (dm > 0) {//存在边缘
myMatrix.setNub(i, j, dm);
}
}
}
return myMatrix;
}
protected Matrix late(Matrix matrix, int size) throws Exception {//迟化处理
int xn = matrix.getX(); int xn = matrix.getX();
int yn = matrix.getY(); int yn = matrix.getY();
int x = xn / 2;//求导后矩阵的行数 int x = xn / size;//求导后矩阵的行数
int y = yn / 2;//求导后矩阵的列数 int y = yn / size;//求导后矩阵的列数
Matrix myMatrix = new Matrix(x, y);//迟化后的矩阵 Matrix myMatrix = new Matrix(x, y);//迟化后的矩阵
for (int i = 0; i < xn - 2; i += 2) { for (int i = 0; i < xn - size; i += size) {
for (int j = 0; j < yn - 2; j += 2) { for (int j = 0; j < yn - size; j += size) {
Matrix matrix1 = matrix.getSonOfMatrix(i, j, 2, 2); Matrix matrix1 = matrix.getSonOfMatrix(i, j, size, size);
double maxNub = 0; double maxNub = 0;
for (int t = 0; t < matrix1.getX(); t++) { for (int t = 0; t < matrix1.getX(); t++) {
for (int k = 0; k < matrix1.getY(); k++) { for (int k = 0; k < matrix1.getY(); k++) {
@ -119,7 +134,7 @@ public class Convolution {
} }
} }
//迟化的最大值是 MAXNUB //迟化的最大值是 MAXNUB
myMatrix.setNub(i / 2, j / 2, maxNub); myMatrix.setNub(i / size, j / size, maxNub);
} }
} }
return myMatrix; return myMatrix;

@ -4,6 +4,7 @@ package org.wlld.imageRecognition;
import org.wlld.MatrixTools.Matrix; import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation; import org.wlld.MatrixTools.MatrixOperation;
import org.wlld.config.Classifier; import org.wlld.config.Classifier;
import org.wlld.config.Kernel;
import org.wlld.config.StudyPattern; import org.wlld.config.StudyPattern;
import org.wlld.function.Sigmod; import org.wlld.function.Sigmod;
import org.wlld.i.OutBack; import org.wlld.i.OutBack;
@ -26,9 +27,11 @@ public class Operation {//进行计算
private MatrixBack matrixBack = new MatrixBack(); private MatrixBack matrixBack = new MatrixBack();
private ImageBack imageBack = new ImageBack(); private ImageBack imageBack = new ImageBack();
private OutBack outBack; private OutBack outBack;
private double avg;
public Operation(TempleConfig templeConfig) { public Operation(TempleConfig templeConfig) {
this.templeConfig = templeConfig; this.templeConfig = templeConfig;
avg = templeConfig.getAvg();
} }
public Operation(TempleConfig templeConfig, OutBack outBack) { public Operation(TempleConfig templeConfig, OutBack outBack) {
@ -57,6 +60,83 @@ public class Operation {//进行计算
return sub(matrix1); return sub(matrix1);
} }
private double one(List<List<Double>> rightLists, double sigma) {
for (List<Double> list : rightLists) {
for (double nub : list) {
sigma = ArithUtil.add(nub, sigma);
}
}
return sigma;
}
private void lastOne(List<List<Double>> rightLists, double avg) {
for (List<Double> list : rightLists) {
for (int i = 0; i < list.size(); i++) {
list.set(i, ArithUtil.sub(list.get(i), avg));
}
}
}
//从一张图片的局部进行学习
public void coverStudy(Matrix matrixRight, Map<Integer, Double> right
, Matrix matrixWrong, Map<Integer, Double> wrong) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Cover_Pattern) {
//先用边缘算子卷一遍
matrixRight = convolution.late(convolution.getBorder(matrixRight, Kernel.ALL_Two), 2);
matrixWrong = convolution.late(convolution.getBorder(matrixWrong, Kernel.ALL_Two), 2);
List<List<Double>> rightLists = getFeatures(matrixRight);
List<List<Double>> wrongLists = getFeatures(matrixWrong);
int nub = rightLists.size() * 2 * 9;
double sigma = one(rightLists, 0);
sigma = one(rightLists, sigma);
avg = ArithUtil.div(sigma, nub);
templeConfig.setAvg(avg);
lastOne(rightLists, avg);
lastOne(wrongLists, avg);
//特征塞入容器完毕
int size = rightLists.size();
for (int j = 0; j < 3; j++) {
for (int i = 0; i < size; i++) {
List<Double> rightList = rightLists.get(i);
List<Double> wrongList = wrongLists.get(i);
intoDnnNetwork(1, rightList, templeConfig.getSensoryNerves(), true, right, null);
intoDnnNetwork(1, wrongList, templeConfig.getSensoryNerves(), true, wrong, null);
//System.out.println("right:" + rightList);
//System.out.println("wrong:" + wrongList);
}
}
} else {
throw new Exception("PATTERN IS NOT COVER");
}
}
public double coverPoint(Matrix matrix, int rightId) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Cover_Pattern) {
matrix = convolution.late(convolution.getBorder(matrix, Kernel.ALL_Two), 2);
List<List<Double>> lists = getFeatures(matrix);
//特征塞入容器完毕
int size = lists.size();
int right = 0;
int all = 0;
lastOne(lists, avg);
for (int i = 0; i < size; i++) {
List<Double> list = lists.get(i);
MaxPoint maxPoint = new MaxPoint();
long pid = IdCreator.get().nextId();
intoDnnNetwork(pid, list, templeConfig.getSensoryNerves(), false, null, maxPoint);
int id = maxPoint.getId();
if (id == rightId) {
right++;
}
all++;
}
//ArithUtil.div(coverBody.getRightNub(), size)
return ArithUtil.div(right, all);
} else {
throw new Exception("PATTERN IS NOT COVER");
}
}
//模板学习 //模板学习
public void study(Matrix matrix, Map<Integer, Double> tagging) throws Exception { public void study(Matrix matrix, Map<Integer, Double> tagging) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) { if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) {
@ -135,19 +215,6 @@ public class Operation {//进行计算
Map<Integer, Double> map = new HashMap<>(); Map<Integer, Double> map = new HashMap<>();
map.put(tagging, 1.0); map.put(tagging, 1.0);
intoDnnNetwork(1, featureALL, templeConfig.getSensoryNerves(), true, map, null); intoDnnNetwork(1, featureALL, templeConfig.getSensoryNerves(), true, map, null);
// int classifier = templeConfig.getClassifier();
// switch (classifier) {
// case Classifier.DNN:
// dnn(tagging, myMatrixR);
// break;
// case Classifier.LVQ:
// lvq(tagging, myMatrixR);
// break;
// case Classifier.VAvg:
// vectorAvg(tagging, myMatrixR);
// break;
// }
} }
} }
@ -214,7 +281,7 @@ public class Operation {//进行计算
Map<Integer, Double> map = new HashMap<>(); Map<Integer, Double> map = new HashMap<>();
map.put(tagging, 1.0); map.put(tagging, 1.0);
List<Double> feature = getFeature(myMatrix); List<Double> feature = getFeature(myMatrix);
System.out.println(feature); //System.out.println(feature);
intoDnnNetwork(1, feature, templeConfig.getSensoryNerves(), true, map, null); intoDnnNetwork(1, feature, templeConfig.getSensoryNerves(), true, map, null);
} }
@ -227,6 +294,7 @@ public class Operation {//进行计算
private void lvq(int tagging, Matrix myMatrix) throws Exception {//LVQ学习 private void lvq(int tagging, Matrix myMatrix) throws Exception {//LVQ学习
LVQ lvq = templeConfig.getLvq(); LVQ lvq = templeConfig.getLvq();
Matrix vector = MatrixOperation.matrixToVector(myMatrix, true); Matrix vector = MatrixOperation.matrixToVector(myMatrix, true);
System.out.println(vector.getString());
MatrixBody matrixBody = new MatrixBody(); MatrixBody matrixBody = new MatrixBody();
matrixBody.setMatrix(vector); matrixBody.setMatrix(vector);
matrixBody.setId(tagging); matrixBody.setId(tagging);
@ -237,12 +305,11 @@ public class Operation {//进行计算
List<Double> list = new ArrayList<>(); List<Double> list = new ArrayList<>();
Normalization normalization = templeConfig.getNormalization(); Normalization normalization = templeConfig.getNormalization();
double middle = normalization.getAvg(); double middle = normalization.getAvg();
//double sub = ArithUtil.sub(normalization.getMax(), normalization.getMin());
for (int i = 0; i < matrix.getX(); i++) { for (int i = 0; i < matrix.getX(); i++) {
for (int j = 0; j < matrix.getY(); j++) { for (int j = 0; j < matrix.getY(); j++) {
double nub = matrix.getNumber(i, j); double nub = matrix.getNumber(i, j);
if (nub != 0) { if (nub != 0) {
nub = ArithUtil.sub(nub, middle);//middle nub = ArithUtil.sub(nub, middle);
list.add(nub); list.add(nub);
} else { } else {
list.add(0.0); list.add(0.0);
@ -253,6 +320,32 @@ public class Operation {//进行计算
return list; return list;
} }
private List<List<Double>> getFeatures(Matrix matrix) throws Exception {
List<List<Double>> lists = new ArrayList<>();
int x = matrix.getX() - 3;//求导后矩阵的行数
int y = matrix.getY() - 3;//求导后矩阵的列数
for (int i = 0; i < x; i += 3) {//遍历行
for (int j = 0; j < y; j += 3) {//遍历每行的列
Matrix myMatrix = matrix.getSonOfMatrix(i, j, 3, 3);
lists.add(getListFeature(myMatrix));
}
}
return lists;
}
private List<Double> getListFeature(Matrix matrix) throws Exception {
List<Double> list = new ArrayList<>();
int x = matrix.getX();
int y = matrix.getY();
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
double nub = matrix.getNumber(i, j) / 300;
list.add(nub);
}
}
return list;
}
//图像视觉 speed 模式 //图像视觉 speed 模式
public void look(Matrix matrix, long eventId) throws Exception { public void look(Matrix matrix, long eventId) throws Exception {
if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) { if (templeConfig.getStudyPattern() == StudyPattern.Speed_Pattern) {
@ -295,8 +388,19 @@ public class Operation {//进行计算
Matrix myMatrix = matrixBack.getMatrix(); Matrix myMatrix = matrixBack.getMatrix();
//卷积层输出即边框回归的输入的特征向量 //卷积层输出即边框回归的输入的特征向量
frameBody.setEndMatrix(myMatrix); frameBody.setEndMatrix(myMatrix);
//Matrix vector = MatrixOperation.matrixToVector(myMatrix, true); int classifier = templeConfig.getClassifier();
int id = getClassificationIdByLVQ(myMatrix); int id = 0;
switch (classifier) {
case Classifier.LVQ:
id = getClassificationIdByLVQ(myMatrix);
break;
case Classifier.DNN:
id = getClassificationIdByDnn(myMatrix);
break;
case Classifier.VAvg:
id = getClassificationIdByVag(myMatrix);
break;
}
frameBody.setId(id); frameBody.setId(id);
} }
return toPosition(frameBodies, frame.getWidth(), frame.getHeight()); return toPosition(frameBodies, frame.getWidth(), frame.getHeight());

@ -1,6 +1,7 @@
package org.wlld.imageRecognition; package org.wlld.imageRecognition;
import org.wlld.MatrixTools.Matrix; import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation;
import org.wlld.config.Classifier; import org.wlld.config.Classifier;
import org.wlld.config.StudyPattern; import org.wlld.config.StudyPattern;
import org.wlld.function.ReLu; import org.wlld.function.ReLu;
@ -31,7 +32,7 @@ public class TempleConfig {
private int row = 5;//行的最小比例 private int row = 5;//行的最小比例
private int column = 3;//列的最小比例 private int column = 3;//列的最小比例
private int deep = 1;//默认深度 private int deep = 1;//默认深度
private int classificationNub = 1;//分类的数量 private int classificationNub = 2;//分类的数量
private int studyPattern;//学习模式 private int studyPattern;//学习模式
private boolean isHavePosition = false;//是否需要锁定物体位置 private boolean isHavePosition = false;//是否需要锁定物体位置
private LVQ lvq;//模型需要返回,精准模式下的原型聚类 private LVQ lvq;//模型需要返回,精准模式下的原型聚类
@ -39,11 +40,21 @@ public class TempleConfig {
private double th = 0.6;//标准阈值 private double th = 0.6;//标准阈值
private boolean boxReady = false;//边框已经学习完毕 private boolean boxReady = false;//边框已经学习完毕
private double iouTh = 0.5;//IOU阈值 private double iouTh = 0.5;//IOU阈值
private int lvqNub = 30;//lvq循环次数默认30 private int lvqNub = 10;//lvq循环次数默认30
private VectorK vectorK; private VectorK vectorK;
private boolean isThreeChannel;//是否启用三通道 private boolean isThreeChannel;//是否启用三通道
private int classifier = Classifier.VAvg;//默认分类类别使用的是向量均值分类 private int classifier = Classifier.VAvg;//默认分类类别使用的是向量均值分类
private Normalization normalization = new Normalization();//统一归一化 private Normalization normalization = new Normalization();//统一归一化
private double avg = 0;//覆盖均值
private int sensoryNerveNub;//输入神经元个数
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
public Normalization getNormalization() { public Normalization getNormalization() {
return normalization; return normalization;
@ -102,10 +113,6 @@ public class TempleConfig {
return kClusteringMap; return kClusteringMap;
} }
public int getLvqNub() {
return lvqNub;
}
public void setLvqNub(int lvqNub) { public void setLvqNub(int lvqNub) {
this.lvqNub = lvqNub; this.lvqNub = lvqNub;
} }
@ -182,6 +189,9 @@ public class TempleConfig {
case StudyPattern.Accuracy_Pattern://精准学习模式 case StudyPattern.Accuracy_Pattern://精准学习模式
initConvolutionVision(initPower, width, height); initConvolutionVision(initPower, width, height);
break; break;
case StudyPattern.Cover_Pattern://覆盖学习模式
initNerveManager(initPower, 9, deep);
break;
} }
} }
@ -226,12 +236,13 @@ public class TempleConfig {
lvq = new LVQ(classificationNub, lvqNub); lvq = new LVQ(classificationNub, lvqNub);
break; break;
case Classifier.VAvg: case Classifier.VAvg:
vectorK = new VectorK(height * width); sensoryNerveNub = height * width;
vectorK = new VectorK(sensoryNerveNub);
break; break;
} }
//加载各识别分类的期望矩阵 //加载各识别分类的期望矩阵
matrixMap.put(0, new Matrix(height, width)); matrixMap.put(0, new Matrix(height, width));
double nub = 1;//每个分类期望参数的跨度 double nub = 0.5;//每个分类期望参数的跨度
for (int k = 1; k <= classificationNub; k++) { for (int k = 1; k <= classificationNub; k++) {
Matrix matrix = new Matrix(height, width);//初始化期望矩阵 Matrix matrix = new Matrix(height, width);//初始化期望矩阵
double t = k * nub;//期望矩阵的分类参数数值 double t = k * nub;//期望矩阵的分类参数数值
@ -266,7 +277,7 @@ public class TempleConfig {
MatrixBody matrixBody = matrixBodies[i]; MatrixBody matrixBody = matrixBodies[i];
Matrix matrix = matrixBody.getMatrix(); Matrix matrix = matrixBody.getMatrix();
MatrixModel matrixModel = new MatrixModel(); MatrixModel matrixModel = new MatrixModel();
List<Double> rowVector = rowVectorToList(matrix); List<Double> rowVector = MatrixOperation.rowVectorToList(matrix);
matrixModel.setId(matrixBody.getId()); matrixModel.setId(matrixBody.getId());
matrixModel.setRowVector(rowVector); matrixModel.setRowVector(rowVector);
matrixModelList.add(matrixModel); matrixModelList.add(matrixModel);
@ -274,15 +285,6 @@ public class TempleConfig {
return matrixModelList; return matrixModelList;
} }
//行向量转LIST
private List<Double> rowVectorToList(Matrix matrix) throws Exception {
List<Double> list = new ArrayList<>();
for (int j = 0; j < matrix.getY(); j++) {
list.add(matrix.getNumber(0, j));
}
return list;
}
private Map<Integer, KBorder> kToBody() throws Exception { private Map<Integer, KBorder> kToBody() throws Exception {
Map<Integer, KBorder> borderMap = new HashMap<>(); Map<Integer, KBorder> borderMap = new HashMap<>();
for (Map.Entry<Integer, KClustering> entry : kClusteringMap.entrySet()) { for (Map.Entry<Integer, KClustering> entry : kClusteringMap.entrySet()) {
@ -301,14 +303,14 @@ public class TempleConfig {
for (Map.Entry<Integer, Box> entryBox : positionMap.entrySet()) { for (Map.Entry<Integer, Box> entryBox : positionMap.entrySet()) {
Box box = entryBox.getValue(); Box box = entryBox.getValue();
BoxList boxList = new BoxList(); BoxList boxList = new BoxList();
List<Double> furList = rowVectorToList(box.getMatrix()); List<Double> furList = MatrixOperation.rowVectorToList(box.getMatrix());
List<Double> borderList = rowVectorToList(box.getMatrixPosition()); List<Double> borderList = MatrixOperation.rowVectorToList(box.getMatrixPosition());
boxList.setList(furList); boxList.setList(furList);
boxList.setPositionList(borderList); boxList.setPositionList(borderList);
myPosition.put(entryBox.getKey(), boxList); myPosition.put(entryBox.getKey(), boxList);
} }
for (int i = 0; i < matrices.length; i++) { for (int i = 0; i < matrices.length; i++) {
lists.add(rowVectorToList(matrices[i])); lists.add(MatrixOperation.rowVectorToList(matrices[i]));
} }
} }
@ -324,18 +326,37 @@ public class TempleConfig {
modelParameter.setDymNerveStudies(modelParameter1.getDymNerveStudies()); modelParameter.setDymNerveStudies(modelParameter1.getDymNerveStudies());
modelParameter.setDymOutNerveStudy(modelParameter1.getDymOutNerveStudy()); modelParameter.setDymOutNerveStudy(modelParameter1.getDymOutNerveStudy());
//获取LVQ模型 //获取LVQ模型
if (classifier == Classifier.LVQ && lvq.isReady()) { switch (classifier) {
case Classifier.LVQ:
if (lvq.isReady()) {
LvqModel lvqModel = new LvqModel(); LvqModel lvqModel = new LvqModel();
lvqModel.setLength(lvq.getLength()); lvqModel.setLength(lvq.getLength());
lvqModel.setTypeNub(lvq.getTypeNub()); lvqModel.setTypeNub(lvq.getTypeNub());
lvqModel.setMatrixModelList(getLvqModel(lvq.getModel())); lvqModel.setMatrixModelList(getLvqModel(lvq.getModel()));
modelParameter.setLvqModel(lvqModel); modelParameter.setLvqModel(lvqModel);
} }
break;
case Classifier.VAvg:
if (vectorK != null) {
Map<Integer, List<Double>> map = vectorK.getKMatrix();
modelParameter.setMatrixK(map);
}
break;
case Classifier.DNN:
if (normalization != null) {
modelParameter.setDnnAvg(normalization.getAvg());
}
ModelParameter modelParameter2 = nerveManager.getModelParameter();
modelParameter.setDepthNerves(modelParameter2.getDepthNerves());
modelParameter.setOutNerves(modelParameter2.getOutNerves());
break;
}
} else if (studyPattern == StudyPattern.Speed_Pattern) { } else if (studyPattern == StudyPattern.Cover_Pattern) {
ModelParameter modelParameter1 = nerveManager.getModelParameter(); ModelParameter modelParameter1 = nerveManager.getModelParameter();
modelParameter.setDepthNerves(modelParameter1.getDepthNerves()); modelParameter.setDepthNerves(modelParameter1.getDepthNerves());
modelParameter.setOutNerves(modelParameter1.getOutNerves()); modelParameter.setOutNerves(modelParameter1.getOutNerves());
modelParameter1.setAvg(avg);
} }
if (isHavePosition && kClusteringMap != null && kClusteringMap.size() > 0) {//存在边框学习模型参数 if (isHavePosition && kClusteringMap != null && kClusteringMap.size() > 0) {//存在边框学习模型参数
Map<Integer, KBorder> kBorderMap = kToBody(); Map<Integer, KBorder> kBorderMap = kToBody();
@ -371,20 +392,13 @@ public class TempleConfig {
return column; return column;
} }
//list转行向量
private Matrix listToRowVector(List<Double> list) throws Exception {
Matrix matrix = new Matrix(1, list.size());
for (int i = 0; i < list.size(); i++) {
matrix.setNub(0, i, list.get(i));
}
return matrix;
}
//注入模型参数 //注入模型参数
public void insertModel(ModelParameter modelParameter) throws Exception { public void insertModel(ModelParameter modelParameter) throws Exception {
if (studyPattern == StudyPattern.Accuracy_Pattern) { if (studyPattern == StudyPattern.Accuracy_Pattern) {
convolutionNerveManager.insertModelParameter(modelParameter); convolutionNerveManager.insertModelParameter(modelParameter);
//LVQ模型参数注入 //LVQ模型参数注入
switch (classifier) {
case Classifier.LVQ:
LvqModel lvqModel = modelParameter.getLvqModel(); LvqModel lvqModel = modelParameter.getLvqModel();
if (lvqModel != null) { if (lvqModel != null) {
int length = lvqModel.getLength(); int length = lvqModel.getLength();
@ -397,7 +411,7 @@ public class TempleConfig {
MatrixModel matrixModel = matrixModels.get(i); MatrixModel matrixModel = matrixModels.get(i);
MatrixBody matrixBody = new MatrixBody(); MatrixBody matrixBody = new MatrixBody();
matrixBody.setId(matrixModel.getId()); matrixBody.setId(matrixModel.getId());
matrixBody.setMatrix(listToRowVector(matrixModel.getRowVector())); matrixBody.setMatrix(MatrixOperation.listToRowVector(matrixModel.getRowVector()));
model[i] = matrixBody; model[i] = matrixBody;
} }
lvq.setLength(length); lvq.setLength(length);
@ -405,10 +419,24 @@ public class TempleConfig {
lvq.setReady(true); lvq.setReady(true);
lvq.setModel(model); lvq.setModel(model);
} }
} }
break;
case Classifier.VAvg:
vectorK = new VectorK(sensoryNerveNub);
vectorK.insertKMatrix(modelParameter.getMatrixK());
break;
case Classifier.DNN:
nerveManager.insertModelParameter(modelParameter);
normalization = new Normalization();
normalization.setAvg(modelParameter.getDnnAvg());
break;
}
} else if (studyPattern == StudyPattern.Speed_Pattern) { } else if (studyPattern == StudyPattern.Speed_Pattern) {
nerveManager.insertModelParameter(modelParameter); nerveManager.insertModelParameter(modelParameter);
} else if (studyPattern == StudyPattern.Cover_Pattern) {
nerveManager.insertModelParameter(modelParameter);
avg = modelParameter.getAvg();
} }
if (isHavePosition) { if (isHavePosition) {
if (modelParameter.getFrame() != null) { if (modelParameter.getFrame() != null) {
@ -429,7 +457,7 @@ public class TempleConfig {
Matrix[] matrices = new Matrix[lists.size()]; Matrix[] matrices = new Matrix[lists.size()];
Map<Integer, Box> boxMap = kClustering.getPositionMap(); Map<Integer, Box> boxMap = kClustering.getPositionMap();
for (int i = 0; i < lists.size(); i++) { for (int i = 0; i < lists.size(); i++) {
Matrix matrix = listToRowVector(lists.get(i)); Matrix matrix = MatrixOperation.listToRowVector(lists.get(i));
matrices[i] = matrix; matrices[i] = matrix;
} }
kClustering.setMatrices(matrices); kClustering.setMatrices(matrices);
@ -438,8 +466,8 @@ public class TempleConfig {
for (Map.Entry<Integer, BoxList> boxEntry : boxListMap.entrySet()) { for (Map.Entry<Integer, BoxList> boxEntry : boxListMap.entrySet()) {
Box box = new Box(); Box box = new Box();
BoxList boxList = boxEntry.getValue(); BoxList boxList = boxEntry.getValue();
box.setMatrix(listToRowVector(boxList.getList())); box.setMatrix(MatrixOperation.listToRowVector(boxList.getList()));
box.setMatrixPosition(listToRowVector(boxList.getPositionList())); box.setMatrixPosition(MatrixOperation.listToRowVector(boxList.getPositionList()));
boxMap.put(boxEntry.getKey(), box); boxMap.put(boxEntry.getKey(), box);
} }
} }
@ -448,20 +476,8 @@ public class TempleConfig {
} }
} }
public int getDeep() {
return deep;
}
public void setDeep(int deep) { public void setDeep(int deep) {
this.deep = deep; this.deep = deep;
} }
public int getClassificationNub() {
return classificationNub;
}
public void setClassificationNub(int classificationNub) {
this.classificationNub = classificationNub;
}
} }

@ -8,7 +8,7 @@ import java.util.*;
public class VectorK { public class VectorK {
private Map<Integer, List<Matrix>> matrixMap = new HashMap<>(); private Map<Integer, List<Matrix>> matrixMap = new HashMap<>();
private Map<Integer, Matrix> matrixK = new HashMap<>(); private Map<Integer, Matrix> matrixK = new HashMap<>();//这个作为模型拿出来
private int length; private int length;
public VectorK(int length) { public VectorK(int length) {
@ -43,31 +43,26 @@ public class VectorK {
return matrix; return matrix;
} }
private Matrix mind(List<Matrix> matrixList) throws Exception {//拿中位数 public void study() throws Exception {
Matrix matrix = new Matrix(1, length); for (Map.Entry<Integer, List<Matrix>> entry : matrixMap.entrySet()) {
List<List<Double>> lists = new ArrayList<>(); List<Matrix> matrixList = entry.getValue();
for (Matrix matrix1 : matrixList) { Matrix matrix = sigma(matrixList);
for (int i = 0; i < matrix1.getY(); i++) { matrixK.put(entry.getKey(), matrix);
if (lists.size() <= i) {
lists.add(new ArrayList<>());
}
List<Double> list = lists.get(i);
list.add(matrix1.getNumber(0, i));
Collections.sort(list);
} }
} }
for (int i = 0; i < length; i++) {
List<Double> list = lists.get(i); public Map<Integer, List<Double>> getKMatrix() throws Exception {
int index = list.size() / 2; Map<Integer, List<Double>> matrixList = new HashMap<>();
matrix.setNub(0, i, list.get(index)); for (Map.Entry<Integer, Matrix> entry : matrixK.entrySet()) {
List<Double> list = MatrixOperation.rowVectorToList(entry.getValue());
matrixList.put(entry.getKey(), list);
} }
return matrix; return matrixList;
} }
public void study() throws Exception { public void insertKMatrix(Map<Integer, List<Double>> matrixList) throws Exception {
for (Map.Entry<Integer, List<Matrix>> entry : matrixMap.entrySet()) { for (Map.Entry<Integer, List<Double>> entry : matrixList.entrySet()) {
List<Matrix> matrixList = entry.getValue(); Matrix matrix = MatrixOperation.listToRowVector(entry.getValue());
Matrix matrix = sigma(matrixList);
matrixK.put(entry.getKey(), matrix); matrixK.put(entry.getKey(), matrix);
} }
} }

@ -86,6 +86,8 @@ public class LVQ {
long type = matrixBody.getId();//类别 long type = matrixBody.getId();//类别
double distEnd = 0; double distEnd = 0;
int id = 0; int id = 0;
double dis0 = 0;
double dis1 = 1;
for (int i = 0; i < typeNub; i++) { for (int i = 0; i < typeNub; i++) {
MatrixBody modelBody = model[i]; MatrixBody modelBody = model[i];
Matrix modelMatrix = modelBody.getMatrix(); Matrix modelMatrix = modelBody.getMatrix();
@ -95,10 +97,16 @@ public class LVQ {
id = modelBody.getId(); id = modelBody.getId();
distEnd = dist; distEnd = dist;
} }
if (i == 0) {
dis0 = dist;
} else {
dis1 = dist;
}
} }
MatrixBody modelBody = model[id]; MatrixBody modelBody = model[id];
Matrix modelMatrix = modelBody.getMatrix(); Matrix modelMatrix = modelBody.getMatrix();
boolean isRight = id == type; boolean isRight = id == type;
System.out.println("type==" + type + ",dis0==" + dis0 + ",dis1==" + dis1);
Matrix matrix1 = op(matrix, modelMatrix, isRight); Matrix matrix1 = op(matrix, modelMatrix, isRight);
modelBody.setMatrix(matrix1); modelBody.setMatrix(matrix1);
} }

@ -10,36 +10,26 @@ import org.wlld.tools.ArithUtil;
* @date 1:29 2020/3/15 * @date 1:29 2020/3/15
*/ */
public class Normalization { public class Normalization {
private double max;
private double min;
private long number; private long number;
private double avg; private double avg;
private double sigma; private double sigma;
public double getMax() {
return max;
}
public double getMin() {
return min;
}
public double getAvg() { public double getAvg() {
return avg; return avg;
} }
public void setAvg(double avg) {
this.avg = avg;
}
public void avg() { public void avg() {
if (avg == 0) {
avg = ArithUtil.div(sigma, number); avg = ArithUtil.div(sigma, number);
//System.out.println("avg==" + avg); }
System.out.println(avg);
} }
public void putFeature(double nub) { public void putFeature(double nub) {
if (nub > max) {
max = nub;
}
if (min == 0 || (nub != 0 && nub < min)) {
min = nub;
}
if (nub != 0) { if (nub != 0) {
sigma = ArithUtil.add(sigma, nub); sigma = ArithUtil.add(sigma, nub);
number++; number++;

@ -1,5 +1,6 @@
package org.wlld.nerveEntity; package org.wlld.nerveEntity;
import org.wlld.MatrixTools.Matrix;
import org.wlld.imageRecognition.border.Frame; import org.wlld.imageRecognition.border.Frame;
import org.wlld.imageRecognition.modelEntity.KBorder; import org.wlld.imageRecognition.modelEntity.KBorder;
import org.wlld.imageRecognition.modelEntity.LvqModel; import org.wlld.imageRecognition.modelEntity.LvqModel;
@ -22,7 +23,34 @@ public class ModelParameter {
private DymNerveStudy dymOutNerveStudy = new DymNerveStudy();//动态神经元输出层 private DymNerveStudy dymOutNerveStudy = new DymNerveStudy();//动态神经元输出层
private Map<Integer, KBorder> borderMap = new HashMap<>();//边框距离模型 private Map<Integer, KBorder> borderMap = new HashMap<>();//边框距离模型
private LvqModel lvqModel;//LVQ模型 private LvqModel lvqModel;//LVQ模型
private Map<Integer, List<Double>> matrixK = new HashMap<>();//均值特征向量
private Frame frame;//先验边框 private Frame frame;//先验边框
private double avg;//特别均值
private double dnnAvg;//
public double getDnnAvg() {
return dnnAvg;
}
public void setDnnAvg(double dnnAvg) {
this.dnnAvg = dnnAvg;
}
public double getAvg() {
return avg;
}
public Map<Integer, List<Double>> getMatrixK() {
return matrixK;
}
public void setMatrixK(Map<Integer, List<Double>> matrixK) {
this.matrixK = matrixK;
}
public void setAvg(double avg) {
this.avg = avg;
}
public Frame getFrame() { public Frame getFrame() {
return frame; return frame;

@ -66,8 +66,8 @@ public class OutNerve extends Nerve {
Matrix myMatrix = dynamicNerve(matrix, eventId, isKernelStudy); Matrix myMatrix = dynamicNerve(matrix, eventId, isKernelStudy);
if (isKernelStudy) {//回传 if (isKernelStudy) {//回传
Matrix matrix1 = matrixMapE.get(E); Matrix matrix1 = matrixMapE.get(E);
//System.out.println("E================" + E); System.out.println("E================" + E);
//System.out.println(myMatrix.getString()); System.out.println(myMatrix.getString());
if (matrix1.getX() <= myMatrix.getX() && matrix1.getY() <= myMatrix.getY()) { if (matrix1.getX() <= myMatrix.getX() && matrix1.getY() <= myMatrix.getY()) {
double g = getGradient(myMatrix, matrix1); double g = getGradient(myMatrix, matrix1);
backMatrix(g, eventId); backMatrix(g, eventId);

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save