|
|
|
@ -1,7 +1,6 @@
|
|
|
|
|
package org.wlld.imageRecognition.segmentation;
|
|
|
|
|
|
|
|
|
|
import org.wlld.MatrixTools.Matrix;
|
|
|
|
|
import org.wlld.MatrixTools.MatrixOperation;
|
|
|
|
|
import org.wlld.imageRecognition.TempleConfig;
|
|
|
|
|
import org.wlld.imageRecognition.border.Knn;
|
|
|
|
|
import org.wlld.pso.PSO;
|
|
|
|
@ -16,11 +15,33 @@ import java.util.*;
|
|
|
|
|
* @Description 维度映射
|
|
|
|
|
*/
|
|
|
|
|
public class DimensionMappingStudy {
|
|
|
|
|
private Knn knn = new Knn(1);//一个knn分类器,决定进这一层哪个类别
|
|
|
|
|
private int id;//类别id
|
|
|
|
|
private Map<Integer, DimensionMappingStudy> deepMappingMap = new HashMap<>();
|
|
|
|
|
private TempleConfig templeConfig;
|
|
|
|
|
private Knn myKnn = new Knn(1);
|
|
|
|
|
private double[] mappingSigma;//映射层
|
|
|
|
|
|
|
|
|
|
public DimensionMappingStudy(TempleConfig templeConfig) throws Exception {
|
|
|
|
|
this.templeConfig = templeConfig;
|
|
|
|
|
//深度克隆
|
|
|
|
|
myKnn.setFeatureMap(cloneFeature(templeConfig.getKnn().getFeatureMap()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Knn getMyKnn() {
|
|
|
|
|
return myKnn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<Integer, List<Matrix>> cloneFeature(Map<Integer, List<Matrix>> feature) throws Exception {
|
|
|
|
|
Map<Integer, List<Matrix>> realFeatures = new HashMap<>();
|
|
|
|
|
for (Map.Entry<Integer, List<Matrix>> entry : feature.entrySet()) {
|
|
|
|
|
List<Matrix> matrixList = new ArrayList<>();
|
|
|
|
|
List<Matrix> matrixAll = entry.getValue();
|
|
|
|
|
for (Matrix matrix : matrixAll) {
|
|
|
|
|
matrixList.add(matrix.getRow(0));
|
|
|
|
|
}
|
|
|
|
|
realFeatures.put(entry.getKey(), matrixList);
|
|
|
|
|
}
|
|
|
|
|
return realFeatures;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double getDist(Matrix data1, Matrix data2) throws Exception {
|
|
|
|
|
int size = data1.getY();
|
|
|
|
|
double sigma = 0;
|
|
|
|
@ -80,10 +101,9 @@ public class DimensionMappingStudy {
|
|
|
|
|
return minSub;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void nextDeep(TempleConfig templeConfig) throws Exception {//对模型数据进行自检测
|
|
|
|
|
Map<Integer, List<Matrix>> featureMap = knn.getFeatureMap();
|
|
|
|
|
int nub = featureMap.size() / 2;
|
|
|
|
|
if (nub > 1) {
|
|
|
|
|
public void selfTest(int nub) throws Exception {//对模型数据进行自检测
|
|
|
|
|
Map<Integer, List<Matrix>> myFeatureMap = myKnn.getFeatureMap();//未映射的克隆的
|
|
|
|
|
Map<Integer, List<Matrix>> featureMap = templeConfig.getKnn().getFeatureMap();
|
|
|
|
|
MinTypeSort minTypeSort = new MinTypeSort();
|
|
|
|
|
Map<Integer, List<MinType>> minMap = new HashMap<>();//保存与该类别最相似的类别,及距离值
|
|
|
|
|
for (Map.Entry<Integer, List<Matrix>> entry : featureMap.entrySet()) {
|
|
|
|
@ -102,62 +122,103 @@ public class DimensionMappingStudy {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Collections.sort(minTypes, minTypeSort);
|
|
|
|
|
int len = 0;
|
|
|
|
|
for (int i = 0; i < minTypes.size(); i++) {
|
|
|
|
|
if (minTypes.get(i).minDist < 0) {
|
|
|
|
|
len++;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (len < nub) {
|
|
|
|
|
minTypes = minTypes.subList(0, nub);
|
|
|
|
|
} else {
|
|
|
|
|
minTypes = minTypes.subList(0, len);
|
|
|
|
|
}
|
|
|
|
|
minMap.put(key, minTypes);
|
|
|
|
|
}
|
|
|
|
|
List<int[]> cups = new ArrayList<>();
|
|
|
|
|
for (Map.Entry<Integer, List<MinType>> entry : minMap.entrySet()) {
|
|
|
|
|
if (nub == 2 || nub == 3) {
|
|
|
|
|
System.out.println("类别===============================" + entry.getKey());
|
|
|
|
|
}
|
|
|
|
|
int[] cup = new int[nub + 1];
|
|
|
|
|
cup[0] = entry.getKey();
|
|
|
|
|
List<MinType> minTypeList = entry.getValue();
|
|
|
|
|
DimensionMappingStudy dimensionMappingStudy = new DimensionMappingStudy();
|
|
|
|
|
Knn myKnn = dimensionMappingStudy.getKnn();
|
|
|
|
|
for (MinType minType : minTypeList) {
|
|
|
|
|
if (nub == 2 || nub == 3) {
|
|
|
|
|
System.out.println("type==" + minType.type + ",dist==" + minType.minDist);
|
|
|
|
|
for (int i = 0; i < minTypeList.size(); i++) {
|
|
|
|
|
cup[i + 1] = minTypeList.get(i).type;
|
|
|
|
|
}
|
|
|
|
|
List<Matrix> featureList = featureMap.get(minType.type);
|
|
|
|
|
for (Matrix matrix : featureList) {
|
|
|
|
|
myKnn.insertMatrix(matrix, minType.type);
|
|
|
|
|
cups.add(cup);
|
|
|
|
|
}
|
|
|
|
|
List<Set<Integer>> setList = getTypeSet(cups);
|
|
|
|
|
for (Set<Integer> set : setList) {
|
|
|
|
|
DimensionMappingStudy dimensionMappingStudy = new DimensionMappingStudy(templeConfig);
|
|
|
|
|
for (int type : set) {
|
|
|
|
|
List<Matrix> features = myFeatureMap.get(type);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
deepMappingMap.put(entry.getKey(), dimensionMappingStudy);
|
|
|
|
|
}
|
|
|
|
|
for (Map.Entry<Integer, DimensionMappingStudy> entry : deepMappingMap.entrySet()) {
|
|
|
|
|
entry.getValue().start(templeConfig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Set<Integer>> getTypeSet(List<int[]> t) {
|
|
|
|
|
//最终输入结果
|
|
|
|
|
List<Set<Integer>> t1 = new ArrayList<>();
|
|
|
|
|
//储存临时值
|
|
|
|
|
Set<Integer> s = new HashSet<>();
|
|
|
|
|
//用来校验重复游标组
|
|
|
|
|
List<List<Integer>> k = new ArrayList<>();
|
|
|
|
|
Boolean x = true;//对循环进行判断
|
|
|
|
|
int num = 0;//循环游标初始值
|
|
|
|
|
while (x) {
|
|
|
|
|
//校验重复游标
|
|
|
|
|
List<Integer> l = new ArrayList<>();
|
|
|
|
|
//清楚临时存储值
|
|
|
|
|
s.clear();
|
|
|
|
|
//单个最终值
|
|
|
|
|
Set<Integer> s1 = new HashSet<>();
|
|
|
|
|
for (int i = num; i < t.size(); i++) {
|
|
|
|
|
Boolean check = false;
|
|
|
|
|
//如果存在已经验证的数组,则跳过
|
|
|
|
|
for (int j = num; j < k.size(); j++) {
|
|
|
|
|
if (k.get(j).contains(i)) {
|
|
|
|
|
check = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (check) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int toClassification(Matrix feature) throws Exception {//进行识别 先映射
|
|
|
|
|
feature = mapping(feature, mappingSigma);
|
|
|
|
|
int type = knn.getType(feature);
|
|
|
|
|
if (deepMappingMap.size() > 0 && deepMappingMap.containsKey(type)) {//继续下一层
|
|
|
|
|
return deepMappingMap.get(type).toClassification(feature);
|
|
|
|
|
} else {//返回最后结果
|
|
|
|
|
return type;
|
|
|
|
|
//循环判断数组是否存在重复(当i=num时,给初始的s赋值)
|
|
|
|
|
//当S中包含重复值的时候,插入
|
|
|
|
|
for (int info : t.get(i)) {
|
|
|
|
|
if (i == num) {
|
|
|
|
|
for (int info1 : t.get(i)) {
|
|
|
|
|
l.add(i);
|
|
|
|
|
k.add(l);
|
|
|
|
|
s.add(info1);
|
|
|
|
|
}
|
|
|
|
|
} else if (s.contains(info)) {
|
|
|
|
|
for (int info1 : t.get(i)) {
|
|
|
|
|
l.add(i);
|
|
|
|
|
k.add(l);
|
|
|
|
|
s.add(info1);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void start(TempleConfig templeConfig) throws Exception {
|
|
|
|
|
Map<Integer, List<Matrix>> featureMap = knn.getFeatureMap();
|
|
|
|
|
}
|
|
|
|
|
//如果临时s不为空,给s1赋值,并存入到最后输出的t1中
|
|
|
|
|
if (!s.isEmpty()) {
|
|
|
|
|
for (Integer info : s) {
|
|
|
|
|
s1.add(info);
|
|
|
|
|
}
|
|
|
|
|
t1.add(s1);
|
|
|
|
|
}
|
|
|
|
|
num++;
|
|
|
|
|
//跳出循环
|
|
|
|
|
if (num >= t.size()) {
|
|
|
|
|
x = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return t1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void mappingStart() throws Exception {
|
|
|
|
|
Map<Integer, List<Matrix>> featureMap = templeConfig.getKnn().getFeatureMap();
|
|
|
|
|
FeatureMapping featureMapping = new FeatureMapping(featureMap);
|
|
|
|
|
int dimensionNub = templeConfig.getFeatureNub() * 3 * 2;//PSO维度
|
|
|
|
|
//创建粒子群
|
|
|
|
|
PSO pso = new PSO(dimensionNub, null, null, 500, 100,
|
|
|
|
|
featureMapping, 0.8, 2, 2, true, 0.2, 0.01);
|
|
|
|
|
featureMapping, 0.9, 2, 2, true, 0.2, 0.01);
|
|
|
|
|
List<double[]> mappings = pso.start();
|
|
|
|
|
int size = mappings.size();
|
|
|
|
|
mappingSigma = new double[dimensionNub];
|
|
|
|
@ -170,10 +231,14 @@ public class DimensionMappingStudy {
|
|
|
|
|
for (int i = 0; i < mappingSigma.length; i++) {
|
|
|
|
|
mappingSigma[i] = mappingSigma[i] / size;
|
|
|
|
|
}
|
|
|
|
|
templeConfig.getFood().setMappingParameter(mappingSigma);
|
|
|
|
|
//还要把所有已存在的knn特征完成映射
|
|
|
|
|
featureMapping(featureMap, mappingSigma, templeConfig);
|
|
|
|
|
//进行下一次映射选择
|
|
|
|
|
nextDeep(templeConfig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void start() throws Exception {
|
|
|
|
|
mappingStart();
|
|
|
|
|
selfTest(3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void featureMapping(Map<Integer, List<Matrix>> featureMap, double[] mapping
|
|
|
|
@ -200,8 +265,4 @@ public class DimensionMappingStudy {
|
|
|
|
|
}
|
|
|
|
|
return matrix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Knn getKnn() {
|
|
|
|
|
return knn;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|