Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
fd64662
正規化層で平均分散を保持しない
kishida Sep 15, 2015
be0b33f
とりあえずJOCL対応コードを書く
kishida Sep 17, 2015
3c2aae7
実行エラーログはコミットに含めない
kishida Sep 17, 2015
569793e
畳込み層の逆伝播にJOCLを使う。1割高速化。90/分→100/分
kishida Sep 19, 2015
c0b9058
畳込み層の順伝播にJOCLを使う。1割高速化。100/分→110/分
kishida Sep 20, 2015
f3e483e
全結合層と活性化関数のJOCL化。しかし遅くなった・・・
kishida Sep 20, 2015
bc37374
全結合層の逆伝播のJOCL化。しかしかなり遅くなった・・・
kishida Sep 20, 2015
46a48f1
MaxPoolingのOpenCL化
kishida Sep 22, 2015
ae91a1f
MultiNormalizeのOpenCL化
kishida Sep 22, 2015
8761e82
畳込み層のフィルタ・バイアスをGPUメモリに置いたまま処理する
kishida Sep 22, 2015
0bc644e
全結合層のフィルタ・バイアスをGPUメモリに置いたまま処理できるようにする。けど遅い。nio.Bufferが遅そう
kishida Sep 22, 2015
e8ae164
畳込み層のフィルタ更新をGPU対応に。0.5割はやくなった
kishida Sep 22, 2015
21a339d
全結合層のフィルタ更新をGPU対応に。alexnetのfc0がGPUに載せれるようになって倍速
kishida Sep 22, 2015
c5da498
順伝播をGPUメモリ上で動かす
kishida Sep 22, 2015
fff7827
逆伝播をGPUメモリ上で動かす。思いのほか速くならず。プロファイルにはノンブロッキング処理の数字がでなくてだまされる。
kishida Sep 23, 2015
0fee26d
必要なバッファをリリースしていたので修整
kishida Sep 23, 2015
e879440
畳込み逆伝播の3つのカーネルを並列実行する
kishida Sep 24, 2015
52b3b63
データ保存の不具合を修整
kishida Sep 24, 2015
568ce27
OpenCL情報取得コマンド
kishida Oct 4, 2015
d10384c
畳込み層の順伝播のときにローカルメモリを使う
kishida Oct 5, 2015
84d5ec3
毎回バイアスを表示すると時間がかかるので省略
kishida Oct 11, 2015
8cd2cb1
update libraries
Oct 22, 2018
e77152f
update jackson
Oct 15, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target/
/nb-configuration.xml
/nbactions*.xml
/nbactions*.xml
/*.log
14 changes: 12 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
<dependency>
<groupId>org.jogamp.gluegen</groupId>
<artifactId>gluegen-rt-main</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.jogamp.jocl</groupId>
<artifactId>jocl-main</artifactId>
<version>2.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -40,4 +50,4 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
</project>
50 changes: 29 additions & 21 deletions src/main/java/kishida/cnn/ConvolutionalNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.io.Writer;
Expand Down Expand Up @@ -59,6 +61,8 @@ public class ConvolutionalNet {
public static final String AVERAGE_PNG = "average.png";
private static final String FILENAME = "C:\\Users\\naoki\\Desktop\\alexnet.json.txt";
private static final String RESOURCE_NAME = "/alexnet_def.json";
//private static final String FILENAME = "C:\\Users\\naoki\\Desktop\\tinynet.json.txt";
//private static final String RESOURCE_NAME = "/tinynet_def.json";

static class Img{

Expand Down Expand Up @@ -89,8 +93,9 @@ BufferedImage readImage(){
static List<Double> historyData = new ArrayList<>();
static LinkedList<Integer> rateData = new LinkedList<>();

@SuppressWarnings({"ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown"})
public static void main(String[] args) throws IOException {
System.setProperty("com.amd.aparapi.enableShowGeneratedOpenCL", "false");
System.setProperty("com.aparapi.enableShowGeneratedOpenCL", "false");
String def = "C:\\Users\\naoki\\Desktop\\sampleimg288";
Path dir = Paths.get(args.length > 0 ? args[0] : def);
List<String> categories = Files.list(dir)
Expand Down Expand Up @@ -184,14 +189,15 @@ public static void main(String[] args) throws IOException {

NeuralNetwork nn;

/*
try(InputStream is = ConvolutionalNet.class.getResourceAsStream(RESOURCE_NAME);
InputStreamReader isr = new InputStreamReader(is)){
nn = NeuralNetwork.readFromJson(isr);
}*/

try(Reader r = Files.newBufferedReader(Paths.get(FILENAME))){
nn = NeuralNetwork.readFromJson(r);
if(true){
try(InputStream is = ConvolutionalNet.class.getResourceAsStream(RESOURCE_NAME);
InputStreamReader isr = new InputStreamReader(is)){
nn = NeuralNetwork.readFromJson(isr);
}
}else{
try(Reader r = Files.newBufferedReader(Paths.get(FILENAME))){
nn = NeuralNetwork.readFromJson(r);
}
}

nn.init();
Expand Down Expand Up @@ -285,23 +291,13 @@ public static void main(String[] args) throws IOException {
Image lineGraph = createLineGraph(500, 200,
historyData, 1, 0);
historyLabel.setIcon(new ImageIcon(lineGraph));
//一段目のフィルタの表示
//全結合一段の表示
firstFc.setIcon(new ImageIcon(createGraph(256, 128, fc1.getResult())));
//全結合二段の表示
lastResult.setIcon(new ImageIcon(createGraph(256, 128, output)));

firstBias.setIcon(new ImageIcon(createGraph(500, 128, conv1.getBias())));
secondBias.setIcon(new ImageIcon(createGraph(500, 128,
conv2.getBias())));
fc1Bias.setIcon(new ImageIcon(createGraph(500, 128, fc1.getBias())));
fc2Bias.setIcon(new ImageIcon(createGraph(500, 128, fc2.getBias())));

//System.out.println(Arrays.stream(output).mapToObj(d -> String.format("%.2f", d)).collect(Collectors.joining(",")));

count[0]++;
nn.setImageIndex(nn.getImageIndex() + 1);
if(count[0] >= MINI_BATCH){

nn.joinBatch();
batchCount[0]++;
System.out.printf("%5d %4d %.2f/m %s %s%n", batchCount[0],
Expand All @@ -319,7 +315,7 @@ public static void main(String[] args) throws IOException {
System.out.printf("weight: %.2f~%.2f average %.2f ",
ws.getMin(), ws.getMax(), ws.getAverage());
DoubleSummaryStatistics bs = ((LerningLayer)layer).getBiasStatistics();
System.out.printf("bias: %.2f~%.2f average %.2f ",
System.out.printf("bias: %.8f~%.8f average %.2f ",
bs.getMin(), bs.getMax(), bs.getAverage());
}
System.out.println();
Expand All @@ -329,6 +325,18 @@ public static void main(String[] args) throws IOException {
pStart[0] = System.currentTimeMillis();
nn.prepareBatch();

//一段目のフィルタの表示
//全結合一段の表示
firstFc.setIcon(new ImageIcon(createGraph(256, 128, fc1.getResult())));
//全結合二段の表示
lastResult.setIcon(new ImageIcon(createGraph(256, 128, output)));

firstBias.setIcon(new ImageIcon(createGraph(500, 128, conv1.getBias())));
secondBias.setIcon(new ImageIcon(createGraph(500, 128,
conv2.getBias())));
fc1Bias.setIcon(new ImageIcon(createGraph(500, 128, fc1.getBias())));
fc2Bias.setIcon(new ImageIcon(createGraph(500, 128, fc2.getBias())));

// 1時間に一回保存する
int hour = LocalTime.now().getHour();
if(lastHour[0] != hour){
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/kishida/cnn/NeuralNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.jogamp.opencl.CLBuffer;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -23,6 +25,7 @@
import java.util.stream.IntStream;
import kishida.cnn.activation.LogisticFunction;
import kishida.cnn.layers.ConvolutionLayer;
import kishida.cnn.layers.FullGpuEnabled;
import kishida.cnn.layers.FullyConnect;
import kishida.cnn.layers.InputLayer;
import kishida.cnn.layers.MaxPoolingLayer;
Expand Down Expand Up @@ -165,8 +168,23 @@ public float[] forward(float[] readData, float[] correctData){
delta[idx] = correctData[idx] - output[idx];
}
//逆伝播
CLBuffer<FloatBuffer> bufDelta = null;
for(int i = layers.size() - 1; i >= 1; --i){
delta = layers.get(i).backward(delta);
FullGpuEnabled layer = layers.get(i) instanceof FullGpuEnabled ?
(FullGpuEnabled) layers.get(i) : null;
FullGpuEnabled pre = layers.get(i).getPreLayer() instanceof FullGpuEnabled ?
(FullGpuEnabled)layers.get(i).getPreLayer() : null;
if(true && layer != null && pre != null && layer.isUseGpu()){
if(bufDelta == null){
bufDelta = layer.backwardBuf(pre.getBufResult(), delta);
}else{
bufDelta = layer.backwardBuf(pre.getBufResult(), bufDelta);
}
delta = null;
}else{
delta = layers.get(i).backward(delta);
bufDelta = null;
}
}

return output;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/kishida/cnn/activation/ActivationFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package kishida.cnn.activation;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

Expand All @@ -28,4 +29,6 @@ public void applyAfter(float[] values) {
/** 微分 */
public abstract float diff(float value);

@JsonIgnore
public abstract String getName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public float diff(float value) {
return value >= 0 && value <= 2 ? 1 : 0;
}

@Override
public String getName() {
return "limitrelu";
}

}
4 changes: 4 additions & 0 deletions src/main/java/kishida/cnn/activation/LinearFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ public float apply(float value) {
public float diff(float value) {
return 1;
}
@Override
public String getName() {
return "linear";
}

}
4 changes: 4 additions & 0 deletions src/main/java/kishida/cnn/activation/LogisticFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ public float apply(float value) {
public float diff(float value) {
return value * (1 - value);
}
@Override
public String getName() {
return "logistic";
}

}
4 changes: 4 additions & 0 deletions src/main/java/kishida/cnn/activation/RectifiedLinear.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ public float apply(float value) {
public float diff(float value) {
return value >= 0 ? 1 : 0;
}
@Override
public String getName() {
return "relu";
}

}
5 changes: 5 additions & 0 deletions src/main/java/kishida/cnn/activation/SoftMaxFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public float diff(float value) {
return value * (1 - value);
}

@Override
public String getName() {
return "softmax";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ private void proc(int chxxyy) {
}
newDelta[chxxyy] = tempDelta;
}
float[] input;
float[] result;
int inputChannels;
int inputWidth;
Expand All @@ -65,11 +64,10 @@ private void proc(int chxxyy) {
float[] delta;
float[] newDelta;

public float[] backword(float[] input, float[] delta, float[] result,
public float[] backword(float[] delta, float[] result,
int inputChannels, int inputWidth, int inputHeight,
float[] filter, int outputChannels, int outputWidth, int outputHeight,
int filterSize, int stride, boolean useGpu) {
this.input = input;
int filterSize, int stride, float[] newDelta, boolean useGpu) {
this.delta = delta;
this.inputChannels = inputChannels;
this.inputWidth = inputWidth;
Expand All @@ -81,12 +79,11 @@ public float[] backword(float[] input, float[] delta, float[] result,
this.filterSize = filterSize;
this.stride = stride;
this.result = result;
this.newDelta = new float[inputChannels * inputWidth * inputHeight];
this.newDelta = newDelta;
if (useGpu) {
put(filter);
put(delta);
put(result);
put(input);
execute(inputChannels * inputWidth * inputHeight);
get(newDelta);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ private void proc(int fchij) {
}
}
}
filter[fchij] += df;
filterDelta[fchij] += df;
}
float[] input;
float[] result;
float[] delta;
int inputChannels;
int inputWidth;
int inputHeight;
float[] filter;
float[] filterDelta;
int outputChannels;
int outputWidth;
int outputHeight;
Expand All @@ -62,14 +62,14 @@ private void proc(int fchij) {

public void backword(float[] delta, float[] result,
float[] input, int inputChannels, int inputWidth, int inputHeight,
float[] filter, int outputChannels, int outputWidth, int outputHeight,
float[] filterDelta, int outputChannels, int outputWidth, int outputHeight,
int filterSize, int stride, float learningRate, boolean useGpu) {
this.input = input;
this.delta = delta;
this.inputChannels = inputChannels;
this.inputWidth = inputWidth;
this.inputHeight = inputHeight;
this.filter = filter;
this.filterDelta = filterDelta;
this.outputChannels = outputChannels;
this.outputWidth = outputWidth;
this.outputHeight = outputHeight;
Expand All @@ -79,11 +79,11 @@ public void backword(float[] delta, float[] result,
this.learningRate = learningRate;// / outputWidth;// * outputHeight);
if (useGpu) {
put(delta);
put(filter);
put(filterDelta);
put(input);
put(result);
execute(outputChannels * inputChannels * filterSize * filterSize);
get(filter);
get(filterDelta);
} else {
IntStream.range(0, outputChannels).parallel().forEach((f) -> {
for (int chij = 0; chij < inputChannels * filterSize * filterSize; ++chij) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void proc(int fxy) {
int outputHeight;
int filterSize;
int stride;
float[] bias;
//float[] bias;
float[] delta;
float learningRate;
float[] tempDelta;
Expand All @@ -74,7 +74,7 @@ public float[] backward(float[] delta, float[] result,
float[] input, int inputChannels, int inputWidth, int inputHeight,
float[] filter, int outputChannels, int outputWidth, int outputHeight,
float[] filterDelta, float[] biasDelta,
int filterSize, int stride, float[] bias, float learningRate, boolean useGpu) {
int filterSize, int stride, float learningRate, boolean useGpu) {
this.delta = delta;
this.input = input;
this.inputChannels = inputChannels;
Expand All @@ -86,7 +86,7 @@ public float[] backward(float[] delta, float[] result,
this.outputHeight = outputHeight;
this.filterSize = filterSize;
this.stride = stride;
this.bias = bias;
//this.bias = bias;
this.result = result;
this.tempDelta = new float[outputChannels * inputChannels * inputWidth * inputHeight];
this.learningRate = learningRate;// / (outputWidth * outputHeight);
Expand Down
Loading