!59 增加混高模型

Merge pull request !59 from 逐光/test
pull/59/MERGE
逐光 4 years ago committed by Gitee
commit 50553eb374

@ -6,7 +6,7 @@
<groupId>com.wlld</groupId>
<artifactId>easyAi</artifactId>
<version>1.0.9</version>
<version>1.0.0</version>
<name>easyAi</name>
<!-- FIXME change it to the project's website -->

@ -56,6 +56,7 @@ public class Matrix {
private void setState(int x, int y) {
if (x == 1 && y == 1) {
isZero = true;
isVector = true;
} else if (x == 1 || y == 1) {
isVector = true;
if (x == 1) {
@ -425,6 +426,7 @@ public class Matrix {
if (this.x >= x && this.y >= y) {
return matrix[x][y];
} else {
//System.out.println("x==" + x + ",y==" + y);
throw new Exception("matrix length too little");
}
}

@ -46,22 +46,43 @@ public class MatrixOperation {
//多元线性回归
public static Matrix getLinearRegression(Matrix parameter, Matrix out) throws Exception {
if (parameter.getX() == out.getX() && out.isVector() && !out.isRowVector()) {
if (parameter.getX() == out.getX() && out.isVector()) {
//将参数矩阵转置
Matrix matrix1 = transPosition(parameter);
//转置的参数矩阵乘以参数矩阵
Matrix matrix2 = mulMatrix(matrix1, parameter);
//求上一步的逆矩阵
//求上一步的逆矩阵 这一步需要矩阵非奇异,若出现奇异矩阵则返回0矩阵意味失败
Matrix matrix3 = getInverseMatrixs(matrix2);
//逆矩阵乘以转置矩阵
Matrix matrix4 = mulMatrix(matrix3, matrix1);
//最后乘以输出矩阵,生成权重矩阵并返回
return mulMatrix(matrix4, out);
if (matrix3.getX() == 1 && matrix3.getY() == 1) {
return matrix3;
} else {
//逆矩阵乘以转置矩阵
Matrix matrix4 = mulMatrix(matrix3, matrix1);
//最后乘以输出矩阵,生成权重矩阵并返回
return mulMatrix(matrix4, out);
}
} else {
throw new Exception("invalid regression matrix");
}
}
public static double getEDistByMatrix(Matrix matrix1, Matrix matrix2) throws Exception {
if (matrix1.getX() == matrix2.getX() && matrix1.getY() == matrix2.getY()) {
int x = matrix1.getX();
int y = matrix1.getY();
double sigma = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
double sub = matrix1.getNumber(i, j) - matrix2.getNumber(i, j);
sigma = sigma + Math.pow(sub, 2);
}
}
return sigma / (x * y);
} else {
throw new Exception("two matrixes is not equals");
}
}
//返回两个向量之间的欧氏距离的平方
public static double getEDist(Matrix matrix1, Matrix matrix2) throws Exception {
if (matrix1.isRowVector() && matrix2.isRowVector() && matrix1.getY() == matrix2.getY()) {
@ -72,7 +93,7 @@ public class MatrixOperation {
}
}
public static double errorNub(Matrix matrix, Matrix avgMatrix, int excNub) throws Exception {//求均方误差
public static double errorNub(Matrix matrix, Matrix avgMatrix) throws Exception {//求均方误差
int y = matrix.getY();
if (matrix.isRowVector() && avgMatrix.isRowVector() && y == avgMatrix.getY()) {
double[] subAll = new double[y];
@ -82,12 +103,11 @@ public class MatrixOperation {
double sub = Math.pow(avg - mySelf, 2);
subAll[j] = sub;
}
Arrays.sort(subAll);
double sigma = 0;
for (int i = 0; i < y - excNub; i++) {
for (int i = 0; i < y; i++) {
sigma = sigma + subAll[i];
}
return sigma / (y - excNub);
return sigma / y;
} else {
throw new Exception("this matrix is not rowVector or length different");
}
@ -322,16 +342,17 @@ public class MatrixOperation {
return inverserNumber;
}
public static Matrix getInverseMatrixs(Matrix matrixs) throws Exception {//矩阵求逆
double def = matrixs.getDet();
public static Matrix getInverseMatrixs(Matrix matrix) throws Exception {//矩阵求逆
double def = matrix.getDet();
if (def != 0) {
def = ArithUtil.div(1, def);
Matrix myMatrix = adjointMatrix(matrixs);//伴随矩阵
Matrix myMatrix = adjointMatrix(matrix);//伴随矩阵
mathMul(myMatrix, def);
return myMatrix;
} else {
System.out.println(matrixs.getString());
throw new Exception("this matrixs do not have InverseMatrixs");
//System.out.println("matrix def is zero error:");
//System.out.println(matrix.getString());
return new Matrix(1, 1);
}
}

@ -8,6 +8,8 @@ public class Kernel {
private static final String Horizontal_Number = "[-1,-2,-1]#[0,0,0]#[1,2,1]#";//横卷积核
private static final String All_Number = "[1,-2,1]#[-2,4,-2]#[1,-2,1]#";//角卷积
private static final String All_Number2 = "[-1,0,-1]#[0,4,0]#[-1,0,-1]#";
private static final String All_Big = "[-1,0,0,0,-1]#[0,-1,0,-1,0]#[0,0,8,0,0]#" +
"[0,-1,0,-1,0]#[-1,0,0,0,-1]";
public static final int Region_Nub = 60;//一张图有多少份
public static final double th = 0.88;//分水岭灰度阈值
public static final double rgbN = 442.0;//442.0;//RGB范数归一化最大值
@ -15,6 +17,7 @@ public class Kernel {
public static Matrix Horizontal;
public static Matrix All;
public static Matrix ALL_Two;
public static Matrix Big;
public static final int Unit = 100;
public static final double Pi = ArithUtil.div(ArithUtil.div(Math.PI, 2), Unit);
@ -23,6 +26,7 @@ public class Kernel {
static {
try {
Big = new Matrix(5, 5, All_Big);
ALL_Two = new Matrix(3, 3, All_Number2);
All = new Matrix(3, 3, All_Number);
Vertical = new Matrix(3, 3, Vertical_Number);

@ -8,13 +8,13 @@ public class Tanh implements ActiveFunction {
public double function(double x) {
double x1 = Math.exp(x);
double x2 = Math.exp(-x);
double son = ArithUtil.sub(x1, x2);
double mother = ArithUtil.add(x1, x2);
return ArithUtil.div(son, mother);
double son = x1 - x2;// ArithUtil.sub(x1, x2);
double mother = x1 + x2;// ArithUtil.add(x1, x2);
return son / mother;//ArithUtil.div(son, mother);
}
@Override
public double functionG(double out) {
return ArithUtil.sub(1, Math.pow(function(out), 2));
return 1 - Math.pow(function(out), 2);//ArithUtil.sub(1, Math.pow(function(out), 2));
}
}

@ -0,0 +1,7 @@
package org.wlld.i;
public interface PsoFunction {//粒子群回调函数
//根据参数返回函数值
double getResult(double[] parameter,int id) throws Exception;
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,163 @@
package org.wlld.imageRecognition;
import org.wlld.MatrixTools.Matrix;
import org.wlld.imageRecognition.border.GMClustering;
import org.wlld.imageRecognition.modelEntity.GMBody;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class CutFood {
private TempleConfig templeConfig;
private Map<Integer, GMClustering> meanMap;//干食混高模型
private Matrix regionMap;
private double foodFilterTh;
public CutFood(TempleConfig templeConfig, Map<Integer, GMClustering> meanMap) {
this.templeConfig = templeConfig;
foodFilterTh = templeConfig.getFood().getFoodFilterTh();
this.meanMap = meanMap;
}
private void mean(ThreeChannelMatrix threeChannelMatrix, GMClustering mean) throws Exception {
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
int x = matrixR.getX();
int y = matrixR.getY();
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
double[] rgb = new double[]{matrixR.getNumber(i, j) / 255, matrixG.getNumber(i, j) / 255
, matrixB.getNumber(i, j) / 255};
mean.setColor(rgb);
}
}
mean.start();
}
private double getAvg(Matrix matrix) throws Exception {
double sigma = 0;
int x = matrix.getX();
int y = matrix.getY();
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
sigma = sigma + matrix.getNumber(i, j);
}
}
return sigma / (x * y);
}
private void getAvgPro(ThreeChannelMatrix threeChannelMatrix) throws Exception {
int size = templeConfig.getFood().getRegionSize();
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
int x = matrixR.getX();
int y = matrixR.getY();
regionMap = new Matrix(x / size, y / size);
for (int i = 0; i <= x - size; i += size) {
for (int j = 0; j <= y - size; j += size) {
double r = getAvg(matrixR.getSonOfMatrix(i, j, size, size));
double g = getAvg(matrixG.getSonOfMatrix(i, j, size, size));
double b = getAvg(matrixB.getSonOfMatrix(i, j, size, size));
double[] rgb = new double[]{r / 255, g / 255, b / 255};
int index = getType(rgb);
regionMap.setNub(i / size, j / size, index);
}
}
}
public Map<Integer, Integer> getTypeNub(ThreeChannelMatrix threeChannelMatrix
, int myType) throws Exception {
getAvgPro(threeChannelMatrix);
return getTypeNub(myType);
}
private Map<Integer, Integer> getTypeNub(int myType) throws Exception {
int size = templeConfig.getFood().getRegionSize();
double s = Math.pow(size, 2);
int xr = regionMap.getX();
int yr = regionMap.getY();
List<GMBody> gmBodies = new ArrayList<>();
for (int i = 0; i < xr; i++) {
for (int j = 0; j < yr; j++) {
int type = (int) regionMap.getNumber(i, j);
if (!insertBodies(gmBodies, i, j, type)) {//需要创建一个新的
gmBodies.add(new GMBody(type, i, j));
}
}
}
Map<Integer, Integer> gmTypeNub = new HashMap<>();
for (int i = 0; i < gmBodies.size(); i++) {//过滤侯选区
GMBody gmBody = gmBodies.get(i);
double regionSize = gmBody.getPixelNub() * s;
int type = gmBody.getType();
if (type != 1) {//背景直接过滤
double oneSize = meanMap.get(type).getRegionSize();
int nub = (int) (regionSize / oneSize);//数量
if (nub > 0) {
if (gmTypeNub.containsKey(type)) {
int myNub = gmTypeNub.get(type);
if (nub > myNub) {
gmTypeNub.put(type, nub);
}
} else {
gmTypeNub.put(type, nub);
}
}
}
}
if (gmTypeNub.size() == 0) {
gmTypeNub.put(myType, 1);
}
return gmTypeNub;
}
private boolean insertBodies(List<GMBody> gmBodies, int x, int y, int type) {
boolean isInsert = false;
for (GMBody gmBody : gmBodies) {
if (gmBody.insertRgb(x, y, type)) {
isInsert = true;
break;
}
}
return isInsert;
}
private int getType(double[] rgb) throws Exception {
int index = 0;
double max = 0;
for (Map.Entry<Integer, GMClustering> entry : meanMap.entrySet()) {
GMClustering gmClustering = entry.getValue();
double probability = gmClustering.getProbabilityDensity(rgb);
if (probability > max) {
max = probability;
index = entry.getKey();
}
}
if (max < 2) {
index = 1;
}
return index;
}
public void study(int type, ThreeChannelMatrix threeChannelMatrix) throws Exception {
Matrix matrixR = threeChannelMatrix.getMatrixR();
int x = matrixR.getX();
int y = matrixR.getY();
GMClustering mean = new GMClustering(templeConfig.getFeatureNub());
mean.setRegionSize(x * y * foodFilterTh);
meanMap.put(type, mean);
mean(threeChannelMatrix, mean);
//记录非背景的单物体面积
}
}

@ -0,0 +1,23 @@
package org.wlld.imageRecognition;
import org.wlld.imageRecognition.modelEntity.MappingBody;
import java.util.Comparator;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class MappingSort implements Comparator<MappingBody> {
@Override
public int compare(MappingBody o1, MappingBody o2) {
if (o1.getMappingNub() > o2.getMappingNub()) {
return -1;
} else if (o1.getMappingNub() < o2.getMappingNub()) {
return 1;
}
return 0;
}
}

@ -1,24 +1,21 @@
package org.wlld.imageRecognition;
import org.wlld.imageRecognition.segmentation.RgbRegression;
import java.util.*;
//K均值聚类
public class MeanClustering {
private List<double[]> matrixList = new ArrayList<>();//聚类集合
protected List<double[]> matrixList = new ArrayList<>();//聚类集合
private int length;//向量长度(模型需要返回)
private int speciesQuantity;//种类数量(模型需要返回)
private List<RGBNorm> matrices = new ArrayList<>();//均值K模型(模型需要返回)
private int size = 10000;
protected int speciesQuantity;//种类数量(模型需要返回)
protected List<RGBNorm> matrices = new ArrayList<>();//均值K模型(模型需要返回)
public List<RGBNorm> getMatrices() {
return matrices;
}
public MeanClustering(int speciesQuantity, TempleConfig templeConfig) {
public MeanClustering(int speciesQuantity) throws Exception {
this.speciesQuantity = speciesQuantity;//聚类的数量
size = templeConfig.getFood().getRegressionNub();
}
public void setColor(double[] color) throws Exception {
@ -73,21 +70,19 @@ public class MeanClustering {
}
}
private void startRegression() throws Exception {//开始聚类回归
Random random = new Random();
for (RGBNorm rgbNorm : matrices) {
RgbRegression rgbRegression = new RgbRegression(size);
List<double[]> list = rgbNorm.getRgbs();
for (int i = 0; i < size; i++) {
double[] rgb = list.get(random.nextInt(list.size()));
rgb[0] = rgb[0] / 255;
rgb[1] = rgb[1] / 255;
rgb[2] = rgb[2] / 255;
rgbRegression.insertRGB(rgb);
private double[] getListAvg(List<double[]> list) {
int len = list.get(0).length;
double[] sigma = new double[len];
for (double[] rgb : list) {
for (int i = 0; i < len; i++) {
sigma[i] = sigma[i] + rgb[i];
}
rgbRegression.regression();
rgbNorm.setRgbRegression(rgbRegression);
}
int size = list.size();
for (int i = 0; i < len; i++) {
sigma[i] = sigma[i] / size;
}
return sigma;
}
public void start() throws Exception {//开始聚类
@ -102,16 +97,17 @@ public class MeanClustering {
}
//进行两者的比较
boolean isNext;
for (int i = 0; i < 30; i++) {
for (int i = 0; i < 50; i++) {
averageMatrix();
isNext = isNext();
if (isNext && i < 29) {
if (isNext && i < 49) {
clear();
} else {
break;
}
}
// startRegression();//开始进行回归
RGBSort rgbSort = new RGBSort();
Collections.sort(matrices, rgbSort);
} else {
throw new Exception("matrixList number less than 2");
}

@ -7,13 +7,11 @@ import org.wlld.config.Classifier;
import org.wlld.config.StudyPattern;
import org.wlld.i.OutBack;
import org.wlld.imageRecognition.border.*;
import org.wlld.imageRecognition.segmentation.RegionBody;
import org.wlld.imageRecognition.segmentation.RgbRegression;
import org.wlld.imageRecognition.segmentation.Specifications;
import org.wlld.imageRecognition.segmentation.Watershed;
import org.wlld.imageRecognition.segmentation.*;
import org.wlld.nerveCenter.NerveManager;
import org.wlld.nerveCenter.Normalization;
import org.wlld.nerveEntity.SensoryNerve;
import org.wlld.param.Food;
import org.wlld.tools.ArithUtil;
import org.wlld.tools.IdCreator;
@ -25,17 +23,18 @@ public class Operation {//进行计算
private MatrixBack matrixBack = new MatrixBack();
private ImageBack imageBack = new ImageBack();
private OutBack outBack;
private int dif;
public TempleConfig getTempleConfig() {
return templeConfig;
}
public Operation(TempleConfig templeConfig) {
this.templeConfig = templeConfig;
dif = templeConfig.getFood().getShrink();
}
public Operation(TempleConfig templeConfig, OutBack outBack) {
this.templeConfig = templeConfig;
this.outBack = outBack;
dif = templeConfig.getFood().getShrink();
}
public List<Double> convolution(Matrix matrix, Map<Integer, Double> tagging) throws Exception {
@ -63,7 +62,6 @@ public class Operation {//进行计算
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
//Matrix matrixRGB = threeChannelMatrix.getMatrixRGB();
Random random = new Random();
int x = matrixR.getX();
int y = matrixR.getY();
@ -77,70 +75,48 @@ public class Operation {//进行计算
rgbRegression.insertRGB(rgb);
}
rgbRegression.regression();
// double[] rgb = new double[]{164 / 255, 189 / 255, 193 / 255};
// double dis = rgbRegression.getDisError(rgb);
// System.out.println("dis==" + dis);
templeConfig.getFood().getTrayBody().add(rgbRegression);
}
private void cutPic(ThreeChannelMatrix threeChannelMatrix, int x, int y, int xSize, int ySize) {
Matrix matrixR = threeChannelMatrix.getMatrixR();
Matrix matrixG = threeChannelMatrix.getMatrixG();
Matrix matrixB = threeChannelMatrix.getMatrixB();
threeChannelMatrix.setMatrixR(matrixR.getSonOfMatrix(x, y, xSize, ySize));
threeChannelMatrix.setMatrixG(matrixG.getSonOfMatrix(x, y, xSize, ySize));
threeChannelMatrix.setMatrixB(matrixB.getSonOfMatrix(x, y, xSize, ySize));
public void finishColorStudy() throws Exception {//进行最后学习
DimensionMappingStudy dimensionAll = new DimensionMappingStudy(templeConfig);
dimensionAll.start();//生成映射层并将已经保存的knn特征进行映射
templeConfig.getFood().setDimensionMappingStudy(dimensionAll);
}
public RegionBody colorStudy(ThreeChannelMatrix threeChannelMatrix, int tag, List<Specifications> specificationsList) throws Exception {
public RegionBody colorStudy(ThreeChannelMatrix threeChannelMatrix, int tag, List<Specifications> specificationsList
, String url, boolean isFood) throws Exception {
Watershed watershed = new Watershed(threeChannelMatrix, specificationsList, templeConfig);
List<RegionBody> regionBodies = watershed.rainfall();
if (regionBodies.size() == 1) {
RegionBody regionBody = regionBodies.get(0);
int minX = regionBody.getMinX() + dif;
int minY = regionBody.getMinY() + dif;
int maxX = regionBody.getMaxX() - dif;
int maxY = regionBody.getMaxY() - dif;
RegionBody regionBodyMax = null;
double max = 0;
for (RegionBody regionBody : regionBodies) {
int minX = regionBody.getMinX();
int minY = regionBody.getMinY();
int maxX = regionBody.getMaxX();
int maxY = regionBody.getMaxY();
int s = (maxX - minX) * (maxY - minY);
if (s > max) {
max = s;
regionBodyMax = regionBody;
}
}
if (max > 0) {
//直接选择面积最大的区域
int minX = regionBodyMax.getMinX();
int minY = regionBodyMax.getMinY();
int maxX = regionBodyMax.getMaxX();
int maxY = regionBodyMax.getMaxY();
int xSize = maxX - minX;
int ySize = maxY - minY;
ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, minX, minY, xSize, ySize);
int times = templeConfig.getFood().getTimes();
for (int i = 0; i < times; i++) {
// List<Double> feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(),
// templeConfig.getFeatureNub(), templeConfig);
List<Double> feature = convolution.getCenterTexture(threeChannelMatrix1, templeConfig.getFood().getRegionSize(),
templeConfig.getPoolSize(), templeConfig, templeConfig.getFeatureNub());
if (templeConfig.isShowLog()) {
System.out.println(tag + ":" + feature);
}
int classifier = templeConfig.getClassifier();
switch (classifier) {
case Classifier.DNN:
Map<Integer, Double> map = new HashMap<>();
map.put(tag, 1.0);
if (templeConfig.getSensoryNerves().size() == templeConfig.getFeatureNub() * 3) {
intoDnnNetwork(1, feature, templeConfig.getSensoryNerves(), true, map, null);
} else {
throw new Exception("nerves number is not equal featureNub");
}
break;
case Classifier.LVQ:
Matrix vector = MatrixOperation.listToRowVector(feature);
lvqStudy(tag, vector);
break;
case Classifier.VAvg:
Matrix vec = MatrixOperation.listToRowVector(feature);
avgStudy(tag, vec);
break;
case Classifier.KNN:
Matrix veck = MatrixOperation.listToRowVector(feature);
knnStudy(tag, veck);
break;
}
}
return regionBody;
List<Double> feature = convolution.getCenterTexture(threeChannelMatrix1, templeConfig, templeConfig.getFeatureNub(), true, tag
, isFood);
Matrix veck = MatrixOperation.listToRowVector(feature);//将特征转化为矩阵
knnStudy(tag, veck);
return regionBodyMax;
} else {
throw new Exception("Parameter exception region size==" + regionBodies.size());
throw new Exception("Parameter exception region size 0 url:" + url);
}
}
@ -166,48 +142,41 @@ public class Operation {//进行计算
Watershed watershed = new Watershed(threeChannelMatrix, specificationsList, templeConfig);
List<RegionBody> regionList = watershed.rainfall();
for (RegionBody regionBody : regionList) {
MaxPoint maxPoint = new MaxPoint();
int minX = regionBody.getMinX() + dif;
int minY = regionBody.getMinY() + dif;
int maxX = regionBody.getMaxX() - dif;
int maxY = regionBody.getMaxY() - dif;
int minX = regionBody.getMinX();
int minY = regionBody.getMinY();
int maxX = regionBody.getMaxX();
int maxY = regionBody.getMaxY();
int xSize = maxX - minX;
int ySize = maxY - minY;
ThreeChannelMatrix threeChannelMatrix1 = convolution.getRegionMatrix(threeChannelMatrix, minX, minY, xSize, ySize);
// List<Double> feature = convolution.getCenterColor(threeChannelMatrix1, templeConfig.getPoolSize(),
// templeConfig.getFeatureNub(), templeConfig);
List<Double> feature = convolution.getCenterTexture(threeChannelMatrix1, templeConfig.getFood().getRegionSize(),
templeConfig.getPoolSize(), templeConfig, templeConfig.getFeatureNub());
if (templeConfig.isShowLog()) {
System.out.println(feature);
}
int classifier = templeConfig.getClassifier();
int id = 0;
switch (classifier) {
case Classifier.LVQ:
Matrix myMatrix = MatrixOperation.listToRowVector(feature);
id = getIdByLVQ(myMatrix);
break;
case Classifier.DNN:
if (templeConfig.getSensoryNerves().size() == templeConfig.getFeatureNub() * 3) {
intoDnnNetwork(IdCreator.get().nextId(), feature, templeConfig.getSensoryNerves(), false, null, maxPoint);
id = maxPoint.getId();
} else {
throw new Exception("nerves number is not equal featureNub");
List<Double> feature = convolution.getCenterTexture(threeChannelMatrix1, templeConfig, templeConfig.getFeatureNub(), false, 0,
false);
int id;
Food food = templeConfig.getFood();
int[] foodTypes = food.getFoodType();
Matrix myMatrix = MatrixOperation.listToRowVector(feature);
DimensionMappingStudy deepMapping = templeConfig.getFood().getDimensionMappingStudy();
id = deepMapping.getType(myMatrix);
//判断该类别是否属于干食
boolean isFood = false;
if (foodTypes != null) {
for (int myType : foodTypes) {
if (id == myType) {
isFood = true;
break;
}
break;
case Classifier.VAvg:
Matrix myMatrix1 = MatrixOperation.listToRowVector(feature);
id = getIdByVag(myMatrix1);
break;
case Classifier.KNN:
Matrix myMatrix2 = MatrixOperation.listToRowVector(feature);
Knn knn = templeConfig.getKnn();
id = knn.getType(myMatrix2);
break;
}
}
if (isFood) {//判断类别,获取积分
Map<Integer, GMClustering> foods = food.getFoodMeanMap();
Map<Integer, GMClustering> test = new HashMap<>();
test.put(1, foods.get(1));
test.put(id, foods.get(id));
CutFood cutFood = new CutFood(templeConfig, test);
regionBody.setTypeNub(cutFood.getTypeNub(threeChannelMatrix1, id));
}
regionBody.setType(id);
//System.out.println("类别" + id);
System.out.println("类别" + id);
}
return regionList;
}

File diff suppressed because it is too large Load Diff

@ -3,16 +3,14 @@ package org.wlld.imageRecognition;
import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation;
import org.wlld.config.Classifier;
import org.wlld.config.Kernel;
import org.wlld.config.RZ;
import org.wlld.config.StudyPattern;
import org.wlld.function.ReLu;
import org.wlld.function.Sigmod;
import org.wlld.function.Tanh;
import org.wlld.i.ActiveFunction;
import org.wlld.imageRecognition.border.*;
import org.wlld.imageRecognition.modelEntity.*;
import org.wlld.imageRecognition.segmentation.RgbRegression;
import org.wlld.imageRecognition.segmentation.DimensionMappingStudy;
import org.wlld.nerveCenter.NerveManager;
import org.wlld.nerveCenter.Normalization;
import org.wlld.nerveEntity.BodyList;
@ -22,10 +20,7 @@ import org.wlld.param.Cutting;
import org.wlld.param.Food;
import org.wlld.tools.ArithUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
public class TempleConfig {
@ -565,6 +560,7 @@ public class TempleConfig {
knnVector.add(bodyList);
}
modelParameter.setKnnVector(knnVector);
modelParameter.setFoodS(food.getFoodS());
}
break;
}
@ -687,6 +683,33 @@ public class TempleConfig {
knn.insertMatrix(matrix, type);
}
}
Map<Integer, Double> foods = modelParameter.getFoodS();
Set<Integer> set = foods.keySet();
int[] foodType = new int[foods.size()];
int index = 0;
for (int type : set) {
foodType[index] = type;
index++;
}
food.setFoodType(foodType);
food.setFoodS(foods);
//注入菜品信息
Map<Integer, List<Matrix>> features = knn.getFeatureMap();
for (Map.Entry<Integer, List<Matrix>> entry : features.entrySet()) {
int key = entry.getKey();
Matrix matrix = entry.getValue().get(0);
GMClustering gmClustering = new GMClustering(featureNub);
gmClustering.insertParameter(matrix);
if (set.contains(key)) {//干食
gmClustering.setRegionSize(foods.get(key));
food.getFoodMeanMap().put(key, gmClustering);
} else {
food.getNotFoodMeanMap().put(key, gmClustering);
}
}
DimensionMappingStudy dimensionAll = new DimensionMappingStudy(this);
dimensionAll.start();//生成映射层并将已经保存的knn特征进行映射
food.setDimensionMappingStudy(dimensionAll);
}
break;
}

@ -8,6 +8,24 @@ public class ThreeChannelMatrix {
Matrix matrixB;
Matrix H;
Matrix matrixRGB;
int similarId;//最大相似id
boolean isLine = false;//是否被连接
public int getSimilarId() {
return similarId;
}
public boolean isLine() {
return isLine;
}
public void setLine(boolean line) {
isLine = line;
}
public void setSimilarId(int similarId) {
this.similarId = similarId;
}
public Matrix getMatrixRGB() {
return matrixRGB;

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

@ -0,0 +1,28 @@
package org.wlld.imageRecognition.border;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class DistBody {
private int id;//id
private double dist;//距离
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getDist() {
return dist;
}
public void setDist(double dist) {
this.dist = dist;
}
}

@ -0,0 +1,100 @@
package org.wlld.imageRecognition.border;
import org.wlld.MatrixTools.Matrix;
import org.wlld.imageRecognition.MeanClustering;
import org.wlld.imageRecognition.RGBNorm;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class GMClustering extends MeanClustering {
private double regionSize;//单区域面积
public double getRegionSize() {
return regionSize;
}
public void setRegionSize(double regionSize) {
this.regionSize = regionSize;
}
public GMClustering(int speciesQuantity) throws Exception {
super(speciesQuantity);
}
public double getProbabilityDensity(double[] feature) throws Exception {//获取总概率密度
double sigma = 0;
for (RGBNorm rgbNorm : matrices) {
sigma = sigma + rgbNorm.getGMProbability(feature);
}
return sigma;
}
@Override
public void start() throws Exception {
super.start();
for (RGBNorm rgbNorm : matrices) {//高斯系数初始化
rgbNorm.gm();
}
for (int i = 0; i < 50; i++) {
gmClustering();
}
}
public void insertParameter(Matrix matrix) throws Exception {
int y = matrix.getY();
int size = y / speciesQuantity;
for (int i = 0; i <= y - size; i += size) {
double[] feature = new double[size];
RGBNorm rgbNorm = new RGBNorm();
matrices.add(rgbNorm);
for (int j = i; j < i + size; j++) {
feature[j - i] = matrix.getNumber(0, j);
}
rgbNorm.insertFeature(feature);
}
}
private void clear() {
for (RGBNorm rgbNorm : matrices) {//高斯系数初始化
rgbNorm.clearRGB();
}
}
private void gmClustering() throws Exception {//进行gm聚类
clear();
for (double[] rgb : matrixList) {//遍历当前集合
double allProbability = 0;//全概率
double[] pro = new double[speciesQuantity];
for (int i = 0; i < speciesQuantity; i++) {
RGBNorm rgbNorm = matrices.get(i);
double probability = rgbNorm.getGMProbability(rgb);
//System.out.println("pro===" + probability);
allProbability = allProbability + probability;
pro[i] = probability;
}
//求每个簇的后验概率
for (int i = 0; i < speciesQuantity; i++) {
pro[i] = pro[i] / allProbability;
}
//判断概率最大的簇
int index = 0;
double max = 0;
for (int i = 0; i < speciesQuantity; i++) {
if (pro[i] > max) {
max = pro[i];
index = i;
}
}
//注入特征
matrices.get(index).setGmFeature(rgb, pro[index]);
}
for (RGBNorm rgbNorm : matrices) {//高斯系数初始化
rgbNorm.gm();
}
}
}

@ -12,13 +12,13 @@ import java.util.*;
* @date 10:14 2020/2/4
*/
public class KClustering {
private List<Box> matrixList = new ArrayList<>();//聚类集合
private int length;//向量长度(模型需要返回)
private int speciesQuantity;//种类数量(模型需要返回)
private Matrix[] matrices;//均值K模型(模型需要返回)
private Map<Integer, List<Box>> clusterMap = new HashMap<>();//簇
private Map<Integer, Box> positionMap = new HashMap<>();//聚类K均值结果(需要返回)
private boolean isReady = false;
protected List<Box> matrixList = new ArrayList<>();//聚类集合
protected int length;//向量长度(模型需要返回)
protected int speciesQuantity;//种类数量(模型需要返回)
protected Matrix[] matrices;//均值K模型(模型需要返回)
protected Map<Integer, List<Box>> clusterMap = new HashMap<>();//簇
protected Map<Integer, Box> positionMap = new HashMap<>();//聚类K均值结果(需要返回)
protected boolean isReady = false;
public void setLength(int length) {
this.length = length;

@ -14,6 +14,10 @@ public class Knn {//KNN分类器
this.nub = nub;
}
public void setFeatureMap(Map<Integer, List<Matrix>> featureMap) {
this.featureMap = featureMap;
}
public Map<Integer, List<Matrix>> getFeatureMap() {
return featureMap;
}
@ -96,12 +100,12 @@ public class Knn {//KNN分类器
int type = entry.getKey();
List<Matrix> matrices = entry.getValue();
for (Matrix matrix : matrices) {
double dist = MatrixOperation.errorNub(vector, matrix, 0);
// double dist = MatrixOperation.getEDist(matrix, vector);
//double dist = MatrixOperation.errorNub(vector, matrix);
double dist = MatrixOperation.getEDist(matrix, vector);
compare(dists, types, dist, type);
}
}
//System.out.println(Arrays.toString(types));
System.out.println(Arrays.toString(types));
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nub; i++) {
int type = types[i];

@ -0,0 +1,64 @@
package org.wlld.imageRecognition.modelEntity;
import java.util.ArrayList;
import java.util.List;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class GMBody {
private int type;//类别
private List<Integer> pixels = new ArrayList<>();
private int pixelNub = 0;
private int nub;
public int getNub() {
return nub;
}
public void setNub(int nub) {
this.nub = nub;
}
public GMBody(int type, int x, int y) {
this.type = type;
pixelNub++;
pixels.add((x << 12) | y);
}
public int getPixelNub() {
return pixelNub;
}
public int getType() {
return type;
}
private boolean isAdjacent(int x, int y, int i, int j) {
boolean adjacent = false;
if (Math.abs(x - i) == 1 || Math.abs(y - j) == 1) {
adjacent = true;
}
return adjacent;
}
public boolean insertRgb(int x, int y, int type) {
boolean isRight = false;
if (this.type == type) {
for (int pixel : pixels) {
int i = (pixel >> 12) & 0xfff;
int j = pixel & 0xfff;
if (isAdjacent(x, y, i, j)) {//相邻
isRight = true;
pixelNub++;
pixels.add((x << 12) | y);
break;
}
}
}
return isRight;
}
}

@ -0,0 +1,32 @@
package org.wlld.imageRecognition.modelEntity;
import org.wlld.imageRecognition.segmentation.DimensionMappingStudy;
import java.util.Set;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class KeyMapping {
private Set<Integer> keys;
private DimensionMappingStudy dimensionMapping;
public Set<Integer> getKeys() {
return keys;
}
public void setKeys(Set<Integer> keys) {
this.keys = keys;
}
public DimensionMappingStudy getDimensionMapping() {
return dimensionMapping;
}
public void setDimensionMapping(DimensionMappingStudy dimensionMapping) {
this.dimensionMapping = dimensionMapping;
}
}

@ -0,0 +1,29 @@
package org.wlld.imageRecognition.modelEntity;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class MappingBody {
private double[] feature;//特征
private double mappingNub;//映射好的值
public MappingBody(double[] feature) {
this.feature = feature;
double sigma = 0;
for (int i = 0; i < feature.length; i++) {
sigma = sigma + Math.pow(feature[i], 2);
}
mappingNub = Math.sqrt(sigma);
}
public double[] getFeature() {
return feature;
}
public double getMappingNub() {
return mappingNub;
}
}

@ -0,0 +1,37 @@
package org.wlld.imageRecognition.modelEntity;
import org.wlld.MatrixTools.Matrix;
import org.wlld.i.OutBack;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class RgbBack implements OutBack {
private int id = 0;
private double out = -2;
public void clear() {
out = -2;
id = 0;
}
@Override
public void getBack(double out, int id, long eventId) {
if (out > this.out) {
this.out = out;
this.id = id;
}
}
@Override
public void getBackMatrix(Matrix matrix, long eventId) {
}
public int getId() {
return id;
}
}

@ -0,0 +1,72 @@
package org.wlld.imageRecognition.segmentation;
import org.wlld.MatrixTools.Matrix;
import org.wlld.i.PsoFunction;
import org.wlld.imageRecognition.ThreeChannelMatrix;
import org.wlld.tools.Frequency;
import java.util.HashMap;
import java.util.Map;
/**
* @param
* @DATA
* @Author LiDaPeng
* @Description
*/
public class ColorFunction extends Frequency implements PsoFunction {
private Matrix matrixR;
private Matrix matrixG;
private Matrix matrixB;
private Map<Integer, double[]> pixels = new HashMap<>();
public ColorFunction(ThreeChannelMatrix threeChannelMatrix) {
matrixR = threeChannelMatrix.getMatrixR();
matrixG = threeChannelMatrix.getMatrixG();
matrixB = threeChannelMatrix.getMatrixB();
}
@Override
public double getResult(double[] parameter, int id) throws Exception {
int x = (int) parameter[0];
int y = (int) parameter[1];
double[] rgb = new double[]{matrixR.getNumber(x, y) / 255, matrixG.getNumber(x, y) / 255,
matrixB.getNumber(x, y) / 255};
pixels.put(id, rgb);
//计算当前方差
return getDist();
//return getRegression();
}
private double getRegression() throws Exception {
RgbRegression rgbRegression = new RgbRegression(pixels.size());
for (Map.Entry<Integer, double[]> entry : pixels.entrySet()) {
double[] rgb = entry.getValue();
rgbRegression.insertRGB(rgb);
}
double sigma = 0;
if (rgbRegression.regression()) {
for (Map.Entry<Integer, double[]> entry : pixels.entrySet()) {
double[] rgb = entry.getValue();
sigma = sigma + rgbRegression.getDisError(rgb);
}
sigma = sigma / pixels.size();
}
return sigma;
}
private double getDist() {//计算当前均方误差
double[] r = new double[pixels.size()];
double[] g = new double[pixels.size()];
double[] b = new double[pixels.size()];
for (Map.Entry<Integer, double[]> entry : pixels.entrySet()) {
double[] rgb = entry.getValue();
int key = entry.getKey();
r[key] = rgb[0];
g[key] = rgb[1];
b[key] = rgb[2];
}
return dc(r) + dc(g) + dc(b);
}
}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save