提速阉割版卷积

pull/1/head
lidapeng 5 years ago
parent 3952c81a0e
commit 7f7841c5c4

@ -18,8 +18,8 @@ import java.util.Map;
public class App { public class App {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
//createNerveTest(); //createNerveTest();
//testPic(); testPic();
test(); //test();
} }
public static void testPic() throws Exception { public static void testPic() throws Exception {
@ -34,7 +34,7 @@ public class App {
Map<Integer, Double> wrongTagging = new HashMap<>();//分类标注 Map<Integer, Double> wrongTagging = new HashMap<>();//分类标注
rightTagging.put(1, 1.0); rightTagging.put(1, 1.0);
wrongTagging.put(1, 0.0); wrongTagging.put(1, 0.0);
for (int i = 1; i < 1000; i++) { for (int i = 1; i < 999; i++) {
System.out.println("开始学习1==" + i); System.out.println("开始学习1==" + i);
//读取本地URL地址图片,并转化成矩阵 //读取本地URL地址图片,并转化成矩阵
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + i + ".png"); Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/c/c" + i + ".png");
@ -51,9 +51,9 @@ public class App {
ModelParameter modelParameter = templeConfig.getModel(); ModelParameter modelParameter = templeConfig.getModel();
String model = JSON.toJSONString(modelParameter); String model = JSON.toJSONString(modelParameter);
System.out.println("一次提取:" + model); System.out.println("一次提取:" + model);
ModelParameter modelParameter1 = JSONObject.parseObject(model, ModelParameter.class);
TempleConfig templeConfig1 = getTemple(false); TempleConfig templeConfig1 = getTemple(false);
ModelParameter modelParameter1 = JSONObject.parseObject(model, ModelParameter.class);
templeConfig1.insertModel(modelParameter1); templeConfig1.insertModel(modelParameter1);
ModelParameter modelParameter2 = templeConfig1.getModel(); ModelParameter modelParameter2 = templeConfig1.getModel();
String model2 = JSON.toJSONString(modelParameter2); String model2 = JSON.toJSONString(modelParameter2);
@ -63,13 +63,8 @@ public class App {
Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/test/a101.png"); Matrix right = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/test/a101.png");
Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b1000.png"); Matrix wrong = picture.getImageMatrixByLocal("/Users/lidapeng/Desktop/myDocment/b/b1000.png");
//进行图像检测 //进行图像检测
operation1.look(right, 2);
operation1.look(wrong, 3); operation1.look(wrong, 3);
} operation1.look(right, 2);
public static void test() throws Exception {
TempleConfig templeConfig = new TempleConfig();
templeConfig.initConvolutionalVision(true, 3024, 4032);
} }
public static TempleConfig getTemple(boolean isFirst) throws Exception { public static TempleConfig getTemple(boolean isFirst) throws Exception {

@ -3,6 +3,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.Kernel; import org.wlld.config.Kernel;
import org.wlld.tools.ArithUtil;
/** /**
* @author lidapeng * @author lidapeng
@ -33,7 +34,6 @@ public class Convolution {
} }
return late(myMatrix); return late(myMatrix);
} }
public Matrix late(Matrix matrix) throws Exception {//迟化处理 public Matrix late(Matrix matrix) throws Exception {//迟化处理
int xn = matrix.getX(); int xn = matrix.getX();
int yn = matrix.getY(); int yn = matrix.getY();

@ -2,6 +2,7 @@ package org.wlld.imageRecognition;
import org.wlld.MatrixTools.Matrix; import org.wlld.MatrixTools.Matrix;
import org.wlld.config.Kernel;
import org.wlld.nerveEntity.SensoryNerve; import org.wlld.nerveEntity.SensoryNerve;
import org.wlld.tools.ArithUtil; import org.wlld.tools.ArithUtil;
@ -35,14 +36,6 @@ public class Operation {//进行计算
intoNerve(1, list, templeConfig.getSensoryNerves(), true, tagging); intoNerve(1, list, templeConfig.getSensoryNerves(), true, tagging);
} }
//卷积网络学习
public void learning(Matrix matrix, double tag) throws Exception {//前者是图片,后者是标注
//intoNerve(1, list, templeConfig.getSensoryNerves(), true, tagging);
intoNerveMatrix(1, matrix,
templeConfig.getConvolutionalNerveManager().getSensoryNerves()
, true, tag);
}
//图像视觉 //图像视觉
public void look(Matrix matrix, long eventId) throws Exception { public void look(Matrix matrix, long eventId) throws Exception {
List<Double> list = convolution(matrix); List<Double> list = convolution(matrix);
@ -73,12 +66,4 @@ public class Operation {//进行计算
sensoryNerveList.get(i).postMessage(eventId, featurList.get(i), isStudy, map); sensoryNerveList.get(i).postMessage(eventId, featurList.get(i), isStudy, map);
} }
} }
private void intoNerveMatrix(long eventId, Matrix matrix,
List<SensoryNerve> sensoryNerveList
, boolean isStudy, double E) throws Exception {
for (int i = 0; i < sensoryNerveList.size(); i++) {
sensoryNerveList.get(i).postMatrixMessage(eventId, matrix, isStudy, E);
}
}
} }

@ -1,6 +1,5 @@
package org.wlld.imageRecognition; package org.wlld.imageRecognition;
import org.wlld.function.ReLu;
import org.wlld.function.Sigmod; import org.wlld.function.Sigmod;
import org.wlld.i.OutBack; import org.wlld.i.OutBack;
import org.wlld.nerveCenter.NerveManager; import org.wlld.nerveCenter.NerveManager;
@ -12,7 +11,6 @@ import java.util.List;
public class TempleConfig { public class TempleConfig {
private NerveManager nerveManager;//神经网络管理器 private NerveManager nerveManager;//神经网络管理器
private NerveManager convolutionalNerveManager;//卷积网络管理器
public double cutThreshold = 10;//切割阈值默认值 public double cutThreshold = 10;//切割阈值默认值
public int row = 5;//行的最小比例 public int row = 5;//行的最小比例
public int column = 3;//列的最小比例 public int column = 3;//列的最小比例
@ -31,28 +29,6 @@ public class TempleConfig {
} }
} }
public NerveManager getConvolutionalNerveManager() {
return convolutionalNerveManager;
}
public void initConvolutionalVision(boolean initPower, int weight, int height) throws Exception {//初始化卷积视觉
//weight 是宽度 是 j 是 cloumn , height 是 高度, row 是 i
//我先要确定卷积层有几层
int deeps = 0;
boolean isNext;
do {
height = height / 3;
weight = weight / 3;
isNext = height > 3 && weight > 3;
if (isNext) {
deeps++;
}
} while (isNext);
convolutionalNerveManager = new NerveManager(1, 1
, 1, deeps - 1, new ReLu());
convolutionalNerveManager.init(initPower, true);
}
public ModelParameter getModel() {//获取模型参数 public ModelParameter getModel() {//获取模型参数
return nerveManager.getModelParameter(); return nerveManager.getModelParameter();
} }

@ -168,7 +168,7 @@ public class NerveManager {
List<Nerve> lastNeveList = depthNerves.get(depthNerves.size() - 1); List<Nerve> lastNeveList = depthNerves.get(depthNerves.size() - 1);
//初始化输出神经元 //初始化输出神经元
for (int i = 1; i < outNerveNub + 1; i++) { for (int i = 1; i < outNerveNub + 1; i++) {
OutNerve outNerve = new OutNerve(i, hiddenNerverNub, 0, studyPoint, initPower, activeFunction, isMatrix); OutNerve outNerve = new OutNerve(i, hiddenNerverNub, 0, studyPoint, initPower, activeFunction);
//输出层神经元连接最后一层隐层神经元 //输出层神经元连接最后一层隐层神经元
outNerve.connectFathor(lastNeveList); outNerve.connectFathor(lastNeveList);
outNevers.add(outNerve); outNevers.add(outNerve);
@ -204,8 +204,7 @@ public class NerveManager {
} else { } else {
downNub = hiddenNerverNub; downNub = hiddenNerverNub;
} }
HiddenNerve hiddenNerve = new HiddenNerve(j, i + 1, upNub, downNub, studyPoint, initPower, activeFunction HiddenNerve hiddenNerve = new HiddenNerve(j, i + 1, upNub, downNub, studyPoint, initPower, activeFunction);
, isMatrix);
hiddenNerveList.add(hiddenNerve); hiddenNerveList.add(hiddenNerve);
} }
depthNerves.add(hiddenNerveList); depthNerves.add(hiddenNerveList);

@ -16,8 +16,8 @@ public class HiddenNerve extends Nerve {
//static final Logger logger = LogManager.getLogger(HiddenNerve.class); //static final Logger logger = LogManager.getLogger(HiddenNerve.class);
public HiddenNerve(int id, int depth, int upNub, int downNub, double studyPoint, public HiddenNerve(int id, int depth, int upNub, int downNub, double studyPoint,
boolean init, ActiveFunction activeFunction, boolean isMatrix) {//隐层神经元 boolean init, ActiveFunction activeFunction) {//隐层神经元
super(id, upNub, "HiddenNerve", downNub, studyPoint, init, activeFunction, isMatrix); super(id, upNub, "HiddenNerve", downNub, studyPoint, init, activeFunction);
this.depth = depth; this.depth = depth;
} }
@ -40,18 +40,4 @@ public class HiddenNerve extends Nerve {
} }
// sendMessage(); // sendMessage();
} }
@Override
protected void inputMatrix(long eventId, Matrix parameter, boolean isStudy
, double E) throws Exception {
//先将输入矩阵进行卷积运算
if (isStudy) {//初始化求和参数
initFeatures(eventId);
}
Matrix matrix = convolution(parameter, eventId, isStudy);//这个神经元卷积结束
if (isStudy) {//求和
outNub = calculation(eventId);
}
sendMatrixMessage(eventId, matrix, isStudy, E);//将矩阵发到下一层神经元
}
} }

@ -1,9 +1,7 @@
package org.wlld.nerveEntity; package org.wlld.nerveEntity;
import org.wlld.MatrixTools.Matrix;
import org.wlld.MatrixTools.MatrixOperation;
import org.wlld.i.ActiveFunction; import org.wlld.i.ActiveFunction;
import org.wlld.test.Ma;
import org.wlld.tools.ArithUtil; import org.wlld.tools.ArithUtil;
import java.util.*; import java.util.*;
@ -50,21 +48,14 @@ public abstract class Nerve {
} }
protected Nerve(int id, int upNub, String name, int downNub, protected Nerve(int id, int upNub, String name, int downNub,
double studyPoint, boolean init, ActiveFunction activeFunction double studyPoint, boolean init, ActiveFunction activeFunction) {//该神经元在同层神经元中的编号
, boolean isMatrix) {//该神经元在同层神经元中的编号
this.id = id; this.id = id;
this.upNub = upNub; this.upNub = upNub;
this.name = name; this.name = name;
this.downNub = downNub; this.downNub = downNub;
this.studyPoint = studyPoint; this.studyPoint = studyPoint;
this.activeFunction = activeFunction; this.activeFunction = activeFunction;
if (init) { initPower(init);//生成随机权重
if (isMatrix) {
initKernel();
} else {
initPower();//生成随机权重
}
}
} }
public void sendMessage(long enevtId, double parameter, boolean isStudy, Map<Integer, Double> E) throws Exception { public void sendMessage(long enevtId, double parameter, boolean isStudy, Map<Integer, Double> E) throws Exception {
@ -77,18 +68,6 @@ public abstract class Nerve {
} }
} }
//正向传播矩阵
public void sendMatrixMessage(long enevtId, Matrix parameter, boolean isStudy,
double E) throws Exception {
if (son.size() > 0) {
for (Nerve nerve : son) {
nerve.inputMatrix(enevtId, parameter, isStudy, E);
}
} else {
throw new Exception("this layer is lastIndex");
}
}
private void backSendMessage(long eventId) throws Exception {//反向传播 private void backSendMessage(long eventId) throws Exception {//反向传播
if (fathor.size() > 0) { if (fathor.size() > 0) {
for (int i = 0; i < fathor.size(); i++) { for (int i = 0; i < fathor.size(); i++) {
@ -102,12 +81,6 @@ public abstract class Nerve {
} }
//接收矩阵参数
protected void inputMatrix(long eventId, Matrix parameter, boolean isStudy
, double E) throws Exception {//输入
}
private void backGetMessage(double parameter, long eventId) throws Exception {//反向传播 private void backGetMessage(double parameter, long eventId) throws Exception {//反向传播
backNub++; backNub++;
sigmaW = ArithUtil.add(sigmaW, parameter); sigmaW = ArithUtil.add(sigmaW, parameter);
@ -135,6 +108,7 @@ public abstract class Nerve {
double wp = ArithUtil.mul(bn, h);//编号为KEY的上层隐层神经元权重的变化值 double wp = ArithUtil.mul(bn, h);//编号为KEY的上层隐层神经元权重的变化值
w = ArithUtil.add(w, wp);//修正后的编号为KEY的上层隐层神经元权重 w = ArithUtil.add(w, wp);//修正后的编号为KEY的上层隐层神经元权重
double dm = ArithUtil.mul(w, gradient);//返回给相对应的神经元 double dm = ArithUtil.mul(w, gradient);//返回给相对应的神经元
// System.out.println("allG==" + allG + ",dm==" + dm);
wg.put(key, dm);//保存上一层权重与梯度的积 wg.put(key, dm);//保存上一层权重与梯度的积
dendrites.put(key, w);//保存修正结果 dendrites.put(key, w);//保存修正结果
} }
@ -157,14 +131,6 @@ public abstract class Nerve {
return allReady; return allReady;
} }
protected void initFeatures(long eventId) {//初始化九个参数和
List<Double> list = new ArrayList<>();
for (int i = 0; i < 9; i++) {
list.add(0.0);
}
features.put(eventId, list);
}
protected void destoryParameter(long eventId) {//销毁参数 protected void destoryParameter(long eventId) {//销毁参数
features.remove(eventId); features.remove(eventId);
} }
@ -183,73 +149,23 @@ public abstract class Nerve {
return ArithUtil.sub(sigma, threshold); return ArithUtil.sub(sigma, threshold);
} }
//进行卷积运算 private void initPower(boolean init) {//初始化权重及阈值
protected Matrix convolution(Matrix matrix, long eventId, boolean isStudy) throws Exception {
Matrix kernel = getKernel();
int xn = matrix.getX();
int yn = matrix.getY();
int x = xn / 3;//求导后矩阵的行数
int y = yn / 3;//求导后矩阵的列数
Matrix myMatrix = new Matrix(x, y);//最终合成矩阵
for (int i = 0; i < xn - 3; i += 3) {//遍历行
for (int j = 0; j < yn - 3; j += 3) {//遍历每行的列
//进行卷积运算
double dm = MatrixOperation.convolution(matrix, kernel, i, j);
if (isStudy) {//如果是学习的话,拿到分块矩阵,对对应权重的值进行求和
Matrix matrix1 = matrix.getSonOfMatrix(i, j, 3, 3);
sigma(matrix1, eventId);
}
if (dm > 0) {//存在边缘
myMatrix.setNub(i, j, dm);
}
}
}
return myMatrix;
}
//对每一项进行求和,只有在学习的时候执行它
private void sigma(Matrix matrix, long eventId) throws Exception {
int n = 0;
List<Double> list = features.get(eventId);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
list.set(n, ArithUtil.add(list.get(n), matrix.getNumber(i, j)));
n++;
}
}
}
private Matrix getKernel() throws Exception {
Matrix kernel = new Matrix(3, 3);
for (int i = 1; i < 10; i++) {
double w = dendrites.get(i);
int t = i - 1;
//将权重填入卷积核当中
int x = t / 3;
int y = t % 3;
kernel.setNub(x, y, w);
}
return kernel;
}
private void initPower() {//初始化权重及阈值
if (upNub > 0) { if (upNub > 0) {
Random random = new Random(); Random random = new Random();
for (int i = 1; i < upNub + 1; i++) { for (int i = 1; i < upNub + 1; i++) {
dendrites.put(i, random.nextDouble());//random.nextDouble() double nub = 0;
if (init) {
nub = random.nextDouble();
} }
//生成随机阈值 dendrites.put(i, nub);//random.nextDouble()
threshold = random.nextDouble();
} }
//生成随机阈值
double nub = 0;
if (init) {
nub = random.nextDouble();
} }
threshold = nub;
public void initKernel() {//对卷积核进行初始化
Random random = new Random();
for (int i = 1; i < 10; i++) {
dendrites.put(i, random.nextDouble());
} }
//生成随机阈值
threshold = random.nextDouble();
} }
public int getId() { public int getId() {

@ -19,8 +19,8 @@ public class OutNerve extends Nerve {
private double allE;//训练累计EK private double allE;//训练累计EK
public OutNerve(int id, int upNub, int downNub, double studyPoint, boolean init, public OutNerve(int id, int upNub, int downNub, double studyPoint, boolean init,
ActiveFunction activeFunction, boolean isMatrix) { ActiveFunction activeFunction) {
super(id, upNub, "OutNerve", downNub, studyPoint, init, activeFunction, isMatrix); super(id, upNub, "OutNerve", downNub, studyPoint, init, activeFunction);
} }
public void setOutBack(OutBack outBack) { public void setOutBack(OutBack outBack) {
@ -54,27 +54,9 @@ public class OutNerve extends Nerve {
} }
} }
@Override
protected void inputMatrix(long eventId, Matrix parameter, boolean isStudy, double E) throws Exception {
if (isStudy) {//初始化求和参数
initFeatures(eventId);
}
Matrix matrix = convolution(parameter, eventId, isStudy);//这个神经元卷积结束
if (isStudy) {//求和
outNub = calculation(eventId);
//进行反向传播
} else {//进入BP网络输出
}
}
private double outGradient() {//生成输出层神经元梯度变化 private double outGradient() {//生成输出层神经元梯度变化
//上层神经元输入值 * 当前神经元梯度*学习率 =该上层输入的神经元权重变化 //上层神经元输入值 * 当前神经元梯度*学习率 =该上层输入的神经元权重变化
//当前梯度神经元梯度变化 *学习旅 * -1 = 当前神经元阈值变化 //当前梯度神经元梯度变化 *学习旅 * -1 = 当前神经元阈值变化
//ArithUtil.sub(E, outNub) 求这个的累计平均值
//allE = ArithUtil.add(ArithUtil.sub(E, outNub), allE);
//double avg = ArithUtil.div(allE, trainNub);
return ArithUtil.mul(activeFunction.functionG(outNub), ArithUtil.sub(E, outNub)); return ArithUtil.mul(activeFunction.functionG(outNub), ArithUtil.sub(E, outNub));
} }
} }

@ -13,7 +13,7 @@ import java.util.Map;
public class SensoryNerve extends Nerve { public class SensoryNerve extends Nerve {
public SensoryNerve(int id, int upNub) { public SensoryNerve(int id, int upNub) {
super(id, upNub, "SensoryNerve", 0, 0.1, false, null, false); super(id, upNub, "SensoryNerve", 0, 0.1, false, null);
} }
public void postMessage(long eventId, double parameter, boolean isStudy, Map<Integer, Double> E) throws Exception {//感知神经元输出 public void postMessage(long eventId, double parameter, boolean isStudy, Map<Integer, Double> E) throws Exception {//感知神经元输出
@ -21,10 +21,6 @@ public class SensoryNerve extends Nerve {
sendMessage(eventId, parameter, isStudy, E); sendMessage(eventId, parameter, isStudy, E);
} }
public void postMatrixMessage(long enevtId, Matrix parameter, boolean isStudy, double E) throws Exception {
sendMatrixMessage(enevtId, parameter, isStudy, E);
}
@Override @Override
public void connect(List<Nerve> nerveList) {//连接第一层隐层神经元 public void connect(List<Nerve> nerveList) {//连接第一层隐层神经元
super.connect(nerveList); super.connect(nerveList);

@ -7,7 +7,10 @@ package org.wlld.test;
*/ */
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
int a = 1 % 3;
System.out.println(a); }
public static void test1() {
} }
} }

Loading…
Cancel
Save