|
|
@ -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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|