All Projects → Pigrecos → Keras4Delphi

Pigrecos / Keras4Delphi

Licence: MIT license
Keras4Delphi is a high-level neural networks API, written in Pascal with Python Binding

Programming Languages

pascal
1382 projects

Projects that are alternatives of or similar to Keras4Delphi

stocktwits-sentiment
Stocktwits market sentiment analysis in Python with Keras and TensorFlow.
Stars: ✭ 23 (-37.84%)
Mutual labels:  keras-neural-networks
AI-Chatbot
AI Chatbot using Dynamic Memory Network in Keras.
Stars: ✭ 64 (+72.97%)
Mutual labels:  keras-neural-networks
Artistic-Style-Transfer-using-Keras-Tensorflow
Art to Image Style Transfer using Keras and Tensorflow.
Stars: ✭ 22 (-40.54%)
Mutual labels:  keras-neural-networks
pytorch2keras
PyTorch to Keras model convertor
Stars: ✭ 788 (+2029.73%)
Mutual labels:  keras-neural-networks
Invoicenet
Deep neural network to extract intelligent information from invoice documents.
Stars: ✭ 1,886 (+4997.3%)
Mutual labels:  keras-neural-networks
RealTime-DigitRecognition
RealTime DigitRecognition using Convolutional Neural Network(CNN) with keras.
Stars: ✭ 108 (+191.89%)
Mutual labels:  keras-neural-networks
stone paper scissor defeator using opencv keras
In this repository i tried to replicate a cool project by a japanese scientist who made a machine which had 100 % accuracy in defeating humans in the game of stone-paper and scissors
Stars: ✭ 22 (-40.54%)
Mutual labels:  keras-neural-networks
cartpole-rl-remote
CartPole game by Reinforcement Learning, a journey from training to inference
Stars: ✭ 24 (-35.14%)
Mutual labels:  keras-neural-networks
Keras Attention Mechanism
Attention mechanism Implementation for Keras.
Stars: ✭ 2,504 (+6667.57%)
Mutual labels:  keras-neural-networks
MI-MVI 2016
Semestral project for the subject Methods of computational inteligence @ fit.cvut.cz
Stars: ✭ 24 (-35.14%)
Mutual labels:  keras-neural-networks
Aspect-Based-Sentiment-Analysis
No description or website provided.
Stars: ✭ 29 (-21.62%)
Mutual labels:  keras-neural-networks
DLSS
Deep Learning Super Sampling with Deep Convolutional Generative Adversarial Networks.
Stars: ✭ 88 (+137.84%)
Mutual labels:  keras-neural-networks
Artificial-Neural-Networks-Visualizer
Visualizing Artificial Neural Networks (ANNs) with just One Line of Code
Stars: ✭ 21 (-43.24%)
Mutual labels:  keras-neural-networks
keras generative pg
A Deep Generative Framework for Paraphrase Generation Implementaion
Stars: ✭ 78 (+110.81%)
Mutual labels:  keras-neural-networks
Final-year-project-deep-learning-models
Deep learning for freehand sketch object recognition
Stars: ✭ 22 (-40.54%)
Mutual labels:  keras-neural-networks
dl-relu
Deep Learning using Rectified Linear Units (ReLU)
Stars: ✭ 20 (-45.95%)
Mutual labels:  keras-neural-networks
GTAV-Self-driving-car
Self driving car in GTAV with Deep Learning
Stars: ✭ 15 (-59.46%)
Mutual labels:  keras-neural-networks
Nearest-Celebrity-Face
Tensorflow Implementation of FaceNet: A Unified Embedding for Face Recognition and Clustering to find the celebrity whose face matches the closest to yours.
Stars: ✭ 30 (-18.92%)
Mutual labels:  keras-neural-networks
Recurrent-Neural-Network-for-BitCoin-price-prediction
Recurrent Neural Network (LSTM) by using TensorFlow and Keras in Python for BitCoin price prediction
Stars: ✭ 53 (+43.24%)
Mutual labels:  keras-neural-networks
Machine-Learning-Notebooks
15+ Machine/Deep Learning Projects in Ipython Notebooks
Stars: ✭ 66 (+78.38%)
Mutual labels:  keras-neural-networks

Keras4Delphi is a high-level neural networks API, written in Pascal(Delphi Rio 10.3) with Python Binding and capable of running on top of TensorFlow, CNTK, or Theano. Based on Keras.NET and Keras

Use Keras if you need a deep learning library that:

Allows for easy and fast prototyping (through user friendliness, modularity, and extensibility). Supports both convolutional networks and recurrent networks, as well as combinations of the two. Runs seamlessly on CPU and GPU.

Keras4Delphi is using:

Prerequisite

Example with XOR sample

//Load train data
var x : TNDarray := TNumPy.npArray<Double>( [ [ 0, 0 ], [ 0, 1 ], [ 1, 0 ], [ 1, 1 ] ] );
var y : TNDarray := TNumPy.npArray<Double>( [ 0, 1, 1, 0 ] );

//Build functional model
var input  : TKInput := TKInput.Create(tnp_shape.Create([2]));
var hidden1: TBaseLayer  := TDense.Create(32, 'relu').&Set([input]);
var hidden2: TBaseLayer  := TDense.Create(64, 'relu').&Set([hidden1]);
var output : TBaseLayer  := TDense.Create(1,  'sigmoid').&Set([hidden2]);

var model : TModel := TModel.Create ( [ input ] , [ output ]);

//Compile and train
model.Compile(TStringOrInstance.Create( TAdam.Create ), 'binary_crossentropy',['accuracy']);

var batch_size : Integer := 2;
var history: THistory := model.Fit(x, y, @batch_size, 10,1);

model.Summary;

var logs := history.HistoryLogs;

//Save model and weights
var json : string := model.ToJson;
TFile.WriteAllText('model.json', json);
model.SaveWeight('model.h5');

//Load model and weight
var loaded_model : TBaseModel := TSequential.ModelFromJson(TFile.ReadAllText('model.json'));
loaded_model.LoadWeight('model.h5');

Output:

MNIST CNN Example

Python example taken from: https://keras.io/examples/mnist_cnn/

var
  res      : TArray<TNDArray>;
begin
    var batch_size : Integer := 128; //Training batch size
    var num_classes: Integer := 10;  //No. of classes
    var epochs     : Integer := 12;  //No. of epoches we will train

    // input image dimensions
    var img_rows: Integer := 28;
    var img_cols: Integer := 28;

    // Declare the input shape for the network
    var input_shape : Tnp_shape := default(Tnp_shape);

    // Load the MNIST dataset into Numpy array
    res := TMNIST.load_data;
    var x_train, y_train ,x_test, y_test : TNDArray;

    x_train := res[0];
    y_train := res[1];
    x_test  := res[2];
    y_test  := res[3];

    //Check if its channel fist or last and rearrange the dataset accordingly
    var K: TBackend := TBackend.Create;
    if(K.ImageDataFormat = 'channels_first') then
    begin
        x_train := x_train.reshape([x_train.shape[0], 1, img_rows, img_cols]);
        x_test  := x_test.reshape ([x_test.shape[0] , 1, img_rows, img_cols]);
        input_shape := Tnp_shape.Create([1, img_rows, img_cols]);
    end else
    begin
        x_train := x_train.reshape([x_train.shape[0], img_rows, img_cols, 1]);
        x_test  := x_test.reshape ([x_test.shape[0] , img_rows, img_cols, 1]);
        input_shape := Tnp_shape.Create([img_rows, img_cols, 1]);
    end;

    //Normalize the input data
    x_train := x_train.astype(vNumpy.float32_);
    x_test  := x_test.astype(vNumpy.float32_);
    x_train := TNDArray.opDiv(x_train, 255);
    x_test  := TNDArray.opDiv(x_test, 255);

    redtOutput.Lines.Add('x_train shape: ' + x_train.shape.ToString);
    redtOutput.Lines.Add( IntToStr(x_train.shape[0]) + ' train samples');
    redtOutput.Lines.Add( IntToStr(x_test.shape[0]) + '  test  samples');

    // Convert class vectors to binary class matrices
    var Util : TUtil := TUtil.Create;
    y_train := Util.ToCategorical(y_train, @num_classes);
    y_test  := Util.ToCategorical(y_test, @num_classes);

    // Build CNN model
    var model : TSequential := TSequential.Create;

    model.Add( TConv2D.Create(32, [3, 3],'relu', @input_shape) );
    model.Add( TConv2D.Create(64, [3, 3],'relu'));
    model.Add( TMaxPooling2D.Create([2, 2]));
    model.Add( TDropout.Create(0.25));
    model.Add( TFlatten.Create);
    model.Add( TDense.Create(128, 'relu'));
    model.Add( TDropout.Create(0.5));
    model.Add( TDense.Create(num_classes, 'softmax'));

    //Compile with loss, metrics and optimizer
    model.Compile(TStringOrInstance.Create(TAdadelta.Create), 'categorical_crossentropy', [ 'accuracy' ]);

    //Train the model
    model.Fit(x_train, y_train, @batch_size, epochs, 1,nil,0,[ x_test, y_test ]);

    //Score the model for performance
    var score : TArray<Double> := model.Evaluate(x_test, y_test, nil, 0);

    redtOutput.Lines.Add('Test loss: '   + FloatToStr(score[0]));
    redtOutput.Lines.Add('Test accuracy:'+ FloatToStr(score[1]));

    // Save the model to HDF5 format which can be loaded later or ported to other application
    model.Save('model.h5');
    // Save it to Tensorflow JS format and we will test it in browser.
    model.SaveTensorflowJSFormat('./');

Output

Reached 98% accuracy within 3 epoches.

Sentiment classification LSTM

Python example taken from: //https://keras.io/examples/imdb_lstm/

var
  res      : TArray<TNDArray>;
begin
    var max_features: Integer := 20000;
    // cut texts after this number of words (among top max_features most common words)
    var maxlen     : Integer := 80;
    var batch_size : Integer := 32;

    redtOutput.Lines.Add('Loading data...');
    res := TIMDB.load_data(@max_features);
    var x_train, y_train ,x_test, y_test,X,Y,tmp : TNDArray;

    x_train := res[0];
    y_train := res[1];
    x_test  := res[2];
    y_test  := res[3];

    redtOutput.Lines.Add('train sequences: ' + x_train.shape.ToString);
    redtOutput.Lines.Add('test sequences: '  + x_test.shape.ToString);

    redtOutput.Lines.Add('Pad sequences (samples x time)');
    var tseq : TSequenceUtil := TSequenceUtil.Create;
    x_train := tseq.PadSequences(x_train, @maxlen);
    x_test  := tseq.PadSequences(x_test,  @maxlen);
    redtOutput.Lines.Add('x_train shape: ' + x_train.shape.ToString);
    redtOutput.Lines.Add('x_test shape: '  + x_test.shape.ToString);

    redtOutput.Lines.Add('Build model...');
    var model : TSequential := TSequential.Create;
    model.Add( TEmbedding.Create(max_features, 128));
    model.Add( TLSTM.Create(128, 0.2, 0.2));
    model.Add( TDense.Create(1, 'sigmoid'));

    //try using different optimizers and different optimizer configs
    model.Compile(TStringOrInstance.Create('adam'), 'binary_crossentropy', [ 'accuracy' ]);
    model.Summary;

    redtOutput.Lines.Add('Train...');
    model.Fit(x_train, y_train, @batch_size, 15, 1,[ x_test, y_test ]);

    //Score the model for performance
    var score : TArray<Double> := model.Evaluate(x_test, y_test, @batch_size);

    redtOutput.Lines.Add('Test score: '   + FloatToStr(score[0]));
    redtOutput.Lines.Add('Test accuracy:'+ FloatToStr(score[1]));

    // Save the model to HDF5 format which can be loaded later or ported to other application
    model.Save('model.h5');

Notes

welcome collaborative testing and improvement of source code. I have little free time

TODO List

  • Test code
  • and Much more
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].