parent
2254885c57
commit
121cfda3f6
@ -1,24 +0,0 @@
|
||||
package org.wlld.imageRecognition;
|
||||
|
||||
import org.wlld.imageRecognition.border.DistBody;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description
|
||||
*/
|
||||
public class DistSort implements Comparator<DistBody> {
|
||||
@Override
|
||||
public int compare(DistBody o1, DistBody o2) {
|
||||
if (o1.getDist() < o2.getDist()) {
|
||||
return -1;
|
||||
} else if (o1.getDist() > o2.getDist()) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package org.wlld.imageRecognition;
|
||||
|
||||
import org.wlld.imageRecognition.modelEntity.RegressionBody;
|
||||
|
||||
public class XYBody {
|
||||
private double[] X;
|
||||
private double[] Y;
|
||||
private RegressionBody regressionBody;
|
||||
|
||||
public RegressionBody getRegressionBody() {
|
||||
return regressionBody;
|
||||
}
|
||||
|
||||
public void setRegressionBody(RegressionBody regressionBody) {
|
||||
this.regressionBody = regressionBody;
|
||||
}
|
||||
|
||||
public double[] getX() {
|
||||
return X;
|
||||
}
|
||||
|
||||
public void setX(double[] x) {
|
||||
X = x;
|
||||
}
|
||||
|
||||
public double[] getY() {
|
||||
return Y;
|
||||
}
|
||||
|
||||
public void setY(double[] y) {
|
||||
Y = y;
|
||||
}
|
||||
}
|
@ -1,112 +0,0 @@
|
||||
package org.wlld.imageRecognition.segmentation;
|
||||
|
||||
import org.wlld.config.RZ;
|
||||
import org.wlld.function.Sigmod;
|
||||
import org.wlld.function.Tanh;
|
||||
import org.wlld.imageRecognition.modelEntity.RgbBack;
|
||||
import org.wlld.nerveCenter.NerveManager;
|
||||
import org.wlld.nerveEntity.SensoryNerve;
|
||||
|
||||
import java.awt.image.Kernel;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description
|
||||
*/
|
||||
public class KNerveManger {
|
||||
private Map<Integer, List<double[]>> featureMap = new HashMap<>();
|
||||
private int sensoryNerveNub;//输出神经元个数
|
||||
private int speciesNub;//种类数
|
||||
private NerveManager nerveManager;
|
||||
private int times;
|
||||
private RgbBack rgbBack = new RgbBack();
|
||||
|
||||
public KNerveManger(int sensoryNerveNub, int speciesNub, int times) throws Exception {
|
||||
this.sensoryNerveNub = sensoryNerveNub;
|
||||
this.speciesNub = speciesNub;
|
||||
this.times = times;
|
||||
nerveManager = new NerveManager(sensoryNerveNub, 24, speciesNub,
|
||||
1, new Tanh(),//0.008 l1 0.02
|
||||
false, false, 0.008, RZ.L1, 0.02);
|
||||
nerveManager.init(true, false, true, true);
|
||||
}
|
||||
|
||||
private Map<Integer, Double> createTag(int tag) {//创建一个标注
|
||||
Map<Integer, Double> tagging = new HashMap<>();
|
||||
tagging.put(tag, 1.0);
|
||||
return tagging;
|
||||
}
|
||||
|
||||
public void look(List<double[]> data) throws Exception {
|
||||
int size = data.size();
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
rgbBack.clear();
|
||||
post(data.get(i), null, false);
|
||||
int type = rgbBack.getId();
|
||||
if (map.containsKey(type)) {
|
||||
map.put(type, map.get(type) + 1);
|
||||
} else {
|
||||
map.put(type, 1);
|
||||
}
|
||||
}
|
||||
double max = 0;
|
||||
int type = 0;
|
||||
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
|
||||
int nub = entry.getValue();
|
||||
if (nub > max) {
|
||||
max = nub;
|
||||
type = entry.getKey();
|
||||
}
|
||||
}
|
||||
double point = max / size;
|
||||
System.out.println("类型是:" + type + ",总票数:" + size + ",得票率:" + point);
|
||||
System.out.println("=================================完成");
|
||||
}
|
||||
|
||||
public void startStudy() throws Exception {
|
||||
for (int j = 0; j < 2; j++) {
|
||||
for (int i = 0; i < times; i++) {
|
||||
for (Map.Entry<Integer, List<double[]>> entry : featureMap.entrySet()) {
|
||||
int type = entry.getKey();
|
||||
System.out.println("=============================" + type);
|
||||
Map<Integer, Double> tag = createTag(type);//标注
|
||||
double[] feature = entry.getValue().get(i);//数据
|
||||
post(feature, tag, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<Integer, List<double[]>> entry : featureMap.entrySet()) {
|
||||
int type = entry.getKey();
|
||||
System.out.println("=============================" + type);
|
||||
List<double[]> list = entry.getValue();
|
||||
look(list);
|
||||
}
|
||||
}
|
||||
|
||||
private void post(double[] data, Map<Integer, Double> tagging, boolean isStudy) throws Exception {
|
||||
List<SensoryNerve> sensoryNerveList = nerveManager.getSensoryNerves();
|
||||
int size = sensoryNerveList.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
sensoryNerveList.get(i).postMessage(1, data[i], isStudy, tagging, rgbBack);
|
||||
}
|
||||
}
|
||||
|
||||
public void setFeature(int type, List<double[]> feature) {
|
||||
if (type > 0) {
|
||||
if (featureMap.containsKey(type)) {
|
||||
featureMap.get(type).addAll(feature);
|
||||
} else {
|
||||
featureMap.put(type, feature);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,206 +0,0 @@
|
||||
package org.wlld.imageRecognition.segmentation;
|
||||
|
||||
import org.wlld.MatrixTools.Matrix;
|
||||
import org.wlld.MatrixTools.MatrixOperation;
|
||||
import org.wlld.imageRecognition.DistSort;
|
||||
import org.wlld.imageRecognition.TempleConfig;
|
||||
import org.wlld.imageRecognition.border.DistBody;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @param
|
||||
* @DATA
|
||||
* @Author LiDaPeng
|
||||
* @Description 修正映射
|
||||
*/
|
||||
public class RegionMapping {
|
||||
private Map<Integer, List<Matrix>> featureMap;
|
||||
private double studyPoint = 0.01;
|
||||
private Map<Integer, Matrix> powerMatrixMap = new HashMap<>();//映射层的权重矩阵
|
||||
private int times = 200;
|
||||
|
||||
public RegionMapping(TempleConfig templeConfig) {
|
||||
this.featureMap = templeConfig.getKnn().getFeatureMap();
|
||||
}
|
||||
|
||||
public void detection() throws Exception {//对模型进行检查,先对当前模型最相近的三个模型进行检出
|
||||
Map<Integer, List<DistBody>> map = new HashMap<>();
|
||||
DistSort distSort = new DistSort();
|
||||
for (Map.Entry<Integer, List<Matrix>> entry : featureMap.entrySet()) {
|
||||
int key = entry.getKey();
|
||||
List<Matrix> myFeature = entry.getValue();
|
||||
//记录 哪个类别 每个类别距离这个类别的最小距离
|
||||
List<DistBody> distBodies = new ArrayList<>();
|
||||
map.put(key, distBodies);
|
||||
for (Map.Entry<Integer, List<Matrix>> entrySon : featureMap.entrySet()) {
|
||||
int type = entrySon.getKey();
|
||||
if (key != type) {//当前其余类别
|
||||
DistBody distBody = new DistBody();
|
||||
List<Matrix> features = entrySon.getValue();//其余类别的所有特征
|
||||
double minError = -1;
|
||||
for (Matrix myMatrix : myFeature) {//待验证类别所有特征
|
||||
for (Matrix otherFeature : features) {
|
||||
double dist = MatrixOperation.getEDistByMatrix(myMatrix, otherFeature);
|
||||
if (minError < 0 || dist < minError) {
|
||||
minError = dist;
|
||||
}
|
||||
}
|
||||
}
|
||||
//当前类别遍历结束
|
||||
distBody.setId(type);
|
||||
distBody.setDist(minError);
|
||||
distBodies.add(distBody);
|
||||
}
|
||||
}
|
||||
}
|
||||
//进行排序
|
||||
for (Map.Entry<Integer, List<DistBody>> entry : map.entrySet()) {
|
||||
List<DistBody> list = entry.getValue();
|
||||
Collections.sort(list, distSort);
|
||||
}
|
||||
adjustWeight(map, 3);
|
||||
test(12, map, 3);
|
||||
// int testType = 5;
|
||||
// List<DistBody> testBody = map.get(testType);
|
||||
// Matrix matrix = powerMatrixMap.get(testType);
|
||||
// System.out.println(matrix.getString());
|
||||
}
|
||||
|
||||
private Matrix mapping(Matrix feature, Matrix mapping) throws Exception {
|
||||
int y = feature.getY();
|
||||
if (y == mapping.getY()) {
|
||||
Matrix matrix = new Matrix(1, y);
|
||||
for (int i = 0; i < y; i++) {
|
||||
double nub = feature.getNumber(0, i) * mapping.getNumber(0, i);
|
||||
matrix.setNub(0, i, nub);
|
||||
}
|
||||
return matrix;
|
||||
} else {
|
||||
throw new Exception("matrix is not equals");
|
||||
}
|
||||
}
|
||||
|
||||
private void test(int key, Map<Integer, List<DistBody>> map, int nub) throws Exception {
|
||||
Matrix matrixMapping = powerMatrixMap.get(key);//映射
|
||||
List<Matrix> rightFeature = featureMap.get(key);//正确的特征
|
||||
System.out.println("正确特征====================");
|
||||
for (Matrix matrix : rightFeature) {
|
||||
System.out.println(matrix.getString());
|
||||
}
|
||||
int y = rightFeature.get(0).getY();
|
||||
Matrix featureMatrix = getFeatureAvg(rightFeature, y);//均值
|
||||
List<DistBody> distBodies = map.get(key);
|
||||
List<Matrix> wrongFeature = new ArrayList<>();
|
||||
for (int i = 0; i < nub; i++) {
|
||||
DistBody distBody = distBodies.get(i);
|
||||
int id = distBody.getId();//相近的特征id
|
||||
wrongFeature.addAll(featureMap.get(id));
|
||||
}
|
||||
System.out.println("错误特征=============");
|
||||
for (Matrix matrix : wrongFeature) {
|
||||
System.out.println(matrix.getString());
|
||||
}
|
||||
//做验证
|
||||
double minOtherDist = -1;//其余特征最小距离
|
||||
for (Matrix wrongMatrix : wrongFeature) {
|
||||
Matrix wrongMapping = mapping(wrongMatrix, matrixMapping);
|
||||
double dist = MatrixOperation.getEDistByMatrix(featureMatrix, wrongMapping);
|
||||
System.out.println("异类距离:" + dist);
|
||||
if (minOtherDist < 0 || dist < minOtherDist) {
|
||||
minOtherDist = dist;
|
||||
}
|
||||
}
|
||||
System.out.println("异类最小距离:" + minOtherDist);
|
||||
for (Matrix rightMatrix : rightFeature) {
|
||||
Matrix rightMapping = mapping(rightMatrix, matrixMapping);
|
||||
double dist = MatrixOperation.getEDistByMatrix(featureMatrix, rightMapping);
|
||||
System.out.println("同类距离:" + dist);
|
||||
}
|
||||
}
|
||||
|
||||
private void adjustWeight(Map<Integer, List<DistBody>> map, int nub) throws Exception {//进行权重调整
|
||||
for (Map.Entry<Integer, List<DistBody>> entry : map.entrySet()) {
|
||||
int key = entry.getKey();//当前类别的id
|
||||
List<Matrix> rightFeature = featureMap.get(key);//正确的特征
|
||||
List<DistBody> distBodies = entry.getValue();
|
||||
List<Matrix> wrongFeature = new ArrayList<>();
|
||||
for (int i = 0; i < nub; i++) {
|
||||
DistBody distBody = distBodies.get(i);
|
||||
int id = distBody.getId();//相近的特征id
|
||||
wrongFeature.addAll(featureMap.get(id));
|
||||
}
|
||||
//对每个分类的特征图进行权重调整
|
||||
updatePower(wrongFeature, rightFeature, key);
|
||||
}
|
||||
}
|
||||
|
||||
//调整权重矩阵
|
||||
private void updatePower(List<Matrix> wrongFeature, List<Matrix> rightFeature, int key) throws Exception {
|
||||
int size = wrongFeature.size();
|
||||
int y = rightFeature.get(0).getY();
|
||||
//特征均值向量
|
||||
Matrix featureMatrix = getFeatureAvg(rightFeature, y);
|
||||
Matrix powerMatrix = new Matrix(1, y);
|
||||
powerMatrixMap.put(key, powerMatrix);
|
||||
Random random = new Random();
|
||||
for (int j = 0; j < times; j++) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
Matrix feature = rightFeature.get(random.nextInt(rightFeature.size()));
|
||||
Matrix noFeature = wrongFeature.get(i);
|
||||
powerDeflection(feature, featureMatrix, true, powerMatrix);
|
||||
powerDeflection(noFeature, featureMatrix, false, powerMatrix);
|
||||
}
|
||||
}
|
||||
end();
|
||||
}
|
||||
|
||||
private void end() throws Exception {
|
||||
for (Map.Entry<Integer, Matrix> entry : powerMatrixMap.entrySet()) {
|
||||
Matrix powerMatrix = entry.getValue();
|
||||
int y = powerMatrix.getY();
|
||||
double min = 0;
|
||||
for (int i = 0; i < y; i++) {
|
||||
double nub = powerMatrix.getNumber(0, i);
|
||||
if (nub < min) {
|
||||
min = nub;
|
||||
}
|
||||
}
|
||||
//获取最小值完毕
|
||||
for (int i = 0; i < y; i++) {
|
||||
double nub = powerMatrix.getNumber(0, i);
|
||||
powerMatrix.setNub(0, i, nub - min);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void powerDeflection(Matrix matrix1, Matrix matrix2, boolean polymerization,
|
||||
Matrix powerMatrix) throws Exception {
|
||||
int y = matrix1.getY();
|
||||
for (int i = 0; i < y; i++) {
|
||||
double sub = Math.abs(matrix1.getNumber(0, i) - matrix2.getNumber(0, i))
|
||||
* studyPoint;
|
||||
double power = powerMatrix.getNumber(0, i);//当前矩阵中的权值
|
||||
if (polymerization) {//同类聚合 聚合是减
|
||||
power = power - sub;
|
||||
} else {//异类离散
|
||||
power = power + sub;
|
||||
}
|
||||
powerMatrix.setNub(0, i, power);
|
||||
}
|
||||
}
|
||||
|
||||
private Matrix getFeatureAvg(List<Matrix> rightFeature, int size) throws Exception {//求特征均值
|
||||
Matrix feature = new Matrix(1, size);
|
||||
int nub = rightFeature.size();
|
||||
for (int i = 0; i < nub; i++) {
|
||||
Matrix matrix = rightFeature.get(i);
|
||||
for (int j = 0; j < size; j++) {
|
||||
double sigma = matrix.getNumber(0, j) + feature.getNumber(0, j);
|
||||
feature.setNub(0, j, sigma);
|
||||
}
|
||||
}
|
||||
MatrixOperation.mathDiv(feature, nub);
|
||||
return feature;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue