Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bika-c committed Jan 28, 2021
0 parents commit f518ed1
Show file tree
Hide file tree
Showing 119 changed files with 4,937 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.vscode
Binary file not shown.
Binary file added Neural Network Application/Debug/SDL2.dll
Binary file not shown.
Binary file added Neural Network Application/Debug/SDL2_gfx.dll
Binary file not shown.
Binary file added Neural Network Application/Debug/SDL2_ttf.dll
Binary file not shown.
Binary file added Neural Network Application/Debug/asset/arial.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Neural Network Application/Debug/graphics.pdb
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Neural Network Application/Debug/main.exe
Binary file not shown.
Binary file added Neural Network Application/Debug/test.exe
Binary file not shown.
Binary file added Neural Network Application/Debug/train.exe
Binary file not shown.
Binary file added Neural Network Application/Debug/zlib1.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
13 changes: 13 additions & 0 deletions Neural Network Application/Release/README-SDL.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

Please distribute this file with the SDL runtime environment:

The Simple DirectMedia Layer (SDL for short) is a cross-platform library
designed to make it easy to write multi-media software, such as games
and emulators.

The Simple DirectMedia Layer library source code is available from:
https://www.libsdl.org/

This library is distributed under the terms of the zlib license:
http://www.zlib.net/zlib_license.html

Binary file added Neural Network Application/Release/SDL2.dll
Binary file not shown.
Binary file added Neural Network Application/Release/SDL2_gfx.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Neural Network Application/Release/example.exe
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Neural Network Application/Release/main.exe
Binary file not shown.
Binary file added Neural Network Application/Release/sample.exe
Binary file not shown.
Binary file added Neural Network Application/Release/sampleC.exe
Binary file not shown.
Binary file added Neural Network Application/Release/test.exe
Binary file not shown.
Binary file not shown.
Binary file added Neural Network Application/Release/train.exe
Binary file not shown.
Binary file added Neural Network Application/Release/zlib1.dll
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
89 changes: 89 additions & 0 deletions Neural Network Application/include/NeuralNetwork.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Simple Neural Network
// Created by William Chen
// Failed to learn on a large scale.
// Suspicious of vanishing gradient

#pragma once

#include "pixel.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define INPUT_ROW 20
#define INPUT_COLUMN 20
#define INPUT_NUM NUM_PIXEL
#define OUTPUT_NUM 10

#define HIDDEN_LAYER_NUM 2
#define HIDDEN_LAYER_NEURONS_NUM 10
#define LEARNING_RATE 0.35

#define RANDOM_ALGORITHM (rand() % 1000) * 1.0 / RAND_MAX
#define QUEUE_LENGTH 10

typedef struct InputSample
{
double inputs[INPUT_NUM];
double labels[OUTPUT_NUM];
} Sample; // Inputs as all the input data and labels as the correct ans/label of the sample/data set

typedef struct Input
{
double activation;
} Input; // Input, each input has an activation

typedef struct FirstHiddenLayerNeuron
{
double activation;
double weights[INPUT_NUM];
double bias;
} NeuronFHL; // First hidden layer neuron - each neuron's weights array has the size depends on the input layer

typedef struct Neuron
{
double activation;
double weights[HIDDEN_LAYER_NEURONS_NUM];
double bias;
} Neuron; // Neuron, each neuron has the size of the weights array depends on the total number of neurons in each layer

typedef struct BackpropagationData
{
double deltaFirstHiddenWeights[HIDDEN_LAYER_NEURONS_NUM][INPUT_NUM];
double deltaHiddenBias[HIDDEN_LAYER_NUM][HIDDEN_LAYER_NEURONS_NUM];
double deltaHiddenWeights[HIDDEN_LAYER_NUM - 1][HIDDEN_LAYER_NEURONS_NUM][HIDDEN_LAYER_NEURONS_NUM];
double deltaOutputWeights[OUTPUT_NUM][HIDDEN_LAYER_NEURONS_NUM];
double deltaOutputBias[OUTPUT_NUM];
} BackpropagationData;

typedef struct NeuralNetwork
{
Input inputLayer[INPUT_NUM];
NeuronFHL firstHiddenLayer[HIDDEN_LAYER_NEURONS_NUM];
Neuron hiddenLayers[HIDDEN_LAYER_NUM - 1][HIDDEN_LAYER_NEURONS_NUM];
Neuron outputLayer[OUTPUT_NUM];
double anticipation[OUTPUT_NUM];
BackpropagationData deltaDataset;
} NeuralNetwork; // The neural network, contains an input layer, hidden layers (first + the rest), an output, and anticipations for a specific sample and a set of delta data

// NeuralNetwork AI;

// Initialize all the weights and Bias with random numbers
void initializeNetwork();

// Train a batch of samples
void trainInBatches(int labels[]);

// Train a single sample
void train(int label);

// Output/predict
int output(const Sample* const input);

// Print result to console
void consoleTest(const Sample* sample);

// Debug purpose (Unfinished)
void DebugDisplayNetwork();
9 changes: 9 additions & 0 deletions Neural Network Application/include/brush.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "pixel.h"
#include <corecrt_math_defines.h>

#define PI M_PI
#define BRUSH_RADIUS PIXEL / 2.0
#define BRUSH_AREA PI* BRUSH_RADIUS* BRUSH_RADIUS
#define BRUSH_COLOR 4294967295 //WHITE
17 changes: 17 additions & 0 deletions Neural Network Application/include/data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

// #include "NeuralNetwork.h"
#include "graphics.h"
#include "lenet.h"

#define FILE_NAME "./asset/data/model.dat"

void FileNotFound();

bool SearchForDataFile();

void generalizeData();

void readFromFile();

void writeToFile();
53 changes: 53 additions & 0 deletions Neural Network Application/include/graphics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include "lenet.h"
#include "brush.h"
#include "specialCircleMathTools.h"
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2\SDL.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>

#define FONT_NAME "./asset/arial.ttf"
#define FONT_SIZE 150
#define TEXT_X_POS CANVAS_WIDTH + 120
#define TEXT_Y_POS 220

#define SCREEN_PIXEL_FORMAT SDL_PIXELFORMAT_RGBA8888

typedef struct UI
{
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Texture* texture;
TTF_Font* font;
Uint32 pixels[CANVAS_AREA];
SDL_PixelFormat* format;
} AppContent;

extern AppContent cont;


// Refresh the screen
void refresh(SDL_Event* event);

// Coresponding key event
void keyBoardEvent(SDL_Keysym key);


// Initialize the program
void initialize();

// Call AI to recognize the screen
void recognize();

// Prompt errors
void errorMessage(const char* const message);

// Display text in the defined area
void displayText(const char* const message);

// Safe quit
void quit();
77 changes: 77 additions & 0 deletions Neural Network Application/include/lenet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// This is a free public source code provided by the author 范文捷 (WenJie Fan)

/*
@author : 范文捷 (WenJie Fan)
@data : 2016-04-20
@note : Based on Yann Lecun's paper "Gradient-based Learning Applied To Document Recognition"
@api :
void TrainBatch(LeNet5 *lenet, Image *inputs, uint8 *labels, int batchSize);
void Train(LeNet5 *lenet, Image input, uint8 label);
uint8 Predict(LeNet5 *lenet, Image input, uint8 count);
void Initial(LeNet5 *lenet);
*/

#pragma once

#define LENGTH_KERNEL 5

#define LENGTH_FEATURE0 32
#define LENGTH_FEATURE1 (LENGTH_FEATURE0 - LENGTH_KERNEL + 1)
#define LENGTH_FEATURE2 (LENGTH_FEATURE1 >> 1)
#define LENGTH_FEATURE3 (LENGTH_FEATURE2 - LENGTH_KERNEL + 1)
#define LENGTH_FEATURE4 (LENGTH_FEATURE3 >> 1)
#define LENGTH_FEATURE5 (LENGTH_FEATURE4 - LENGTH_KERNEL + 1)

#define INPUT 1
#define LAYER1 6
#define LAYER2 6
#define LAYER3 16
#define LAYER4 16
#define LAYER5 120
#define OUTPUT 10

#define ALPHA 0.5
#define PADDING 2

//Modified
#include "brush.h"

typedef unsigned char uint8;
typedef unsigned char Image[PIXELS_WIDTH][PIXELS_WIDTH];

typedef struct LeNet5
{
double weight0_1[INPUT][LAYER1][LENGTH_KERNEL][LENGTH_KERNEL];
double weight2_3[LAYER2][LAYER3][LENGTH_KERNEL][LENGTH_KERNEL];
double weight4_5[LAYER4][LAYER5][LENGTH_KERNEL][LENGTH_KERNEL];
double weight5_6[LAYER5 * LENGTH_FEATURE5 * LENGTH_FEATURE5][OUTPUT];

double bias0_1[LAYER1];
double bias2_3[LAYER3];
double bias4_5[LAYER5];
double bias5_6[OUTPUT];

} LeNet5;

typedef struct Feature
{
double input[INPUT][LENGTH_FEATURE0][LENGTH_FEATURE0];
double layer1[LAYER1][LENGTH_FEATURE1][LENGTH_FEATURE1];
double layer2[LAYER2][LENGTH_FEATURE2][LENGTH_FEATURE2];
double layer3[LAYER3][LENGTH_FEATURE3][LENGTH_FEATURE3];
double layer4[LAYER4][LENGTH_FEATURE4][LENGTH_FEATURE4];
double layer5[LAYER5][LENGTH_FEATURE5][LENGTH_FEATURE5];
double output[OUTPUT];
} Feature;

void TrainBatch(LeNet5* lenet, Image* inputs, uint8* labels, int batchSize);

void Train(LeNet5* lenet, Image input, uint8 label);

uint8 Predict(LeNet5* lenet, Image input, uint8 count);

void Initial(LeNet5* lenet);
50 changes: 50 additions & 0 deletions Neural Network Application/include/mnist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Takafumi Hoiruchi. 2018.
https://github.com/takafumihoriuchi/MNIST_for_C
*/

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// set appropriate path for data
#define TRAIN_IMAGE "../data/train-images.idx3-ubyte"
#define TRAIN_LABEL "../data/train-labels.idx1-ubyte"
#define TEST_IMAGE "../data/t10k-images.idx3-ubyte"
#define TEST_LABEL "../data/t10k-labels.idx1-ubyte"

#define SIZE 784 // 28*28
#define NUM_TRAIN 60000
#define NUM_TEST 10000
#define LEN_INFO_IMAGE 4
#define LEN_INFO_LABEL 2

#define MAX_IMAGESIZE 1280
#define MAX_BRIGHTNESS 255
#define MAX_FILENAME 256
#define MAX_NUM_OF_IMAGES 1

// void read_mnist_char(char* file_path, int num_data, int len_info, int arr_n, unsigned char data_char[][arr_n], int info_arr[]);

void FlipLong(unsigned char* ptr);

void read_mnist_char(char* file_path, int num_data, int len_info, int arr_n, unsigned char data_char[][arr_n], int info_arr[]);

void image_char2double(int num_data, unsigned char data_image_char[][SIZE], double data_image[][SIZE]);

void label_char2int(int num_data, unsigned char data_label_char[][1], int data_label[]);

void load_mnist();

void print_mnist_pixel(double data_image[][SIZE], int num_data);

void print_mnist_label(int data_label[], int num_data);

// name: path for saving image (ex: "./images/sample.pgm")
void save_image(int n, char name[]);

// save mnist image (call for each image)
// store train_image[][] into image[][][]
void save_mnist_pgm(double data_image[][SIZE], int index);
17 changes: 17 additions & 0 deletions Neural Network Application/include/pixel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "screen.h"
#include <SDL2/SDL_rect.h>

#define PIXEL 20
#define PIXELS_WIDTH (int)(CANVAS_WIDTH / PIXEL)
#define PIXELS_HEIGHT (int)(CANVAS_HEIGHT / PIXEL)
#define NUM_PIXEL (int)(CANVAS_AREA / (PIXEL * PIXEL))

typedef struct PixelSquares
{
SDL_Rect rect;
float activation;
} Pixel; // Scaled pixel

void initializePixelSquares();
8 changes: 8 additions & 0 deletions Neural Network Application/include/screen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#define CANVAS_WIDTH 560
#define CANVAS_HEIGHT 560
#define WIDTH CANVAS_WIDTH + 300
#define HEIGHT CANVAS_HEIGHT
#define SCREEN WIDTH* HEIGHT
#define CANVAS_AREA CANVAS_WIDTH* CANVAS_HEIGHT
17 changes: 17 additions & 0 deletions Neural Network Application/include/specialCircleMathTools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Unsuccess, turned into a different approach

#pragma once

#include <math.h>

float centralAngle(float chordLength, float radius);

float archLength(float chordLength, float radius);

float sectorArea(float chordLength, float radius);

float segmentArea(float chordLength, float radius);

float chordLength(float radius, float distance);

float cornerArea(float chordLength, float radius, float x, float h);
Loading

0 comments on commit f518ed1

Please sign in to comment.