Skip to content

Commit

Permalink
added spring boot logging for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
RusFortunat committed Nov 13, 2024
1 parent b7ab8ca commit d9b95e7
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 78 deletions.
1 change: 1 addition & 0 deletions spring.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-11-13T16:03:24.015+01:00 ERROR 16956 --- [guessNumbersWithAI] [http-nio-8080-exec-3] c.g.controller.ViewController : src\main\resources\net_params_size784_256_10_lr0.001_trainEps1002.txt (The system cannot find the file specified)
36 changes: 18 additions & 18 deletions src/main/java/com/guessNumbersWithAI/controller/ViewController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

import com.guessNumbersWithAI.model.DrawnImages;
import com.guessNumbersWithAI.model.NeuralNetwork;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import java.io.FileWriter;
import java.util.ArrayList;

@Controller
public class ViewController {

Logger logger = LoggerFactory.getLogger(ViewController.class);

@GetMapping("/write-number")
public String hangleGetMapping(Model theModel){

Expand All @@ -29,31 +34,26 @@ public String hangleGetMapping(Model theModel){
public String hanglePostMapping(@ModelAttribute("NeuralNetwork") NeuralNetwork ourNeuralNetwork,
@ModelAttribute("DrawnImages") DrawnImages drawnImages){

// load network parameters from the file
ourNeuralNetwork.loadNetworkParameters();
try{
// load network parameters from the file
ourNeuralNetwork.loadNetworkParameters();

// create input vector by processing raw pixel image data; we turn 280x280 px image into 28x28 px image
double[] inputVector = drawnImages.processRawInput();
// create input vector by processing raw pixel image data; we turn 280x280 px image into 28x28 px image
double[] inputVector = drawnImages.processRawInput();

// pass input vector to network and get the prediction; the answer will be displayed on the html view page
ourNeuralNetwork.forward(inputVector);
System.out.println("Answer: " + ourNeuralNetwork.getAnswer());
// pass input vector to network and get the prediction; the answer will be displayed on the html view page
ourNeuralNetwork.forward(inputVector);
System.out.println("Answer: " + ourNeuralNetwork.getAnswer());

// save image to the H2 database for future neural networks training

// save image to the H2 database for future neural networks training
try{
drawnImages.saveImageToDB();
}catch(Exception e){
System.out.println(e.getMessage());
}

// list all saved images; uncomment to view DB content in sdout
/*try{
System.out.println("Our Image database:");
ArrayList<String> storedImages = drawnImages.readAllImages();
for(String image: storedImages) System.out.println(image);
}catch(Exception e){
logger.error(e.getMessage());
System.out.println(e.getMessage());
}*/

}

return "main-view";
}
Expand Down
112 changes: 53 additions & 59 deletions src/main/java/com/guessNumbersWithAI/model/NeuralNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public NeuralNetwork() {

// Forward propagation: the neural network gets user image input, that is converted to the input vector
// and returns as an output the probabilities for different numbers
public void forward(double[] input){
public void forward(double[] input) throws RuntimeException{
// forward propagation is not that hard, just do the following:
// 1. Take matrix product and add biases [z] = [firstLayerWeights]*[input] + [firstLayerBiases];
// 2. Obtain hidden activation values [y] by applying to [z] some activation function f([z]), i.e., mapping.
Expand Down Expand Up @@ -99,71 +99,65 @@ public void forward(double[] input){


// loads the parameters of the chosen network
public void loadNetworkParameters(){

try{

// specify filename for the model that user selected
String filename = "src/main/resources/net_params_size784_256_10_lr0.001_trainEps100.txt";
// working on it
/*if(chosenNetworkModel.equals("PyTorch")){
filename = "src/main/resources/PyTorch_params.txt";
}else if(chosenNetworkModel.equals("MyModel")){
filename = "src/main/resources/net_params_size784_256_10_lr0.001_trainEps100.txt";
}*/

// load the file
File networkParamsFile = new File(filename);
Scanner reader = new Scanner(networkParamsFile);

// read the file data
while(reader.hasNextLine()){

// skip metadata
for(int skip = 0; skip < 4; skip++) reader.nextLine();

// load first layer weights
String firstLayerWeightsStr = reader.nextLine();
String[] firstLrWeightsStr = firstLayerWeightsStr.split(",");
for(int i = 0; i < hiddenSize; i++){
for(int j = 0; j < inputSize; j++) {
int index = i*inputSize + j;
this.firstLayerWeights[i][j] = Double.valueOf(firstLrWeightsStr[index]);
}
}

// load first layer biases
for(int skip = 0; skip < 2; skip++) reader.nextLine();
String firstLayerBiasesStr = reader.nextLine();
String[] firstLrBiasesStr = firstLayerBiasesStr.split(",");
for(int i = 0; i < hiddenSize; i++){
firstLayerBiases[i] = Double.valueOf(firstLrBiasesStr[i]);
public void loadNetworkParameters() throws Exception{

// specify filename for the model that user selected
String filename = "src/main/resources/net_params_size784_256_10_lr0.001_trainEps100.txt";
// working on it
/*if(chosenNetworkModel.equals("PyTorch")){
filename = "src/main/resources/PyTorch_params.txt";
}else if(chosenNetworkModel.equals("MyModel")){
filename = "src/main/resources/net_params_size784_256_10_lr0.001_trainEps100.txt";
}*/

// load the file
File networkParamsFile = new File(filename);
Scanner reader = new Scanner(networkParamsFile);

// read the file data
while(reader.hasNextLine()){

// skip metadata
for(int skip = 0; skip < 4; skip++) reader.nextLine();

// load first layer weights
String firstLayerWeightsStr = reader.nextLine();
String[] firstLrWeightsStr = firstLayerWeightsStr.split(",");
for(int i = 0; i < hiddenSize; i++){
for(int j = 0; j < inputSize; j++) {
int index = i*inputSize + j;
this.firstLayerWeights[i][j] = Double.valueOf(firstLrWeightsStr[index]);
}
}

// load second layer weights
for(int skip = 0; skip < 2; skip++) reader.nextLine();
String secondLayerWeightsStr = reader.nextLine();
String[] secondLrWeightsStr = secondLayerWeightsStr.split(",");
for(int i = 0; i < outputSize; i++){
for(int j = 0; j < hiddenSize; j++) {
int index = i*hiddenSize + j;
this.secondLayerWeights[i][j] = Double.valueOf(secondLrWeightsStr[index]);
}
}
// load first layer biases
for(int skip = 0; skip < 2; skip++) reader.nextLine();
String firstLayerBiasesStr = reader.nextLine();
String[] firstLrBiasesStr = firstLayerBiasesStr.split(",");
for(int i = 0; i < hiddenSize; i++){
firstLayerBiases[i] = Double.valueOf(firstLrBiasesStr[i]);
}

// load first layer biases
for(int skip = 0; skip < 2; skip++) reader.nextLine();
String secondLayerBiasesStr = reader.nextLine();
String[] secondLrBiasesStr = secondLayerBiasesStr.split(",");
for(int i = 0; i < outputSize; i++){
secondLayerBiases[i] = Double.valueOf(secondLrBiasesStr[i]);
// load second layer weights
for(int skip = 0; skip < 2; skip++) reader.nextLine();
String secondLayerWeightsStr = reader.nextLine();
String[] secondLrWeightsStr = secondLayerWeightsStr.split(",");
for(int i = 0; i < outputSize; i++){
for(int j = 0; j < hiddenSize; j++) {
int index = i*hiddenSize + j;
this.secondLayerWeights[i][j] = Double.valueOf(secondLrWeightsStr[index]);
}
}
reader.close();

}catch(Exception e){
System.out.println(e.getMessage());
// load first layer biases
for(int skip = 0; skip < 2; skip++) reader.nextLine();
String secondLayerBiasesStr = reader.nextLine();
String[] secondLrBiasesStr = secondLayerBiasesStr.split(",");
for(int i = 0; i < outputSize; i++){
secondLayerBiases[i] = Double.valueOf(secondLrBiasesStr[i]);
}
}
reader.close();
}

// getters and setters
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
spring.application.name=guessNumbersWithAI

spring.main.banner-mode=off
logging.level.root=WARN

logging.file.path=.
logging.level.root=ERROR
logging.level.org.hibernate=ERROR
Binary file modified src/main/resources/image-database.mv.db
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ class GuessNumbersWithAiApplicationTests {
void contextLoads() {
}

// test to check if the new image has been added to DB

}

0 comments on commit d9b95e7

Please sign in to comment.